function ps_round(value, precision){
	if (typeof(roundMode) == 'undefined') roundMode = 2;
	if (typeof(precision) == 'undefined') precision = 2;
	
	method = roundMode;
	if (method == 0) return ceilf(value, precision);
	else if (method == 1)
		return floorf(value, precision);
	precisionFactor = precision == 0 ? 1 : Math.pow(10, precision);
	return Math.round(value * precisionFactor) / precisionFactor;
}

function autoUrl(name, dest){
	var loc;
	var id_list;

	id_list = document.getElementById(name);
	loc = id_list.options[id_list.selectedIndex].value;
	if (loc != 0)
		location.href = dest+loc;
	return ;
}

function autoUrlNoList(name, dest){
	var loc;

	loc = document.getElementById(name).checked;
	location.href = dest + (loc == true ? 1 : 0);
	return ;
}
function toggle(e, show){
	e.style.display = show ? '' : 'none';
}

function toggleMultiple(tab){
    var len = tab.length;

    for (var i = 0; i < len; i++)
        if (tab[i].style)
            toggle(tab[i], tab[i].style.display == 'none');
}

/**
* Show dynamicaly an element by changing the sytle "display" property
* depending on the option selected in a select.
*
* @param string $select_id id of the select who controls the display
* @param string $elem_id prefix id of the elements controlled by the select
*   the real id must be : 'elem_id'+nb with nb the corresponding number in the
*   select (starting with 0).
*/
function showElemFromSelect(select_id, elem_id){
	var select = document.getElementById(select_id);
	for (var i = 0; i < select.length; ++i){
	    var elem = document.getElementById(elem_id + select.options[i].value);
		if (elem != null)
			toggle(elem, i == select.selectedIndex);
	}
}

/**
* Get all div with specified name and for each one (by id), toggle their visibility
*/
function openCloseAllDiv(name, option){
	var tab = $('*[name='+name+']');
	for (var i = 0; i < tab.length; ++i)
		toggle(tab[i], option);
}

/**
* Toggle the value of the element id_button between text1 and text2
*/
function toggleElemValue(id_button, text1, text2)
{
	var obj = document.getElementById(id_button);
	if (obj)
		obj.value = ((!obj.value || obj.value == text2) ? text1 : text2);
}

function addBookmark(url, title)
{
	if (window.sidebar)
		return window.sidebar.addPanel(title, url, "");
	else if ( window.external )
		return window.external.AddFavorite( url, title);
	else if (window.opera && window.print)
		return true;
	return true;
}

function writeBookmarkLink(url, title, text, img)
{
	var insert = '';
	if (img)
		insert = writeBookmarkLinkObject(url, title, '<img src="' + img + '" alt="' + escape(text) + '" title="' + escape(text) + '" />') + '&nbsp';
	insert += writeBookmarkLinkObject(url, title, text);
	document.write(insert);
}

function writeBookmarkLinkObject(url, title, insert)
{
	if (window.sidebar || window.external)
		return ('<a href="javascript:addBookmark(\'' + escape(url) + '\', \'' + escape(title) + '\')">' + insert + '</a>');
	else if (window.opera && window.print)
		return ('<a rel="sidebar" href="' + escape(url) + '" title="' + escape(title) + '">' + insert + '</a>');
	return ('');
}

function checkCustomizations(){
	var pattern = new RegExp(' ?filled ?');

	if (typeof customizationFields != 'undefined')
		for (var i = 0; i < customizationFields.length; i++)
			/* If the field is required and empty then we abort */
			if (parseInt(customizationFields[i][1]) == 1 && ($('#' + customizationFields[i][0]).html() == '' ||  $('#' + customizationFields[i][0]).html() != $('#' + customizationFields[i][0]).val()) && !pattern.test($('#' + customizationFields[i][0]).attr('class')))
				return false;
	return true;
}

function emptyCustomizations(){
	if(typeof(customizationFields) == 'undefined') return;
	
	for (var i = 0; i < customizationFields.length; i++)
	{
		$('#' + customizationFields[i][0]).html('');
		$('#' + customizationFields[i][0]).val('');
	}
}

function ceilf(value, precision){
	if (typeof(precision) == 'undefined')
		precision = 0;
	var precisionFactor = precision == 0 ? 1 : Math.pow(10, precision);
	var tmp = value * precisionFactor;
	var tmp2 = tmp.toString();
	// If the current value has already the desired precision
	if (tmp2.indexOf('.') === false)
		return (value);
	if (tmp2.charAt(tmp2.length - 1) == 0)
		return value;
	return Math.ceil(tmp) / precisionFactor;
}

function floorf(value, precision){
	if (typeof(precision) == 'undefined')
		precision = 0;
	var precisionFactor = precision == 0 ? 1 : Math.pow(10, precision);
	var tmp = value * precisionFactor;
	var tmp2 = tmp.toString();
	// If the current value has already the desired precision
	if (tmp2.indexOf('.') === false)
		return (value);
	if (tmp2.charAt(tmp2.length - 1) == 0)
		return value;
	return Math.floor(tmp) / precisionFactor;
}

function setCurrency(id_currency){
	$.ajax({
		type: 'POST',
		url: baseDir + 'changecurrency.php',
		data: 'id_currency='+parseInt(id_currency),
		success: function(msg)
		{
			location.reload(true);
		}
	});
}

function isArrowKey(k_ev)
{
	var unicode=k_ev.keyCode? k_ev.keyCode : k_ev.charCode;
	if (unicode >= 37 && unicode <= 40)
		return true;

}

