/*
Title:      Main JavaScript	

*/

// ------[ IE6 Cache Control (remove flicker on mouseover) ]------------------------------------------------- //	
try {
  document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}

// ------[ Master Onload for all page. ]------------------------------------------------- //	
window.PPW = window.PPW || {};

PPW = {	};

// ------[ Feature, for the featured items on the home ]------------------------------------------------- //	
PPW.TabBrowser = {
	currentTab: 0,
	Init : function() {	
		//Get all the LI from the #tabMenu UL
		if($('#tabMenu > li')) {
			$('#tabMenu > li').click(function(){		
				//perform the actions when it's not selected
				if (!$(this).hasClass('selected')) {    						
					$('#tabMenu a').attr("href", "javascript:void(0);");
					
					//remove the selected class from all LI    
					$('#tabMenu > li').removeClass('selected');
					
					//After cleared all the LI, reassign the class to the selected tab
					$(this).addClass('selected');
					
					//Hide all the DIV in .boxBody
					$('.boxBody div').hide();
					
					//Look for the right DIV index based on the Navigation UL index
					$('.boxBody div.boxContent:eq(' + $('#tabMenu > li').index(this) + ')').show();						
					
					PPW.TabBrowser.currentTab = $('#tabMenu > li').index(this);
				}		
			});
			$('#tabMenu').mouseover(function(){
				clearTimeout(tabtimer);
			}).mouseout(function(){
				PPW.TabBrowser.setTimer();
			});
			
			$('.boxBody').mouseover(function(){
				clearTimeout(tabtimer);
			}).mouseout(function(){
				PPW.TabBrowser.setTimer();
			});		
			
			PPW.TabBrowser.setTimer();

		}			
	},
	goToTab : function(tabToShow,callback){
		clearTimeout(tabtimer);

		
		// Select the correct paging item
		$('#tabMenu > li.selected').removeClass('selected');
		$('.boxBody div').hide();
		$('#tabMenu > li:eq('+tabToShow+')').addClass('selected');		
		$('.boxBody div.boxContent:eq('+tabToShow+')').show();
		
		
		PPW.TabBrowser.currentTab = tabToShow;
		
		if(callback) callback();
	},	
	setTimer : function(){
		tabtimer = setTimeout(function(){
			nextTab = PPW.TabBrowser.currentTab+1;
			if(nextTab == $('#tabMenu > li').size()){
				nextTab = 0;
			}
			
			PPW.TabBrowser.goToTab(nextTab,function(){ PPW.TabBrowser.setTimer(); });
		},4000);
	}	
};

$(document).ready(function(){
	PPW.TabBrowser.Init();
});


/* ------------------------------------------------------------------------
	feature, for the featured items on the home
------------------------------------------------------------------------- */

	feature = {
		currentPage:0,
		init : function() {
			$('#feature .items li:gt(0)').hide();
			
			// Build the paging
			toInject = "";
			$('#feature .items li').each(function(i){
				toInject += "<li><a href='#' onclick='feature.goToPage("+i+");return false;'>"+(i+1)+"</a></li>";
			});
			
			$('#feature .paging').html(toInject);
			$('#feature .paging li:first').addClass('selected');
			
			$('#feature').hover(function(e){
				clearTimeout(slideshow);
			},
			function(){
				feature.setTimer();
			});
			
			feature.setTimer();
		},
		goToPage : function(pageToShow,callback){
			clearTimeout(slideshow);
			
			// Fade out the current page
			$('#feature .items li:eq('+feature.currentPage+')').fadeTo('normal',0,function(){
				$(this).hide();
				
				$('#feature .items li:eq('+pageToShow+')').css('opacity',0).show().fadeTo('normal',1);
			});
			
			// Select the correct paging item
			$('#feature .paging li.selected').removeClass('selected');
			$('#feature .paging li:eq('+pageToShow+')').addClass('selected');
			
			feature.currentPage = pageToShow;
			
			if(callback) callback();
		},
		setTimer : function(){
			slideshow = setTimeout(function(){
				nextPage = feature.currentPage+1;
				if(nextPage == $('#feature .paging li').size()){
					nextPage = 0;
				}
				
				feature.goToPage(nextPage,function(){ feature.setTimer(); });
			},10000);
		}
	}

	
