/*global jQuery, moveSelect, DateChooser, addEventListener, alert, blur, clearInterval, clearTimeout, close, closed, confirm, console, Debug, defaultStatus, document, window, event, events, focus, frames, getComputedStyle, history, Image, length, location, moveBy, moveTo, name, navigator, onblur: true, onerror: true, onfocus: true, onload: true, onresize: true, onunload: true, open, opener, opera, Option, parent, print, prompt, resizeBy, resizeTo, screen, scroll, scrollBy, scrollTo, setInterval, setTimeout, status, top, XMLHttpRequest */


/* ! Pseudo Filter engine */


function showFilterResults(event){
	event.preventDefault();

	jQuery(this).parent().parent().children("li").removeClass("current");
	jQuery(this).parents("li").addClass("current");
	
	/* ! get link value */
	var filterID = jQuery(this).attr("id");
	
	/* ! hide all catalog product then show only filter result */
	jQuery(".catalog .product-list > li").removeClass("alpha").hide();
	
	jQuery(".catalog .product-list > li[class*="+filterID+"]").fadeIn("normal");

	
}

/* ! Two kinds of filter, based on filter type, either Category name, either category points*/
/* --> filterSelector1 (ex : category points) // --> filterSelector2 (ex : category name) */

function showFilterSelector(event){
	event.preventDefault();
	
	jQuery(this).parent().parent().children("li").removeClass("current");
	jQuery(this).parents("li").addClass("current");
		
	var toggleTarget = this.hash;
	
	jQuery("ul[id^=filterSelector]").hide();
	jQuery(toggleTarget).fadeIn("normal");
	
	jQuery(toggleTarget+" > li.current a").trigger("click");
}





/* ! Suggestion Search -  Step 1 */
/* ! lookin for input text value */
function lookup(event) {
	var myInputLength = event.target.value.length;
	var myInputValue = event.target.value;
	var siblingBlock = jQuery(this).siblings(".suggestionsLayer");
	
	if(myInputLength === 0) {
		jQuery(siblingBlock).slideUp("fast"); // Hide the suggestions box
		} else if(myInputLength >=3){
			jQuery.post("ajax/suggestions.html", {queryString: ""+myInputValue+""}, function(data) { // Do an AJAX call
			jQuery(siblingBlock).html(data); // Fill the suggestions box
			jQuery(siblingBlock).find("a").click(function(eventClick){
				eventClick.preventDefault();
				var myLinkValue = jQuery(this).text();
				event.target.value = myLinkValue;
				jQuery(siblingBlock).slideUp("fast");
			});
			
			jQuery(siblingBlock).slideDown("fast"); // Show the suggestions box
		});
	}
}
/* ! Hide Suggestion Layer on blur */
function toggleSuggestionsLayer(){
	jQuery(".suggestionsLayer").slideUp("fast");
}


/*

function setAutoComplete() {
	if(jQuery(".suggestionsLayer").length){
		jQuery("#punto1, #punto2").each(function() {
		// this file maybe a PHP file accepting URL parameters, taken from the input 
		var serchfile = "ajax/suggestions.html";
		var active = 0;
		var showLayer = true;
		var suggestionLayer = jQuery(this).siblings(".suggestionsLayer");
		var thisLength = jQuery(this).val().length;
		
		// KEYDOWN
		jQuery(this).bind("keydown", function(event){
			var myInputLength = event.target.value.length;
			
			if (myInputLength >= 3) {
				switch (event.keyCode) {
					case 38: // up
						event.preventDefault();
						moveSelect(-1);
						break;
					case 40: // down
						event.preventDefault();
						moveSelect(1);
						break;
					case 13: // return
						event.preventDefault(); 
						jQuery(suggestionLayer).find("li.current a").trigger("click");
						break;
				}
			}
			
			thisLength = jQuery(this).val().length;
		
		});	
		
		// KEYUP
		jQuery(this).bind("keyup", function(event){
			
			var myInputLength = event.target.value.length;
			var myInputValue = event.target.value;
			console.log(myInputValue);
			
			if(myInputLength === 0) {
				showLayer = true;
				jQuery(suggestionLayer).slideUp("fast"); // Hide the suggestions box
			} else if(myInputLength >=3 && thisLength !== myInputLength && showLayer === true){
				active = 0;
				jQuery.post(serchfile, {queryString: ""+myInputValue+""}, function(data) { // Do an AJAX call
					jQuery(suggestionLayer).find(".suggestionsLayerContent").html(data); // Fill the suggestions box
					jQuery(suggestionLayer).find(".suggestionsLayerContent").find("li a").click(function(eventClick){
						eventClick.preventDefault();
						var myLinkValue = jQuery(this).find(".heading").text();
						event.target.value = myLinkValue;
						jQuery(suggestionLayer).slideUp("fast");
					});
					if (jQuery(suggestionLayer).find(".suggestionsLayerContent").find("li").length) {
						jQuery(suggestionLayer).slideDown("fast"); // Show the suggestions box
					}
				});
			}
		});
		
		function moveSelect(step) {
			var lis = jQuery("li", suggestionLayer);
			if (!lis || lis.length === 0) {
				return false;
			}
			active += step;
			 //loop through list
			if (active < 0) {
				active = lis.size();
			} else if (active > lis.size()) {
				active = 0;
			}
			lis.removeClass("current");
			jQuery(lis[active-1]).addClass("current");
		}
			
			jQuery(this).bind("blur", function(){
				jQuery(suggestionLayer).slideUp("fast");
			});
			
			jQuery(".suggestionsLayer .btCloseSearch a").bind("click", function(){
				jQuery(this).parents(".suggestionsLayer").slideUp("fast");
				showLayer = false;
				jQuery(this).trigger("focus");
				return false;
			});
		});
	}
}
*/