//On dom ready
$().ready(function()
{
	// Hide all elements with .hideOnSubmit class when parent form is submit
	$('form').submit(function()
	{
		$(this).find('.hideOnSubmit').hide();
	});

 

});
function cambiaImg(obj,img){document.images[obj].src=img;}
var Testbed =  {};
Testbed.vertical_carousel = function(event, ui) {
	console.log(ui.position.left);
	var position = $("#scrollbt").position();
	if (position.left < 6 ) {
		$("#scrollbt").css("left","6px") ;
	}
	if (position.left > 558 ) {
		$("#scrollbt").css("left","558px") ;
	}

}

function verFotoBann(cuentaB){
  clearTimeout(tiempoBanner);
  pausa=true;
  if(bannerSel!=cuentaB){
	clearTimeout(tiempoBanner);
	contBanner=cuentaB;
	nuevaFoto=preImagenes[contBanner].src;
	Effect.Appear("fotoBanner",{ duration: 0.25, from:1, to:0, 
		afterFinish:function(){
			cambiaF(nuevaFoto);
		} 
	});
	
	//document.getElementById("banbol"+contBanner).className ='bolaBa';
	//document.getElementById("banbol"+bannerSel).className ='bolaB';
	bannerSel=contBanner;
  }
}


function precarga(){
	contadorPRE2=0;
	for (pre = 0; pre < imagenes.length-1; pre++){
		if(idImg==imagenes[pre][0]){
			contadorPRE2=pre;	
			contBanner=contadorPRE2;
		}
		preImagenes[pre] = new Image();
		preImagenes[pre].src = dire + imagenes[pre][0]+"-categoria_g.jpg";
	}
	preCargaBanner();
}
function preCargaBanner() {
     _imgPRE2 = new Image();
     if (contadorPRE2 < 1) {
         $(_imgPRE2).load(function() {
               contadorPRE2++;
               preCargaBanner();
          }).attr({	
               src: dire + imagenes[contadorPRE2][0]+"-categoria_g.jpg",
          }).hide();
     }else {
	tiempoBanner=setTimeout('subeFoto()',segundos*1000);
     }
};

function subeFoto(){
	clearTimeout(tiempoBanner);
  	$("#fotobannsup").animate({top: '-154px'}, 7000, "linear" , function() {
		if(!detenido){
			if(imagenes.length>1){tiempoBanner2=setTimeout('presImagen()',segundos*1000);}
		}else{
			tiempoBanner=setTimeout('bajaFoto()',segundos*1000);
		}
	});

} 
function bajaFoto(){
	clearTimeout(tiempoBanner);
  	$("#fotobannsup").animate({top: '0px'}, 7000, "linear" , function() {
		if(!detenido){
		}else{
			tiempoBanner=setTimeout('subeFoto()',segundos*1000);
		}
	});
} 


function cambiaF(nf){
	cambiaImg("fotoBanner",nf);
	$("#fotobannsup").css("top","0px") ;
	$("#fotoBannerC").fadeTo(1000, 1,function() {
		tiempoBanner=setTimeout('subeFoto()',segundos*1000);		
	}); 
}
function presImagen(){
	clearTimeout(tiempoBanner);
	if (contBanner < imagenes.length-2){contBanner ++}
	else{contBanner =0;}
	nuevaFoto=preImagenes[contBanner].src;
	$("#fotoBannerC").animate({"opacity": 0},  1000, "linear", function(){ cambiaF(nuevaFoto); } ); 
	//document.getElementById("banbol"+contBanner).className ='bolaBa';
	//document.getElementById("banbol"+bannerSel).className ='bolaB';
	bannerSel=contBanner;
	if(!detenido && imagenes.length>1){
		//tiempoBanner=window.setTimeout('presImagen()',segundos*1000);
	}
}


function pausar(){
	pausa=true;
	clearTimeout(tiempoBanner);
}
function seguir(){
	clearTimeout(tiempoBanner);
	pausa=false;
	tiempoBanner=setTimeout('presImagen()',1000);
}
function verTodos(){
	$('#paginacionx').submit();
}

/*escaparate*/
function precarga2(){
	ventana_alto = $(window).height();  
	ventana_ancho = $(window).width();  
	cxlogo=Math.ceil((ventana_ancho-1000)/2)+3;
	$("#fondomenu").css("opacity", "0.75"); 
	$("#headerlogo").css("left",cxlogo+"px") ;
	$("#headerlogo").css("display","block") ;
	$("#subcategories").css("left",cxlogo+"px") ;
	$("#subcategories").css("opacity", "0");
	$("#subcategories").show();
	$("#subcategories").animate({opacity: '1'}, 3000,'linear');
	$("#pie_cg").css("left",cxlogo+"px") ;
	contadorPRE=0;
	preCargaX();
}

function preCargaX() {
	_imgPRE = new Image();
	if (contadorPRE < imagenes.length) {
         $(_imgPRE).load(function() {
               contadorPRE++;
               preCargaX();
          }).attr({	
               src: imagenes[contadorPRE],
          }).hide();
     }else {
	iniciar2()
     }
};
function iniciar2(){

	capa_ancho = $("#headerescapF").width();  
	dif=ventana_ancho-capa_ancho;
	if(dif<0){
		dif-=10
		izqCM();
	  $(document).ready(function() {
	    $("#scrollbt").draggable({axis: 'x',drag: Testbed.vertical_carousel });
  	  });
	}
}
function izqCM(){
	tiempoBannerX=setTimeout('izqC()',8000);
}
function izqC(){
	clearTimeout(tiempoBannerX);
	tiempo=Math.abs(dif)*40;
	$("#headerescapF").animate({left: dif+'px'}, tiempo, 'linear', function() {
		$("#headerescapF").animate({left: '10px'}, tiempo,'linear', function() {
			izqC();
		});

	});
}
