$(document)
        .ready(
                function() {

                    $("#searchFieldH").bind("focus", function(e) {
                        $("ul#mainNav > li.ricerca").addClass("hover");
                    });

                    $("#searchFieldH").bind("blur", function(e) {
                        $("ul#mainNav > li.ricerca").removeClass("hover");
                    });

                    /* condividi pagina */
                    $("#shareButt").toggle(
                            function(e) {
                                $("div#shareUrl").slideDown(300);
                                $(this).removeClass("sdButtSGrey").addClass(
                                        "sdButtSRed");
                            },
                            function(e) {
                                $("div#shareUrl").slideUp(300);
                                $(this).removeClass("sdButtSRed").addClass(
                                        "sdButtSGrey");
                            });

                    $("input#shareField").bind("click", function(e) {
                        $(this).select();
                    });

                    /* crea argomento forum */
                    $("#threadButt").toggle(
                            function(e) {
                                $("div#threadCreate").slideDown(300);
                                $(this).removeClass("sdButtSGrey").addClass(
                                        "sdButtSRed");
                            },
                            function(e) {
                                $("div#threadCreate").slideUp(300);
                                $(this).removeClass("sdButtSRed").addClass(
                                        "sdButtSGrey");
                            });

                    /* rating */
                    var vote = false;

                    function unrate() {
                        if (!vote) {
                            var currRating = $("span#currRating").attr("title");
                            for ( var i = currRating; i <= 5; i++) {
                                $("a.star[title=" + i + "]").removeClass("On")
                                        .addClass("Off");
                            }
                            for ( var i = 1; i <= currRating; i++) {
                                $("a.star[title=" + i + "]").removeClass("Off")
                                        .addClass("On");
                            }
                        }
                    }

                    function rate(whichA) {
                        var whichStar = $(whichA).attr("title");
                        for ( var i = whichStar; i <= 5; i++) {
                            $("a.star[title=" + i + "]").removeClass("On")
                                    .addClass("Off");
                        }
                        for ( var i = 1; i <= whichStar; i++) {
                            $("a.star[title=" + i + "]").removeClass("Off")
                                    .addClass("On");
                        }
                    }

                    function alertRate(whichAlert) {
                        $("p.status").hide();
                        if (whichAlert == "already")
                            $("p.already").show();
                        if (whichAlert == "thanks")
                            $("p.thanks").show();
                    }

                    $("a.star").bind("mouseenter", function(e) {
                        if (!vote)
                            rate(this);
                    });

                    $("ul.stars").bind("mouseleave", function(e) {
                        unrate();
                    });

                    $("a.star").bind("click", function(e) {
                        if (!vote) {
                            rate(this);
                            alertRate("thanks");
                            vote = true;
                        } else {
                            alertRate("already");
                        }
                    });

                    /* tab sidebar */

                    $("ul.contentBox_Sidebar_tab a, ul#Message_tab a")
                            .each(
                                    function(n) {
                                        $(this).bind(
                                                        "click",
                                                        function(event) {
                                                            event.preventDefault();

                                                            /* accendo/spengo il tab */
                                                            $(this).parent().addClass("on").removeClass("off");
                                                            $(this).parent().prev().addClass("off").removeClass("on");
                                                            $(this).parent().next().addClass("off").removeClass("on");

                                                            /* mostro/nascondo l'elenco */
                                                            var whichListToShow = $(this).attr("href");
                                                            whichListToShow = whichListToShow.substring(1);
                                                            $("#" + whichListToShow).show();

                                                            var whichListToHide = whichListToShow.substring(0,(whichListToShow.length - 1));
                                                            if (whichListToShow.indexOf("comm1") > -1) whichListToHide = whichListToHide + "2";
                                                            if (whichListToShow.indexOf("comm2") > -1) whichListToHide = whichListToHide + "1";
                                                            $("#" + whichListToHide).hide();
                                                        });
                                    });

                    /* bottone commenta */
                    $("a.msgQuoteLink").bind(
                            "click",
                            function(event) {
                                $("li#msgComment").removeClass("msgClose")
                                        .addClass("msgOpen");
                                $("a#msgCommentButt")
                                        .removeClass("sdButtSGrey").addClass(
                                                "sdButtSRed");
                            });

                    /* bottone modifica */
                    $("a.msgEditLink").bind(
                            "click",
                            function(event) {
                                $("li#msgComment").removeClass("msgClose")
                                        .addClass("msgOpen");
                                $("a#msgCommentButt")
                                        .removeClass("sdButtSGrey").addClass(
                                                "sdButtSRed");
                            });

                    $("a#msgCommentButt")
                            .bind(
                                    "click",
                                    function(event) {
                                        event.preventDefault();

                                        var closed = $(this).parent().parent()
                                                .hasClass("close");

                                        if (closed) {
                                            $("li#msgComment").removeClass(
                                                    "msgClose").addClass(
                                                    "msgOpen");
                                            $(this).removeClass("sdButtSGrey")
                                                    .addClass("sdButtSRed");
                                            $(this).parent().parent().children(
                                                    "h4").children("a").click();
                                        }

                                        if (!closed) {
                                            if ($("li#msgComment").hasClass(
                                                    "msgOpen")) {
                                                $("li#msgComment").removeClass(
                                                        "msgOpen").addClass(
                                                        "msgClose");
                                                $(this).removeClass(
                                                        "sdButtSRed").addClass(
                                                        "sdButtSGrey");
                                            } else {
                                                $("li#msgComment").removeClass(
                                                        "msgClose").addClass(
                                                        "msgOpen");
                                                $(this).removeClass(
                                                        "sdButtSGrey")
                                                        .addClass("sdButtSRed");
                                            }
                                        }
                                    }

                            );

                    $("a#reportAs").bind(
                            "click",
                            function(event) {
                                $(this).parent().html(
                                        "<b>Hai inviato la segnalazione</b>");
                            });

                    $("a#showMoreCatA").bind(
                            "click",
                            function(event) {
                                var status = $(this).parent().nextAll(
                                        ".contentBox_ros").hasClass("hide");

                                if (status) {
                                    $(this).parent().nextAll(".contentBox_ros")
                                            .removeClass("hide");
                                    $(this).html("Nascondi le categorie");
                                } else {
                                    $(this).parent().nextAll(".contentBox_ros")
                                            .addClass("hide");
                                    $(this).html("Mostra tutte categorie");
                                }
                            });

                    /* Pagina Beta -> Apro chiudo form download */
                    $("a.downloadLink").bind(
                            "click",
                            function(event) {
                                event.preventDefault();
                                var status = $(this).parent().parent()
                                        .hasClass("close");

                                if (status) {
                                    $(this).parent().parent().removeClass(
                                            "close").addClass("open");
                                    $(this).parent().children(".contentBoxSH")
                                            .slideDown(1000);
                                } else {
                                    $(this).parent().nextAll(".contentBox_ros")
                                            .removeClass("open").addClass(
                                                    "close");
                                    $(this).parent().children(".contentBoxSH")
                                            .slideUp(1000);
                                }
                            });

                    /* Common -> Inizializzo apertura/chiusura login */

                    $("#loginLB").click( function(event) {
                        if (event.target.id == 'loginLB')
                            closeLogin();
                    });

                    $("#loginLB_bg").click( function(event) {
                        if (event.target.id == 'loginLB_bg')
                            closeLogin();
                    });

                    $("#loginLBClose, #pwreminderLBClose").click(
                            function(event) {
                                closeLogin();
                            });

                    $("a#loginButt").bind("click", function(event) {
                        event.preventDefault();
                        openLogin();
                    });

                    /* Inizializzo apertura/chiusura pw reminder */

                    $("a#pwreminderLink").bind("click", function(event) {
                        event.preventDefault();
                        $("#loginBody").hide();
                        $("#pwreminderBody").show();
                    });

                    $("a#loginLink").bind("click", function(event) {
                        event.preventDefault();
                        $("#loginBody").show();
                        $("#pwreminderBody").hide();
                    });

                    var myHost = "http://www.nextinnovation.it";

                    /* FCK EDITOR: scalo dimensioni immagini negli articoli */
                    $("div.article img").each(
                            function(i) {
                                totalWidth = $(this).width()
                                        + $(this).attr("hspace")
                                        + $(this).attr("border");
                                if (totalWidth > 620) {
                                    var vheight = $(this).height() * 620
                                            / $(this).width();
                                    $(this).width(620).height(vheight);
                                }
                            });

                    /* FCK EDITOR: scalo dimensioni immagini nel forum */
                    $("div.msg_text img, div.msg_text embed").each(
                            function(i) {
                                totalWidth = $(this).width()
                                        + $(this).attr("hspace")
                                        + $(this).attr("border");
                                if (totalWidth > 513) {
                                    var vheight = $(this).height() * 513
                                            / $(this).width();
                                    $(this).width(513).height(vheight);
                                }
                            });

                    $("div.article a, div.msg_text a").each(
                            function(i) {
                                if ($(this).attr("href") && ($(this).attr("target") != "_blank")
                                        && ($(this).attr("href")
                                                .indexOf(myHost) == -1)) {
                                    $(this).attr("target", "_blank");
                                }
                            });

                    /* lightbox notifiche dashboard */                  

                    $("#notificheLB").click( function(event) {
                        if (event.target.id == 'notificheLB')
                            closeNotification();
                    });
					
					$("#notificheLB_bg").click( function(event) {
                        if (event.target.id == 'notificheLB_bg')
                            closeNotification();
                    });

                    $("a#notificationButtDB").bind("click", function(event) {
                        event.preventDefault();
                        openNotification();
                    });

                    $("#notificheBody div.custumButt a.close").unbind().click( function(event) {
						event.preventDefault();
                        closeNotification();
                    });
					
					
					/* lightbox facebook */   
					
					 $("#facebookLB").click( function(event) {
                        if (event.target.id == 'facebookLB')
                            closeFacebook();
                    });
					
					$("#facebookLB_bg").click( function(event) {
                        if (event.target.id == 'facebookLB_bg')
                            closeFacebook();
                    });

                    $("a#fbQuestion").bind("click", function(event) {
                        event.preventDefault();
                        openFacebook();
                    });
					
					$("a#facebookLBClose").click( function(event) {
						event.preventDefault();
                        closeFacebook();
                    });
					
					/* lightbox video beta */   
					$("#videoLB, #videoLB_bg").click( function(event) {
                        if ((event.target.id == 'videoLB') || (event.target.id == 'videoLB_bg'))
                            closeVideobeta();
                    });					
				

                    $("a.linkVideo").bind("click", function(event) {
                        event.preventDefault();
						
						var videoSrc = $(this).attr("href");
						var videoEmbed = "<embed id=\"player\" width=\"730\" height=\"420\" flashvars=\"file=" + videoSrc + "&controlbar=over\" wmode=\"opaque\" allowscriptaccess=\"always\" allowfullscreen=\"true\" quality=\"high\" bgcolor=\"#ffffff\" name=\"player\" type=\"application/x-shockwave-flash\" " +
								"src=\"" + contextPath + "/.resources/media/players/player.swf\" />";
						$("#videoEmbed").html(videoEmbed);
						
		                   
                        openVideobeta();
                    });

				  /* equal height box chi siamo */
		
                    function equalHeight(group) {
                    	tallest = 0;
                    	group.each(function() {
                    		thisHeight = $(this).height();
                        	if(thisHeight > tallest) tallest = thisHeight;
                    	});
			
                    	group.height(tallest);
                    }		
                    equalHeight($("ul.contentBoxEdit_list li"));
				
		
                   /* userPannel utente logato */
                    var myLayer=$('#userSelect');
		  
                    $('#userPannel').click(function(event){

                    	event.preventDefault();

                    	if((myLayer.css('display') == "none") || (myLayer.css('display') == "")){
                    		myLayer.css('display', 'block');
                    		$('#userPannel').removeClass('closed');
		    
                    	} else { 
                    		myLayer.css('display', 'none');
                    		$('#userPannel').addClass('closed');
                    	}
		    
                    });

                }); /* chiusura document.ready */
				
					/* Inizio funzioni per gestione L I G H T B O X */
					
					/* lightbox facebook */					
					function closeFacebook() {
						$("select").show();
                        $("#facebookLB, #facebookLB_bg").hide();
                    }

                    function openFacebook() { //730x420
						
						$("select").hide();
						
                        getPageSize();
                        getPageScroll(); 
                        var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 710) / 2);
                        if (lightboxTop < arrayPageScroll[1]) { lightboxTop = arrayPageScroll[1]; }

                        $("#facebookBody").css({top : lightboxTop + 'px'});						
						$("#facebookLB_bg").hide().css( {width : '100%', height : arrayPageSize[1] + 'px' }).fadeTo("fast", 0.80).show();	
                        $("#facebookLB").hide().css( {width : '100%', height : arrayPageSize[1] + 'px'}).show();
						
                    }
					
					/* lightbox video beta */					
					function closeVideobeta() {
						$("select").show();
                        $("#videoLB, #videoLB_bg").hide();
                    }

                    function openVideobeta() { //730x420
						
						$("select").hide();
						
                        getPageSize();
                        getPageScroll(); 
                        var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 420) / 2);
                        if (lightboxTop < arrayPageScroll[1]) { lightboxTop = arrayPageScroll[1]; }

                        $("#videoBody").css({top : lightboxTop + 'px'});						
						$("#videoLB_bg").hide().css( {width : '100%', height : arrayPageSize[1] + 'px' }).fadeTo("fast", 0.80).show();	
                        $("#videoLB").hide().css( {width : '100%', height : arrayPageSize[1] + 'px'}).show();
						
                    }
					
					
					/* lightbox notifiche dashboard */					
					function closeNotification() {
						$("select").show();
                        $("#notificheLB, #notificheLB_bg").hide();
                    }

                    function openNotification() {
						
						$("select").hide();
						
                        getPageSize();
                        getPageScroll();
                        var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 377) / 2);
                        if (lightboxTop < arrayPageScroll[1]) { lightboxTop = arrayPageScroll[1]; }

                        $("#notificheBody").css({top : lightboxTop + 'px'});						
						$("#notificheLB_bg").hide().css( {width : '100%', height : arrayPageSize[1] + 'px' }).fadeTo("fast", 0.80).show();	
                        $("#notificheLB").hide().css( {width : '100%', height : arrayPageSize[1] + 'px'}).show();
						
						
						$("ul.scroll").jScrollPane( {
							showArrows : false,
							scrollbarWidth : 21,
							scrollbarMargin : 0,
							dragMaxHeight : 21
						});
                    }
				
				
					/* funzioni lightbox login */

