var stepToMove = 208;
var startPoint = 0;
var currentPoint = 0;
var timeout = 15000;
var currentId = 1;

Event.observe(window, 'load', function() { 
	
	if($('homePicContainer')){
		setInterval('homePagePicAutoChange();', timeout);
		
		Event.observe(document, 'mousemove', function(event) {
			try {
				ofset = $('homePicContainer').cumulativeOffset();
				rightposition = ofset[0] + 1000;
				bottomposition = ofset[1] + 500;

				x = Event.pointerX(event);
				y = Event.pointerY(event);

				if (x > ofset[0] && x < rightposition && y > ofset[1]
						&& y < bottomposition) {
					showList();
				} else {
					hideList();
				}
			} catch (err) {
			}

		}); 
		
		correctPNG();
	}
	
	if($('jobVacanciesTabs')){
		
		Event.observe($('beijing'), 'click', function() {
			switchBlocks('beijing');
		}); 
		
		Event.observe($('shanghai'), 'click', function() {
			switchBlocks('shanghai');
		}); 

		Event.observe($('changchun'), 'click', function() {
			switchBlocks('changchun');
		}); 

		Event.observe($('liuzhou'), 'click', function() {
			switchBlocks('liuzhou');
		}); 

		Event.observe($('others'), 'click', function() {
			switchBlocks('others');
		}); 
}
	
	if($('onstarFrame')){
		
		var endPoint = $('picturesList').select('li').length;
		//already 3 links displayed
		endPoint = endPoint - 3;
		
		Event.observe('toLeft', 'click', function() {
			moveContent('left', startPoint, endPoint, stepToMove);
		}); 
		
		Event.observe('toRight', 'click', function() {
			moveContent('right', startPoint, endPoint, stepToMove);
		}); 
	}
	
	if($('buick_cars')){
		
		Event.observe('toLeft', 'click', function(event) {
			moveCarsPicture('left', event, 'buick', 138);
		}); 
		
		Event.observe('toRight', 'click', function(event) {
			moveCarsPicture('right', event, 'buick', 138);
		}); 
	}
	
	if($('cadillac_cars')){
		
		Event.observe('toLeft', 'click', function(event) {
			moveCarsPicture('left', event, 'cadillac', 148);
		}); 
		
		Event.observe('toRight', 'click', function(event) {
			moveCarsPicture('right', event, 'cadillac', 148);
		}); 
	}
	
	//CSRTips page
	if($('CSRTips')){
		Event.observe('carSeatSafety', 'click', function() {
			try {
				BlindUpAndDown("carSeatSafety");
				return false;
			} catch (err) {
			}
		}); 
		
		Event.observe('backover', 'click', function() {
			try {
				BlindUpAndDown("backover");
				return false;
			} catch (err) {
			}
		}); 
		
		Event.observe('heatPrevention', 'click', function() {
			try {
				BlindUpAndDown("heatPrevention");
				return false;
			} catch (err) {
			}
		}); 
		
		Event.observe('trunk', 'click', function() {
			try {
				BlindUpAndDown("trunk");
				return false;
			} catch (err) {
			}
		}); 
		
		Event.observe('seatSafety', 'click', function() {
			try {
				BlindUpAndDown("seatSafety");
				return false;
			} catch (err) {
			}
		}); 
	}
	
	//Environmental Commitment
	if($('ec_tabs')){
		Event.observe('EnvironmentalPrinciples', 'click', function() {
			blockSelected("EnvironmentalPrinciples");
		});
		
		Event.observe('GreenerVehicles', 'click', function() {
			blockSelected("GreenerVehicles");
		});
		
		Event.observe('GreenMaterialsVehicleRecycling', 'click', function() {
			blockSelected("GreenMaterialsVehicleRecycling");
		});
		
		Event.observe('SustainableManufacturingFacilities', 'click', function() {
			blockSelected("SustainableManufacturingFacilities");
		});
		
		Event.observe('EnvironmentalActionInChina', 'click', function() {
			blockSelected("EnvironmentalActionInChina");
		});
	}
	
	//Environmental Commitment
	if($('fuelCellContent')){
		Event.observe(document, 'mousemove', function(event) {
			showMenuItem('fuelCell', event);
			showMenuItem('Equinox', event);
			showMenuItem('greenCar', event);
			hideMenuItem('fuelCell', event);
			hideMenuItem('Equinox', event);
			hideMenuItem('greenCar', event);
		});
		
		Event.observe('fuelCellSystem', 'mouseover', function() {
				Effect.Appear('fuelCellInto',{duration: 0.5});
		});
		Event.observe('fuelCellSystem', 'mouseout', function() {
				Effect.Fade('fuelCellInto',{delay: 0.5});
		});
		
		Event.observe('securityLink', 'click', function() {
			$('securityBlock').show();
			$('EquinoxIntro').hide();
		});
		
		Event.observe('speedLink', 'click', function() {
			$('speedBlock').show();
			$('EquinoxIntro').hide();
		});
		
		Event.observe('officialLink', 'click', function() {
			$('officialBlock').show();
			$('EquinoxIntro').hide();
		});
		
		Event.observe('deliveryCeremony', 'mouseover', function() {
			Effect.Appear('deliveryCeremonyContent',{duration: 0.5});
		});
		
		Event.observe('deliveryCeremony', 'mouseout', function() {
			Effect.Fade('deliveryCeremonyContent',{delay: 0.5});
		});
		
		Event.observe('securityBlock', 'mousemove', function(event) {
			$('security_left').show();
			$('security_right').show();
		}); 
		Event.observe('securityBlock', 'mouseout', function(event) {
			$('security_left').hide();
			$('security_right').hide();
		}); 
		
		Event.observe('speedBlock', 'mousemove', function(event) {
			$('speed_left').show();
			$('speed_right').show();
		}); 
		Event.observe('speedBlock', 'mouseout', function(event) {
			$('speed_left').hide();
			$('speed_right').hide();
		}); 
		
		Event.observe('officialBlock', 'mousemove', function(event) {
			$('official_left').show();
			$('official_right').show();
		}); 
		Event.observe('officialBlock', 'mouseout', function(event) {
			$('official_left').hide();
			$('official_right').hide();
		}); 
		
		Event.observe('security_left', 'click', function(event) {
			$('securityBlock').hide();
			$('officialBlock').show();
		});
		Event.observe('security_right', 'click', function(event) {
			$('securityBlock').hide();
			$('speedBlock').show();
		});
		
		Event.observe('official_left', 'click', function(event) {
			$('officialBlock').hide();
			$('speedBlock').show();
		});
		Event.observe('official_right', 'click', function(event) {
			$('officialBlock').hide();
			$('securityBlock').show();
		});
		
		Event.observe('speed_left', 'click', function(event) {
			$('speedBlock').hide();
			$('securityBlock').show();
		});
		Event.observe('speed_right', 'click', function(event) {
			$('speedBlock').hide();
			$('officialBlock').show();
		});
		
		correctPNG();
	}
	
	//GM In China
	if($('operations')){
		Event.observe('sgm', 'mouseover', function(event) {
			showOnsiteIntro('sgm');
		});
		Event.observe('sgm', 'mouseout', function(event) {
			hideOnsiteIntro('sgm');
		});
		
		Event.observe('PATAC', 'mouseover', function(event) {
			showOnsiteIntro('PATAC');
		});
		Event.observe('PATAC', 'mouseout', function(event) {
			hideOnsiteIntro('PATAC');
		});
		
		Event.observe('sgmw', 'mouseover', function(event) {
			showOnsiteIntro('sgmw');
		});
		Event.observe('sgmw', 'mouseout', function(event) {
			hideOnsiteIntro('sgmw');
		});
		
		Event.observe('norsom', 'mouseover', function(event) {
			showOnsiteIntro('norsom');
		});
		Event.observe('norsom', 'mouseout', function(event) {
			hideOnsiteIntro('norsom');
		});
		
		Event.observe('dongyue', 'mouseover', function(event) {
			showOnsiteIntro('dongyue');
		});
		Event.observe('dongyue', 'mouseout', function(event) {
			hideOnsiteIntro('dongyue');
		});
		
		Event.observe('dongyuePT', 'mouseover', function(event) {
			showOnsiteIntro('dongyuePT');
		});
		Event.observe('dongyuePT', 'mouseout', function(event) {
			hideOnsiteIntro('dongyuePT');
		});
		
		Event.observe('gmacsaic', 'mouseover', function(event) {
			showOnsiteIntro('gmacsaic');
		});
		Event.observe('gmacsaic', 'mouseout', function(event) {
			hideOnsiteIntro('gmacsaic');
		});
		
		Event.observe('acdelco', 'mouseover', function(event) {
			showOnsiteIntro('acdelco');
		});
		Event.observe('acdelco', 'mouseout', function(event) {
			hideOnsiteIntro('acdelco');
		});
		
		Event.observe('onstar', 'mouseover', function(event) {
			showOnsiteIntro('onstar');
		});
		Event.observe('onstar', 'mouseout', function(event) {
			hideOnsiteIntro('onstar');
		});
		
		Event.observe('FAW', 'mouseover', function(event) {
			showOnsiteIntro('FAW');
		});
		Event.observe('FAW', 'mouseout', function(event) {
			hideOnsiteIntro('FAW');
		});
		
		Event.observe('SAICInvestment', 'mouseover', function(event) {
			showOnsiteIntro('SAICInvestment');
		});
		Event.observe('SAICInvestment', 'mouseout', function(event) {
			hideOnsiteIntro('SAICInvestment');
		});
		
		Event.observe('GMjiaotong', 'mouseover', function(event) {
			showOnsiteIntro('GMjiaotong');
		});
		Event.observe('GMjiaotong', 'mouseout', function(event) {
			hideOnsiteIntro('GMjiaotong');
		});
		
	}
	
	if($('404_redirection')){
		countDown();
	}
	
});

