(function($)
{

    jQuery.fn.customPngFix = function(settings)
    {

        // Settings
        settings = jQuery.extend({
            blankgif: 'blank.gif'
        }, settings);

        var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
        var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);

        if (jQuery.browser.msie && (ie55 || ie6))
        {

            //fix images with png-source
            jQuery(this).find("img[src$=.png]").each(function()
            {

                jQuery(this).attr('width', jQuery(this).width());
                jQuery(this).attr('height', jQuery(this).height());

                var prevStyle = '';
                var strNewHTML = '';
                var imgId = (jQuery(this).attr('id')) ? 'id="' + jQuery(this).attr('id') + '" ' : '';
                var imgClass = (jQuery(this).attr('class')) ? 'class="' + jQuery(this).attr('class') + '" ' : '';
                var imgTitle = (jQuery(this).attr('title')) ? 'title="' + jQuery(this).attr('title') + '" ' : '';
                var imgAlt = (jQuery(this).attr('alt')) ? 'alt="' + jQuery(this).attr('alt') + '" ' : '';
                var imgAlign = (jQuery(this).attr('align')) ? 'float:' + jQuery(this).attr('align') + ';' : '';
                var imgHand = (jQuery(this).parent().attr('href')) ? 'cursor:hand;' : '';
                if (this.style.border)
                {
                    prevStyle += 'border:' + this.style.border + ';';
                    this.style.border = '';
                }
                if (this.style.padding)
                {
                    prevStyle += 'padding:' + this.style.padding + ';';
                    this.style.padding = '';
                }
                if (this.style.margin)
                {
                    prevStyle += 'margin:' + this.style.margin + ';';
                    this.style.margin = '';
                }
                var imgStyle = (this.style.cssText);

                strNewHTML += '<span ' + imgId + imgClass + imgTitle + imgAlt;
                strNewHTML += 'style="position:relative;white-space:pre-line;display:inline-block;background:transparent;' + imgAlign + imgHand;
                strNewHTML += 'width:' + jQuery(this).width() + 'px;' + 'height:' + jQuery(this).height() + 'px;';
                strNewHTML += 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + jQuery(this).attr('src') + '\', sizingMethod=\'crop\');';
                strNewHTML += imgStyle + '"></span>';
                if (prevStyle != '')
                {
                    strNewHTML = '<span style="position:relative;display:inline-block;' + prevStyle + imgHand + 'width:' + jQuery(this).width() + 'px;' + 'height:' + jQuery(this).height() + 'px;' + '">' + strNewHTML + '</span>';
                }
                jQuery(this).hide();
                jQuery(this).after(strNewHTML);

            });

            // fix css background pngs
            jQuery(this).find("*").each(function()
            {
                var bgIMG = jQuery(this).css('background-image');
                if (bgIMG.indexOf(".png") != -1)
                {
                    var iebg = bgIMG.split('url("')[1].split('")')[0];
                    jQuery(this).css('background-image', 'none');
                    jQuery(this).get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + iebg + "',sizingMethod='crop')";
                }
            });

            //fix input with png-source
            jQuery(this).find("input[src$=.png]").each(function()
            {
                return;

                var bgIMG = jQuery(this).attr('src');
                jQuery(this).get(0).runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + bgIMG + '\', sizingMethod=\'crop\');';
                jQuery(this).attr('src', settings.blankgif)
            });

        }

        return jQuery;

    };

})(jQuery);


