// JavaScript Document to animate Olay UK microsites.

$(document).ready(function(){

	var initiativeNo = 0;
	
	function autoAnimateInitiatives() {
		
		//this is to remove the flash object before full fade out, due to ie6 script error
		fixUnloadFlash(2000);
		
		$('#switchOver').fadeOut(2500, function() {
			
			$('#containerMain').removeClass();
			$('#containerMain').addClass(initiativeClasses[initiativeNo]);
			
			for (var i = 0; i < initiatives.length; i++) {
				
				if (i == initiativeNo) {
					
					$('.' + initiativeShow[i]).css('display', 'block');
					$('.' + initiativeShow[i]).find('div').css('display', 'block');
					
				} else {
					
					$('.' + initiativeShow[i]).css('display', 'none');
					$('.' + initiativeShow[i]).find('div').css('display', 'none');
					
				}
				
			}
			
			if (initiativeNo == 0) {
				
				$('#prevUrl').attr('href', initiatives[initiatives.length - 1]);
				$('#nextUrl').attr('href', initiatives[1]);
				initiativeNo = 1;
				
			} else if (initiativeNo == initiatives.length -1) {
				
				$('#prevUrl').attr('href', initiatives[initiativeNo - 1]);
				$('#nextUrl').attr('href', initiatives[0]);
				initiativeNo = 0;
			
			} else {
				
				$('#prevUrl').attr('href', initiatives[initiativeNo - 1]);
				$('#nextUrl').attr('href', initiatives[initiativeNo + 1]);
				initiativeNo = initiativeNo + 1;
				
			}
			
			//fade in of content and on content loaded execute dynamic sifr
			$('#switchOver').fadeIn(2500, function() {
				fixMicroSiteLoadSifr();
			});
			
		});
			
	};
	
	var anim = setInterval (autoAnimateInitiatives, 7000);
	
	$('.animationPause').click(function() {
																			
		if (isPlaying) {
			
			clearInterval (anim);
			$('.animationPause').html('> Play');
			$('.animationPause').attr('title', '> Play');
			isPlaying = false;
			
		} else {
			
			anim = setInterval (autoAnimateInitiatives, 7000);
			$('.animationPause').html('II Pause');
			$('.animationPause').attr('title', 'II Pause');
			isPlaying = true;
			
		}
		
		return false;
	
	});
	
	$('.animation').click(function() {
																			
		clearInterval (anim);
		$('.animationPause').html('> Play');
		$('.animationPause').attr('title', '> Play');
		isPlaying = false;
		
		return false;
		
	});
	
});

function fixUnloadFlash(param){
	/*this is to remove the flash object before fade out due to ie6 script error */
	window.setTimeout(function(){
		var contClass = $('#containerMain').attr('class');
			if($('.' + contClass + ' .sIFR-replaced').length){
				$('.' + contClass + ' .sIFR-replaced').find('object').remove();
				var text = $('.' + contClass).find('.sIFR-alternate').html();
				$('.' + contClass).find('.sIFR-alternate').remove()
				$('.' + contClass + ' .sIFR-replaced').html(text);
				$('.' + contClass + ' .sIFR-replaced').removeClass();
			}
	},param);	
}


