var href = "";

$(document).ready(function(){
	// tipsy
	$('#header-skype').tipsy({gravity: 's'});
	$('#header-msn').tipsy({gravity: 's'});
	$('#header-mail').tipsy({gravity: 's'});
	
	// keep session alive
	setTimeout("keepalive()", 600000);

	//calculo de portes
	$('.zona_pais').live('change',function(){
		var zid = $(this).val();
		if (zid != 'ES') {
			$('.zona_provincia').fadeOut();
		} else if ( zid == 'ES') {
			$('.zona_provincia').fadeIn();
		}
	});
	//bandericas
	$("#sel_idiomas").msDropDown().change(function(){
		var lang = $(this).val();
		var url  = window.location.href;
		var old  = url;
		switch (lang) {
			case 'en':
				url = url.replace(/http:\/\/(www.)?deco/,'http://en.deco');
			break;
			case 'es':
				url = url.replace(/http:\/\/(en.)?deco/,'http://www.deco');
			break;
			default:
				url = url.replace(/http:\/\/(en.)?deco/,'http://www.deco');
				url = 'http://www.google.com/translate_c?hl=es&langpair=es%7C'+lang+'&u='+url;
			break;
		}
		
		if (old != url) {
			window.location=url;
		}
	});
	
	$("a[rel!='galeria']").click(function(event){
		if (event.target.tagName == "IMG") {
			href = event.target.parentNode.href;
		}
		else {
			href = event.target.href;
		}
		
		if (href != undefined) {
			var arrayHref = href.split("/");
			var arrayUrl = document.URL.split("/");
			if ((arrayHref[0] == "http:" || arrayHref[0] == "https:") && arrayHref[2] != arrayUrl[2]) {
				$.fancybox($('#aviso_confianza').get(0).innerHTML);
				return false;
			}
		}
	});

});

function redirige(){
	//_blank
	window.open(href, '_blank');
	//Redirect
	//window.location.href = href;
	$.fancybox.close();
}

jQuery.fn.nuevoprecio = function(txt,negativo){
	if (negativo !== undefined) {
		txt = '-'+txt;
	}
    return this.each(function(){
		jQuery(this).fadeOut(300,function(){
			jQuery(this).html(txt);
			jQuery(this).fadeIn(300);
		});
	});
};
jQuery.extend({
   precio: function(pvp,iva){
		if (iva === undefined ) {
			iva = 1.18;
		} else {
			iva = 1;
		}
		var num = new Number(Math.round(pvp*iva * 100) / 100);
		return num.toFixed(2)+' €';
   },
   cofidis: function(pvp){
		var num = new Number(Math.round((100*pvp*0.02*Math.pow(1.02,0))/(1-Math.pow(1.02,-48)))/100);
		return num.toFixed(2);
   },
   stock: function(cantidad){
       	var val = '';
		if (lang !== undefined && lang == 1) {
			var msg = ['In stock','Few units','Upon request'];
		} else {
			var msg = ['En stock','Pocas unidades','Bajo pedido'];
		}
		
		
	    if (cantidad >= 5) {
	        val = '<div class="texto-verde">Stock: '+msg[0]+'</div>';
	    } else if (cantidad > 0) {
	        val = '<div class="texto-amarillo">Stock: '+msg[1]+'</div>';
	    } else {
	        val = '<div class="texto-rojo">Stock: '+msg[2]+'</div>';
	    }
	    return val;
   },
   acabado: function(imagen,id){
		if (imagen > 0) {
			return '/img/acabados/' + id + '.' + $.id_to_ext(imagen);
		} else {
			return '/img/acabados/default_b.jpg';
		}
   },
   id_to_ext: function(id){
		var ret = '';
		id = parseInt(id);
		switch (id) {
		case 1:
		ret = 'gif';
		break;
		case 2:
		ret = 'jpg';
		break;
		case 3:
		ret = 'png';
		break;            
		}
		return ret;
   }
});
function keepalive() {
  $.post("/action/keepalive.php", null,
    function(data) { 
        setTimeout("keepalive()", 600000);
    }
  );
}