function showOnsiteIntro(id){
	$$('div.onsiteIntro').each(function(element){
		element.hide();
	});
	$(id + "Intro").show();
}

function hideOnsiteIntro(id){
	$(id + "Intro").hide();
}

function asd(){
	Event.observe(document, 'mousemove', function(event) {
		try {
			ofset = $('securityBlock').cumulativeOffset();
			var width = $('securityBlock').getStyle('width');
			var height = $('securityBlock').getStyle('height');
			width = parseInt(width.substring(0, width.lastIndexOf("px")));
			height = parseInt(height.substring(0, height.lastIndexOf("px")))
			
			rightposition = ofset[0] + width;
			bottomposition = ofset[1] + height;

			x = Event.pointerX(event);
			y = Event.pointerY(event);

			if (x > ofset[0] && x < rightposition && y > ofset[1]
					&& y < bottomposition) {
				$('security_left').show();
				$('security_right').show();
			} else {
				$('security_left').hide();
				$('security_right').hide();
			}
		} catch (err) {
		}

	}); 
}

function showMenuBlock(selectedId){
	$$("div.blockContent").each(function(element){
		if(selectedId + 'Block' != element.id){
			$(element).hide();
		}else{
			$(element).show();
		}
	});
}

function showMenuItem(menuId, event){
	
	ofset = $('item_' + menuId).cumulativeOffset();
	//use px as default
	var width = $('item_' + menuId).getStyle('width');
	var height = $('item_' + menuId).getStyle('height');
	width = parseInt(width.substring(0, width.lastIndexOf("px")));
	height = parseInt(height.substring(0, height.lastIndexOf("px")))
	rightposition = ofset[0] + width;
	bottomposition = ofset[1] + height;

	x = Event.pointerX(event);
	y = Event.pointerY(event);

	if (x > ofset[0] && x < rightposition && y > ofset[1]
			&& y < bottomposition) {
		if($('opacityLayer_' + menuId).getStyle('height') == '0px'){
			new Effect.Morph('opacityLayer_' + menuId, {
				style: 'height: 220px;opacity: 0.5;', 
				duration: 0.3
			});
			new Effect.Appear('middleImg_' + menuId, {
				duration: 0.3,
				delay: 0.3
			});
			$('snapShot_' + menuId).hide();
		}
	}
}

