﻿
jQuery.fn.fadeToggle = function(speed, easing, callback) { 
   return this.animate({opacity: 'toggle'}, speed, easing, callback); 
};

var search_value, newsletter_value;
var current_selected_family;
var current_phrase;

$(window).load(function(){

	if (typeof gc_lang != "undefined") {
		loadNav(gc_lang);
		loadGroups(gc_lang);
	}

	if ($("div#gallery").length > 0) {
		setTimeout('$("div#gallery").slideView();',100);
	}

	if ($("div#events_gallery").length > 0) {
		setTimeout('$("div#events_gallery").slideView();',100);
	}

	if (typeof gc_home_slides != 'undefined' && gc_home_slides >= 1) {
		slide_interval = setInterval("homeLoop();",8000);
		if ($("div#home_gallery").length > 0) {
			$("div#home_gallery").mouseenter(function(){
				clearInterval(slide_interval);
			});
			$("div#home_gallery").mouseleave(function(){
				slide_interval = setInterval("homeLoop()",8000);
			});
		}			
	}

	if ($("ul#all_recipes").length > 0) {
		$("ul#all_recipes li").bind('mouseover',function(){
			//$(this).find("div").show();
		});
		$("ul#all_recipes li").bind('mouseout',function(){
			//$(this).find("div").hide();
		});
	}

	if ($("div#home_gallery").length > 0) {
		setTimeout('$("div#home_gallery").slideView({link_to_next:false});',100);
	}

	
	if ($("div#right_col div.product_details").length > 0) {
		setTimeout('$("div.product_details").jScrollPane();',100);
	}

	if ($("div#recipes_strip").length > 0) {
		setTimeout('$("div#recipes_strip").jSkrollHorizontalPane({arrowSize:1, showArrows:true, scrollbarHeight:10, scrollbarMargin:0}); $("div#recipes_strip").fadeIn(400);',100);
	}

	if ($("div#recipes_related_strip").length > 0) {
		setTimeout('$("div#recipes_related_strip").jSkrollHorizontalPane({arrowSize:20, showArrows:true, scrollbarHeight:10, scrollbarMargin:0}); $("div#recipes_related_strip").fadeIn(400);',100);
	}
	if ($("div#tiendas1").length > 0) {
$("#pikame").PikaChoose();
}
	if ($("div#products_related div.product_strip").length > 0) {
		setTimeout('$("div#products_related div.product_strip").jSkrollHorizontalPane({scrollbarHeight:10, scrollbarMargin:0});',100);
		setTimeout('$("div#products_related div.product_strip div img").fadeIn(300);',200);
	}	
	
	if (typeof gc_open_prod != 'undefined' && gc_open_prod >= 1 && $("#newsletter_prod").length > 0) {
		$("#newsletter_prod").click();
	}
	
	if ($("#recipe_select").length > 0) {
		$("#recipe_select select").change(function(){
			if($("#recipe_select select option:selected").val() >= 1) {
				$("#recipe_select").submit();
			}
		});		
	}
	
});

