var host;
var hostweb;

function return_back_by_cookie(url_){
	//leemos los parametros de la cooki
	var nameEQ = "back_url_params=";
	back_url_params = "";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) back_url_params = c.substring(nameEQ.length,c.length);
	}
	url = "http://" + window.location.hostname
	if (back_url_params!="") {
		url += "?" + back_url_params;
	}
	
	if (url_!="")
		url = url_;
		
	window.open(url,"_self");
	//window.location.href = url;

	return false;
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

var addthis_config = { 
	username: "compegps",
	data_track_clickback: true,
	ui_click: false
}

function roundNumber(rnum, rlength) {
	var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
	return newnumber; 
}

function es_evadeo(id) {
	var newid = id;
	if (newid.indexOf("|")!=-1)
		newid = newid.substr(0,newid.indexOf("|"));		
	if (newid==380 || newid==456 || newid==433 || newid==434 || 
		newid==435 || newid==436 || newid==452 || newid==451 || 
		newid==428 || newid==429 || newid==430 || newid==439 || 
		newid==440 || newid==444 || newid==724 || newid==725 || 
		newid==731 || newid==793 || newid==794 || newid==802 || newid==870 || newid==871 || 
		newid==1135 || newid==1141 || newid==928 || newid==994 || 
		(newid>=776 && newid<=784) || 
		(newid>=805 && newid<=812) ||
		(newid>=1080 && newid<=1089)
		)
		return 1;
	return 0;
}

var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");

function hide_loading(){
	$.unblockUI();
	return true;	
}

function show_loading(timer) {
	$.blockUI(
			{ message: '<h1><img src="'+hostweb+'images/loader.gif" /> Loading...</h1>'},
			{ css: { 			
            border: 'none', 
            padding: '15px', 
            backgroundColor: '#000', 
            '-webkit-border-radius': '10px', 
            '-moz-border-radius': '10px', 
            opacity: .5, 
            color: '#fff' 
			}
			}
		); 		
	if (timer==undefined)
		timer = false;
	if (timer)
		setTimeout("hide_loading();", 6000);
	return true;
}

var browser=navigator.appName;
var b_version=navigator.appVersion;
var version=parseFloat(b_version);

// This next section sets up the fading images for the Sponsors section on the sidebar. 
// SPEED: In milliseconds - how long the transition should take.
// TYPE: Type of slideshow: 'sequence', 'random' or 'random_start'
// CONTAINERHEIGHT: The height of the div that is calling the innerfade method.
var menu_num_elem;
var menu_width;
var menu_pos_middle;
var menu_last_li_left;
var width_logo;
var height_space;
function posi_banner(){

	if (!$('#header')[0])
		return;
		
	if (menu_num_elem==undefined)
		menu_num_elem 	= $('#header .lavaLampBottomStyle li').length;
	
	if (menu_width==undefined)
		menu_width 		= $('#header .lavaLampBottomStyle').width();	//599
		
	//position of generic menu
	gen_head=$('#header').position();
	if ($('.lavaLampBottomStyle')[0] && $('#menu_space')) {
		tam_head = gen_head.left + $('#header').width();
		menu_last_li_left = $('#header .lavaLampBottomStyle li')[menu_num_elem-1].offsetLeft + 
				   $('#header .lavaLampBottomStyle li')[menu_num_elem-1].offsetWidth; //463
		menu_pos_middle = ((menu_width/2) - (menu_last_li_left/2));
		gen_elem_left = 0;
		if ($('.fade.innerfade')[0]) {
			gen_elem = $('.fade.innerfade').position();
			gen_elem_left = gen_elem.left;
		}
		pos_logo = gen_elem_left + width_logo;
		ul_left = pos_logo + menu_pos_middle;
		$('#menu_space').css('left',ul_left);
	}
	//height_space = $('.corner_container').height();
	height_space = $(document).height();
	hl = $('#main_left').height();
	//hr = $('#main_right').height();
	var last_div;
	if ($('#tier3')[0])
		last_div = $('#tier3');
	else if ($('#mapas')[0])
		last_div = $('#mapas');
	else if ($('#tier2')[0]) {
		last_div = $('#tier2');
	}else if ($('#main_left')[0])
		last_div = $('#main_left');
	
	hr = last_div.height() + last_div.position().top;
	max = hl + 90; //take account menu expands
	if (hr>max)
		max = hr;		
	$('.rounder_mainContent').css('height', max);
	$('#main_right').css('height', max + 37 );
	//$('.rounder_mainContent').css('height', height_space - 90);
	//$('#main_right').css('height', height_space - 53);

}