function hideMenuItem(menuId, event) {
	ofset = $('opacityLayer_' + menuId).cumulativeOffset();
			// use px as default
	var width = $('opacityLayer_' + menuId).getStyle('width');
	var height = $('opacityLayer_' + menuId).getStyle('height');
	width = parseInt(width.substring(0, width.lastIndexOf("px")));
	height = parseInt(height.substring(0, height.lastIndexOf("px")))
	rightposition = ofset[0] + width;
	bottomposition = ofset[1] + height;

	x = Event.pointerX(event);
	y = Event.pointerY(event);
			
	if (x < ofset[0] || x > rightposition || y < ofset[1] || y > bottomposition) {
		if($('opacityLayer_' + menuId).getStyle('height') == '220px'){
			new Effect.Fade('middleImg_' + menuId,{
				duration: 0.3
			});
			new Effect.Morph('opacityLayer_' + menuId, {
				style: 'height: 0px;opacity: 0;', 
				duration: 0.3,
				delay: 0.3
			});
			$('snapShot_' + menuId).show();
		}
	}
}


function blockSelected(selectedId){
	//banner pictures
	$('ec_'+ selectedId).appear({ duration: 0.5 });
	
	$$("img.ec_topic").each(function(element){
		if('ec_'+ selectedId != element.id){
			element.fade({ duration: 0.5 });
		}
	});
	
	//tabs
	$$("#ec_tabs img").each(function(element){
		if(element.visible()){
			$(element).hide();
		}
	});
	$$("#" + selectedId +" img").each(function(element){
		element.appear({ duration: 0.3 });
	});
	
	//block
	$$("div.contentBlock").each(function(element){
		if(selectedId + '_block' != element.id){
			$(element).hide();
		}else{
			$(element).show();
		}
	});
	
	
}

