var currentTabTextItem=new Array(1,1,1,1,1,1)
//var currentTextItem=1;
var iTimerRotation=null;
var iTimerRotationMainService=null;
var intervalRotationMainService=1000;
//we have main service
//inside main service we have subservice
//inside we have item

//With space gallery rotate corrresponding texts as well,
//this function is executed when user clicks on item
function rotateTexts(el){
	stopRotation();
	if(iTimerRotationMainService!=null)window.clearTimeout(iTimerRotationMainService);
	stopMainService();
	
	//number of item divs we have
	total=$(el).find('.item').length;
	
	switch (el.id){
		case 'print':
		  currentTextItem=currentTabTextItem[0]
		break;
		case 'websites':
			currentTextItem=currentTabTextItem[1]
		break;
		case 'e-commerce':
			currentTextItem=currentTabTextItem[2]
		break;
		case 'online_software':
			currentTextItem=currentTabTextItem[3]
		break;
		case 'mapcms':
			currentTextItem=currentTabTextItem[4]
		break;
	}
	
	currentTextItem++;
	if(currentTextItem>total){
		currentTextItem=1
	}
	
	switch (el.id){
		case 'print':
		  currentTabTextItem[0]=currentTextItem
		break;
		case 'websites':
			currentTabTextItem[1]=currentTextItem
		break;
		case 'e-commerce':
			currentTabTextItem[2]=currentTextItem
		break;
		case 'online_software':
			currentTabTextItem[3]=currentTextItem
		break;
		case 'mapcms':
			currentTabTextItem[4]=currentTextItem
		break;
	}
	//hide description that is shown globally (sub-service description)
	$(el).find('.section-text').hide();
	
	//hide description of all items
	$(el).find('.item').hide();
	
	//loop through each item and select next item
	n=0;
	$(el).find('.item').each(function(){
			n++
			if(n==currentTextItem){
				$(this).show();
				
			}
	});
	
	//to add reflection on the image
		//$(el).find("img").reflect();
		
	//user is viewing items let's stop sub-service rotation for a while
	
}

