$(document).ready(function () {


    //image Preview calls
    $(".tip_trigger").hover(function () {
        tip = $(this).find('.imagePreview');
        tip.show(); //Show tooltip
    }, function () {
        tip.hide(); //Hide tooltip		  
    }).mousemove(function (e) {
        var mousex = e.pageX + 20; //Get X coodrinates
        var mousey = e.pageY + 20; //Get Y coordinates
        var tipWidth = tip.width(); //Find width of tooltip
        var tipHeight = tip.height(); //Find height of tooltip

        //Distance of element from the right edge of viewport
        var tipVisX = $(window).width() - (mousex + tipWidth);
        //Distance of element from the bottom of viewport
        var tipVisY = $(window).height() - (mousey + tipHeight);

        if (tipVisX < 20) { //If tooltip exceeds the X coordinate of viewport
            mousex = e.pageX - tipWidth - 5;
        } if (tipVisY < 20) { //If tooltip exceeds the Y coordinate of viewport
            mousey = e.pageY - tipHeight - 5;
        }
        tip.css({ top: mousey, left: mousex });
    });


    //Pretty Photo calls and settings
    if ($("#summary").length) {
        $.getScript('/scripts/jquery.prettyPhoto.js', function () {
            $("a[rel^='prettyPhoto[flash]']").prettyPhoto({
                theme: 'light_square',
                padding: 5,
                allowresize: false,
                counter_separator_label: ' di ',
                showTitle: 0
            }
				);
            $("a[rel^='prettyPhoto[flashDown]']").prettyPhoto({
                theme: 'light_square',
                padding: 5,
                allowresize: false,
                counter_separator_label: ' di ',
                showTitle: 0
            }
				);
        });
    }

//    if ($(".mediaContent").length) {
//        $.getScript('/scripts/jquery.prettyPhoto.js', function () {
//            $("a[rel^='prettyPhoto[flash]']").prettyPhoto({
//                theme: 'light_square',
//                padding: 5,
//                allowresize: false,
//                counter_separator_label: ' di ',
//                showTitle: 0
//            }
//				);
//        });
//    }

//    //Pretty Photo video calls and settings
//    $('#summary .prettyCall').each(function () {
//        var prettyPhotoCall = $(this).find('a').first();
//        var simpleLink = $(this).find('a.action');
//        $(simpleLink).click(function () {
//            $(prettyPhotoCall).click()
//        });
//    });




    //Pretty Photo video calls and settings
    if ($("a[rel^='prettyPhoto[flash]']").length > 0) {
        $.getScript('/scripts/jquery.prettyPhoto.js', function () {
            $("a[rel^='prettyPhoto[flash]']").prettyPhoto({
                theme: 'light_square',
                padding: 5,
                allowresize: false,
                counter_separator_label: ' di ',
                showTitle: 0
            }
				);
        });
    }


    $("a[rel^='prettyPhoto[flash]']").each(function () {
        $(this).click(function () {
            $(prettyPhotoCall).click()
            alert('ok1');
        });
    });




    // columnize everything!
    if ($('.columns').length > 0) {
        $.getScript('/scripts/jquery.autocolumn.min.js', function () {
            $('.columns').columnize();
        });
    }

    // superfish menu fix
    $("ul.sf-menu").superfish();
    lastUl = ($(".sf-menu li ul:last").width()) - 100;

    $(".sf-menu li ul").last().css("left", "-" + lastUl + "px");
    if ($.browser.msie) {
        $(".sf-menu li ul").css("left", "3px");
    }


    // summary last item fix	
    var c = 1;
    $("#summary div").each(function () {

        if ((c % 3) == 0)
            $(this).addClass("last").after('<br clear="all" />');
        c++;
    });


    // styling the current item
    var liActive = $("#mainNav ul li.active")
    var activeLeft = parseInt((liActive.width() - 16) / 2);

    liActive.append('<div class="isActive"></div>')
    $(".isActive").css("left", activeLeft)


    // current styling
    $(".current").find("li").css("background", "white")

    if ($('.imgPreview').length > 0) {
        $.getScript('/scripts/jquery.imgpreview.min.0.22.js', function () {
            $('.imgPreview a').imgPreview({
                containerID: 'imgPreviewWithStyles',
                imgCSS: { height: 400, border: "1px solid #ccc" },
                srcAttr: 'rel'
            });
        });
    }
    // styling the form
    if ($('.jqtransform').length > 0) {
        $.getScript('/scripts/jquery.jqtransform.js', function () {
            $(".jqtransform").jqTransform();
        });
    }

    // accordion
    if ($('#accordion').length != 0) {
        $('#accordion').accordion({
            active: false,
            header: '.aHead',
            autoheight: false,
            animated: 'easeslide',
            alwaysOpen: false
        });
    }
});

/* FUNZIONI PER IL SEARCH XSLT */

 function GoSearch() {
     var field = document.getElementById('searchField');
     if (field != null) {
         var testo = document.getElementById('searchField').value;
         if (testo != '') {
			 if (testo.length >= 3)
			 {
             window.location.href = '/ricerca.aspx' + '?search=' + encodeURIComponent(testo);
			 }
			 else
			 {
				$("#dialog").html('<br/><strong>Si prega di specificare una parola di almeno 3 caratteri.</strong>');
				$("#dialog").dialog({ closeOnEscape: true, 
				title: 'Attenzione!',
				dialogClass: 'alert',
				draggable: false,
				width: 300,
				height:120,
				modal: true,
				resizable: false,
				zIndex: 3999,
				stack: true});
			 }
         }
     }
     return true;
 }

 function gup(name) {
     name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
     var regexS = "[\\?&]" + name + "=([^&#]*)";
     var regex = new RegExp(regexS);
     var results = regex.exec(window.location.href);
     if (results == null)
         return "";
     else
         return results[1];
 }

 function setPreviousSearchValue() {
     var prevSearchString = gup('search');
     if (prevSearchString != '') {
         var field = document.getElementById('searchField');
         if (field != null) {
             //document.getElementById('searchField').value = escape(unescape(prevSearchString));
             document.getElementById('searchField').value = decodeURIComponent(prevSearchString);
         }
         else {
             alert('Error: search field not available!');
         }
     }
 }

 function addLoadEvent(func) {
     var oldonload = window.onload;
     if (typeof window.onload != 'function') {
         window.onload = func;
     } else {
         window.onload = function () {
             if (oldonload) {
                 oldonload();
             }
             func();
         }
     }
 }
 addLoadEvent(setPreviousSearchValue);

 function disableEnterKey(e) {
     var key;
     if (window.event)
         key = window.event.keyCode; //IE
     else
         key = e.which; //firefox     

     if (key == 13) {
         GoSearch();
     }
     return (key != 13);
 }

 /* END FUNZIONI PER IL SEARCH */
 
  function SetMaxCharsNumber()
 {
				$("#dialog").html('<br/><strong>Limite di caratteri massimi raggiunto!</strong>');
				$("#dialog").dialog({ closeOnEscape: true, 
				title: 'Attenzione!',
				dialogClass: 'alert',
				draggable: false,
				width: 300,
				height:120,
				modal: true,
				resizable: false,
				zIndex: 3999,
				stack: true});
 }
 