function changeBlockTabPic(selectedId){
	var imagePath = $(selectedId).style;
	alert(imagePath);
}

function BlindUpAndDown(selectedId){
	var durationTime = 0.3;
	var srcFolderPath = $(selectedId + "Arrow").src;
	srcFolderPath = srcFolderPath.substring(0, srcFolderPath.lastIndexOf("/") + 1);
	
	$$("img.greenArrow").each(function(element){
		element.src = srcFolderPath + "green_arrow.jpg";
	});
	
	//blind up other block if they are visiable
	$$("div.tipsContent").each(function(element){
		if($(element.id).visible()){
			new Effect.BlindUp(element.id, { duration: durationTime });
		}
	});
	
	//blind up or blind down
	if($(selectedId + "Block").visible()){
		$(selectedId + "Arrow").src = srcFolderPath + "green_arrow.jpg";
		new Effect.BlindUp(selectedId + "Block", { duration: durationTime });
	}else{
		$(selectedId + "Arrow").src = srcFolderPath + "green_arrow_pendding.jpg";
		new Effect.BlindDown(selectedId + "Block", { duration: durationTime });
	}
}

function selectCSRTabs(areaBlock, selectedId){
	selectGrayTabs(areaBlock, selectedId);
}

function selectGrayTabs(areaBlock, selectedId){
	if($(areaBlock + "_content")){
		$$("#" + areaBlock + "_content div").each(function(element){
			if(selectedId + '_tab' == element.id){
				$(element.id).style.display = "block";
			}else{
				$(element.id).hide();
			}
		});
	}
	if($(areaBlock + "_title")){
		$$("#" + areaBlock + "_title a").each(function(element){
			if(selectedId == element.id){
				$(element.id).addClassName('current');
			}else{
				$(element.id).removeClassName('current');
			}
		});
	}
}