function closeLogin() {
	$("select").show();
    $("#loginLB, #loginLB_bg").hide();
}

function openLogin() {
	$("select").hide();
    // dimensioni login 470px x 280px

    getPageSize();
    getPageScroll();
    var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 280) / 2);
    if (lightboxTop < arrayPageScroll[1]) {
        lightboxTop = arrayPageScroll[1];
    }

    $("#loginBody").css( {
        top : lightboxTop + 'px'
    }).show();
    $("#pwreminderBody").css( {
        top : lightboxTop + 'px'
    }).hide();

    $("#loginLB_bg").hide().css( {
        width : '100%',
        height : arrayPageSize[1] + 'px'
    }).fadeTo("fast", 0.80).show();
    $("#loginLB").hide().css( {
        width : '100%',
        height : arrayPageSize[1] + 'px'
    }).show();

}




var arrayPageScroll = "";
function getPageScroll() {
    var xScroll, yScroll;

    if (self.pageYOffset) {
        yScroll = self.pageYOffset;
        xScroll = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
        yScroll = document.documentElement.scrollTop;
        xScroll = document.documentElement.scrollLeft;
    } else if (document.body) {// all other Explorers
        yScroll = document.body.scrollTop;
        xScroll = document.body.scrollLeft;
    }

    arrayPageScroll = new Array(xScroll, yScroll);
    return arrayPageScroll;
};

