﻿// JavaScript Document

$(document).ready(function(){
var navp = naviPopup();
	
	// build brandbox
	var bb  = new brandBox();
	
	// build carousel
	var car = new carousell();

	// build newsTicker
	var nt =  newsTicker();
	
	
	
	// bind the opener scripts for the navigation
	
	$("#subNav a").each(function (){
		if($(this).attr("id") != "popupOpener"){				  
			$(this).hover(closePopup, null);
		}
	});
	
	$("#popupOpener").hover(openPopup, null);
	
	
});

function openPopup(){
	var target = $("#naviPopup");
	target.slideDown();
	target.hover(null,closePopup);
}

function handleSelect(target){
	
	if($(target, 'option:selected').attr('value') != ""){
		//location.href=$(target, 'option:selected').attr('value');
		window.open($(target, 'option:selected').attr('value'), 'newWindow');
	}
}


function closePopup(){
	 $("#naviPopup").slideUp();
}

function brandBox(){
	var imageLength = 585;
	var selIamge = '<img class="selectedImage" src="/content/s2/images/2009-home/brandbox/brandBox_icon_sel.png"/>';//'<img class="selectedImage" src="/content/s2/images/2009-home/brandbox/brandBox_icon_sel.png"/>';
	var unselImage = " "//'<img class="unselectedImage" src="/content/s2/images/2009-home/brandbox/brandBox_icon_unsel.png"/>';
	
	
	$(".brandBoxLeft").click(showPrevImage);
	
	$(".brandBoxRight").click(showNextImage);
	
	// add the position buttons to the bottom
	var counter = 1;
	$(".brandBoxImagesList li").each(function(){
		var target = $(this); // target.html() if you want to get some content out of it
		var elem = '<a href="#" class="png_bg" itemid="'+ counter +'">'+ selIamge +'</a> ';
		var linkElem = $(elem);
		
		linkElem.click(function(){
			showImage($(this).attr("itemid"));
			return false;
		});
		
		var listElem = $("<li/>").append(linkElem);
		
		$(".brandBoxLinkList").append(listElem);
		
		counter++;
	});
	
	$("#positionContainer > a:first").addClass("active");
	
	
	//number the imagesCount
	
	
	var counter = 1;
	var imagesCount = 0;
	$(".brandBoxImagesList li").each(function(){
		var target = $(this);
		target.attr("itemid", counter);
		
		counter++;
		imagesCount++;
	});
	
	
	var counter = 1;
	
	$(".brandBoxLinkContainer a").each(function(){
		var target = $(this);
		target.attr("itemid", counter);
		counter++;
	});
	
	
	
	// set the container width to match the images
	$(".brandBoxImagesList").css("width", (imagesCount*imageLength) +"px");
	
	//number the contents
	var counter = 1;
	$(".brandBoxOverayContentItem").each(function(){
		var target = $(this);
		target.attr("itemid", counter);
		
		var content = target.find(".heading").text();
		
		var tooltip = $('<div class="brandBoxToolTip" style="display:none"><div class="brandBoxToolTipContent png_bg"><div class="padding">' + content + '</div></div><div class="brandBoxToolTipFooter png_bg"> </div></div>');
		
		$('.brandBoxLinkList li a[itemid="'+ counter + '"]').append(tooltip);
		
		$('.brandBoxLinkList li a[itemid="'+ counter + '"]').hover(showToolTip,hideToolTip);
		
		function showToolTip(){
			$(this).children(".brandBoxToolTip").fadeIn(50);
		}
		function hideToolTip(){
			$(this).children(".brandBoxToolTip").fadeOut(50);
		}
		
		counter++;
	});
	
	
	
	
	var currentImage = 0;
	var prevImage = 0;
	function showImage(itemid){
		
		$('.brandBoxLinkContainer a[itemid="'+ prevImage + '"]').css("display","none");
		$('.brandBoxLinkContainer a[itemid="'+ itemid + '"]').css("display","block");
		
		
		$('.brandBoxOverayContentItem[itemid="'+ prevImage + '"]').fadeOut(500,function(){
																							
																								   $('.brandBoxOverayContentItem[itemid="'+ itemid + '"]').fadeIn(500);
																								   });
		currentImage = itemid;
		
		prevImage = currentImage;
		
		
				$(".brandBoxImagesScroller").animate({ 
		left: 0 - ( (itemid-1) * imageLength)
		}, 1500 );	

		
		// updates active sign
		$(".brandBoxLinkList  a").each(function(){
			if($(this).attr("itemid") != itemid){
				$(this).removeClass("selected");
				$(this).addClass("unselected");
				
				$(this).find("img").each(function(){
												$(this).css("display","none");
												  });
				//$(this).attr("class","unselected");
			}else{
				$(this).removeClass("unselected");
				$(this).addClass("selected");
								$(this).find("img").each(function(){
												$(this).css("display","block");
												  });
			}
		}); 
		
		return false;
	}
	
	function showNextImage(){
		currentImage++;
		if(currentImage > imagesCount){
			currentImage = 1;
		}			
		showImage(currentImage);
		return false;
	}
	
	function showPrevImage(){
		currentImage--;
		if(currentImage < 1){
			currentImage = imagesCount;
		}			
		showImage(currentImage);
		return false;
	}
	$("#scroller").css("left","1000px");
	showImage(1);
	
	/* disable timers */
	$(".brandBox").hover(disableBrandTimer,enableBrandTimer);
	
	
	function timerToNext(){
		currentImage++;
		if(currentImage > imagesCount){
			currentImage = 1;
		}			
		showImage(currentImage);
	}
	
	
	function disableBrandTimer(){
		$(document).stopTime("brandtimer");
	}
	
	function enableBrandTimer(){
		$(document).everyTime(6000, "brandtimer",timerToNext);
	}
	$(document).everyTime(6000, "brandtimer",timerToNext);
	
	createBrandBoxGoogleTrackings();
}