/* ! Toggle Content based on href value (ex:#grid) */
function toggleHandlerLevel0(event){
	event.stopPropagation();
	event.preventDefault();
	var toggleTarget = this.hash;
	jQuery(this).removeClass("toggle");
	jQuery(toggleTarget).animate({
		"opacity": "toggle",
		"height": "toggle"
	}, 200, "swing");
	var linkClass = jQuery(this).hasClass("hidden");
	switch (linkClass) {
		case false:
			jQuery(this).addClass("hidden");
			break;
		case true:
			jQuery(this).removeClass("hidden");
	}
	/* ! Hide select on main_nav opening */
	if (jQuery.browser.msie && jQuery.browser.version < 7) {
		if (toggleTarget === "#subsection-nav1" && linkClass) {
			jQuery("form.side_selectBloc select, #search_map .apertura select").hide();
		}
		else 
			if (toggleTarget === "#subsection-nav1" && !linkClass) {
				jQuery("form.side_selectBloc select, #search_map .apertura select").show();
			}
	}
}


/* ! Toggle e-coupon Content based on href value (ex:#grid) */
function toggleCouponsHandler(event){
	event.stopPropagation();
	event.preventDefault();
	var toggleTarget = this.hash;
	var allItemsIdentifier = jQuery(this).parent().hasClass("allItems");
	
	jQuery(".coupon-items").hide();
	
	if(allItemsIdentifier){
		jQuery(".coupon-items").fadeIn("normal");
	}else{
		
		jQuery(toggleTarget).fadeIn("normal");
	}
	
	jQuery(this).parents("ul").children("li").removeClass("current");
	jQuery(this).parent().addClass("current");
	

}

function displayInviaSpesamica(event){
	jQuery(".inviaComplete").fadeIn("normal");

}

// ! select unselect all radios
function selectAllRadios(event){
	event.stopPropagation();
	event.preventDefault();
	jQuery(".stdSelectorCoupon .btSelectAll a").children("span").text("DESELECCIONAR TODOS");
	jQuery(".box .coupon-items .product-input input:checkbox:visible").attr("checked", true);
}

function unSelectAllRadios(event){
	event.stopPropagation();
	event.preventDefault();
	jQuery(".stdSelectorCoupon .btSelectAll a").children("span").text("SELECCIONAR TODOS");
	jQuery(".box .coupon-items .product-input input:checkbox:visible").attr("checked", false);
}

/* ! Toggle Content based on href value (ex:#grid), only on mouseover */
function toggleHoverHandlerLevel0(event){
	event.stopPropagation();
	event.preventDefault();
	var toggleTarget = this.hash;
	jQuery("li.hover ul").hide();
	jQuery(toggleTarget).fadeIn("slow");
	jQuery(this).parents("ul").mouseleave(function() {jQuery(toggleTarget).hide();});
}

/* ! Show Content  based on href value (ex:#grid) */
function showHandlerLevel0(event) {
	event.stopPropagation();
	event.preventDefault();
	var toggleTarget = this.hash;
	//change current z-index property to fit with IE
	jQuery(".product-list li, .product-description").css("z-index", "97");
	jQuery(this).parents("li").css("z-index", "100");
	
	//Hide all current tooltip before showing a new one
	jQuery("[id^='pop-list-']").hide();
	jQuery(toggleTarget).fadeIn("fast");
	if ( toggleTarget == '#pop-login' ) {

		var popinDiv = document.getElementById('popin')  ;
        if ( !document.getElementById('remember').checked )
        {
            document.getElementById('email').focus() ;
        }
		if ( popinDiv == null ) {
			popinDiv = document.createElement('div') ;
			popinDiv.setAttribute('id','popin') ;
			popinDiv.style.opacity = '0.6' ;
			popinDiv.style.filter = 'alpha(opacity = 50)' ;
			popinDiv.setAttribute('class','login') ;
			document.body.appendChild(popinDiv);
		}
		jQuery(popinDiv).fadeIn("fast");
	}
}

