
(function($) {

	$.fn.fixBroken = function(){
	var $this = $(this);
		$this.each(function(i){
			var tag = $(this);
			var showFlash = $("#showFlash");
			if( showFlash.val() == "Y" ){
				if( tag.is("img") ) {
					tag.css ({"display": "none"});
				} else {
					tag.css ({"display": "block"});
				}
			}else{
				if( tag.is("img") ) {
					tag.css ({"display": "block"});
				} else {
					tag.css ({"display": "none"});
				}
			}
		});
	};
	

    $.fn.backgroundScale = function(options) {

        // build main options before element iteration
        
		var defaults = {
			constraint	: 	"height",
			minWidth	:	990,
			maxWidth	:	1255,
			minHeight	: 	507,//modifica B.E. era 609
			maxHeight	:	680,
			ratioWidth	:	1280,
			ratioHeight	:	900,
			imgHolder	:	".bg-container",
			imgClass	:	".bg-image",
			containerClass : ".main-container"
		}
		

    	var settings = $.extend({}, defaults, options);

		var $this = $(this);
		var holder = $(settings["imgHolder"]);
		var bgImage = $(settings["imgClass"]);
		var mainContainer = $(settings["containerClass"]); 
		
		var calcHeight = function(w){
			return w*settings["ratioHeight"]/settings["ratioWidth"];
		};
		
		var calcWidth = function(h){
			return h*settings["ratioWidth"]/settings["ratioHeight"];
		};		
		var winWidth,winHeight;
		
		var getWindowSize = function(){
			
			winWidth = window.innerWidth
					|| document.documentElement && document.documentElement.clientWidth
					|| document.body.clientWidth;
			winHeight = window.innerHeight
				|| document.documentElement && document.documentElement.clientHeight
				|| document.body.clientHeight;
		}		
		
		var resetImage = function(){

		// get window height and width
		

		var customSize = {
			imgWidth	:	calcWidth(winHeight),
			imgHeight	:	winHeight,
			holderWidth	:	calcWidth(winHeight),
			holderHeight:	winHeight,
			imgMargin	:	0,
			holderMargin: 	0
		}		

 
//console.log("window " + winWidth +"     " + winHeight);

			if (winWidth < settings["minWidth"]){
				// window width is smaller than content area
				// background image will crop
				
//console.log("small")

			
				
				if (winHeight < settings["minHeight"]){
	//console.log("short");		
						
					customSize = {
						imgWidth	:	settings["minWidth"],
						imgHeight	:	calcHeight(settings["minWidth"]),
						holderWidth	:	settings["minWidth"],
						holderHeight:	settings["minHeight"],
						holderMargin: 	0,
						imgMargin	:	0
					}
					
				}else{
	//console.log("tall");	
		
					
					customSize = {
						imgWidth	:	calcWidth(winHeight),
						imgHeight	:	winHeight,
						holderWidth	:	settings["minWidth"],
						holderHeight:	winHeight,
						holderMargin: 	0,
						imgMargin	:	-(customSize["imgWidth"]-winWidth)/2
					}
					
					if (customSize["imgWidth"]<settings["minWidth"]){
	//console.log("tall2");
	
						customSize = {
							imgWidth	:	settings["minWidth"],
							imgHeight	:	calcHeight(settings["minWidth"]),
							holderWidth	:	settings["minWidth"],
							holderHeight:	winHeight,
							holderMargin: 	0,
							imgMargin	:	0
						}
											
					}
					
					
				}
				
			}else{
			
//console.log("large");



				if (winHeight < settings["minHeight"]){
	//console.log("short");
						
					customSize = {
						imgWidth	:	settings["minWidth"],
						imgHeight	:	calcHeight(settings["minWidth"]),
						holderWidth	:	settings["minWidth"],
						holderHeight:	settings["minHeight"],
						holderMargin: 	(winWidth-settings["minWidth"])/2-8,
						imgMargin	:	0
					}
					
				}else{
	//console.log("tall");

					
					customSize = {
						imgWidth	:	calcWidth(winHeight),
						imgHeight	:	winHeight,
						holderWidth	:	winWidth,
						holderHeight:	winHeight,
						holderMargin: 	0,
						imgMargin	:	-(customSize["imgWidth"]-winWidth)/2
					}
					
					if (customSize["imgWidth"]<settings["minWidth"]){
	//console.log("tall2");

						customSize = {
							imgWidth	:	settings["minWidth"],
							imgHeight	:	calcHeight(settings["minWidth"]),
							holderWidth	:	settings["minWidth"],
							holderHeight:	winHeight,
							holderMargin: 	(winWidth-settings["minWidth"])/2,
							imgMargin	:	0
						}						
					}	
				}
			}
		
		
		
			    
				//set new height and width for image
                bgImage.css({
                    height: customSize["imgHeight"],
                    width: customSize["imgWidth"],
                    "marginLeft": customSize["imgMargin"]
                });
                

				// set height and width for image holder
                holder.css({
                    height: customSize["holderHeight"],
                    width: customSize["holderWidth"],
					"marginLeft": customSize["holderMargin"],
					left:0,
					overflow:"hidden"
                });

			
				mainContainer.css ({
					"marginLeft": customSize["holderMargin"]
				});
				
				// reset content wrapper height to match the height of the background image
				$this.css({
					height : customSize["holderHeight"],
					"minHeight": customSize["holderHeight"]
				});
				
				
				
				/*B.S. 10/03/2011 - new visual mode*/
				
				if(typeof(alternativeView)!="undefined" && alternativeView==true){
					
					 holder.css({
		                    height: customSize["holderHeight"]-110,
							top: "80px"							
		                });
						
					bgImage.css({
							height: customSize["holderHeight"]-110                  
					
					});
				
				}		
				
				
				
				// custom resize header/footer
				var resetHeader = function(){
					var _header = $(".header");
					if (parseInt($(".bg-container").css("width")) <= settings["minWidth"]) {
					
						_header.find(".hd-transparent-holder").css({
	                    	width: customSize["holderWidth"],
	                    	"marginLeft":  customSize["holderMargin"]
						});

						mainContainer.css ({
							"marginLeft": customSize["holderMargin"]
						});
						
						mainContainer.find(".content").css({
	                    	"marginLeft":  0
						});

					} else {
						
						_header.find(".hd-transparent-holder").css({
	                    	width: customSize["imgWidth"],
	                    	"marginLeft":customSize["imgMargin"]
						});
						
						mainContainer.css ({
							"marginLeft": 0 //customSize["imgMargin"]
						});
						
						mainContainer.find(".content").css({
	                    	"marginLeft":  "auto"
						});
					}
				}
				var resetFooter = function(){
					var _footer = $(".footer");
					if (parseInt($(".bg-container").css("width")) <= settings["minWidth"]) {
					
						_footer.find(".transparent-holder").css({
	                    	width: customSize["holderWidth"],
	                    	"marginLeft":  customSize["holderMargin"]
						});

					} else {
						
						_footer.find(".transparent-holder").css({
	                    	width: customSize["imgWidth"],
	                    	"marginLeft":customSize["imgMargin"]
						});
					}
				}
				resetHeader();
				resetFooter();
				
				// trigger custom callback
				if(typeof settings.onScaleComplete == "function"){
        			settings.onScaleComplete.call(this,customSize);
     	 		}
		}
		
		
		
		$(window).resize(function(){
			getWindowSize()
			resetImage();
			
		});
		
		$(window).load(function(){
			getWindowSize()
			resetImage();
		})

        return;
    };

    function init() {
      
		
    };
    
    $.fn.headerFooterReplace = function(options){
    	
    	var defaults = {
		}
		
    	var settings = $.extend({}, defaults, options);

		var $this = $(this);
		
		$this.each(function(i){
			
			var $img = $(this);
			
			var rollsrc = $img.attr("src");
			var rollON = rollsrc.replace("default","selected");
			var rollOFF = rollsrc.replace(/selected$/ig,"default");
			var imgsrc="";	
				$img.mouseover(function(){
					var matches = rollsrc.match("selected");
					if (!matches) {
						$img.attr("src", rollON);
						}
					});
					
				$img.mouseout(function(){
					$img.attr("src", rollsrc);
				});
    	});
    };
    
    
    $.fn.selectReplace = function(options) {
    	
        // build main options before element iteration
        
		var defaults = {
		}
		
    	var settings = $.extend({}, defaults, options);

		var $this = $(this);
		
		$this.each(function(i){
			
			var $select = $(this);
			var $parent = $(this).parents("li");
			count = i;
			
			if($(this).attr("class")=="form-check")
				generateCheckSelect($select);				
			else
				generateSelect($select);
			
			addEvents($parent);

	        if (($.browser.msie) && parseInt(jQuery.browser.version) == 6) {
	
	            $(".ddlFilter:visible").hover(
	                function() {
	                    $(this).addClass("over");
	                },
	                function() {
	                    $(this).removeClass("over");
	                });
	
	        };
		});
		
    };
	
    
    
    $.fn.selectCheckReplace = function(options) {

        // build main options before element iteration
    	
        
		var defaults = {
		}
		
    	var settings = $.extend({}, defaults, options);

		var $this = $(this);
		
		$this.each(function(i){
			
			var $select = $(this);
			var $parent = $(this).parents("li");
			count = i;
			generateCheckSelect($select);
			
			addEvents($parent);

	        if (($.browser.msie) && parseInt(jQuery.browser.version) == 6) {
	
	            $(".ddlFilter:visible").hover(
	                function() {
	                    $(this).addClass("over");
	                },
	                function() {
	                    $(this).removeClass("over");
	                });
	
	        };
		});
		
    };
    
    
	function addEvents($this){
	                
	                

	                
					$this.click(function(ev) {
					
						var $overClass = $(this).find("dd").attr('class');
						
						if( $overClass != "overALL" ){
						
							//sbianco tutte le altre
							$(".js").find(".overALL").each(function(){
								$(this).css({"display":"none"});
								$(this).removeClass("overALL");
								$(".js").unbind("click");
							});
							
							
							$(this).find("dd").fadeIn("slow");
							$(this).find("dd").addClass( "overALL" );
		                   // $(this).find("dd").css({"display":"block"});
		                    
		                   	$(".js").click(function(e) {
		                    	if(e.target==ev.target){
		                    		//var d = new Date();
		                    		//window.status=d.getTime() + " ok";
		                    	}else{
		                    		//var d = new Date();
		                    		//window.status=d.getTime() +  " ko";
		                    		$(this).find("dd").css({"display":"none"});
		                    		$(this).find("dd").removeClass( "overALL" );
		                    		$(".js").unbind("click");
		                    	}
		                    	
		               		 } );						
						}else{
							
							$(this).find("dd").css({"display":"none"});
							$(this).find("dd").removeClass( "overALL" );
							$(".js").unbind("click");
						}
						

	                    
	                } );
	                		
		 $this.find(".ddl-filter dd a").each(function() {
            $(this).click(function() {

				$(this).parents("dl").find("dt span").html($(this).text());
				
				// selects the option in the select box
				$(this).parents("li").find("option:eq(" + $(this).attr('rel') + ")").trigger("select");					
				
				var $parentClass = $(this).parents("li").eq(1).attr('class');
				
				if($parentClass != "news") location.href = $(this).attr("href");
				
				$(".js").unbind("click");
				$(this).parents("dl").find("dd").removeClass( "overALL" );
				$(this).parents("dl").find("dd").css({"display":"none"});
				
				return false;
            });
        });
	};
	
	
	function generateCheckSelect($this) {

        var _options, _default;
        $this.each(function() {
			 _default = "";
             _options = "";
             _selId = "";

            $(this).find("option:selected").each(function(i) { 					
					
					_default = '<span  rel="' + i + '">' + $(this).text() + '</span>';
					
            });
            
            $(this).find("option").each(function(i) {
            
			
                	_options += '<li><a href="'+ $(this).val() +'" onclick="if(typeof setSelectedValue == \'function\') setSelectedValue(\'' + this.parentNode.id +'\',\''  +  $(this).val().replace(/'/g, '') + '\');" rel="' + i + '">' + $(this).text() + '</a></li>';
            });
							
                        
        });

        $this.hide().after('<dl class="ddl-filter" style="z-index:'+(10-count)+'"><dt>' + _default + '<div class="transparent-holder"></div></dt><dd><ul>' + _options + '</ul></dd></dl>');

       
    }
    



	
	
	
	function generateSelect($this) {

        var _options, _default;
        $this.each(function() {
			 _default = "";
             _options = "";
             _selId = "";

      
            $(this).find("option").each(function(i) {
				if (i == 0){
					
					
					_default = '<span  rel="' + i + '">' + $(this).text() + '</span>';
					
				}
				if(i > 0){
                	_options += '<li><a href="'+ $(this).val() +'" onclick="if(typeof setSelectedValue == \'function\') setSelectedValue(\'' + this.parentNode.id +'\',\''  +  $(this).val().replace(/'/g, '�')  + '\');" rel="' + i + '">' + $(this).text() + '</a></li>';
				}
				
            });
          	
                        
        });

        $this.hide().after('<dl class="ddl-filter" style="z-index:'+(10-count)+'"><dt>' + _default + '<div class="transparent-holder"></div></dt><dd><ul>' + _options + '</ul></dd></dl>');

       
    }
   
})(jQuery);



function setSelectedValue( parentName , value){

	var selHidden = document.getElementById(parentName);

	if(selHidden){
		
		for(var i=0;i < selHidden.options.length;i++){
    		if(selHidden.options[i].value==value){
    			selHidden.options[i].selected=true;
    		}
    		else
    			selHidden.options[i].selected=false;
    		
    	}
	}
}

function setSelectedText(parentName , value){
	
	
		  value= value.replace(/\n/gi,'').replace( "/\"/ig", "" ).replace( "/\'/ig", "\\\'" ).replace(/^\s+|\s+$/g, '');
	var selHidden = document.getElementById(parentName);
	if(selHidden){
    	for(var i=0;i < selHidden.options.length;i++){
    		if(selHidden.options[i].text==value){
    			selHidden.options[i].selected=true;
    		}
    		else
    			selHidden.options[i].selected=false;
    		
    			
    	}
	}
}



/*Increase background height based on the number of elements in the page (used in the checkout pages)*/
function resizeBackPanelHeight(elementCount, panelName, elementHeight){
	
	
	var backGroundWhite = document.getElementById(panelName);			
	var panelHeight = backGroundWhite.style.height;	 
	
	if(elementHeight!=null)
		elHeight = elementHeight;
	else
		elHeight = 120;
	
	panelHeight = panelHeight.substring(0, 3);	 		
	panelHeight = parseInt(panelHeight);
	
	var newHeight = 0;
	if(elementCount>1){
		newHeight = panelHeight + ((elementCount-1)*elHeight) + 100;		
	}
	else{
		newHeight = panelHeight + 100;
	}

	backGroundWhite.style.height = newHeight + 'px';

}


function closeDetectBrowser(){
	jQuery(".unsupported").css({"display":"none"});
}



function detectBrowser(selectedLang)
{		
  	if (jQuery.browser.msie == true) { 
		if (jQuery.browser.version < 7){
			
			var styleSheet ="<style> .unsupported {padding:10px;text-align:center;position:relative;margin:auto;background:#000000;opacity:0.55;filter:alpha(opacity=55);color:white;width:600px;top:20px;z-index:99;font-family:Arial;font-size:12px;} </style>";
  			var closebutton = "<a onclick=\"closeDetectBrowser();\" style=\"cursor:pointer;float:right;display:block;clear:both;border:1px solid #1e1e1e;font-family:Arial;font-size:8px;padding:2px;width:10px;heigth:10px;\">X</a><br>";
  			
  			jQuery(".header").find(".content").after(styleSheet+"<div class=\"unsupported\" >" +closebutton+BrowserDetect_Msg+"</div>");
  	
		
		}
		else{ 
  		//Do Nothing
		}
	}
}