function createBrandBoxGoogleTrackings(){
  
  function googleAction(){
	    try{
		   pageTracker._trackEvent('BRANDBOX', 'Click', $(this).attr("href"));
		}catch(err){
					
		}
  }
  
  $(".brandBoxLinkContainer a").each(
		function(){
			var target = $(this);
			target.click(googleAction);
		});
    $(".brandBoxOverayContentItem a").each(
		function(){
			var target = $(this);
			target.click(googleAction);
		});
}


function carousell(){
	var imageLength = 150;
	var startX = 70;
	
	$(".carousellLeft").click(showPrevImage);
	
	$(".carousellRight").click(showNextImage);
	
	
	
	//number the imagesCount
	var counter = 1;
	var imagesCount = 0;
	$(".carousellImagesListItem").each(function(){
		var target = $(this);
		target.attr("itemid", counter);
		
		counter++;
		imagesCount++;
	});
	
	
	//number the link
	var counter = 1;
	$(".carousellCenter").each(function(){
		var target = $(this);
		target.attr("itemid", counter);
		
		counter++;
	});
	
	
	// set the container width to match the images
	$(".carousellImagesScroller").css("width", (imagesCount*imageLength) +"px");
	
	var currentImage = 0;
	var prevImage = 0;
	function showImage(itemid){
		
		
		$('.carousellCenter[itemid="'+ prevImage + '"]').css("display","none");
		$('.carousellCenter[itemid="'+ itemid + '"]').css("display","block");
		
		
		/*
		
		$('.carousellOverayContentItem[itemid="'+ prevImage + '"]').fadeOut(500,function(){
																							
																								   $('.carousellOverayContentItem[itemid="'+ itemid + '"]').fadeIn(500);
																								   });*/
		
		$('.carousellImagesListItem[itemid="'+ prevImage + '"]').animate({ 
			top: 0,opacity:0.5
		}, 1000 );
		
		$('.carousellImagesListItem[itemid="'+ itemid + '"]').animate({ 
			top: 10,opacity:1
		}, 1000 );	
		
		currentImage = itemid;
		
		prevImage = currentImage;
		
		
		$(".carousellImagesScroller").animate({ 
			left: startX - ( (itemid-1) * imageLength)
		}, 1500 );	

		
		return false;
	}
	
	function showNextImage(){
		currentImage++;
		if(currentImage > imagesCount){
			currentImage = 1;
		}			
		showImage(currentImage);
		return false;
	}
	
	function showPrevImage(){
		currentImage--;
		if(currentImage < 1){
			currentImage = imagesCount;
		}			
		showImage(currentImage);
		return false;
	}
	$("#scroller").css("left","1000px");
	
	//add fade to all images
	
	//$('.carousellImagesListItem').css("opacity","0.5")
	
	var centerNumber = Math.ceil(imagesCount / 2);
	
	$('.carousellImagesListItem[itemid!="'+ centerNumber + '"]').css("opacity","0.5")
	$('.carousellImagesListItem[itemid="'+ centerNumber + '"]').css("top","10px")
	
	//showImage(1);
	
	currentImage = centerNumber;
	prevImage = centerNumber;
	
	// need to show the center object
	
	/* remove the timer
	$(".carousell").hover(disableCarousellTimer,enableCarousellTimer);
	

	function timerToNext(){
		currentImage++;
		if(currentImage > imagesCount){
			currentImage = 1;
		}			
		showImage(currentImage);
	}
	
	function disableCarousellTimer(){
		$(document).stopTime("carouselltimer");
	}
	
	function enableCarousellTimer(){
		$(document).everyTime(6000, "carouselltimer",timerToNext);
	}
	
	$(document).everyTime(6000, "carouselltimer",timerToNext);*/
}

