var currentIndex = 0;
var jumpindex = -1;
//Rotated banner
function rotated(){
	var container = jQuery('#h_banner div.image_banner');
	
	var numitem = container.length;
	if(jumpindex != currentIndex){//if click on the same current banner not do animate
		jQuery(container[currentIndex]).animate({left: -970},400, function(){
			jQuery(this).css({left: 970});
		});
	}
	if(jumpindex == -1){//if default not click on navigatable
		currentIndex +=1;
	}
	else{
		
		currentIndex = jumpindex;
	}
	jumpindex = -1;//reset the jumbindex
	
	if(currentIndex == numitem){
		currentIndex = 0;
	}
	jQuery(container[currentIndex]).animate({left: 0},400);
	
	//Navigate on Home banner
	var navigatable = jQuery('div.h_navigate a');
	jQuery(navigatable).each(function(index){
		jQuery(this).removeAttr('id');		
	});
	jQuery(navigatable[currentIndex]).attr('id','current');
	//End Navigate on Home banner
	
}
//End Rotated banner
//Slide News
var news_currentIndex = 0;
function slide_news(){
	var news_content = jQuery('div#latestnews-box ul li');
	var newsitem = news_content.length;
	jQuery(news_content[news_currentIndex]).fadeOut();
	
	news_currentIndex +=1;
	
	if(news_currentIndex == newsitem){
		news_currentIndex = 0;
	}
	jQuery(news_content[news_currentIndex]).fadeIn();
}
//End Slide News
var currentLogoIndex = 0;
function slideSwitch_Logo(increase){
	
	var container_logo = jQuery('#casestudy div.box_image');
	var container_title = jQuery('#casestudy span.client_name');
	var numitem = container_logo.length;
	var title_position = 0;
	var logo_position = 0;
	
	if((currentLogoIndex < numitem-1 && increase == 1) || (currentLogoIndex > 0 && increase == -1)){
		jQuery(container_logo[currentLogoIndex]).animate({left: increase*(-244)},400);
		jQuery(container_title[currentLogoIndex]).animate({left: increase*(-244)}, 400);
				
		currentLogoIndex += increase;
		title_position = (244-jQuery(container_title[currentLogoIndex]).width())/2;
		logo_position = 244-jQuery(container_logo[currentLogoIndex]).width(); //Change style for display image
		//logo_position = jQuery(container_logo[currentLogoIndex]).width();
		//middle_position = parseInt((106-jQuery(container_logo[currentLogoIndex]).height())/2);
		
		jQuery(container_logo[currentLogoIndex]).animate({left: logo_position},400);
		jQuery(container_title[currentLogoIndex]).animate({left:title_position}, 400);
		
		//jQuery(container_logo[currentLogoIndex]).css('top',middle_position);
		
		if(currentLogoIndex == numitem-1){
			jQuery('.moveimage img.iconright').attr('src',jQuery('.moveimage img.iconright').attr('src').replace('casestudy-icon', 'casestudy_disable-icon'));
		}
		else{
			jQuery('.moveimage img.iconright').attr('src',jQuery('.moveimage img.iconright').attr('src').replace('casestudy_disable-icon', 'casestudy-icon'));
		}
		if(currentLogoIndex == 0){
			jQuery('.moveimage img.iconleft').attr('src',jQuery('.moveimage img.iconleft').attr('src').replace('casestudy_enable-icon-left', 'casestudy-icon-left'));
		}
		else{
			jQuery('.moveimage img.iconleft').attr('src',jQuery('.moveimage img.iconleft').attr('src').replace('casestudy-icon-left', 'casestudy_enable-icon-left'));
		}
	}
}