$(document).ready(function(){
	//$('#bannerhome ul').slideImages('horizontal', 521, 90, 500, 'easeOutExpo', 'timer=8000'); 

	$('#pane1').RScrollPane();
	
	if ($("div#video_link").length > 0) {
		$("div#video_link").find("a").click(function(){
			$("div#video_link").addClass("hidden");
			$("div#images_link").removeClass("hidden")
			$("div#gallery").addClass("hidden");
			$("div#video").removeClass("hidden");
			$("div.stripTransmitter").hide();
			return false;
		});
	}



	if ($("div#images_link").length > 0) {
		$("div#images_link").find("a").click(function(){
			$("div#video_link").removeClass("hidden");
			$("div#images_link").addClass("hidden")
			$("div#gallery").removeClass("hidden");
			$("div#video").addClass("hidden");
			$("div.stripTransmitter").show();
			return false;
		});
	}

	if ($("div.event_detailed").length > 0) {
		var div_height = $("div.event_detailed").height();
		$("div.event_detailed").css('height', div_height + 'px');
	}

	$.preloadCssImages();

	$("p.ml").mouseover(function () {
		
		if ($(this).hasClass("menu_links")) {
			$(this).removeClass("menu_links");
			$(this).addClass("menu_links_hover");
		}
		
	}).mouseout(function () {
		
		if ($(this).hasClass("menu_links_hover")) {
			$(this).removeClass("menu_links_hover");
			$(this).addClass("menu_links");
		}

	});

	if ($('#stores_thumbs').length > 0) {
		$('#stores_thumbs').children().each(function(){
			$(this).children().click(function(){
				$("#photo").html('<img src="' + $(this).attr("href") + '" width="664" height="443" alt="" />');
				return false;
			});
		});
	}

	$("#mes").jSuggest({
		url: "/autocompletar.php?tipo=mes&lang="+lang,
		type: "GET",
		data: "searchQuery",
		autoChange: true
	});
	/*$("#tags").jSuggest({
		url: "/autocompletar.php?tipo=tags&lang="+lang,
		type: "GET",
		data: "searchQuery",
		autoChange: true
	});*/

if ($('#all_recipes').length > 0) {
	$('#all_recipes li').each(function () {
		$(this).mouseover(function () {
			//	$(this +"div").find('ul').css('display', 'block');
				$(this).find(".events").show();
				
			}).mouseout(function () {
				$(this).find(".events").hide();
			});
		
		
		
		
		//var distance = 10;
//		var time = 250;
//		var hideDelay = 1;
//		var hideDelayTimer = null;
//		var beingShown = false;
//		var shown = false;
//		var trigger = $(this);
//		var popup = $('.events ul', this).css('opacity', 0);
//		var popevents = $('.events', this);
//
//		$([trigger.get(0), popup.get(0)]).mouseover(function () {
//			if (hideDelayTimer) clearTimeout(hideDelayTimer);
//			if (beingShown || shown) {
//				return;
//			} else {
//				beingShown = true;
//				popup.css({
//					top: 0,
//					left: 0,
//					display: 'block',
//					zindex: 9999
//				})
//				.animate({
//					bottom: '+=' + distance + 'px',
//					opacity: 1
//				}, time, 'swing', function() {
//					beingShown = false;
//					shown = true;
//				});
//			}
//		}).mouseout(function () {
//			if (hideDelayTimer) clearTimeout(hideDelayTimer);
//			hideDelayTimer = setTimeout(function () {
//				hideDelayTimer = null;
//				popup.animate({
//					bottom: '-=' + distance + 'px',
//					opacity: 0
//				}, time, 'swing', function () {
//					shown = false;
//					popup.css('display', 'none');
//				});
//			}, hideDelay);
//		});
	});	
	
}


if ($('.searchList').length > 0) {
	$('.searchList').each(function () {
		var distance = 10;
		var time = 250;
		var hideDelay = 1;
		var hideDelayTimer = null;
		var beingShown = false;
		var shown = false;
		var trigger = $(this);
		var popup = $('.events ul', this).css('opacity', 0);
		var popevents = $('.events', this);

		$([trigger.get(0), popup.get(0)]).mouseover(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			if (beingShown || shown) {
				return;
			} else {
				beingShown = true;
				popup.css({
					bottom: 20,
					left: -76,
					display: 'block'
				})
				.animate({
					bottom: '+=' + distance + 'px',
					opacity: 1
				}, time, 'swing', function() {
					beingShown = false;
					shown = true;
				});
			}
		}).mouseout(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				popup.animate({
					bottom: '-=' + distance + 'px',
					opacity: 0
				}, time, 'swing', function () {
					shown = false;
					popup.css('display', 'none');
				});
			}, hideDelay);
		});
	});
}
if ($('.date_has_event').length > 0) {
	$('.date_has_event').each(function () {
		var distance = 10;
		var time = 250;
		var hideDelay = 1;
		var hideDelayTimer = null;
		var beingShown = false;
		var shown = false;
		var trigger = $(this);
		var popup = $('.events ul', this).css('opacity', 0);
		var popevents = $('.events', this);

		$([trigger.get(0), popup.get(0)]).mouseover(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			if (beingShown || shown) {
				return;
			} else {
				beingShown = true;
				popup.css({
					bottom: 20,
					left: -76,
					display: 'block'
				})
				.animate({
					bottom: '+=' + distance + 'px',
					opacity: 1
				}, time, 'swing', function() {
					beingShown = false;
					shown = true;
				});
			}
		}).mouseout(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				popup.animate({
					bottom: '-=' + distance + 'px',
					opacity: 0
				}, time, 'swing', function () {
					shown = false;
					popup.css('display', 'none');
				});
			}, hideDelay);
		});
	});
	}
	if ($("div.money").length > 0) {
		$("div.money img").click(function(){ 
			$("div.money img").removeClass("selected");
			$(this).addClass("selected");
			$("#gifts_price").val($(this).attr("id").split("_")[1]);
			$("div.money img").each(function(){
				$(this).attr("src","/img/layout/money_" + $(this).attr("id").split("_")[1] + ".gif");
			});
			$(this).attr("src","/img/layout/money_" + $(this).attr("id").split("_")[1] + "_on.gif");
		});
		$("div.money img").hover(function(){ 
			if (!$(this).hasClass("selected")) {
				$(this).attr("src","/img/layout/money_" + $(this).attr("id").split("_")[1] + "_on.gif");
			}
		}, function() {
			if (!$(this).hasClass("selected")) {
				$(this).attr("src","/img/layout/money_" + $(this).attr("id").split("_")[1] + ".gif");
			}
		});
	}

	if ($("div.gifts div.gifts_padding div.phrases").length > 0) {
		
		$("#gifts_phrases").val('');
		
		$("div.gifts div.gifts_padding div.phrases div.item").click(function(){ 
			var strings_id = '';
			var is_selected = false;
			my_id = $(this).attr("id").split("_")[1];
			anchor_obj = $(this).children("p").children("a");
			selected_values = $("#gifts_phrases").val().split(",");
			jQuery.each(selected_values,function(){
				if (this > 0 && this == my_id) {
					is_selected = true;
				}
			});
			if (!is_selected) {
				$(anchor_obj).addClass('enabled');
			}	 else {
				$(anchor_obj).removeClass('enabled');
			}		
			
			if ($(this).children("div").children("p").children("a").hasClass('enabled')) {
				current_phrase = $(this).attr("id");
				$(this).children("div").children("p").children("a").removeClass('enabled');
			} else {
				current_phrase = $(this).attr("id");
				$(this).children("div").children("p").children("a").addClass('enabled');
			}
			$("div.gifts div.gifts_padding div.phrases").children("div").each(function(){
				if ($(this).children("p").children("a").hasClass('enabled')) {
					strings_id += $(this).attr("id").split("_")[1] + ",";
				}
			});
			$("#gifts_phrases").val(strings_id);
		});

		$("div.gifts div.gifts_padding div.phrases div.item").hover(function(){ 
			if ($(this).children("p").children("a").attr("class") != 'enabled') {
				$(this).children("p").children("a").addClass("enabled");
			}
		}, function() {
			var is_selected = false;
			if (current_phrase != $(this).attr("id")) {
				selected_values = $("#gifts_phrases").val().split(",");
				my_id = $(this).attr("id").split("_")[1];
				anchor_obj = $(this).children("p").children("a");
				jQuery.each(selected_values,function(){
					if (this > 0 && this == my_id) {
						is_selected = true;
					}
				});
				if (!is_selected) {
					$(anchor_obj).removeClass('enabled');
				}
			}
			current_phrase = '';
		});

	}

	if ($("div#gallery").length > 0) {
		self.parent.$('div#main div.item').hide();
	}

	if (typeof gc_in_catalog != "undefined") { //autoscroll to catalog
		$.scrollTo($("div.catalog_products"), 100);
	}
	
	fixFamilies();
	fix_families = setInterval("fixFamilies()",200);

	if ($("#form_subfamily select#subfamily_select").length > 0) {
		$("#form_subfamily select#subfamily_select").change(function(){
			if($("#form_subfamily select#subfamily_select option:selected").val() >= 1) {
				$("#form_subfamily").submit();
			}
		});
	}

	if ($("div.catalog_subcategory_items").length > 0) {
		$("div.catalog_subcategory_items").children().each(function(){
			if ($(this).attr("class") == 'active' || $(this).attr("class") == 'active last') {
				current_selected_family = $(this).attr('id');
			}
			$(this).hover(function(){ 
				if ($(this).attr("class") != 'active') {
					var image_file = $(this).attr('id').split("_");
					$(this).children().children().attr('src','/img/layout/families/' + image_file[1] + '_on.gif');
					$(this).addClass("active");
				}
			}, function() {
				if ($(this).attr('id') != current_selected_family) {
					var image_file = $(this).attr('id').split("_");
					$(this).children().children("img").attr('src','/img/layout/families/' + image_file[1] + '_off.gif');
					$(this).removeClass("active");					
				}
			});
		});
	}
	
	if ($("div.send_recipe").length > 0 && $("div.send").length > 0) {
		$("div.send").click(function(){
			if ($("#recipe_person_name").val() != '' && $("#recipe_person_email").val() != '' && $("#recipe_person_lastname").val() != '' && $("#recipe_person_phone").val() != '' && $("#recipe_recipe_body").val() != '') {
				$("#form_recipes").submit();
			} else {
				alert((gc_lang == 'cat') ? 'Error: dades incompletes' : 'Error: datos incompletos');
			}
		});
	}
	
	if ($("div.send_recipe").length > 0 && $("div.send").length > 0) {
		$("div.send").click(function(){
			if ($("#staf_your_email").val() != '' && $("#staf_your_name").val() != '' && $("#staf_friend_email").val() != '' && $("#staf_friend_name").val() != '' && $("#staf_comments").val() != '') {
				$("#form_staf").submit();
			} else {
				alert((gc_lang == 'cat') ? 'Error: dades incompletes' : 'Error: datos incompletos');
			}
		});
	}
	
	if ($("#newsletter_field").length > 0) {
		newsletter_value = $("#newsletter_field").val();
		$("#newsletter_field").blur(function(){
			if ($(this).val() == '') {
				$(this).val(newsletter_value);
			}
		});
		$("#newsletter_field").focus(function(){
			if ($(this).val() == newsletter_value) {
				$(this).val('');
			}
		});
	}
	
	$("#banner_newsbanner").click(function(){window.location.href = "/?section=listadoFotoblog&lang="+lang+"";});

	if ($("#search_form").length > 0) {
		$("#search_form div.submit").click(function(){
			$("#search_form").submit();
		});
	}



	$("#etags").click(function(){
		$("#mes").val("");	
		$("#year").val("");	
		$("#enviafecha").submit();
	});
	$("#fechas").click(function(){
		$("#tags").val("");	
		$("#enviafecha").submit();
	});
	
	
		$("#subscribe div.submit").click(function(){
			$("#subscribe").submit();
		});
	if ($("#search_field").length > 0) {
		search_value = $("#search_field").val();
		$("#search_field").blur(function(){
			if ($(this).val() == '') {
				$(this).val(search_value);
			}
		});
		$("#search_field").focus(function(){
			if ($(this).val() == search_value) {
				$(this).val('');
			}
		});
	}	
	
	$("#tiendasSlide").click(function(){window.location.href = "/?section=stores&lang="+lang+"";});
	/*slide stores*/
	$(function() {	
	   $("#tiendasSlide").append('<img class="active" src="carga.php?carpeta=stores&'+Date.parse(new Date())+'">');
		setInterval( "cambiafoto('#tiendasSlide')", 8000 );
	});
	
	$("#fotoblog").click(function(){window.location.href = "/?section=listadoFotoblog&lang="+lang+"";});
	$('.div0').click(function(){window.location.href = "/?section=fotoblog&id="+this.id+"&lang="+lang+"";});
	$('.div00').click(function(){window.location.href = "/?section=fotoblog&id="+this.id+"&lang="+lang+"";});
	$('.div02').click(function(){
		var id = ($(this).attr('id').split("-"));
		window.open('/img/fotoblog/' + id[0] + '/' + id[1] + '.jpg','_blank');
	});
	
	$(".div00, .div02").bind('mouseover',function(){
		$(this).children("p.plistado").css("background","#e31836");
		$(this).children("p.plistado").css("cursor","pointer");
		$(this).children("p.plistado").css("color","#fff");
		$(this).children("img").css("cursor","pointer");
	});
	$(".div00, .div02").bind('mouseout',function(){
		$(this).children("p.plistado").css("background","#f7f7f7");
		$(this).children("p.plistado").css("color","#000");
		$(this).children("p.plistado").css("cursor","pointer");
		$(this).children("img").css("cursor","pointer");

	});
	
	$("div.products div img").bind('mouseover',function(){
		//$(this).parent().parent().children("p").siblings("a").css("color","#fff");
		$(this).parent().parent().children("p").children("a").addClass("pcolor");
		
	}).bind('mouseout',function(){
		$(this).parent().parent().children("p").children("a").removeClass("pcolor");
	
	});	
	
	
	
	/*slide stores*/
	$(function() {	
	   $("#fotoblog").append('<img class="active" src="carga.php?carpeta=fotoblog&'+Date.parse(new Date())+'">');
		//setTimeout("setInterval( \"slideSwitch('#fotoblog','fotoblog') \", 8000 );",4000);
		setTimeout("setInterval( \"cambiafoto('#fotoblog') \", 8000 );",4000);
	});
});
//fotos
	
	//Funcion recursiva para sacar aleatorio de imagen sin que se repita con las que hay
		function aleatorio(f1,clase) {
			var randomnumber = Math.random(); 
			var rand1 = Math.round( (imagenumber-1) * randomnumber); 
			var imagenretorno
			if(clase=='#fotoblog'){
				imagenretorno = fotoblog_array[rand1];
			}
			else{
				imagenretorno = fotostores_array[rand1];
			} 
			if (imagenretorno==f1){
				return aleatorio(f1);
			}else
			{
				return imagenretorno;
			}
		}
		//Funcion que hace fade de imagenes
		function cambiafoto(clase) {
			var imagnueva;
			var $active = $(clase+' IMG.active');
			if(clase=='#fotoblog'){
				imagnueva=aleatorio(imgn1,clase);
				$(clase).append('<img src="img/fotoblog/'+imagnueva+'">');
				imgn1=imagnueva;
			}
			else{
				imagnueva=aleatorio(imgn2,clase);
				$(clase).append('<img src="img/stores/'+imagnueva+'">');
				imgn2=imagnueva;
			}
			if ( $active.length == 0 ) $active = $(clase+' IMG:last');
			var $next =  $active.next().length ? $active.next()
				: $(clase+' IMG:first');
			$active.addClass('last-active');
			$next.css({opacity: 0.0})
				.addClass('active')
				.animate({opacity: 1.0}, 3000, function() {
				$active.removeClass('active last-active');
				 $active.remove();
		
			});			
			
		}