function newsTicker(){
	
	//number the imagesCount
	var counter = 1;
	var newsItems = 0;
	$(".newstickerNewsItem").each(function(){
		var target = $(this);
		target.attr("itemid", counter);
		
		counter++;
		newsItems++;
	});
	
	var currentItem = 1;
	
	function timerAnim(){
		$('.newstickerNewsItem[itemid="'+ currentItem + '"]').animate({top: -39}, 500,resetTop);
		currentItem++;
		
		if(currentItem > newsItems){
			currentItem = 1;
		}
		
		
		$('.newstickerNewsItem[itemid="'+ currentItem + '"]').animate({top: 0}, 500);
		function resetTop(){
			var target = currentItem-1;
			$(this).css("top","39px");
		}
	}
	
	//timerAnim();
	
	$(document).everyTime(4500, "nestickertimer", timerAnim);

}

function naviPopup(){
	var target = $("#naviPopup");
	
	
	var highest = 0;
	
	target.css("display", "block");
	target.css("visibility", "hidden");
	
	target.find(".naviContent").each(function(){
			$(this).css("display", "block");
			if($(this).height() > highest){
				highest = $(this).height();
			}
	});
	
	target.find(".naviContents").css("height", highest+"px");
	
	
	//number the links and containers
	var counter = 1;
	
	target.find(".naviContent").each(function(){
		var target = $(this);
		target.attr("itemid", counter);
		// set the first to visible
		if(counter == 1){
			target.css("display","block");
		}else{
			target.css("display","none");
		}
	
		counter++;
	});
	
	
	
	var counter = 1;
	target.find(".naviItems a").each(function(){
		var target = $(this);
		target.attr("itemid", counter);
		
		//set the fist to active
		
		if(counter == 1){

			target.addClass("active");
		}
		
		
		// add function to show the right container when clicked	
		
		target.click(showContainer);
		
		counter++;
	});
	
	
	
	
	target.css("visibility", "visible");
	target.css("display","none");
	
	
	
	target.find('.naviContent[itemid="1"]').css("display","block");
	
	
	// set the first as the previous item
	var prevItem = target.find('.naviItems a[itemid="1"]');
	
	function showContainer(){
		var callerLink = $(this);
		
		// return if clicked the active
		
		if(callerLink.attr("itemid") == prevItem.attr("itemid")){
			return;
		}
		
		callerLink.addClass("active");
		prevItem.removeClass("active");
		
		target.find('.naviContent').css("display","none");
		target.find('.naviContent[itemid="'+callerLink.attr("itemid")+'"]').css("display","block");
		
		/*target.find('.naviContent[itemid="'+prevItem.attr("itemid")+'"]').slideUp(500,hide);
		target.find('.naviContent[itemid="'+callerLink.attr("itemid")+'"]').slideDown(1000);
		function hide(){
			
		}*/
		
		prevItem = callerLink;
		return false;
	}
	
	
	
}