function selectGasNav(selectedId){
	$$("div.gasNavParts").each(function(element){
		if(selectedId + '_content' == element.id){
			$(element.id).style.display = "block";
		}else{
			$(element.id).hide();
		}
	});
	
	$$("img.gasNav").each(function(element){
		var srcPath = $(element.id).src;
		srcPath = srcPath.substring(0, srcPath.lastIndexOf("_"));
		if(selectedId == element.id){
			$(element.id).src = srcPath + "_over.jpg";
		}else{
			$(element.id).src = srcPath + "_out.jpg";
		}
	});
}

function selectTabs(selectedId){
	$$("div.greenTechnologyContent").each(function(element){
		if(selectedId + '_content' == element.id){
			$(element.id).style.display = "block";
		}else{
			$(element.id).hide();
		}
	});
	
	$$("li.greenTechnologyList").each(function(element){
		if(selectedId == element.id){
			$(element.id).addClassName('selected');
		}else{
			$(element.id).removeClassName('selected');
		}
	});
}

function switchBlocks(selectTabId){
	$$("div.contentBlock").each(function(element){
		if(selectTabId + 'Block' == element.id){
			$(element.id).style.display = "block";
		}else{
			$(element.id).hide();
		}
	});
	
	$$("li.contentTabs").each(function(element){
		if(selectTabId == element.id){
			$(element.id).addClassName('current');
		}else{
			$(element.id).removeClassName('current');
		}
	});
}

function moveContent(direction, startPoint, endPoint, stepToMove){
	if(direction == 'left'){
		if(currentPoint > startPoint){
			new Effect.Move('picturesList', { x: stepToMove, delay:0.3, duration:0.3, mode: 'relative'  });
			currentPoint--;
		}
	}
	if(direction == 'right'){
		if(currentPoint < endPoint){
			new Effect.Move('picturesList', { x: -stepToMove, delay:0.3, duration:0.3, mode: 'relative' });
			currentPoint++;
		}
	}
}

function moveCarsPicture(direction, event, brand, step){
	var rightBorder=0;
	var rightLimit=0;
	if(brand == 'buick'){
		rightBorder = 1093;
		rightLimit = 560;
	}
	
	if(brand == 'cadillac'){
		rightBorder = 725;
		rightLimit = 560;
	}
	
	var leftBorder = $('brand_cars_pic').cumulativeOffset()[0];
	var leftLimit = $(brand+'_cars').cumulativeOffset()[0];	
	rightBorder += $('brand_cars_pic').cumulativeOffset()[0];
	rightLimit += $(brand+'_cars').cumulativeOffset()[0];
	
	if(direction == 'left'){
		if(leftBorder < leftLimit)
			new Effect.Move('brandPictures', { x: step, delay:0.3, duration:0.3, mode: 'relative'  });
	}
	if(direction == 'right'){
		if(rightBorder > rightLimit)
			new Effect.Move('brandPictures', { x: -step, delay:0.3, duration:0.3, mode: 'relative' });
	}
	return Event.stop(event)
}


function homePagePicAutoChange(){	
	var pictureId = currentId + 1;
	if(pictureId > $$('div.homePic').size())
		pictureId = 1;
	changehomePic(pictureId);
}

function changehomePic(pictureId){
	if(currentId != pictureId){
		new Effect.Fade('homePic'+currentId, { duration: 0.9 } );
		new Effect.Appear('homePic'+pictureId, { duration: 0.9 } );
		$('picNum' + currentId).removeClassName('selected');
		$('picNum' + pictureId).addClassName('selected');
		currentId = pictureId;
	}
}


function showList() {
	if ($('buttomBanner').getStyle('top') == '500px') {
		new Effect.Move('buttomBanner', {
			x : 0,
			y : 450,
			mode : 'absolute',
			duration : 0.3,
			transition : Effect.Transitions.sinoidal
		});
	}
}
function hideList() {
	if ($('buttomBanner').getStyle('top') == '450px') {
		new Effect.Move('buttomBanner', {
			x : 0,
			y : 500,
			mode : 'absolute',
			duration : 0.3,
			transition : Effect.Transitions.sinoidal
		});
	}
}

