var _cii42jq = jQuery.noConflict();
var _cii42timeout = null;
var _cii42initRight;

function updateShoppingCartInfo(productId, forceShowLatestProduct){
	var ajax, x;
	
	if(document.getElementById('data_shopping_cart_cii42')){
		ajax = new Ajax.Updater(
			'data_shopping_cart_cii42',
			'http://www.deskidea.com/es/families/0001/mesas-de-oficina.php',
			{
				parameters: 'update_cii42=1',
				onComplete: function(response){
					//_cii42jq('html, body').animate({scrollTop:0}, 'slow');
					if(document.getElementById('process_shopping_cii42')){
						if(response.status == 200)
							document.getElementById('process_shopping_cii42').innerHTML = '<a href="http://www.deskidea.com/es/proceso-compra.php?process=1" title="Finalizar compra"><span>Finalizar compra</span></a>';
						else
							document.getElementById('process_shopping_cii42').innerHTML = 'Tu cesta está vacía';
					}
					if(productId)
						_cii42showLatestProduct(true, productId);
					else if (forceShowLatestProduct)
						_cii42showLatestProduct(true);
				}
			}
		);
	}
}

function _cii42showLatestProduct(autoClose, productId){
	var ajax;
	if(!productId)
		productId = 0;

	if(document.getElementById('cont_update_product_cii42')){
		ajax = new Ajax.Updater(
			'cont_update_product_cii42',
			'http://www.deskidea.com/es/families/0001/mesas-de-oficina.php',
			{
				parameters: 'update_product_cii42=1&product_cii42='+productId,
				onComplete: function(response){
					var scrollBody;
					
					if(response.status == 200){
						if(_cii42initRight && document.getElementById('cont_update_product_cii42')){
							scrollBody = _cii42getWindowScrollTop();
							if(scrollBody > 125){
								document.getElementById('cont_update_product_cii42').style.right = _cii42initRight+'px';
								document.getElementById('cont_update_product_cii42').className = "top_sc_info_fixed";
							} else {
								document.getElementById('cont_update_product_cii42').style.right = '0px';
								document.getElementById('cont_update_product_cii42').className = "top_sc_info";
							}
						}
						
						_cii42jq('#cont_update_product_cii42').fadeIn("slow");
						if(autoClose){
							_cii42timeout = setTimeout('_cii42closeLatestProduct();', 3000);
							_cii42jq('#cont_update_product_cii42').hover(
								function(){
									if(_cii42timeout != null)
										clearTimeout(_cii42timeout);
								},
								function(){
									if(_cii42timeout != null)
										clearTimeout(_cii42timeout);
									_cii42timeout = setTimeout('_cii42closeLatestProduct();', 3000);
								}
							);
						}
					}
				}
			}
		);
	}
}
function _cii42closeLatestProduct(){
	if(document.getElementById('cont_update_product_cii42')){
		if(_cii42timeout != null){
			clearTimeout(_cii42timeout);
			_cii42jq('#cont_update_product_cii42').hover(function(){}, function(){});
		}
		_cii42jq('#cont_update_product_cii42').fadeOut("slow");
	}
}
function _cii42init(){
	_cii42defineRightPosition();
	window.setInterval('_cii42updateSession();', 300000);
	if(typeof(_cii42initSearch) == "function")
		_cii42initSearch();
}
function _cii42updateSession(){
	var ajax = new Ajax.Request('http://www.deskidea.com/es/families/0001/mesas-de-oficina.php', {parameters: 'maintains_session_cii42=1'});
}
function _cii42defineRightPosition(){
	if(!_cii42initRight && document.getElementById('cont_update_product_cii42') && document.getElementById('cont_top_header_cii42')){
		x = _cii42jq('#cont_top_header_cii42').offsetParent().outerWidth();
		_cii42initRight = (x-_cii42jq('#cont_top_header_cii42').outerWidth())/2;
	}
}
function _cii42getWindowScrollTop(){
	var y = 0;
	
	if( typeof( window.pageYOffset ) == 'number' ) //Netscape compliant
		y = window.pageYOffset;
	else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) //DOM compliant
		y = document.body.scrollTop;
	else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) //IE6 standards compliant mode
		y = document.documentElement.scrollTop;
	
	return parseInt(y, 10);
}

updateShoppingCartInfo();
objectListen('load', window, _cii42init);

function _cii42checkSearch(focus){
	var value, type;
	
	if(document.getElementById('search_cii42')){
		value = document.getElementById('search_cii42').value;
		if(value == 'Encuentra cualquier producto' && focus)
			value = "";
		else if(value == "" && !focus)
			value = 'Encuentra cualquier producto';

		document.getElementById('search_cii42').value = value;
	}
}
function _cii42search(){
	if(document.getElementById('search_cii42')){
		_cii42checkSearch(true);
		if(document.getElementById('search_cii42').value != "")
			document.getElementById('form_cii42').submit();
		else
			_cii42checkSearch();
	}
	return false;
}

function _cii42initSearch() {
		
	suggest_cii42	= new FFSuggest();
	suggest_cii42.init(
		'/client/clients/deskidea/factfinder/FactFSuggest.php',
		'form_cii42',
		'search',
		'suggestLayer_cii42',
		'suggest_cii42',
		false,
		'channel',
		'es',
		false
	);
	document.observe("mousedown",suggest_cii42.hideLayerOutsideCall);
	
		var el = $('search_cii42');
	el.observe('focus', function () { _cii42checkSearch(true); } );
	el.observe('blur', function () { _cii42checkSearch(); } );
}

var suggest_cii42;