function do_display(id,on){
	if (on==undefined)
		on = false;
	t_id = id;
	t_id_sub = id.substring(0,1); 
		
	id_sel = '#sub_'+t_id;
	if (t_id_sub!="1") //only option1 can be showed
		return false;
	
	//es submenu?
	//if ($(id_sel)[0])
	displayed = !($(id_sel).css('display')=='none') 
	//var pa = document.getElementById(id_sel);
	//pa.style.display = 'block' 
	//$(id_sel).css('display', 'none'),
	if (!displayed || on)
		$(id_sel).show(500);
	else
		$(id_sel).hide(500);		

	/*if (off)
		$(id_sel).hide(500);
	else if (!displayed)
		$(id_sel).show(500);
	*/
	return false;
}
function do_display_sub(id,on){
	if (on==undefined)
		on = false;
	t_id = id;
	t_id_sub = id.substring(0,1); 

	if (t_id_sub!="1") //only option1 can be showed
		return false;
	
	id_sel2 = '#2sub_'+t_id
	if ($(id_sel2)[0]) {
		displayed2 = !($(id_sel2).css('display')=='none') 
		if (!displayed2 || on) {
			$(id_sel2).show(500);
			//$(id_sel2).css({display:"block !important"});
		} else
			$(id_sel2).hide(500);
	}
	posi_banner();
}

jQuery.easing['BounceEaseOut'] = function(p, t, b, c, d) {
	if ((t/=d) < (1/2.75)) {
		return c*(7.5625*t*t) + b;
	} else if (t < (2/2.75)) {
		return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
	} else if (t < (2.5/2.75)) {
		return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
	} else {
		return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
	}
};

var striped_rowIndex = 0;
var rowClass = 'first';
function layoutTableShopcart(){
	if ($('table[class=table_shopcart] tr:even')[0]) 
		$('table[class=table_shopcart] tr:even').addClass('parBlanco');	
}
function logged_checker(){
	$.get(hostweb+"get_content_login_searcher.php", function(data){ 
		$('#login_searcher').html(data);
	});	
}	

function goto_search(){
	txt_search = $('#search').val();
	if (txt_search=="")
		return;
	if (txt_search==txt_search_original)
		return;
	txt_search = encodeURIComponent(txt_search);
	link = hostweb_search+txt_search+"&category=searcher";
	document.location = link;
}
function goto_search_event(event) {
  if (event.keyCode == 13) {
	goto_search();
  }  
}