/* ! Show Content  based on field value (ex:#grid) */
function showSecondaryStores(event) {
	
	jQuery(this).focus(function(){
		
		alert("bite");
	})
	
	
	var inputValue = jQuery(this).children("input:first").val();
	// ! define digits range
	var regZipCode = new RegExp(/^[0-9]{2,5}$/);
	var matchZipCode= inputValue.match(regZipCode);
	switch(matchZipCode){
		case null:
			// Load or display Error message (AJAX)
			jQuery(this).parent().find(".text").addClass("error");
			//jQuery(this).closest(".box-selections").find("div").fadeIn("fast");
		break;
		default:
			//Hide all current tooltip before showing a new one
			// Load Secondary Store List (AJAX)
			jQuery(this).parent().find(".text").removeClass("error");
			jQuery(this).closest(".box-selections").find("div").fadeIn("fast");
		break;
	}
	return false;
}



/* ! Hide Content  based on href value (ex:#grid) */
function hideHandlerLevel0(event) {
	event.stopPropagation();
	event.preventDefault();
	var ePopin = document.getElementById('popin') ;
	var toggleTarget = this.hash;
        $(':input[name=url_retorno]').val('');
        $(':input[name=ejecutar_js]').val('');
	
	jQuery(toggleTarget).stop().fadeOut("fast");
	if(! document.getElementById('popinContent') ) {
		if ( ePopin != null ) {
			jQuery(ePopin).stop().fadeOut("fast") ;
			document.body.removeChild(ePopin) ;
		}
	}	
}

/* ! Show for 3s. then hide Welcome/login pop content, base on href value */
function welcomeHandler(event) {
	event.stopPropagation();
	event.preventDefault();
	var toggleTarget = this.hash;
	jQuery(toggleTarget).fadeIn("slow");
}

/* ! Show tooltip */
function showTooltip(event) {
	event.stopPropagation();
	event.preventDefault();
	var toggleTarget = this.hash;
	var relativeX = event.pageX - jQuery(toggleTarget).width() - event.data.decalX;
	var relativeY = event.pageY -	jQuery(toggleTarget).height() - event.data.decalY;
	
	jQuery(toggleTarget).css({'left': relativeX, 'top':relativeY});
	jQuery(toggleTarget).fadeIn("slow").animate({"border":"0"}, 5000).fadeOut("fast");
}

/* ! add to list ajax simulation*/
function addToListsAJAX(event){
	event.stopPropagation();
	event.preventDefault();
	var eventIdentifier = jQuery(this);
	var URLTarget = jQuery(this).attr("href");
	var PIDReference = jQuery(this).attr("rel");
	
	jQuery.ajax({
		type: "POST",
		url: URLTarget,
		data: PIDReference,
		beforeSend: function(){
			jQuery(".tempError").remove();
			jQuery(".tempSuccess").remove();
			jQuery(".tempLoading").remove();
			jQuery(eventIdentifier).after("<img src='core/media/img/ajax-loader.gif' class='tempLoading' alt='ajax-loader' />");
	   },
		success: function(msg){
			alert( "Success - Data Saved: " + msg );
			jQuery(".tempError").remove();
			jQuery(".tempSuccess").remove();
			jQuery(".tempLoading").remove();
			jQuery(eventIdentifier).after("<img src='core/media/img/ajax-success.gif' class='tempSuccess' alt='ajax-loader' />");
		},
		error: function(msg){
			alert( "Error - Data Not Saved" );
			jQuery(".tempError").remove();
			jQuery(".tempSuccess").remove();
			jQuery(".tempLoading").remove();
			jQuery(eventIdentifier).after("<img src='core/media/img/ajax-error.gif' class='tempError' alt='ajax-loader' />");
		}
	});
}

/* ! Toggle tabs */
// Tab buttons are li a
// Tab panels are xx.tab
function toggleTabs(event) {
	event.stopPropagation();
	event.preventDefault();
	// Remove class 'current' from all other li and add to parent li
	var TabControls=jQuery(this).parents("ul").children("li");
	jQuery(TabControls).removeClass("current");
	jQuery(this).parent("li").addClass("current");
	// Hides all tab panels and shows the hashed one
	var TabPanels=jQuery(this.hash).siblings(".tab");
	jQuery(TabPanels).hide();
	jQuery(this.hash).show();
	// Do we really want to remove the outline ? It may have to go...
	jQuery(this).blur();
}

