/*-=-==-=-=--=-=-
  Offer Rotator
	v2.1 - 7/22/09 - K. Praslowicz
 ---=-==-=-=-=-=-==-=-*/
var FBSM=window.FBSM;
if(typeof FBSM=='undefined'){FBSM=new Object();FBSM.C=new Object();FBSM.PD=new Object();FBSM.U=new Object();FBSM.SC=new Object();FBSM.W=new Object();FBSM.WC=new Object();FBSM.F=new Object();FBSM.TFG=new Object();FBSM.R=new Object();}
 
FBSM.R.rotator_autorotate = 1;
FBSM.R.rotator_current = -1;
FBSM.R.rotator_beenClicked = 0;
FBSM.R.rotator_zIndex = 20;
FBSM.R.rotator_initPosition = "relative";
FBSM.R.rotator_interval=0;
FBSM.R.rotator_mouseX = 0;
FBSM.R.rotator_mouseY = 0;
FBSM.R.rotator_div = '';
FBSM.R.rotator_isHover = 0;
FBSM.R.rotator_prev = "media/buttons/back.gif";
FBSM.R.rotator_next = "media/buttons/next.gif";
FBSM.R.rotator_pause = "media/buttons/pause.gif";
FBSM.R.rotator_paused = "media/buttons/paused.gif";
FBSM.R.rotator_delay = 6000;