function correctPNG() {
	try{
	for ( var i = 0; i < document.images.length; i++) {
		var img = document.images[i];
		var imgName = img.src.toUpperCase();
		if (imgName.substring(imgName.length - 3, imgName.length) == "PNG") {
			var imgID = (img.id) ? "id='" + img.id + "' " : "";
			var imgClass = (img.className) ? "class='" + img.className + "' "
					: "";
			var imgTitle = (img.title) ? "title='" + img.title + "' "
					: "title='" + img.alt + "' ";
			var imgStyle = "display:inline-block;" + img.style.cssText;
			if (img.align == "left")
				imgStyle = "float:left;" + imgStyle;
			if (img.align == "right")
				imgStyle = "float:right;" + imgStyle;
			if (img.parentElement.href)
				imgStyle = "cursor:hand;" + imgStyle;
			var strNewHTML = "<span "
					+ imgID
					+ imgClass
					+ imgTitle
					+ " style=\""
					+ "width:"
					+ img.width
					+ "px; height:"
					+ img.height
					+ "px;"
					+ imgStyle
					+ ";"
					+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
					+ "(src='" + img.src
					+ "', sizingMethod='scale');\"></span>";
			img.outerHTML = strNewHTML;
			i = i - 1;
		}
	}
	}catch(err){
		//do nothing, just ignore the error will be fine
	}
}

var num = 2; 

function countDown(){
	setInterval("setTime()", 1000);
}

function setTime(){
	if(num > 0){
		$('count').innerHTML = num;
		num--;
	}else{
		window.location = "/";
	}
}

var menu=function(){
	var t=15,z=50,s=6,a;
	function dd(n){this.n=n; this.h=[]; this.c=[]}
	dd.prototype.init=function(p,c){
		a=c; var w=document.getElementById(p), s=w.getElementsByTagName('ul'), l=s.length, i=0;
		for(i;i<l;i++){
			var h=s[i].parentNode; this.h[i]=h; this.c[i]=s[i];
			h.onmouseover=new Function(this.n+'.st('+i+',true)');
			h.onmouseout=new Function(this.n+'.st('+i+')');
		}
	}
	dd.prototype.st=function(x,f){
		var c=this.c[x], h=this.h[x], p=h.getElementsByTagName('a')[0], img=h.getElementsByTagName('img')[0];
		clearInterval(c.t); c.style.overflow='hidden';
		//menuPicChange(img, f);
		if(f){
			p.className+=' '+a;
			if(!c.mh){c.style.display='block'; c.style.height=''; c.mh=c.offsetHeight; c.style.height=0}
			if(c.mh==c.offsetHeight){c.style.overflow='visible'}
			else{c.style.zIndex=z; z++; c.t=setInterval(function(){sl(c,1)},t)}
		}else{p.className=p.className.replace(a,''); c.t=setInterval(function(){sl(c,-1)},t)}
	}
	function sl(c,f){
		var h=c.offsetHeight;
		if((h<=0&&f!=1)||(h>=c.mh&&f==1)){
			if(f==1){c.style.filter=''; c.style.opacity=1; c.style.overflow='visible'}
			clearInterval(c.t); return
		}
		var d=(f==1)?Math.ceil((c.mh-h)/s):Math.ceil(h/s), o=h/c.mh;
		c.style.opacity=o; c.style.filter='alpha(opacity='+(o*100)+')';
		c.style.height=h+(d*f)+'px'
	}
	return{dd:dd}
}();

function menuPicChange(element, isOver){
	if(element){
		var srcPath = element.src;
		if(element.className.indexOf('currentChannel')<0){
			if(isOver){
				srcPath = srcPath.substring(0, srcPath.lastIndexOf(".jpg"));
				element.src = srcPath + "_over.jpg";
			}else{
				srcPath = srcPath.substring(0, srcPath.lastIndexOf("_over.jpg"));
				element.src = srcPath + ".jpg";
			}
		}
	}
}