(function($)
{
    $.fn.animatedMenuItem = function(options)
    {
        var defaults = { imageToLoadUrl: "" };
        var options = $.extend(defaults, options);
        return this.each(function()
        {
            var $this = $(this);
            var $image = $this.find("img");
            $this.attr("imgZIndex", $image.css("z-index"));
            $this.attr("imgWidth", $image.width());
            $this.attr("imgHeight", $image.height());
            $this.attr("imgTop", $image.position().top);
            $this.attr("imgLeft", $image.position().left);
            $this.attr("imageToLoad", options.imageToLoadUrl);
			$this.attr("customLink", options.customLinkUrl);
            $this.mouseover(function()
            {
                if (DebbyUi.MainImageAutoSlideTimer != null)
                    window.clearTimeout(DebbyUi.MainImageAutoSlideTimer);
				
				var $this = $(this);
                var $image = $this.find("img");
                $image.css("z-index", "102");
                $this.siblings().find("img").css("z-index", "101");
                $image.animate(
                                {
                                    width: parseInt($this.attr("imgWidth"), 10) + 20
                                    , left: (parseInt($this.attr("imgLeft"), 10) - 10) + "px"
                                    , height: parseInt($this.attr("imgHeight"), 10) + 20
                                    , top: (parseInt($this.attr("imgTop"), 10) - 10) + "px"
                                }
                                );
            });

            $this.mouseout(function()
            {
                var $this = $(this);
                var $image = $this.find("img");
                $image.animate(
                                    {
                                        width: parseInt($this.attr("imgWidth"), 10)
                                        , left: (parseInt($this.attr("imgLeft"), 10)) + "px"
                                        , height: parseInt($this.attr("imgHeight"), 10)
                                        , top: (parseInt($this.attr("imgTop"), 10)) + "px"
                                    }
                                );
            });

            $this.click(function()
            {
                var $this = $(this);
                var imageToLoadUrl = $this.attr("imageToLoad");
				var imageLink = $this.attr("customLink");
                var $hpMainImage = $("#hpMainImage");
                if ($hpMainImage.attr("src").indexOf(imageToLoadUrl) != -1)
                    return false;
				$hpMainImage.unbind("load");
                $hpMainImage.fadeOut(500, function()
                {
                    $hpMainImage.load(function()
                    {
						$("#hpMainImageLink").attr("href", imageLink);





                        $hpMainImage.fadeIn(500);
                    }).attr("src", imageToLoadUrl);

                });
                return false;
            });
        });
    };
})(jQuery);

var DebbyUi = {};
DebbyUi.isIE6 = function()
{
    var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
    var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);
    return (ie6 || ie55);
}

DebbyUi.CurrentMainImageIndex = 0;
DebbyUi.MainImageAutoSlideTimer = null;
DebbyUi.MainImageAutoSlideTime = 4000;

DebbyUi.StartMainImageAutoSlide = function()
{
    if (DebbyUi.MainImageAutoSlideTimer != null)
        window.clearTimeout(DebbyUi.MainImageAutoSlideTimer);
    DebbyUi.MainImageAutoSlideTimer = window.setTimeout(DebbyUi.MainImageAutoSlide, 0);
}

DebbyUi.MainImageAutoSlide = function()
{
    if (DebbyUi.MainImageAutoSlideTimer != null)
        window.clearTimeout(DebbyUi.MainImageAutoSlideTimer);

    $imagesMenu = $("div.imageSelector a");
    $($imagesMenu[DebbyUi.CurrentMainImageIndex]).mouseout();
    DebbyUi.CurrentMainImageIndex++;
    if (DebbyUi.CurrentMainImageIndex > ($imagesMenu.length - 1))
        DebbyUi.CurrentMainImageIndex = 0;
    $($imagesMenu[DebbyUi.CurrentMainImageIndex]).click();
    $($imagesMenu[DebbyUi.CurrentMainImageIndex]).mouseover();

    if (DebbyUi.MainImageAutoSlideTimer != null)
        window.clearTimeout(DebbyUi.MainImageAutoSlideTimer);

    DebbyUi.MainImageAutoSlideTimer = window.setTimeout(DebbyUi.MainImageAutoSlide, DebbyUi.MainImageAutoSlideTime);
}

function changeImage(imgtagid,src) {
	$("#"+imgtagid).attr("src",src);
}

function changeImageFondo(imgtagid,src) {
	$("#"+imgtagid).css("background-image","url("+src+")");
}



function loadwho(id) {

	$.get('ajax.loadwho.php?x=' + id, function(data) {
			var arr = data.split('|');
			$("#de_nome").html(arr[0]);
			$("#de_testo").html(arr[1]);
			$("#imgwho").attr("src",arr[2]);
		}
	);
}


function loadpagewho(s,ps,lang) {
	$.get('ajax.loadpagewho.php?s=' + s + '&ps='+ ps +'&lang='+lang, function(data) {
			var arr = data.split('|');	//per paginare eventualmente la paginazione...
			$("#box2").html(arr[0]);
		}
	);
}

function loadpagewhere(s,lang) {
	$.get('ajax.loadwhere.php?x=' + s +'&lang='+lang, function(data) {
			var arr = data.split('|');	//per paginare eventualmente la paginazione...
			$("#mainfotocitta").attr("src",arr[0]);
			$("#testocitta").html(arr[1]);
		}
	);
}