$(function() {	
	$('.rounder_mainContent').wrap('<div class="corner_container">'+
        '<div class="bdc"><div class="cc"><div class="ss">'+
        '</div></div></div>'+
        '</div>');		
	$('.rounder_mainContent_embed').wrap('<div class="corner_container embed">'+
        '<div class="bdc"><div class="cc"><div class="ss">'+
        '</div></div></div>'+
        '</div>');		
	$('.rounder').wrap('<div class="dialog">'+
        '<div class="bd"><div class="c"><div class="s">'+
        '</div></div></div>'+
        '</div>');		
	$('.rounder_urgent2').wrap('<div class="dialog">'+
        '<div class="bd"><div class="c"><div class="s">'+
        '</div></div></div>'+
        '</div>');		
	$('.rounder_second').wrap('<div class="dialog">'+
        '<div class="bd"><div class="c"><div class="s">'+
        '</div></div></div>'+
        '</div>');		
	$('.rounder_prod_list').wrap('<div class="dialog with_100">'+
        '<div class="bd"><div class="c"><div class="s">'+
        '</div></div></div>'+
        '</div>');
	$('.tab_container').wrap('<div class="dialog with_100 pad_0">'+
        '<div class="bd"><div class="c"><div class="s">'+
        '</div></div></div>'+
        '</div>');
	$('.rounder_shop').wrap('<div class="dialog with_100">'+
        '<div class="bd"><div class="c"><div class="s">'+
        '</div></div></div>'+
        '</div>');
	$('.rounder_news').wrap('<div class="dialog with_100">'+
        '<div class="bd"><div class="c"><div class="s">'+
        '</div></div></div>'+
        '</div>');
	$('.rounder_urgent').wrap('<div class="dialog">'+
        '<div class="bd"><div class="c"><div class="s">'+
        '</div></div></div>'+
        '</div>');
	$('.dialog').prepend('<div class="hd">'+
			'<div class="c"></div>'+
			'</div>')
	$('.dialog').append('<div class="ft">'+
			'<div class="c"></div>'+
			'</div>');
	$('.corner_container').prepend('<div class="hdc">'+
			'<div class="cc"></div>'+
			'</div>')
	$('.corner_container').append('<div class="ftc">'+
			'<div class="cc"></div>'+
			'</div>');
			
	//totalheight space of vertical separator
	height_space = $('.corner_container').height();
	$('.rounder_mainContent').css('height', height_space - 90);

	$(window).resize(function() {
		posi_banner();
	});
	$("#tier2").resize(function() {
		posi_banner();
	});
	$(window).load(function() {
		posi_banner();
	});

	if ($('.fade')[0])
		$('.fade').innerfade({
						speed: 1000,
						timeout: 5000,
						type: 'sequence',
						containerheight: '150px',
						containerwidth: '350px'
		});
	
	//menu position
	width_logo = $('.fade.innerfade').width();
	menu_num_elem 	= $('#header .lavaLampBottomStyle li').length;
	menu_width 		= $('#header .lavaLampBottomStyle').width();	//599
	
	if ($('#nav')[0])
		$("#nav").lavaLamp({
			fx: "easeOutBack", 
			speed: 500
			//click: function(event, menuItem) {
			//	return false;
			//}
		});
	
	/*$('#sidebar ul li a').mouseover(function(){	
		id = $(this).attr('id');
		len = $('#sidebar ul li a').length;
		//close all others
		for (i=0;i<len;i++){
			ida = $('#sidebar ul li a')[i].id;
			ida = ida.substr(0,1);
			idb = id.substr(0,1);
			if (ida!=idb)
				do_display(ida,true);
		}
		do_display(id);
	});	*/
	$('#sidebar ul li a').click(function(){	
		id = $(this).attr('id');
		do_display(id);
	});	
	/*$('#sidebar ul li a').mouseout(function(){	
		id = $(this).attr('id');
		//sss = "#sidebar #sub_" + id;
		//if (!$(sss)[0]) do_display(id,true)
	});	*/
	$('#sidebar ul ul li a').click(function(){	
		id = $(this).attr('id');
		do_display(id);
		do_display_sub(id);
	});	
	$('#sidebar ul ul ul li a').click(function(){	
		id = $(this).attr('id');
		//do_display_sub(id);
	});	
	
	//table evens
	layoutTableShopcart();
	
	//sliding banner
	if ($('#slides')[0])
		$('#slides').slides({
			preload: true,
			preloadImage: 'images/loading.gif',
			play: 3000,
			pause: 2500,
			hoverPause: true,
			animationStart: function(){
				$('.caption').animate({
					left:-736
				},100);
			},
			animationComplete: function(current){
				$('.caption').animate({
					left:0,
					bottom:0
				},200);
				//if (window.console && console.log) {
					// example return of current slide number
				//	console.log(current);
				//};
			}
		});
	
	if ($(".banner_central_link")[0] || $("#video_mapwizard")[0]) {
		id_pre = ".banner_central_link";
		wp=853; hp=480;
		if ($("#video_mapwizard")[0]){
			id_pre = "#video_mapwizard";
			wp=940; hp=540;		
		}
		$(id_pre).click(function(){
			var href = $(this).attr("href");
			es_youtube = (href.indexOf("youtube")!=-1)
			es_vimeo = (href.indexOf("vimeo")!=-1)			
			if (es_youtube)
				href = href.replace(new RegExp("watch\\?v=", "i"), 'v/');
			if (es_vimeo)	
				href = href.replace(new RegExp("([0-9])","i"),'moogaloop.swf?&amp;fullscreen=1&amp;autoplay=1&amp;clip_id=$1')				
			if (es_youtube || es_vimeo) {
				$.fancybox({
					'padding'		: 0,
					'autoScale'		: false,
					'openEffect' 	: 'elastic',
					'closeEffect' 	: 'elastic',
					'width' 		: wp,
					'height' 		: hp,
					'centerOnScroll': true,
					'href'			: href,
					'type'			: 'swf',
					'swf'			: {
						 'wmode'		: 'transparent',
						'allowfullscreen'	: 'true'
					}
				});
				return false;
			}
		});
	}	
	
	$('#click_win_register_form').live({
		click: function() {
			if (_gaq!=undefined)
				_gaq.push(['_trackEvent','Web_access','Register','Go']);
			}
	});
	$('#click_win_login_form').live({
		click: function() {
			if (_gaq!=undefined)
				_gaq.push(['_trackEvent','Web_access','Login','Go']);
			link = hostweb+"get_content_win_login.php";
			$.fancybox({
				'type'		:'iframe',
				/*'content'		: $('#win_login').html(),*/
				'href'		: link,
				'padding'		: 0,
				'autoScale'		: false,
				'openEffect' 	: 'elastic',
				'closeEffect' 	: 'elastic',
				'width' 		: 450,
				'height' 		: 250,
				'scrolling' 	: 'no',
				'titleShow'		: false,
				'centerOnScroll': true,
				'autoDimensions': false,
				'onClosed'		: function() {
					$("#login_error").hide();
				}
			});
		}
	});	

	var ver = true;
	$('#open_menu_profile').live({
		click: function() {
			if (ver) {
				ver = false;
				$('#menu_profile').hide();
			}
		},
		mouseover: function() {
			ver = true;
			$('#menu_profile').show().animate({opacity: '+=0'}, 5000).fadeOut('fast');;
		}
	});
	
	$('#menu_profile').mouseout(function () {
		if (!ver)
			$('#menu_profile').hide();
	});

	//TABS
	$('#pestanyas').tabs({ 
		tabTemplate: '<li class="set_tab"><a href="#{href}"><span>#{label}</span></a></li>',
		show: function(event, ui) { 
			if ($('.tab_container')[0] && 
				$('.tab_container')[0].children.length>1) {
				last_div = $('#tier2');
				max_tab = $('#tier2').height();
				if (max_tab<hl) 
					max_tab = hl
				//si hay tabs... los recorremos
				total = $('.tab_container')[0].children.length;
				for (i=0;i<total;i++){
					$('.tab_container')[0].children[i];
					elemi =  $($('.tab_container')[0].children[i]).attr('id');
					if (elemi!="") {
						elemi = $("#" + elemi);
						if (elemi[0]) {
							he_ele = elemi.height() + elemi.position().top;
							if (max_tab<he_ele)
								last_div = elemi;
						}
					}
				}	
				hr = last_div.height() + last_div.position().top;
				max = hl + 90; //take account menu expands
				if (hr>max)
					max = hr;		
				$('.rounder_mainContent').css('height', max);
				$('#main_right').css('height', max + 37 );
			}
		}
	});
	
	//mapas!!!
	$('input[name="address"]').keypress(function(e){ 
		if(e.which==13) {
			if($(".is_distributors")[0])
				showAddress();
			else if(!$("#is_roadbook")[0])
				ShowMaps();
			else
				ShowRoadbooks();
			return false;
		}
	});	
	$('#address').focus(function() {
		$(this).val("");
	});	
	
	
	$('#click_search').click(function(){	
		goto_search();
		//do_display_sub(id);
	});	
	$('#search').keydown(goto_search_event);
	$('#search').focus(function() {
		$(this).val("");
	});	
	//gallery!!
	if ($('#mycarousel_gal')[0]) 
		$('#mycarousel_gal').jcarousel({
			animation: 1000,
			easing: 'BounceEaseOut'
		});
		
	if ($("a.fancy_gal")[0])
		$("a.fancy_gal").attr('rel', 'gallery').fancybox({titlePosition:'inside'});

	$(".fancy_gal_click").click(function(){
		id_gal = $(this).attr('id_gal');
		str = "a." + id_gal;
		$(str).fancybox({titlePosition: 'inside'});		
		$(str).click();
		return false;
	});
	
	if ($("INPUT.spinbox")[0])
		$("INPUT.spinbox").spinbox({
		  min: 1,    // Set lower limit or null for no limit.
		  max: 99,  // Set upper limit or null for no limit.
		  step: 1 // Set increment size.
		});
		
	$(".show_faq").click(function(){
		id = $(this).attr('ident');
		//hay problemas in firefox con posibanner
		te = "#faq_" + id;
		if ($(te)[0]) {
			displayed = !($(te).css('display')=='none') 
			if (!displayed) {
				siz = $('.rounder_mainContent').height();
				siz += $(te).height();
				$('.rounder_mainContent').css('height', siz);
				$('#main_right').css('height', siz + 37 );			
				$(te).show("fast");
			} else {
				posi_banner();
				$(te).hide("fast");
			}
		}
		return false;
	});
	$('#show_betas').live({
		click: function() {
			te = ".txt_betas";
			pe = "#pestanyas";
			if ($(te)[0]) {
				displayed = !($(te).css('display')=='none') 
				if (!displayed) {
					siz = $('.rounder_mainContent').height();
					siz += $(te).height();
					$('.rounder_mainContent').css('height', siz);
					$('#main_right').css('height', siz + 37 );			
					$(pe).hide("fast");
					$(te).show("fast");
				} else {
					posi_banner();
					$(pe).show("fast");
					$(te).hide("fast");
				}
			}
			return false;
		}
	});	

	$(".btn_shop,.ga_event").click(function(){
		event_name = $(this).attr('ga_event');
		event_cat = $(this).attr('ga_event_cat');
		/*if(undefined!= $(this).attr('ga_event_cat'))
			event_cat = $(this).attr('ga_event_cat');
		else{
			event_cat=undefined;
		}*/
		tipo=$("input:radio[name=payament_type]:checked").val();
		if (event_name!="" && _gaq!=undefined){
			if (tipo!=undefined)
				event_name += '_TYPE_' + tipo;
			if (event_cat!=undefined)
				_gaq.push(['_trackEvent',event_cat,'Login',event_name]);
			else
				_gaq.push(['_trackEvent','Cart','Checkout',event_name]);
		}
		return true;
	});	
});


