/* TIMEINC namespace initializer */
if (typeof TIMEINC != 'object' || !TIMEINC) { window.TI = window.TIMEINC = {}; }
if (typeof TIMEINC.TAB_SLIDER != 'object' || !TIMEINC.TAB_SLIDER) {
	TIMEINC.TAB_SLIDER = {
		slider: null,
		currentTab: 0,
		isPlaying: true,
		delay: 5000,
		timer1: null,
		tabCount: null,
		init: function(ob){
			TIMEINC.TAB_SLIDER.slider = $("#"+ob);
			TIMEINC.TAB_SLIDER.tabCount = TIMEINC.TAB_SLIDER.slider.children(".tab").length;
			
/* bind controllers with onclick functions */
			TIMEINC.TAB_SLIDER.slider.children("#ctrls_cont").children("#ctrls").children("#back").click(function(e) {
				e.preventDefault();
				TIMEINC.TAB_SLIDER.goToTab((TIMEINC.TAB_SLIDER.currentTab + TIMEINC.TAB_SLIDER.tabCount - 1) % TIMEINC.TAB_SLIDER.tabCount);
			});
			TIMEINC.TAB_SLIDER.slider.children("#ctrls_cont").children("#ctrls").children("#playpause").click(function(e) {
				e.preventDefault();
				TIMEINC.TAB_SLIDER.togglePlay();
			});
			TIMEINC.TAB_SLIDER.slider.find("#ctrls_cont").children("#ctrls").children("#forward").click(function (e) {
				e.preventDefault();
				TIMEINC.TAB_SLIDER.goToTab((TIMEINC.TAB_SLIDER.currentTab + 1) % TIMEINC.TAB_SLIDER.tabCount);
			});
			TIMEINC.TAB_SLIDER.slider.find("ul > li > a").click(TIMEINC.TAB_SLIDER.tabClick);
		
/* hide slides after first and set up images for them */
			TIMEINC.TAB_SLIDER.slider.children(".tab:gt(0)").each(function(i) {
				$(this).hide();
				var imgHref = $(this).children("h2.demoHeaders").children("a").attr("href");
				var imgSrc = $(this).children("a.img_link").attr("href");
				var imgAlt = $(this).children("a.img_link").attr("title");
				var imgLinkObj = $(this).find(".img_link");
				var imgLinkObjInner = imgLinkObj.html();
				imgLinkObj.replaceWith('<a href="' + imgHref + '">'+imgLinkObjInner+'<img src="' + imgSrc + '" alt="' + imgAlt + '" /></a>');
			});
		
/* wait until images are loaded then start rotation */
			TIMEINC.TAB_SLIDER.slider.children(".tab:gt(0) img").load();
			TIMEINC.TAB_SLIDER.timer1 = setTimeout('TIMEINC.TAB_SLIDER.doRotation()', TIMEINC.TAB_SLIDER.delay);
		},
		
		change: function(tabNum){
			TIMEINC.TAB_SLIDER.currentTab = tabNum;
			var thisSlider = TIMEINC.TAB_SLIDER.slider.find(".tab").eq(tabNum);
			var allTabLinks = TIMEINC.TAB_SLIDER.slider.find("ul > li > a");
			var thisTabLink = TIMEINC.TAB_SLIDER.slider.find("ul > li").eq(tabNum).children("a");
			allTabLinks.attr("href", "#");
			TIMEINC.TAB_SLIDER.slider.find(".tab").hide().eq(tabNum).show();
			TIMEINC.TAB_SLIDER.slider.find("ul > li").removeClass("ui-state-active").eq(tabNum).addClass("ui-state-active");
			var newUrl = thisSlider.children("a:eq(0)").attr("href");
			thisTabLink.attr("href", newUrl);
			allTabLinks.click(TIMEINC.TAB_SLIDER.tabClick);
			thisTabLink.unbind("click");
		},
		doRotation: function(){
			/* change picture, wait some seconds, repeat */
			TIMEINC.TAB_SLIDER.currentTab = (TIMEINC.TAB_SLIDER.currentTab + 1) % TIMEINC.TAB_SLIDER.tabCount;
			TIMEINC.TAB_SLIDER.change(TIMEINC.TAB_SLIDER.currentTab);
			if (TIMEINC.TAB_SLIDER.currentTab == 0) {
				TIMEINC.TAB_SLIDER.togglePlay();
			}
			else 
				TIMEINC.TAB_SLIDER.timer1 = setTimeout("TIMEINC.TAB_SLIDER.doRotation()", TIMEINC.TAB_SLIDER.delay);
		},
		togglePlay: function(){
			/* executed when the play/pause button is selected */
			TIMEINC.TAB_SLIDER.isPlaying = !TIMEINC.TAB_SLIDER.isPlaying;
			$("#playpause").attr("class", TIMEINC.TAB_SLIDER.isPlaying ? "pause" : "play"); /*carousel controls*/
			if (TIMEINC.TAB_SLIDER.isPlaying) 
				TIMEINC.TAB_SLIDER.doRotation(); /* restart the image loop */
			else 
				clearTimeout(TIMEINC.TAB_SLIDER.timer1); /* stop the image loop */
		},
		goToTab: function(tabNum){
			/*executed when a tab is clicked */
			TIMEINC.TAB_SLIDER.isPlaying = false;
			$("#playpause").attr("class", "play"); /*carousel controls*/
			clearTimeout(TIMEINC.TAB_SLIDER.timer1);
			TIMEINC.TAB_SLIDER.change(tabNum);
		},
		
		tabClick: function(){
			var position = $(this).parent().prevAll("li").length;
			$("#pauseplay").attr("class", "play"); /*carousel controls*/
			TIMEINC.TAB_SLIDER.goToTab(position);
			return false;
		}
	}
}
$(document).ready(function() {
	TIMEINC.TAB_SLIDER.init("slider");
	//start : homepage only -- link loading for middle medium tout
	var blogsLink = $('#medium_touts div.mtout:eq(1) h3 a');
	var imgLink = $('#medium_touts div.mtout:eq(1) a:has(img)');
	imgLink.attr('href', blogsLink.attr('href'));
	//end : homepage only -- link loading for middle medium tout
	
});