// JavaScript Document

/* INIT */
var site = {
	global: {
                slide_interval: '',

		initTabHomepage: function () {
			//When page loads...
			$(".item-container").hide(); //Hide all content
			$("ul.list-galleryTab li:first a").addClass("active").show(); //Activate first tab
			$(".item-container:first").show(); //Show first tab contenta

			var slide_it = function (next_li_a) {
				var active_elem = $("ul.list-galleryTab li a.active"),
				    active_li = active_elem.parent('li'),
				    next_li, activeTab;

				if (!next_li_a) {
					next_li = active_li.is("li:last-child") ? $("ul.list-galleryTab li:first") : active_li.next('li');
					next_li_a = next_li.children('a');
				}
				
				$("ul.list-galleryTab li a").removeClass("active"); //Remove any "active" class
				next_li_a.addClass("active"); //Add "active" class to selected tab
				$(".item-container").hide(); //Hide all tab content

				activeTab = next_li_a.attr("href"); //Find the href attribute value to identify the active tab + content
				$(activeTab).fadeIn(); //Fade in the active ID content
				return false;
			}
			//On Click Event
			$("ul.list-galleryTab li a").click(function() {
                                clearInterval(site.global.slide_interval);
				slide_it($(this));
				return false;
			});

			//Bug: setInterval is passing a number into the function reference unless explicitly expressed as below.
			site.global.slide_interval = setInterval(function(){
				slide_it()
			}, 6000);
		},
		
		
		initWowWindow: function () {
			jQuery('a[rel=video]').wowwindow({
				draggable: true,
		                height: 225,
                		width: 400,
		                videoIframe: false
			});
       
			jQuery('.popups a').wowwindow({
				draggable: true,
		                height: 225,
                		width: 400
			});
       
	        	jQuery('#youtube-auto-thumbnails a').wowwindow({
        		        draggable: true,
	                	width: 480,
        		        height: 390,
	                	videoIframe: false,
        		        autoYouTubeThumb: 'default'
			});
		},

		initImpactPopup: function () {
			jQuery("div.box-impactContainer ul.list-famousPeople li:not(.item-story)").mouseover(function(){
				var itemSummary = jQuery("div.item-storyPop");					
				var popupW = itemSummary.innerWidth();
				var popupH = itemSummary.innerHeight();
				
				var pos = jQuery(this).position();
				
				itemSummary.css("left", (jQuery(this).innerWidth()-popupW)/2+pos.left );
				itemSummary.css("top", (jQuery(this).innerHeight()-popupH)/2+pos.top );
				itemSummary.html(jQuery(this).children("div.item-storySummary").html());
				itemSummary.fadeIn();//("hide");
				
			});
			
			jQuery("div.item-storyPop").mouseleave(function(){
				jQuery(this).fadeOut(10);//addClass("hide");
				itemSummary.text("");
			});
		}
		
	}
};

jQuery(document).ready(function() {
	site.global.initTabHomepage();
	site.global.initImpactPopup();
	site.global.initWowWindow();
});