$(document).ready(function(){
	var rot_ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);
	if($('#rotatorDelay').is('*')) FBSM.R.rotator_delay = $('#rotatorDelay').html();
	/* Select Rotator Version */
	if($('.merchRotateMain').is('*'))
		FBSM.R.rotator_div = $('.merchRotateMain');
	else if($('.merchRotateMain_v2').is('*'))
		FBSM.R.rotator_div = $('.merchRotateMain_v2');
	else
		FBSM.R.rotator_div = '';
	//*Start rotator if one of the rotator divs are present.*/
	if(FBSM.R.rotator_div != ''){
		FBSM.R.rotator_div.css('overflow','hidden');
		if(FBSM.R.rotator_div.attr('class') == 'merchRotateMain_v2'){
			/*-=-=-==-=-=-=-==--==-=-=-=-=-=---==-=-=-=-
			  - v2.1 behavior
			*/
			//Generate Nav
			FBSM.R.createRotatorNav();
			$('.merchRotate img').css('border','0px');
			//Create animated loading layer
			FBSM.R.rotator_div.append("<div id='rotateLoaderDiv'></div>");
			$('#rotateLoaderDiv').css('opacity', '0.5');
			//Start autorotate chain
			FBSM.R.swap();
			FBSM.R.rotator_div.hover(function(){
				/* Add click events to ie6 Spans generated via pngFix() */
				if ($.browser.msie && rot_ie6) {
						FBSM.R.addClickables('span');
						rot_ie6 = false;
				}
				FBSM.R.rotator_isHover = 1;
			}, function(){
				FBSM.R.rotator_isHover = 0;
			});

		}/* End if */
		else{
			/*-=-=-==-=-=-=-==--==-=-=-=-=-=---==-=-=-=-
			  - v1 behavior
			*/
			///Preload Frames
			$('.merchRotate img').css('border','0px').each(function(){$("<img>").attr("src", $(this).attr("src"));});
			//Start autorotate chain
			FBSM.R.swap();
			//Pause animation on mouseover
			var isIE = 0;
			$.each($.browser, function(i) { if($.browser.msie) isIE = 1; });
			//Simple jquery hover for non-IE browsers.
			if(isIE == 0){
				FBSM.R.rotator_div.hover(function(){
					window.clearTimeout(FBSM.R.rotator_interval);
					if(FBSM.R.rotator_beenClicked == 0) FBSM.R.rotator_autorotate = 0;
				}, function(){
					if(FBSM.R.rotator_beenClicked == 0){
						FBSM.R.rotator_autorotate = 1;
						FBSM.R.swap();
					}
				});
			}else{
				//Crazy over-the-top function to make IE not break.
				//Track Mouse Coords
					$().mousemove(function(e){
					FBSM.R.rotator_mouseX = e.pageX;
					FBSM.R.rotator_mouseY = e.pageY;
			   });
				FBSM.R.rotator_div.bind("mouseenter", function(){
					if(FBSM.R.rotator_beenClicked == 0) FBSM.R.rotator_autorotate = 0;
						FBSM.R.merchVsMouse();
				});
			}
		} /*End Version behavior */
	}/*End conditional to only start if a rotator is present.*/
});
FBSM.R.swap = function(){
	//Pause on hover, and stop auto rotate if any merch links have been clicked
	if((FBSM.R.rotator_autorotate == 1) && (FBSM.R.rotator_beenClicked == 0)){
		//attempt to make a crossfade
		FBSM.R.advancePromo('+');
		FBSM.R.animateSwap(FBSM.R.rotator_current);
		//Pause before advancing to next frame.
		window.clearTimeout(FBSM.R.rotator_interval);
		FBSM.R.rotator_interval = setTimeout('FBSM.R.swap()', FBSM.R.rotator_delay);
	}
}
//IE has mouseover difficulty. This function checks to see if the mouse has left the merch area, and restarts the autorotate if its outside
FBSM.R.merchVsMouse = function(){
	var offset = FBSM.R.rotator_div.offset();
	var height = FBSM.R.rotator_div.height();
	var width = FBSM.R.rotator_div.width();
	if((FBSM.R.rotator_mouseX >= offset.left) && (FBSM.R.rotator_mouseX <= (offset.left+width)) && (FBSM.R.rotator_mouseY >= offset.top) && (FBSM.R.rotator_mouseY < (offset.top+height))){
		window.clearTimeout(FBSM.R.rotator_interval);
		FBSM.R.rotator_interval = setTimeout("FBSM.R.merchVsMouse()", FBSM.R.rotator_delay);
	}else{
		if(FBSM.R.rotator_beenClicked == 0) FBSM.R.rotator_autorotate = 1;
		FBSM.R.swap();
	}
}
//Function to Tween slides.
FBSM.R.animateSwap = function(swapto){
	//Update Pause Button
	FBSM.R.setPauseButton();
	FBSM.R.rotator_zIndex++;
	if($('#rotator_slide'+swapto).is('*')){
		//If div is already created, shuffle to top to avoid memory leaking.
	   $('#rotator_slide'+swapto).hide();
		$('#rotator_slide'+swapto).css('z-index', FBSM.R.rotator_zIndex);
		$('#rotator_slide'+swapto).fadeIn(function(){
			//update Status if v2
			FBSM.R.updateControls(swapto, true);
		});
	}else{
		var img = new Image();
		$(img).load(function () {
			//Create a new div layer above the current. Load it with new content, and fade it in.
			var newSlide = $.create('div', {'class':'merchSwaperator','id':'rotator_slide'+swapto,'style':'display:none;position:'+FBSM.R.rotator_initPosition+';top:0;left:0;z-index:'+FBSM.R.rotator_zIndex});
			$(newSlide).append($.trim($('.merchRotate:eq('+swapto+')').html()));
			FBSM.R.rotator_div.append($(newSlide));
			$(newSlide).fadeIn(function(){
				//update Status if v2
				FBSM.R.updateControls(swapto, true);
			});
			//Sebsequent frames need absolute to sit on top of first frame.
			FBSM.R.rotator_initPosition = 'absolute';
       }).error(function () {
	       // notify the user that the image could not be loaded
       }).attr('src', $('.merchRotate:eq('+swapto+') img').attr('src'));
	}
}
//Function to handle clicks.
FBSM.R.swapClick = function(swapto){
	FBSM.R.showLoadingFrame(swapto-1);
	//Disable Autorotate
	FBSM.R.rotator_autorotate = 0;
	FBSM.R.rotator_beenClicked = 1;
	//Swap slides
	FBSM.R.animateSwap(swapto-1);
}
//Include for now, this should fade away soon
swapClick = function(swapto){
	FBSM.R.showLoadingFrame(swapto-1);
	FBSM.R.rotator_autorotate = 0;
	FBSM.R.rotator_beenClicked = 1;
	FBSM.R.animateSwap(swapto-1);
}
FBSM.R.showLoadingFrame = function(swapto){
	if(FBSM.R.rotator_div.attr('class') == 'merchRotateMain_v2'){
		if(!$('#rotator_slide'+(swapto)).is('*')){
			$('#rotateLoaderDiv').css('z-index',++FBSM.R.rotator_zIndex);
		}
	}
}
/* Function for v2 to create the slide-in controls over the rotator. */
FBSM.R.createRotatorNav = function(){
	//Create Slide-in Div
	var controlDiv = $.create('div', {'id':'rotator_controls'});
	$(controlDiv).css('opacity','0.9');
	var innerControlDiv = $.create('div',
										{'id':'rotator_inner_controls',
										'style':'padding: 0 10px',
										'class':'pngFix'}
										);
	//Create Control buttons
	/* Check for custom button graphics */
	if($('#customButtons').is('*')){
		/* Overwrite defaults */
		$('#customButtons img').each(function(){
			if($(this).attr('alt') == 'prev')
				FBSM.R.rotator_prev = $(this).attr('src');
			else if($(this).attr('alt') == 'next')
				FBSM.R.rotator_next = $(this).attr('src');
			else if($(this).attr('alt') == 'pause')
				FBSM.R.rotator_pause = $(this).attr('src');
			else if($(this).attr('alt') == 'paused')
				FBSM.R.rotator_paused = $(this).attr('src');
		});
	}
	$(innerControlDiv).append('<img src="'+FBSM.R.rotator_prev+'" id="" class="rotator_prev rotatorControlButton"><img src="'+FBSM.R.rotator_pause+'" id="" class="rotator_pause rotatorControlButton"><img src="'+FBSM.R.rotator_next+'" id="" class="rotator_next rotatorControlButton">');
	//Create 1,2,...,x Controls
	var changeLinks = '';
	var minichangeLinks = '';
	var k = 1;
	$('.merchRotate').each(function(){
		changeLinks += '<span class="promoChangeLink" id="promo_'+k+'">'+k+'</span>';
		k++;
	});
	$(innerControlDiv).prepend(changeLinks+'&nbsp;|&nbsp;');
	$(innerControlDiv).prepend('<span id="rotator_promo_summary"></span>');
	controlDiv.append($(innerControlDiv));
	controlDiv.css('bottom', '0px');
	FBSM.R.rotator_div.append($(controlDiv));
	/* Bind events to newly created control bar */
	$('.promoChangeLink').css('cursor','pointer');
	$('.promoChangeLink').click(function(){
		var clickId = $(this).attr('id');
		clickId = clickId.split('_');
		FBSM.R.rotator_current = clickId[1]-1;
		FBSM.R.swapClick(clickId[1]);
	});
	$('.promoChangeLink').hover(function(){
		$(this).addClass('hoverPromoNumber');
		var hoverId = $(this).attr('id');
		hoverId = hoverId.split('_');
		var hover = parseInt(hoverId[1])-1;
		$('#rotator_promo_summary').html($('.merchRotate:eq('+hover+')').children('a').attr('title')+' | ');
		FBSM.R.updateControls(hover, false);
	},function(){
		$(this).removeClass('hoverPromoNumber');
		$('#rotator_promo_summary').html('');
	});
	FBSM.R.addClickables('img');
}
FBSM.R.updateControls = function(activeFrame, markActive){
	if(markActive){
		$('.promoChangeLink, .miniIndicator').removeClass('activePromoNumber');
		$('#promo_'+(activeFrame+1)+', #miniInd_'+(activeFrame+1)).addClass('activePromoNumber');
	}
}
FBSM.R.setPauseButton = function(){
	if(FBSM.R.rotator_div.attr('class') == 'merchRotateMain_v2'){
		if(FBSM.R.rotator_autorotate == 0){
			$('.rotator_pause').attr('src', FBSM.R.rotator_paused);
		}else{
			$('.rotator_pause').attr('src', FBSM.R.rotator_pause);
		}
	}
}
FBSM.R.advancePromo = function(direction){
	direction == '+' ? FBSM.R.rotator_current++ : FBSM.R.rotator_current--;
	if(FBSM.R.rotator_current < 0){
		FBSM.R.rotator_current = ($('.merchRotate').size()-1);
	}else if(FBSM.R.rotator_current == ($('.merchRotate').size())) FBSM.R.rotator_current = 0;
}
FBSM.R.addClickables = function(span){
	/*Lousy span variable is needed for IE6.pngfix() support*/
	$(span+'.rotator_prev').click(function(){
		FBSM.R.advancePromo('-');
		FBSM.R.showLoadingFrame(FBSM.R.rotator_current);
		FBSM.R.rotator_autorotate = 0;
		FBSM.R.animateSwap(FBSM.R.rotator_current);
	});
	$(span+'.rotator_next').click(function(){
		FBSM.R.advancePromo('+');
		FBSM.R.showLoadingFrame(FBSM.R.rotator_current);
		FBSM.R.rotator_autorotate = 0;
		FBSM.R.animateSwap(FBSM.R.rotator_current);
	});
	$(span+'.rotator_pause').click(function(){
		FBSM.R.rotator_autorotate == 0 ? FBSM.R.rotator_autorotate = 1 : FBSM.R.rotator_autorotate = 0;
		FBSM.R.setPauseButton();
		if(FBSM.R.rotator_autorotate == 1){
			FBSM.R.rotator_beenClicked = 0;
			FBSM.R.swap();
		}
	});
}