function aviso(){}

/*slide stores*/
/*http://jonraasch.com/blog/a-simple-jquery-slideshow*/
	function slideSwitch(clase,carpeta) {
    var $active = $(clase+' IMG.active');
	$(clase).append('<img src="carga.php?carpeta='+carpeta+'&'+Date.parse(new Date())+'">');
    if ( $active.length == 0 ) $active = $(clase+' IMG:last');

    var $next =  $active.next().length ? $active.next()
        : $(clase+' IMG:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 3000, function() {
        $active.removeClass('active last-active');
		 $active.remove();

    });
		
}
function fixFamilies() {
	if ($("div.catalog_subcategory_items").length > 0) {
		$("div.catalog_subcategory_items").children().each(function(){
			if($(this).children("p").height() < 24) {
				new_height = ($(this).children("p").height() / 2) + 5;
				$(this).children("p").css("margin-top",new_height + "px");
			} else {
				$(this).children("p").css("margin-top","3px");
			}
		});
	}
}

function loadNav(lang) {
	var flashvars = {};
	var params = {
		bgcolor: "#353133",
		wmode: "transparent",
		menu:"false"
	};
	var attributes = {};

	if ($("#nav_recipes").length > 0) {
		swfobject.embedSWF("swf/receptes_" + lang + ".swf", "nav_recipes", "165", "221", "8.0.0", null, flashvars, params, attributes);
	}
	if ($("#nav_gifts").length > 0) {
		swfobject.embedSWF("swf/regals_" + lang + ".swf", "nav_gifts", "165", "221", "8.0.0", null, flashvars, params, attributes);
	}
	if ($("#nav_catalog").length > 0) {
		swfobject.embedSWF("swf/cataleg_" + lang + ".swf", "nav_catalog", "165", "221", "8.0.0", null, flashvars, params, attributes);
	}
	if ($("#nav_activities").length > 0) {
		swfobject.embedSWF("swf/activitats_" + lang + ".swf", "nav_activities", "165", "221", "8.0.0", null, flashvars, params, attributes);
	}
	if ($("div.notebooks").length > 0) {
		$("div.notebooks div.item").toggle();
	}	
}