var arrayPageSize = "";
function getPageSize() {
    var xScroll, yScroll;

    if (window.innerHeight && window.scrollMaxY) {
        xScroll = window.innerWidth + window.scrollMaxX;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }

    var windowWidth, windowHeight;

    if (self.innerHeight) { // all except Explorer
        if (document.documentElement.clientWidth) {
            windowWidth = document.documentElement.clientWidth;
        } else {
            windowWidth = self.innerWidth;
        }
        windowHeight = self.innerHeight;
    } else if (document.documentElement
            && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }

    // for small pages with total height less then height of the viewport
    if (yScroll < windowHeight) {
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }

    // for small pages with total width less then width of the viewport
    if (xScroll < windowWidth) {
        pageWidth = xScroll;
    } else {
        pageWidth = windowWidth;
    }

    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
    return arrayPageSize;
};






/* Fine funzioni per gestione L I G H T B O X */

function initSearchFields(emptySearchText){
	$.each(['#searchFieldHsearchFormH', '#searchFieldHsearchFormF'], function(i, value){
		var $input = $(value);
		if ($input && $input.length){
			$input.bind('focus', function(){
				if ($input.val() == emptySearchText) $input.val('');
			}).bind('blur', function(){
				if ($input.val().match(/^\s*$/)) $input.val(emptySearchText);
			});
			if ($input.val().match(/^\s*$/)) $input.val(emptySearchText);
		}
	});
}