function loadpagewhat(s,ps,lang,y) {
	$.get('ajax.loadpagewhat.php?s=' + s + '&ps='+ ps +'&lang='+lang+'&y='+y, function(data) {
			var arr = data.split('|');	//per paginare eventualmente la paginazione...
			$("#newsBoxPaginato").html(arr[0]);
			$("#elencopagine").html(arr[1]);
		}
	);
}

function loadpagemakeup(s,ps,lang) {
	$.get('ajax.loadpagemakeup.php?x=' + id, function(data) {
			var arr = data.split('|');
			$("#videoBlock").html(arr[0]);
			//$("#de_testo").html(arr[1]);
			//$("#imgwho").attr("src",arr[2]);
		}
	);
}

function playVideo(id,ps,lang) {
	$.get('ajax.playvideo.php?x=' + id + '&ps='+ ps +'&lang='+lang, function(data) {
			var arr = data.split('|');	//per paginare eventualmente la paginazione...
			//alert(arr[0]);
			$("#scriptVideo").html(arr[0]);
			$("#testoVideo").html(arr[1]);
		}
	);
}


function cercaform() {
	$("#cercaform").submit();
}

function newsletterErr(s) {
	$("#containernl").hide("fast");
	$("#rispostanl").html(s);
	$("#rispostanl").show("fast");
	setTimeout(function() {
		$("#rispostanl").hide("fast");
		$("#containernl").show("fast");
	}, 2000);
}
function newsletter() {
		$.get('ajax.newsletter.php?x=' + $("#emailnl").val() +'&check='+($('#checknl').attr("checked")?"1":"0"), function(data) {
			var arr = data.split('|');	
			newsletterErr(arr[0]);
		}
	);
}

function newsletter2() {
		$.get('ajax.newsletter2.php?x=' + $("#email").val() + '&nome=' + $("#nome").val() + '&cognome=' + $("#cognome").val()  + '&indirizzo=' + $("#indirizzo").val() + '&nascita=' + $("#nascita").val()  + '&citta=' + $("#citta").val() + '&prov=' + $("#prov").val() +'&check='+($('#checknl').attr("checked")?"1":"0"), function(data) {
			$('#risposta').html(data);
			//var arr = data.split('|');	
			//newsletterErr(arr[0]);
		}
	);
}



function gotonewsletter() {
	document.location.href = "newsletter.php?email=" + escape($("#emailnl").val());
}


function youtubeErr(s) {
	$("#containery").hide("fast");
	$("#rispostay").html(s);
	$("#rispostay").show("fast");
	setTimeout(function () {
		$("#rispostay").hide("fast");
		$("#containery").show("fast");
	}, 2000);
}
function youtube() {
		$.get('ajax.youtube.php?x=' + $("#urly").val(), function(data) {
			var arr = data.split('|');	
			youtubeErr(arr[0]);
		}
	);
}



$(document).ready(function()
{
	$(document).customPngFix();

	//styleswitcher
	var cookie = readCookie("style");
	var title = cookie ? cookie : getPreferredStyleSheet();
	setActiveStyleSheet(title);

});


function aggiornaS( base, agg1, agg2) {
	url  =  'ajax.aggiornasearch.php?base='+base+'&baseval=' + $('#'+base).val()+'&agg1='+agg1+'&agg1val=' + $('#'+agg1).val()+'&agg2='+agg2+'&agg2val=' + $('#'+agg2).val();
	//alert( url);
	$.get(url, function(data) {
			var arr = data.split('|');	
			$('#'+agg1).html(arr[0]);
			$('#'+agg2).html(arr[1]);
		}
	);
}


function nextUl(idul) {
	//var pos = $("#nextPageLi").show().position();
	//alert(pos.left + " " + pos.top);
	//$("#nextPageLi").css({ position: "absolute", top: pos.top+'px', left: pos.left+'px' });
	//pos = $("#nextPageLi").show().position();
	//alert(pos.left + " " + pos.top);
	$('#'+idul+' li').each( function () { $(this).toggle();} );
	$('#nextPageLi').html() =='next' ? $('#nextPageLi').html("prev") : $('#nextPageLi').html("next");
	$('#nextPageLi').blur();
}