//auto rotate service tabs
 $(document).ready(function() {

		   rotateMainService(true);
		   window.setTimeout("startRotation(true)",6000);
		   //$('#service-highlight> ul').tabs({onSelect:mainServiceRotated});


			$('.main-services.ui-tabs-hide .sub-services > ul').tabs({ onSelect:checkTab}).tabs('rotate',0, false);
		   
		    //on click on image show image & it's detail
		   $('#e-commerce').spacegallery({loadingClass: 'loading',meroAfter:rotateTexts,before:function(){stopRotation();}});	   
			//set time to load  reflection and image of first frame of service banner
		   //window.setTimeout('$("#e-commerce").find("img").reflect();',3000);	
		   
		   //on click on image show image & it's detail
		   $('#online_software').spacegallery({loadingClass: 'loading',meroAfter:rotateTexts,before:function(){stopRotation();}});	   
			//set time to load  reflection and image of first frame of service banner
		   //window.setTimeout('$("#online_software").find("img").reflect();',3000);	
		   
		   //on click on image show image & it's detail
		   $('#websites').spacegallery({loadingClass: 'loading',meroAfter:rotateTexts,before:function(){stopRotation();}});	   
			//set time to load  reflection and image of first frame of service banner
		   //window.setTimeout('$("#websites").find("img").reflect();',3000);	
		   
		   //on click on image show image & it's detail
		   $('#print').spacegallery({loadingClass: 'loading',meroAfter:rotateTexts,before:function(){stopRotation();}});	   
			//set time to load  reflection and image of first frame of service banner
		   //window.setTimeout('$("#print").find("img").reflect();',3000);	
		   
		   //on click on image show image & it's detail
		  $('#mapcms').spacegallery({loadingClass: 'loading',meroAfter:rotateTexts,before:function(){stopRotation();}});	   
			//set time to load  reflection and image of first frame of service banner
		   //window.setTimeout('$("#mapcms").find("img").reflect();',3000);		   
		   
		   //on click on image show image & it's detail
		  // $('#audio-video').spacegallery({loadingClass: 'loading',meroAfter:rotateTexts,before:function(){stopRotation();$('#audio-video').find("img").unreflect();}});	   
			//set time to load  reflection and image of first frame of service banner
		 //  window.setTimeout('$("#audio-video").find("img").reflect();',3000);	
		
		   
	   //stop rotation when clicked on div 
		 $('.sub-services').click(function(){
			stopRotation();
			
		})
		 
		 // 
		 $('#service-highlight  ul li a').click(function(){
			 intervalRotationMainService=4000;	
		 })
		   

});

 function stopRotation(){
	 	$('.sub-services > ul').tabs('rotate', 0, false);
		if(iTimerRotation!=null)window.clearTimeout(iTimerRotation);
		iTimerRotation=window.setTimeout("startRotation()",5000);
 }
 
 
 function startRotation(first){
	 //if there is no sub-services in first tab rotate main service
	 if( first && $('.main-services.ui-tabs-show .sub-services > ul').length <1){
		iTimerRotationMainService=window.setTimeout("rotateMainService()",1000);
		return false;
	 }
	 	if(iTimerRotation!=null)window.clearTimeout(iTimerRotation);
		if(iTimerRotationMainService!=null)window.clearTimeout(iTimerRotationMainService);

	 	$('.main-services.ui-tabs-show .sub-services > ul').tabs({ onSelect:checkTab}).tabs('rotate',3000, false);
		
		//show subnav here
		$('.subnav').show();
 }
 
 function checkTab(index,tabs){
	if(index!=tabs.length-1){
		return false;
	}
	//reached last tab let's stop rotation
	stopRotation()
	
	//select next main-service;
	intervalRotationMainService=1000;
	if(iTimerRotationMainService!=null)window.clearTimeout(iTimerRotationMainService);
	iTimerRotationMainService=window.setTimeout("rotateMainService()",intervalRotationMainService);


	//let's continue to start rotation of subservices
	// startRotation()
	
 }
 //let's stop the main service for a while
 function mainServiceRotated(index, tabs){
	 //main service needs to be rotated only once so let's stop it
	 stopRotation()
	 stopMainService();
	
	if(iTimerRotationMainService!=null)window.clearTimeout(iTimerRotationMainService);

	if($('.main-services.ui-tabs-show .sub-services > ul').length <1){
		iTimerRotationMainService=window.setTimeout("rotateMainService()",intervalRotationMainService);
		
	}else{
		//start rotating sub-services;
		
		 startRotation()
	}
	$(".ui-tabs-show").attr("style","opacity:1;");
	 
 }
 
 function stopMainService(){
	 
 	$('#service-highlight > ul').tabs('rotate',0,false);
 }
 var mainTab
 
 
 function rotateMainService(first){
	 if(first){
		 mainTab=$('#service-highlight > ul').tabs({onSelect:mainServiceRotated });}
	 
	 else{
		mainTab.tabs('rotate',3000,false);
	 }
	 //main service needs to be rotated only once
	
 }

 
 jQuery(document).ready(function() {
	jQuery("#home_blocks .item:eq(3)").addClass("margin");
	jQuery("#home_blocks .item:gt(2)").hide();
	
	//to show/hide highlights of homepage
	jQuery("#home_blocks #buttons .show").click(function(){
		jQuery("#home_blocks .item:gt(2)").show();
		jQuery("#home_blocks .hide").show();
		jQuery("#home_blocks .show").hide();

	})
	
	jQuery("#home_blocks #buttons .hide").click(function(){
		jQuery("#home_blocks .item:gt(2)").hide();
		jQuery("#home_blocks .hide").hide();
		jQuery("#home_blocks .show").show();
	})

	jQuery('.show-teaser').hover(function(){
         jQuery(this).parent().parent().find('.teaser').show();
         
        jQuery('.teaser').click(function(){
         jQuery(this).hide();
         })
        jQuery('.teaser').mouseout(function(){
         jQuery(this).hide();
         })
        
         jQuery('.teaser span a').click(function(){
         jQuery('.teaser').hide();
         })
    })
							

});
 
 
 