var currentCaseStudyIndex = 0;
function current_study_page(){
	//Loop container casestudy page	
		var casestudypage = jQuery('div#container-left div.casestudy_page');
		casestudypage.each(function(index){
			if(jQuery(this).attr('id') == "current_content"){
				currentCaseStudyIndex = index;
			}			
		});
		
	//End loop	
}
function slideSwitch_CaseStudy(increase){
	
	var container_CaseStudy = jQuery('.box-images img');
	
	var numitem = container_CaseStudy.length;

	//current_study_page();//Get current Index
	
	if((currentCaseStudyIndex < numitem-1 && increase == 1) || (currentCaseStudyIndex > 0 && increase == -1)){
		jQuery(container_CaseStudy[currentCaseStudyIndex]).animate({left: increase*(-583)},400);
				
		currentCaseStudyIndex += increase;
			
		jQuery(container_CaseStudy[currentCaseStudyIndex]).animate({left: 0},400);
		
		if(currentCaseStudyIndex == numitem-1){
			jQuery('.box-casestudy img.icon-next').attr('src',jQuery('.box-casestudy img.icon-next').attr('src').replace('icon-next', 'icon_disable-next'));
		}
		else{
			jQuery('.box-casestudy img.icon-next').attr('src',jQuery('.box-casestudy img.icon-next').attr('src').replace('icon_disable-next', 'icon-next'));
		}
		if(currentCaseStudyIndex == 0){
			jQuery('.box-casestudy img.icon-prev').attr('src',jQuery('.box-casestudy img.icon-prev').attr('src').replace('icon_enable-prev', 'icon-prev'));
		}
		else{
			jQuery('.box-casestudy img.icon-prev').attr('src',jQuery('.box-casestudy img.icon-prev').attr('src').replace('icon-prev', 'icon_enable-prev'));
		}
		showContent_CaseStudy(currentCaseStudyIndex);
		jQuery('div#container-right ul.menu-right li.current_page_parent ul li').removeClass('current_page_item');
		var right_menu_current_page = jQuery('div#container-right ul.menu-right li.current_page_parent ul li');
		jQuery(right_menu_current_page[currentCaseStudyIndex]).addClass('current_page_item');
		
//Remove all AttributeID and add the current
		var casestudypage = jQuery('div#container-left div.casestudy_page');
		casestudypage.each(function(index){				
			jQuery(this).removeAttr('id');
		});		
		jQuery(casestudypage[currentCaseStudyIndex]).attr('id','current_content');
	}
}

