var productInfoTabHeight = new Object;
//var productInfoTabWidth = new Object;
var productInfoDivs;
var productInfoTimeouts = new Object;
var productInfoSpeed;

Event.observe(window, 'load', pi_Init);

function pi_Init(event){
		//Stop listen Event
	Event.stop(event);
	Event.stopObserving(window, 'load', pi_Init);

		//Vars init
	var buttons, tmp, adviceLink;

		//Get elements width/height
	productInfoDivs = $$('#product_info .tab');
	//productInfoDivs.each(function(div){this[div.getAttribute('id')] = div.getWidth() - 2;}, productInfoTabWidth);
	productInfoDivs.each(function(div){this[div.getAttribute('id')] = div.getHeight();}, productInfoTabHeight);

		//Reduce width/height to 0 except $technical
	productInfoDivs.each(function(div){if(div.getAttribute('id') != 'technical'){div.setStyle({/*width:'0px', */height:'0px'});}});

		//Define button event
	if($('tabs') != null){
		buttons = $('tabs').childElements();
		if(buttons != null){
			buttons.each(function(button){
				button.observe('click', function(event){pi_display($(button.getAttribute('id').substring(5)), event);});
			});
		}
		tmp = $$('#technical .score')[0];
		adviceLink = tmp.childElements()[tmp.childElements().length - 1].childElements()[0];
		adviceLink.observe('click', function(event){event.preventDefault(); pi_display($('advice'), event);});
	}

	if($('advice_error_string') != null){
		pi_display($('advice'));
		$('tabs_advice').scrollTo();
	}
}

function pi_display(tab, event){
	if(event){
		Event.stop(event);
		productInfoSpeed = event.shiftKey ? 100 : 10;
	}else{
		productInfoSpeed = 10;
	}

	var shift;

		//Change button hilight
	pi_changeButton(tab);

		//Get extendeds div and collapse'em
	productInfoDivs.each(function(div){
		if(parseInt(div.getStyle('height')) > 0 && div != tab){
			if(productInfoTimeouts[div.getAttribute('id')] != null){
				clearTimeout(productInfoTimeouts[div.getAttribute('id')]);
			}
			pi_collapseTab(div, 0);
		}
	});

		//Reset timeout and expand required tab
	if(productInfoTimeouts[tab.getAttribute('id')] != null){
		clearTimeout(productInfoTimeouts[tab.getAttribute('id')]);
	}
	pi_extendTab(tab, 0);
}

function pi_changeButton(tab){
		//Vars init
	var buttons, img, imgSrc, imgNewSrc;

		//Remove hilight from all button
	buttons = $('tabs').childElements();
	buttons.each(function(img){
		imgSrc = img.getAttribute('src');
		imgNewSrc = imgSrc.replace('Active.', '.');
		img.setAttribute('src', imgNewSrc);
	});

		//Hilight only needed
	img = $('tabs_' + tab.getAttribute('id'));
	imgSrc = img.getAttribute('src');
	imgNewSrc = imgSrc.replace(/(\.[a-zA-Z]+)$/g, 'Active$1');
	img.setAttribute('src', imgNewSrc);
}

function pi_extendTab(tab, mod){
	var width, height, speedw, speedh;

	//width = parseInt(tab.getStyle('width'));
	//speedw = Math.ceil(productInfoTabWidth[tab.getAttribute('id')] / productInfoSpeed);
	height = parseInt(tab.getStyle('height'));
	speedh = Math.ceil(productInfoTabHeight[tab.getAttribute('id')] / productInfoSpeed);

	//tab.setStyle({'display':'block'});

	if(/*productInfoTabWidth[tab.getAttribute('id')] > width || */productInfoTabHeight[tab.getAttribute('id')] > height){
		//width = (width + speedw > productInfoTabWidth[tab.getAttribute('id')]) ? productInfoTabWidth[tab.getAttribute('id')] + 'px' : width + speedw + 'px';
		height = (height + speedh > productInfoTabHeight[tab.getAttribute('id')]) ? productInfoTabHeight[tab.getAttribute('id')] + 'px' : height + speedh + 'px';
		tab.setStyle({/*'width':width, */'height':height});
		productInfoTimeouts[tab.getAttribute('id')] = setTimeout(function(){pi_extendTab(tab, mod);}, 10);
	}
}

function pi_collapseTab(tab, mod){
	var width, height, speedw, speedh;

	//width = parseInt(tab.getStyle('width'));
	//speedw = Math.ceil(productInfoTabWidth[tab.getAttribute('id')] / productInfoSpeed);
	height = parseInt(tab.getStyle('height'));
	speedh = Math.ceil(productInfoTabHeight[tab.getAttribute('id')] / productInfoSpeed);

	if(/*width == 0 && */height == 0){
		//tab.setStyle({'display':'none'});
	}

	if(/*0 < width || */0 < height){
		//width = (width - speedw < 0) ? '0px' : width - speedw + 'px';
		height = (height - speedh < 0) ? '0px' : height - speedh + 'px';
		tab.setStyle({/*'width':width, */'height':height});
		productInfoTimeouts[tab.getAttribute('id')] = setTimeout(function(){pi_collapseTab(tab, mod);}, 10);
	}
}