function initialize_stores_map() {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map_arago_map"));
		map.setMapType(G_NORMAL_MAP);
		map.setCenter(new GLatLng(41.3909263,2.1632069), 16);
		map.setUIToDefault();
		var point = new GLatLng(41.3909263,2.1632069);
		map.addOverlay(new GMarker(point));
		var map_2 = new GMap2(document.getElementById("map_illa_map"));
		map_2.setMapType(G_NORMAL_MAP);
		map_2.setCenter(new GLatLng(41.39008,2.1351914), 16);
		map_2.setUIToDefault();
		var point_2 = new GLatLng(41.39008,2.1351914);
		map_2.addOverlay(new GMarker(point_2));
	}
}

function homeLoop() {
	if (gc_home_current_slide >= gc_home_slides) {
		gc_home_current_slide = 0;
	} else {
		gc_home_current_slide++;
	}
	gc_goto = jQuery("div.stripTransmitter").children().find("li");
	$(gc_goto).eq(gc_home_current_slide).children().trigger("click");
}

function loadGroups(lang) {

	var params = {};
	var attributes = {};
	
	if ($("div.catalog_subcategories").length > 0) {
		$("div.catalog_subcategories").children().each(function(){
			var id = ($(this).attr('id').split("_"));
			if ($("#families_" + id[1]).length > 0 && $("#families_swf_" + id[1]).length > 0) {
				var flashvars = {
					dest_section: "catalog_products",
					dest_family: id[1],
					dest_lang: lang
				};
				/* code for groups SWFs:
				on (press) {
					getURL("?section=" + _root.dest_section + "&f=" + _root.dest_family + "&lang=" + _root.dest_lang);
				}
				*/
				 isFound = $( this ).attr( 'class' ).toString().search( new RegExp( /noswf/i ) );

				if (isFound==-1){
				swfobject.embedSWF("swf/families/" + id[1] + "_" + gc_lang + ".swf", "families_swf_" + id[1], "210", "150", "8.0.0", null, flashvars, params, attributes);
				}
				
				
				
				
				
				
				//swfobject.embedSWF("swf/families/" + id[1] + "_" + gc_lang + ".swf", "families_swf_" + id[1], "210", "150", "8.0.0", null, flashvars, params, attributes);
			}
		});
	}
		
}

function loadSection(section) {
	switch(section) {
		case 'catalog':
			document.location.href = '?section=catalog_subcategories';
		break;
	}
}

function envia(){
		if($(".search_form_form #search_field").val()==''){
			alert("El campo de búsqueda no puede estar vacio.");
			return false;
		}
		else
		{
			var varselec=false;
			$("input[type='radio']").each(function() {
				if(this.checked==true)varselec=true;
			});
			if(varselec==false){
				m=confirm('Se hará una búsqueda en todas las Categorías.\n¿Está usted seguro?');
				if(m==true) {document.search_forms.submit();return true;} else {return false};
			}
			else{
				document.search_forms.submit();	
			}
		}
	}
	
	
	
		
		
	
	
		
		