/* ! Fire popin on href value (ex:#grid) */
function popinHandler(event) {
	event.preventDefault();
	var popinHref = jQuery(this).attr("href").split("#")[0];
	var popinHash = jQuery(this).attr("hash");
	if (popinHash === "") {
		popinHash = "#popinContent";
	}
	
	jQuery("body").append("<div id='popin'></div><div id='loading' alt='Loading'><span>caricamento</span></div><div id='popinData'></div>");
		jQuery("#popin, #popinData").css({opacity: 0});
		
		var windowHeight = jQuery(window).height();
		var loaderPosition = (windowHeight - jQuery("#loading").height()) /2;
		jQuery("#loading").css("margin-top",loaderPosition);  
		
		jQuery("#popinData, #loading").css("top", jQuery(window).scrollTop());
		
		jQuery("#popin").animate({opacity: 0.6}, 200, "swing",function(){

			// HIDE selects for IE6
			if (typeof document.body.style.maxHeight === "undefined") {
				jQuery(".container_16 select").css("visibility","hidden");
			}
			jQuery.ajax({
				type: "GET",
				url: popinHref,
				dataType: "html",
				processData: "false",
				error: function(XMLHttpRequest, textStatus, errorThrow) {
				},
				success: function(XMLHttpRequest, textStatus) {
					jQuery(XMLHttpRequest).find(popinHash).each(function(){
						var dataFilter = jQuery(this);					
						jQuery("#popinData").append(dataFilter);
						jQuery("#popinData").find(popinHash).prepend("<a href='#' class='closePopin'>Cerrar</a>");
						
						if(windowHeight>jQuery("#popinData").find(popinHash).height()){
							var popinPosition = (windowHeight - jQuery("#popinData").find(popinHash).height()) /2;
							jQuery("#popinData").find(popinHash).css("margin-top",popinPosition);
						}
						
					});
					
				},
				complete: function(XMLHttpRequest, textStatus){
					jQuery("#popinData").animate({opacity: 1}, 200, "swing");
					jQuery("#loading").remove();
					jQuery("body").addClass("popinActiveBody");
				}
			});
		
		});
		
}

function ajaxLoadSL(event){
	var hrefValue = event.target.href;
	var hrefHash = event.target.hash;
	var subHREF = hrefValue.substring(0,  hrefValue.indexOf("#"));
	
	jQuery.ajax({
		type: "POST",
		url: subHREF,
		dataType:"xml",
		ifModified:false,
		error: function(XMLHttpRequest, textStatus, errorThrown) {
		},
		success: function(XMLHttpRequest, textStatus) {
			//console.log(XMLHttpRequest+" "+textStatus);
			jQuery(XMLHttpRequest).find(hrefHash).each(function(){
				var dataFilter = jQuery(this).html();
				jQuery("#test").append(dataFilter);
			});
		},
		complete: function(XMLHttpRequest, textStatus){
			jQuery("#popinData").animate({opacity: 1}, 200, "swing");
			}
		});
		
	event.preventDefault();
	event.stopPropagation();
}

function toggleDirections(event) {
	event.stopPropagation();
	event.preventDefault();
	jQuery(this.hash).siblings().hide();
	jQuery(this.hash).show();
	jQuery("#getSearchDirections").toggle();
}

// ! clear input
function removeValueField(){
	var inputDefaultValue = jQuery(this).attr("defaultValue");
	if (jQuery(this).attr("value") === inputDefaultValue) {
		jQuery(this).attr("value","");
	}
	jQuery(this).bind("blur", function(){
		if (!jQuery(this).attr("value")) {
			jQuery(this).attr("value",inputDefaultValue);
		}	
	});
}