function showContent_CaseStudy(index){
	var blockquote = jQuery('div.casestudy_page blockquote');
	var box_casestudy = jQuery('div.casestudy_page .box-casestudy');
	var casestudy_title = jQuery('div.casestudy_page .casestudy-tab');
	var casestudy_content = jQuery('div.casestudy_page .solution-content');
	
	jQuery(blockquote).css('display','none');
	jQuery(blockquote[index]).css('display','block');
	
	jQuery(box_casestudy).css('display','none');
	jQuery(box_casestudy[index]).css('display','block');
	
	jQuery(casestudy_title).css('display','none');
	jQuery(casestudy_title[index]).css('display','block');
	jQuery(casestudy_content).css('display','none');
	jQuery(casestudy_content[index]).css('display','block');
}
function content_height_casestudy(){
	
//Content height 	
			
			var content_current = parseInt(jQuery('div#current_content div.casestudy_content div.current').height()) + 47;
						
			var content_tab = parseInt(jQuery('div.casestudy_page ul.current').height());
			var content_box_casestudy = parseInt(jQuery('div.box-casestudy').height());
			
			var container_right = parseInt(jQuery('div#container-right').height());
			var blockquote_title = jQuery('div#current_content blockquote');
			var blockquote_height = 0;
			if(blockquote_title.length){
				blockquote_height = parseInt(blockquote_title.height())+22;
			}
			
			//alert(content_box_casestudy+">" + content_tab +">"+ content_current +">"+ blockquote_height);
			
			var content_left_casestudy = content_box_casestudy + content_tab + content_current + blockquote_height + 32;
			
			if(content_left_casestudy > container_right){
				
				jQuery('div#container-left').css('height',content_left_casestudy); //32 the top padding of content
			}
			else{
				
				jQuery('div#container-left').css('height',container_right-102);
			}
//End Content height	
}
jQuery(document).ready(function() {
	
	jQuery('div#online_strategy').addClass('hidetab');
	jQuery('div#content-social').addClass('hidetab');
	jQuery('div#search_engine_visibility').addClass('hidetab');
	jQuery('div#search_engine_marketing').addClass('hidetab');
	
	//current_study_page();//Get current Index

	var tab = jQuery('ul#tab-menu li');
	var tab_content = jQuery('div#tab-content div');
	
	jQuery('ul#tab-menu').find('li').each(function(i){
		jQuery(this).click(function(event){
			jQuery(tab).each(function(index){
				jQuery(this).removeAttr('id'); //Remove the id=current of all element tab
				jQuery(tab_content[index]).removeClass('current');
				jQuery(this).removeClass('item'+index);
				jQuery(this).removeClass('marginright');
			});
			
			
			/*
			jQuery(tab[0]).removeAttr('id');
			jQuery(tab[1]).removeAttr('id');
			jQuery(tab[2]).removeAttr('id');
			jQuery(tab[3]).removeAttr('id');
			*/
			//jQuery('ul#tab-menu li[id!="current"]').removeAttr('id');
			
			jQuery(this).attr('id', 'current'); //Add the id=current to the currently tab
			jQuery(this).addClass('item'+i);
			jQuery(tab_content[i]).addClass('current');
			var menutab = jQuery('ul#tab-menu li');
			if(i==0){
				jQuery(menutab[1]).addClass('marginright');
				jQuery(menutab[2]).addClass('marginright');
			}
			if(i==1){				
				jQuery(menutab[2]).addClass('marginright');
			}
			if(i==2){				
				jQuery(menutab[0]).addClass('marginright');
			}
			if(i==3){
				jQuery(menutab[0]).addClass('marginright');				
				jQuery(menutab[1]).addClass('marginright');
			}
			
//Get Content Height			
			var tab_menu = parseInt(jQuery('ul#tab-menu').height());
			var content_height = parseInt(jQuery(tab_content[i]).height());
			var content_right = tab_menu + content_height;
			if(hcontent_left > content_right){
				jQuery('div#right-content').css('height', hcontent_left+24);
			}
			else{
				jQuery('div#right-content').css('height', content_right+40);
			}
//End get content height			
			event.preventDefault();
		});
	});
	
	
	if(jQuery('#h_banner').length){
		jQuery('#h_banner div.image_banner').each(function(i){
			
			if(i > 0){			
				jQuery(this).css('left', '970px');
			}
		});
//Navigate
		var navigatable = jQuery('div.h_navigate a');
		jQuery(navigatable).each(function(index){
			jQuery(this).click(function(event){
				event.preventDefault();
				jumpindex = index;
				clearInterval($intervalid_banner);				
				rotated();
				$intervalid_banner = setInterval( "rotated()", 10000 );
			});
		});
		
//End Navigate		
		$intervalid_banner = setInterval( "rotated()", 10000 );
	}
	
	// Latest News
	if(jQuery('#latestnews-box ul li').length){
		jQuery('#latestnews-box ul li').each(function(news_index){
			if(news_index > 0){
				jQuery(this).fadeOut();
			}
		});
		$intervalid = setInterval( "slide_news()", 30000 );
	}
	// End Latest News
	// Case Studies logo animation
	var client_name = jQuery('#casestudy span.client_name');
	if(jQuery('#casestudy').length){	
		var title_position_init = 0;
		var logo_position_init = 0;
		title_position_init = (244-jQuery('#casestudy span.client_name').width())/2;
		logo_position_init = (244-jQuery('#casestudy img.client_logo').width())/2;
		
		//middle_position_init = (106-jQuery('#casestudy img.client_logo').height())/2;
		
		
		//jQuery('#casestudy img.client_logo').css({'display':'block','left':logo_position_init});
		
		//jQuery('#casestudy img.client_logo').css('top',middle_position_init);
		
		jQuery('#casestudy span.client_name').css({'display':'block','left':title_position_init});
		jQuery('.moveimage img.iconleft').css('display','block');
		jQuery('.moveimage img.iconright').css('display','block');
		jQuery(client_name[0]).css('left',((244-jQuery(client_name[0]).width())/2));//Init title
		jQuery('#casestudy div.box_image').each(function(i){
			if(i>0){
				jQuery(this).css({left: 244});
				jQuery(client_name[i]).css({left: 244});
			}
		});
		
		jQuery('#casestudy img.iconleft').live('click',function(event){
			slideSwitch_Logo(-1);
		});
		jQuery('#casestudy img.iconright').live('click',function(event){
			slideSwitch_Logo(1);
		});
	}
	
	// Case Studies pages
	var casestudies_content = jQuery('div.casestudy_content div.content_block');
	
	jQuery('ul.casestudy_title li').each(function(i){
		jQuery(this).click(function(event){
			jQuery('ul.casestudy_title li').removeClass('current');
			jQuery(casestudies_content).removeClass('current');
			
			var get_title = jQuery('ul.casestudy_title li.title1');
			var get_content = jQuery('div.casestudy_content div.content1');
			if(jQuery(this).attr('class') == 'title2'){
				get_title = jQuery('ul.casestudy_title li.title2');
				get_content = jQuery('div.casestudy_content div.content2');
			}
			if(jQuery(this).attr('class') == 'title3'){
				get_title = jQuery('ul.casestudy_title li.title3');
				get_content = jQuery('div.casestudy_content div.content3');
			}
						
			jQuery(get_title).addClass('current');
			jQuery(get_content).addClass('current');
			
			
			
			
//Content height 	
			content_height_casestudy();
		
//End Content height			
		});
	});
	// Find current Index
	var current_index = 0;
	
	jQuery('div.solution-content').each(function(i){
		if(jQuery(this).attr('class') == 'casestudy_content solution-content current'){
			current_index = i;
		}
	});
	currentCaseStudyIndex = current_index;
	if(currentCaseStudyIndex > 0){
		currentCaseStudyIndex -=1;
		slideSwitch_CaseStudy(1);
	}
	
	if(jQuery('#container-left div.casestudy_page').length){	
				
		jQuery('.box-images img').each(function(i){
			if(i>currentCaseStudyIndex){
				jQuery(this).css({left: 583});				
			}
			else if(i<currentCaseStudyIndex){
				jQuery(this).css({left: -583});
			}
		});
		
		jQuery('.box-casestudy img.icon-prev').live('click',function(event){
			slideSwitch_CaseStudy(-1);
			content_height_casestudy();
		});
		jQuery('.box-casestudy img.icon-next').live('click',function(event){
			slideSwitch_CaseStudy(1);
			content_height_casestudy();
		});
	}		
	
	//Check the height of Left side and Rigth Side
	if(jQuery('div#container-left').length && jQuery('div#container-right').length){
		var container_left = parseInt(jQuery('div#container-left').height());
		var container_right = parseInt(jQuery('div#container-right').height());
		
		if(container_left == 0){
			content_height_casestudy();
		}
		else if((container_right) > (container_left+20)){			
			jQuery('div#container-left').css('height',container_right-102);
		}
		
	}
	
//Check the height of Left side and Right the same in home page
	if(jQuery('div#left-content').length && jQuery('div#right-content').length){
		var hcontent_left = parseInt(jQuery('div#left-content').height());
		var hcontent_right = parseInt(jQuery('div#right-content').height());
		
		
		if(hcontent_left > hcontent_right){
			jQuery('div#right-content').css('height', hcontent_left+24);			
		}
	}
});


