var footMenuIE6 = ""; // variabile globale per footer IE6

var rapidSearchFrmAutoComplete = 0;

	
$(document).ready(function() {

	// PRODUCTS
	$("#serieSplash").height(218).css({"background-image":"url("+$("#serieSplash > img").attr("src")+")","background-repeat":"no-repeat"}).html('<img src="/images/curva.png" />');

	// COMMON
	if($.browser.msie && $.browser.version=="6.0") $("#serviceMenu").css("right","-10px");
	
	// LENS
	if(!($.browser.msie && $.browser.version=="6.0")) $("#mainSearch").append("<div id=\"lens\"></div>");	
	
	//LAYOUT
	if($.browser.msie && $.browser.version=="6.0") $("#leftContents").css("height","auto");
	
	// SCROLL TOP BUTTON
	$(".scrollTopButton").click(function() {
		$("html, body").animate({
			scrollTop : 0
		},"slow");
	});

	// MAINMENU
	//// MAINMENU
	$("#mainMenu li").each(function() {		
		if(!$(this).hasClass("last")) $(this).after("<li class=\"mainMenuDv\"></li>");
		w = ($(this).width() - 39) / 2;
		$(this).prepend("<div class=\"menu_arrow\" style=\"left:"+w+"px\"></div>");
	});	
	//$("#mainMenu li a").corner("2px");
	//
	//$("#mainMenu li").click(function(event) {
	//	location.href = $(this).find("a").attr("href");
	//});
	//
	//$("#mainMenu li").hover(function() {
	//	$(this).addClass("hover");
	//},function() {				
	//	$(this).removeClass("hover");
	//});
	
	// MAIN SEARCH
	$("#mainSearch .searchQuery").click(function() {
		if(rapidSearchFrmAutoComplete==0) {
			$(this).val("");
			rapidSearchFrmAutoComplete = 1;
		}
	});	
	
	/* IE6 HACKS */		
	if($.browser.msie && $.browser.version=="6.0") {		
		//FOOTER
		footMenuIE6 = "<p id=\"footMenuIE6\">"+$("#footMenu").html()+"</p>";
		$("#wrap").prepend(footMenuIE6);
		ie6Footer();	
		
		
		$(".fileList .fileListDownload").each(function() {
			fClass = ($(this).attr("class")).replace("fileListDownload ","");
			$(this).removeClass("pdf").removeClass("jpg").removeClass("xls").removeClass("doc").removeClass("gif");
			$(this).addClass(fClass+"IE6");
		});	
		
		
		$(".privacyBox .formData .p span").css("left","-200px");	
	}

});

function ie6Footer() {
	if($.browser.msie && $.browser.version=="6.0") {
		
		var offsetTop = ($("#footer").offset().top)+45;
		$("#footMenu").hide();
		

		$("#footMenuIE6").css("top",""+offsetTop+"px");									
		
		$("#footMenuIE6").css({
			"font-size" : "10px" ,
			"color" : "#FFF",
			"word-spacing" : "5px"
		});

		$("#footMenuIE6 a").css({
			"text-decoration" : "none" ,
			"color" : "#FFF",
			"word-spacing" : "normal"
		});

		$("#copy").css("top","85px");	
	}
}

function addOption(selectObject,value,text) {
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;
	selectObject.options.add(optn);	
}

function endsWith(str,ending) {
	str = str.toUpperCase();
	ending = ending.toUpperCase();
    var lastIndex = str.lastIndexOf(ending);
    return (lastIndex != -1) && (lastIndex + ending.length == str.length);	
}

function openWindow(theURL,w,h,scrollbars) {
	var myleft=(screen.width)?(screen.width-w)/2:100;
	var mytop=(screen.height)?(screen.height-h)/2:100;	
	var i = Math.floor(Math.random()*1111);
	window.open(theURL,''+i+'','width='+w+',height='+h+',scrollbars='+scrollbars+',top='+mytop+',left='+myleft+'');	
}

function substr_count( haystack, needle, offset, length ) {
	var pos = 0, cnt = 0; 
	haystack += '';
	needle += '';
		if(isNaN(offset)) {offset = 0;}
		if(isNaN(length)) {length = 0;}
		offset--;

		while( (offset = haystack.indexOf(needle, offset+1)) != -1 ){
			if(length > 0 && (offset+needle.length) > length){
				return false;
			} else{
				cnt++;
			}
		}

		return cnt;
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}


var Url = {
 
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
 
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}