// ! start cookie management
function getCookie(name){
	var arg=name+"=";
	var alen=arg.length;
	var clen=document.cookie.length;
	var i=0;
	while (i<clen) {
		var j=i+alen;
		if (document.cookie.substring(i, j)==arg) return getCookieVal(j);
		i=document.cookie.indexOf(" ",i)+1;
		if (i==0) break;
	}
	return null;
}
function getCookieVal(offset) {
	var endstr=document.cookie.indexOf (";", offset);
	if (endstr==-1) endstr=document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function setCookie(name,val){
	var argv=setCookie.arguments;
	var argc=setCookie.arguments.length;
	var expires=(argc > 2) ? argv[2] : null;
	var path=(argc > 3) ? argv[3] : null;
	var domain=(argc > 4) ? argv[4] : null;
	var secure=(argc > 5) ? argv[5] : false;
	document.cookie=name+"="+escape(val)+ ((expires==null) ? "" : ("; expires=Mon, 01 Jan 2011 03:00:00 GMT"))+ ((path==null) ? "" : ("; path="+path))+ ((domain==null) ? "" : ("; domain="+domain))+ ((secure==true) ? "; secure" : "");
}

function removeCookie(name){
	var exp=new Date();
	exp.setTime (exp.getTime() - 100000);
	var cval=getCookie(name);
	document.cookie=name+"="+cval+"; expires="+exp.toGMTString(); 
}
// ! end of cookie management

function handleNews() {
	jQuery("p#news").html(jQuery(this).parent().html());
}
// ! init timer global variable to contain setinterval for news
var timer;

// ! newsticker behavior : move elements one step forward
function newsTicker(newsElt){	
	var currentItem = newsElt.find(".current");
	var indexCurrent = jQuery(newsElt).find(" > li").index(jQuery(currentItem));
	indexCurrent += 1;
	var controlLength = newsElt.find("> li").length;
	if (indexCurrent >= controlLength) {
		indexCurrent = 0;
	}
	jQuery(newsElt).find("> li:eq("+indexCurrent+") a").trigger("click");		
}
// ! set the setinterval for the newsticker
function runNews(){
	jQuery("ul[id*=news-remote]").each(function(){
		var newsElt = jQuery(this);			
		timer = setInterval(function(){
			newsTicker(newsElt);
		}, 5000);
	});
}

function highlightCurrent(event) {
	event.preventDefault();

	var Controllers=jQuery(this).parents("ul").children("li");
	jQuery(Controllers).removeClass("current");
	jQuery(this).parents("li[class!=next][class!=previous][class!=more]").addClass("current");
}

function setCarrousel() {
	
	// Class Reg
	var classReg = new RegExp(/^carrouselDisplay/);
	var classReg2 = new RegExp(/^alphaRow/);
	
	jQuery("div[class*=carrouselDisplay]").each(function(){
		var visibleItems=4;
		var carrousel = jQuery(this);
		
		var eltClass = jQuery(this).attr("class").split(" ");
		eltClass = jQuery.grep(eltClass, function(txt){
			return (classReg.test(txt));
	    });
		
		switch (eltClass[0]) {
			case "carrouselDisplay3Pdt":
				visibleItems = 3;
				break;
			case "carrouselDisplayTxt":
				visibleItems = 1;
				break;
			case "carrouselDisplay7Pdt":
				visibleItems = 7;
				break;
			case "carrouselDisplay5Pdt":
				visibleItems = 5;
				break;
		}
		
		if (visibleItems < jQuery(this).find(" > ul >li").length) {
		
			// Carousel Controller
			if (!jQuery(this).siblings(".boxHeading").find(".remote").length) {
				jQuery(this).siblings(".boxHeading").append("<ul class=\"remote\"></ul>");
			}
			
			var itemNum = jQuery(this).find(" > ul >li").length;
			itemNum = Math.ceil(itemNum / visibleItems);
			var controllerList = "";
			
			for (var i = 0; i < itemNum; i++) {
			
				controllerList += "<li><a class=\"" + parseInt(i + 1, 10) + "\" ><span>" + parseInt(i + 1, 10) + "</span></a></li>";
			}
			jQuery(this).siblings(".boxHeading").find(".remote").prepend("<li class=\"previous\"><a><span>Previous Product line</span></a>" + controllerList + "</li><li class=\"next\"><a><span>Next Product line</span></a></li>");
			jQuery(this).addClass("carrousel_active");
			jQuery(this).siblings(".boxHeading").find(".remote .1").parent().addClass("current");
			
			
			// Carousel Display
			jQuery(this).jCarouselLite({
				btnNext: jQuery(this).siblings(".boxHeading").find(".remote li.next a"),
				btnPrev: jQuery(this).siblings(".boxHeading").find(".remote li.previous a"),
				visible: visibleItems,
				scroll: visibleItems,
				speed: 800,
				afterEnd: function(a){
					jQuery(carrousel).siblings(".boxHeading").find(".remote .current").removeClass("current");
					var alphaClass = jQuery(a[0]).attr("class").split(" ");
					alphaClass = jQuery.grep(alphaClass, function(txt){
						return (classReg2.test(txt));
					});
					
					alphaClass = alphaClass[0].split("alphaRow")[1];
					
					jQuery(carrousel).siblings(".boxHeading").find(".remote ." + alphaClass).parent().addClass("current");
				},
				btnGo: jQuery(this).siblings(".boxHeading").find(".remote li:not([class*=previous]):not([class*=next]):not([class*=more]) a")
			});
		}			
	});
}

function initPdtCarrousel(){
	jQuery(".stdCarrouselContainer").each(function() {
		
		var itemNum = jQuery(this).find(" > ul >li").length;
		itemNum = Math.ceil(itemNum / 4);
		var controllerList = "";
		
		for (var i = 0; i < itemNum; i++) {
			controllerList += "<li><a class=\"" + parseInt(i + 1, 10) + "\" ><span>" + parseInt(i + 1, 10) + "</span></a></li>";
		}
		
		jQuery(this).after("<div class=\"stdCarrouselController\"><ol>"+controllerList+"</ol></div>");
		jQuery(this).siblings(".stdCarrouselController").find(".1").parent().addClass("active");
		jQuery(this).find(">ul").addClass("activeCarrousel");
		
		var carrousel = jQuery(this);
		jQuery(this).jCarouselLite({
			visible:4,
			scroll:4,
			speed:800,
			afterEnd: function(a){
				jQuery(carrousel).siblings(".stdCarrouselController").find(".active").removeClass("active");
				var indexCurrent = jQuery(carrousel).find("ul > li").index(jQuery(a));
				indexCurrent = indexCurrent/4-1;
				jQuery(carrousel).siblings(".stdCarrouselController").find("li:eq("+indexCurrent+")").addClass("active");
			},
			btnGo: jQuery(this).siblings(".stdCarrouselController").find("a")

		});
	});
	
}

jQuery(document).ready(function () {
	// ! run code when DOM is ready
// setCarrousel("#carrousel-1");
	// ! init headings toggle 
	jQuery("a[id*=toggle]").bind("click", toggleHandlerLevel0);
	
	// ! init headings, mouseover only
	jQuery("a[id*=hover]").bind("mouseover", toggleHoverHandlerLevel0);
	
	// ! init links show handler 
	jQuery("a[id*=show]").bind("click", showHandlerLevel0);
	
	 // ! Show secondary store list (need to use ajax to extract store list, based on postal code value)   
	jQuery(".extract form").bind("submit", showSecondaryStores);
	
	
	// ! init links show handler 
	jQuery("a[class*=close]").bind("click", hideHandlerLevel0);
	
	// ! init Welcome Message handler - run once
	jQuery("a#show-welcome").one("click",welcomeHandler);
	
	// ! show tooltips and errortips
	jQuery("a[id*=show-infotip]").bind("click",{decalX:-15, decalY:60},showTooltip);
	jQuery("a[id*=show-errortip]").bind("click",{decalX:-90, decalY:60},showTooltip);
	
	// ! show tabs, add/remove style "current"
	jQuery("a[id*=controltab]").live("click", toggleTabs);
	
	// ! handle news controller 
	jQuery("ul[id*=news-remote] a").bind("click", handleNews);
	
	// ! Highlight current li a
	jQuery("ul[id*=news-remote] a").bind("click", highlightCurrent);
	
	
	// ! Fires Carrousels
	setCarrousel();
	initPdtCarrousel();

	// ! input gives suggestions when a certain number of letters are typed
	//setAutoComplete();
	/* ! Add Search Suggestions */
	//jQuery("#punto1, #punto2").bind("keyup", lookup);
	//jQuery("#punto1, #punto2").bind("blur", toggleSuggestionsLayer);
	
	
	// ! Popins
	jQuery("a[class*=popin]").bind("click", popinHandler);
	jQuery("a.closePopin, a.removePopin").live("click", function(){
		jQuery("#popin, #popinData").remove();
		if (typeof document.body.style.maxHeight === "undefined") {
			jQuery(".container_16 select").css("visibility", "visible");
		}
		jQuery("body").removeClass("popinActiveBody");
		return false;
	});
	
	// ! init add to list/Wish link (AJAX)
	jQuery(".addToList, .addToWish").bind("click", addToListsAJAX);
	
	// ! clear input : clic = memorize text, blur = (restore previous text if empty)
	jQuery("#searchField, .search input:text, #numero, #location_searchField, .spesamica_search input:text, .box-pop #password, #email, .extract .text").bind("focus", removeValueField);
	
	// ! storelocator : click search, get directions
	jQuery("#submitDirections").bind("click", toggleDirections);
	jQuery("#getSearchDirections").bind("click", toggleDirections);
	
		
	// ! Print
	jQuery(".stdPopinDisplay .btPrint a").live("click", function(){
		window.print();
		return false;
	});
	
	
	/* ! show filter selector */
	jQuery("a[class^=filterLevel]").bind("click", showFilterSelector );
	
	/* ! trigger filter selector, based on active filter (current class) */
	jQuery(".catalog_filter li.current a").trigger("click");
	
	
	/* ! Display filter results based on filter value (filter1SelectorN, filter2SelectorN)*/
	
	jQuery("a[id^=filter1Selector], a[id^=filter2Selector]").bind("click", showFilterResults);
	
	
	/* ! Display filter results based on filter value (filter1SelectorN, filter2SelectorN)*/
	
	jQuery(".tabNavigationList > li > a").bind("click", toggleCouponsHandler);
	
	/* nivia spesamica, display more fields on focus */
	jQuery(".spesAmica_fid #numero").bind("focus", displayInviaSpesamica);
	
	
	// ! Select all Coupons
	jQuery(".stdSelectorCoupon:not(.omegaSelectorCoupon) .btSelectAll a").toggle(selectAllRadios, unSelectAllRadios);

	jQuery(".omegaSelectorCoupon .btSelectAll a").bind("click", function(){
		event.preventDefault();
		event.stopPropagation();
		jQuery(".stdSelectorCoupon:not(.omegaSelectorCoupon) .btSelectAll a").trigger("click");
	
	})
	
});
		
jQuery(window).load(function (event) {
	// ! run code when All media loaded	
	
	// ! fire headings toggle
	jQuery("a[class*=toggle]").trigger("click");
	jQuery("li[class*=hover] ul").hide();
	
	// ! fire welcome message
	if(getCookie("pop-welcome") != 2)jQuery("a#show-welcome").trigger("click");
	
	// ! News Auto
	runNews();
	
	// ! behavior for rollover/rollout on news
	// ! over : stop ticker
	jQuery("#news").bind("mouseover",function(){		
		clearInterval(timer);
	});
	// ! out : relaunch ticker
	jQuery("#news").bind("mouseout",function(){
		runNews();
	});
	
});


function closePopin()
{
    $("#popin").stop().fadeOut("slow")
    $("#popinData").hide(); 
}

// IE6 Warning

var msg1 = "¿Sabías que tu navegador web es antiguo?";
var msg2 = "Para poder disfrutar de nuestro sitio debes actualizarlo. Elige una opción:";
var msg3 = "";
var br1 = "Internet Explorer 8+";
var br2 = "Firefox 3+";
var br3 = "Safari 4+";
var br4 = "Opera 10+";
var br5 = "Chrome 2.0+";
var url1 = "http://www.microsoft.com/windows/Internet-explorer/default.aspx";
var url2 = "http://www.mozilla.com/firefox/";
var url3 = "http://www.apple.com/safari/download/";
var url4 = "http://www.opera.com/download/";
var url5 = "http://www.google.com/chrome";
var imgPath;
function ie6warning(str) { 
	imgPath = str;var _body = document.getElementsByTagName('body')[0];var _d = document.createElement('div'); var _l = document.createElement('div'); _l.style.zIndex = '99979' ;  _d.style.zIndex = '99999' ; var _h = document.createElement('h1');var _p1 = document.createElement('p'); _p1.style.fontSize = '1.2em' ; var _p2 = document.createElement('p');var _ul = document.createElement('ul'); _ul.style.fontSize = '1.1em' ; var _li1 = document.createElement('li');var _li2 = document.createElement('li');var _li3 = document.createElement('li');var _li4 = document.createElement('li');var _li5 = document.createElement('li');var _ico1 = document.createElement('div');var _ico2 = document.createElement('div');var _ico3 = document.createElement('div');var _ico4 = document.createElement('div');var _ico5 = document.createElement('div');var _lit1 = document.createElement('div');var _lit2 = document.createElement('div');var _lit3 = document.createElement('div');var _lit4 = document.createElement('div');var _lit5 = document.createElement('div');_body.appendChild(_l);_body.appendChild(_d);_d.appendChild(_h);_d.appendChild(_p1);_d.appendChild(_p2);_d.appendChild(_ul);_ul.appendChild(_li1);_ul.appendChild(_li2);_ul.appendChild(_li3);_ul.appendChild(_li4);_ul.appendChild(_li5);_li1.appendChild(_ico1);_li2.appendChild(_ico2);_li3.appendChild(_ico3);_li4.appendChild(_ico4);_li5.appendChild(_ico5);_li1.appendChild(_lit1);_li2.appendChild(_lit2);_li3.appendChild(_lit3);_li4.appendChild(_lit4);_li5.appendChild(_lit5);_d.setAttribute('id','_d');_l.setAttribute('id','_l');_h.setAttribute('id','_h');_p1.setAttribute('id','_p1');_p2.setAttribute('id','_p2');_ul.setAttribute('id','_ul');_li1.setAttribute('id','_li1');_li2.setAttribute('id','_li2');_li3.setAttribute('id','_li3');_li4.setAttribute('id','_li4');_li5.setAttribute('id','_li5');_ico1.setAttribute('id','_ico1');_ico2.setAttribute('id','_ico2');_ico3.setAttribute('id','_ico3');_ico4.setAttribute('id','_ico4');_ico5.setAttribute('id','_ico5');_lit1.setAttribute('id','_lit1');_lit2.setAttribute('id','_lit2');_lit3.setAttribute('id','_lit3');_lit4.setAttribute('id','_lit4');_lit5.setAttribute('id','_lit5');var _width = document.documentElement.clientWidth;var _height = document.documentElement.clientHeight;var _dl = document.getElementById('_l');_dl.style.width =  _width+"px";_dl.style.height = _height+"px";_dl.style.position = "absolute";_dl.style.top = "0px";_dl.style.left = "0px";_dl.style.filter = "alpha(opacity=50)";_dl.style.background = "#fff";var _dd = document.getElementById('_d');_ddw = 650;_ddh = 260;_dd.style.width = _ddw+"px";_dd.style.height = _ddh+"px";_dd.style.position = "absolute";_dd.style.top = ((_height-_ddh)/2)+"px";_dd.style.left = ((_width-_ddw)/2)+"px";_dd.style.padding = "20px";_dd.style.background = "#fff";_dd.style.border = "1px solid #ccc";_dd.style.fontFamily = "'Lucida Grande','Lucida Sans Unicode',Arial,Verdana,sans-serif";_dd.style.listStyleType = "none";_dd.style.color = "#4F4F4F";_dd.style.fontSize = "12px";_h.appendChild(document.createTextNode(msg1));var _hd = document.getElementById('_h');_hd.style.display = "block";_hd.style.fontSize = "1.3em";_hd.style.marginBottom = "0.5em";_hd.style.color = "#333";_hd.style.fontFamily = "Helvetica,Arial,sans-serif";_hd.style.fontWeight = "bold";_p1.appendChild(document.createTextNode(msg2));var _p1d = document.getElementById('_p1');_p1d.style.marginBottom = "1em";_p2.appendChild(document.createTextNode(msg3));var _p2d = document.getElementById('_p2');_p2d.style.marginBottom = "1em";var _uld = document.getElementById('_ul');_uld.style.listStyleImage = "none";_uld.style.listStylePosition = "outside";_uld.style.listStyleType = "none";_uld.style.margin = "0 px auto";_uld.style.padding = "0px";_uld.style.paddingLeft = "10px";var _li1d = document.getElementById('_li1');var _li2d = document.getElementById('_li2');var _li3d = document.getElementById('_li3');var _li4d = document.getElementById('_li4');var _li5d = document.getElementById('_li5');var _li1ds = _li1d.style;var _li2ds = _li2d.style;var _li3ds = _li3d.style;var _li4ds = _li4d.style;var _li5ds = _li5d.style;_li1ds.background = _li2ds.background = _li3ds.background = _li4ds.background = _li5ds.background = "transparent url('"+imgPath+"background_browser.gif') no-repeat scroll left top";_li1ds.cursor = _li2ds.cursor = _li3ds.cursor = _li4ds.cursor = _li5ds.cursor = "pointer";_li1d.onclick = function() {window.location = url1 }; _li2d.onclick = function() {window.location = url2 }; _li3d.onclick = function() {window.location = url3 }; _li4d.onclick = function() {window.location = url4 }; _li5d.onclick = function() {window.location = url5 }; _li1ds.styleFloat = _li2ds.styleFloat = _li3ds.styleFloat = _li4ds.styleFloat = _li5ds.styleFloat = "left";_li1ds.width = _li2ds.width = _li3ds.width = _li4ds.width = _li5ds.width = "120px";_li1ds.height = _li2ds.height = _li3ds.height = _li4ds.height = _li5ds.height = "122px";_li1ds.margin = _li2ds.margin = _li3ds.margin = _li4ds.margin = _li5ds.margin = "0 10px 10px 0";var _ico1d = document.getElementById('_ico1');var _ico2d = document.getElementById('_ico2');var _ico3d = document.getElementById('_ico3');var _ico4d = document.getElementById('_ico4');var _ico5d = document.getElementById('_ico5');var _ico1ds = _ico1d.style;var _ico2ds = _ico2d.style;var _ico3ds = _ico3d.style;var _ico4ds = _ico4d.style;var _ico5ds = _ico5d.style;_ico1ds.width = _ico2ds.width = _ico3ds.width = _ico4ds.width = _ico5ds.width = "100px";_ico1ds.height = _ico2ds.height = _ico3ds.height = _ico4ds.height = _ico5ds.height = "100px";_ico1ds.margin = _ico2ds.margin = _ico3ds.margin = _ico4ds.margin = _ico5ds.margin = "1px auto";_ico1ds.background = "transparent url('"+imgPath+"browser_ie.gif') no-repeat scroll left top";_ico2ds.background = "transparent url('"+imgPath+"browser_firefox.gif') no-repeat scroll left top";_ico3ds.background = "transparent url('"+imgPath+"browser_safari.gif') no-repeat scroll left top";_ico4ds.background = "transparent url('"+imgPath+"browser_opera.gif') no-repeat scroll left top";_ico5ds.background = "transparent url('"+imgPath+"browser_chrome.gif') no-repeat scroll left top";_lit1.appendChild(document.createTextNode(br1));_lit2.appendChild(document.createTextNode(br2));_lit3.appendChild(document.createTextNode(br3));_lit4.appendChild(document.createTextNode(br4));_lit5.appendChild(document.createTextNode(br5));var _lit1d = document.getElementById('_lit1');var _lit2d = document.getElementById('_lit2');var _lit3d = document.getElementById('_lit3');var _lit4d = document.getElementById('_lit4');var _lit5d = document.getElementById('_lit5');var _lit1ds = _lit1d.style;var _lit2ds = _lit2d.style;var _lit3ds = _lit3d.style;var _lit4ds = _lit4d.style;var _lit5ds = _lit5d.style;_lit1ds.color = _lit2ds.color = _lit3ds.color = _lit4ds.color = _lit5ds.color = "#808080";_lit1ds.fontSize = _lit2ds.fontSize = _lit3ds.fontSize = _lit4ds.fontSize = _lit5ds.fontSize = "0.8em";_lit1ds.height = _lit2ds.height = _lit3ds.height = _lit4ds.height = _lit5ds.height = "18px";_lit1ds.lineHeight = _lit2ds.lineHeight = _lit3ds.lineHeight = _lit4ds.lineHeight = _lit5ds.lineHeight = "17px";_lit1ds.margin = _lit2ds.margin = _lit3ds.margin = _lit4ds.margin = _lit5ds.margin = "1px auto";_lit1ds.width = _lit2ds.width = _lit3ds.width = _lit4ds.width = _lit5ds.width = "118px";_lit1ds.textAlign = _lit2ds.textAlign = _lit3ds.textAlign = _lit4ds.textAlign = _lit5ds.textAlign = "center";
	var _cl = document.createElement('a') ;
	_cl.innerHTML = 'Ignorar y continuar' ;
	_cl.setAttribute('href','#cerrar') ;
	_cl.onclick = function() {
		document.getElementById('_d').style.display = 'none' ;
		document.getElementById('_l').style.display = 'none' ;
		document.body.style.overflow = "visible" ;
	};
	_d.appendChild(_cl);
	document.body.style.overflow = "hidden" ;
} ;