function testprotocolhandler(url) {
	if (navigator.appName=="Microsoft Internet Explorer") {	
		if (g_compeinstallwizard==undefined)
			return false;
		if (g_compeinstallwizard==1)
			alert("IE con g_compeinstallwizard=1");
		//window.location = "#";		
	} else {
		if ("protocolLong" in el) {
		   return false;
		}		
		var ifr = document.getElementById("protocolTester");
		try {
			ifr.contentWindow.location = el.href;
			window.setTimeout(function () {
			   try {
			       alert(ifr.contentWindow.location); 
			   } catch (e) { alert("Application is not installed"); }
			}, 0);
		} catch(e) {
		    if (e.name == "NS_ERROR_UNKNOWN_PROTOCOL") {
		        alert("Application is not installed");
		    }
		}		
	}
	return false;	
}

function change(id) {
	
	idmanual_type_ 		= "#idmanual_type_" + id;
	idmanual_lang_ 		= "#idmanual_lang_" + id;

	idmanual_type = $(idmanual_type_).val();
	idmanual_lang = $(idmanual_lang_).val();
	if (idmanual_type==undefined ||
		idmanual_lang==undefined || 
		id==undefined)
		return false;
	if ( idmanual_type==-1 ||
		idmanual_lang==-1 || 
		id==undefined)
		return false;
			
	show_loading();
	$.ajax({
		type	: "POST",
		cache	: false,
		url		: hostweb + "post_manual_download.php",
		data	: {idmanual_type:idmanual_type,idmanual_lang:idmanual_lang,idmanual_product:id},
		success: function(data) {
			if (data=="")
				return false;
			ok = (data.indexOf("STATUS=OK")!=-1);
			if (ok) {
				data = data.replace("STATUS=OK\r\n","");
				//html_ = "<script>window.open(\"" + data + "\",\"_blank\");<\/script>";
				//$('#l_download').html(html_);
				if (data=="")
					data = "#";
				$('#manual_product_' + id).attr("href", data);
				$('#manual_product_' + id).attr("target", "_blank");
				//window.open(data,"_blank");
			}else {
				$('#manual_product_' + id).attr("href", "#");
				$('#manual_product_' + id).removeAttr("tarjet");
				$('#dialog').html(data);
				$('#dialog').dialog('open');
			}
		},
		complete: function(){
			hide_loading();
		}
	});
	return false;
}


