/* - - - - - - - - - - - - - - - - - - - - 
C O N F I G U R A T O R E   W E B
website by websolute
http://www.websolute.it/
- - - - - - - - - - - - - - - - - - - - */


/* MENU
- - - - - - - - - - - - - - - - - - - - */
$.fn.menu = function(options) {
    return this.each(function() {
        var $this = $(this);
        $this.find("ul ul").hide();
        if ($("#hfCurrentMenu").val() != "") {
            $(".menu a:eq(" + $("input[id$=hfCurrentMenu]").val() + ")").addClass("on").click();
            $(".menu a:eq(" + $("input[id$=hfCurrentMenu]").val() + ")").parent().parent().show().prev().addClass("on").parent().parent().show().prev().addClass("on");
        }
        $this.find("ul a").click(function() {
            $(this).parent().siblings().find("a").removeClass("on");
            $this.find("ul").not($(this).parents("ul")).slideUp(800, "easeInOutQuint");
            $(this).addClass("on").next().slideDown(800, "easeInOutQuint").find("a.on").removeClass("on");
            $("#hfCurrentMenu").val($(".menu a").index($(this)));
        });
    });
}


/* EVENODD
- - - - - - - - - - - - - - - - - - - - */
$.fn.evenodd = function(tag) {
    return this.each(function() {
        var $this = $(this);
        $this.find(tag + ":odd").addClass("odd clearfix");
        $this.find(tag + ":even").addClass("even clearfix");
    });
}


/* TABS
- - - - - - - - - - - - - - - - - - - - */
$.fn.tabs = function() {
    return this.each(function() {

        pusher = [];
        $(".tabs ul li a").each(function() {
            pusher.push("#" + $(this).attr("href").split("#")[1]);
        });

        //var current = "#" + $("#hfCurrentTab").val();
        var current = "#" + $("#" + $("#hfCurrentTabId").val()).val();
        //alert('currentTab = ' + current);
        if (current == "#") {
            //alert('current --');
            //$(".tabs ul li a:eq(0)").addClass("on").click();
            $(".tabs ul li a:eq(0)").addClass("on");
            if (pusher.length > 0) {
                $.each(pusher, function(i, val) {
                    $(val).hide();
                });
            }
            $("#" + $(".tabs ul li a:eq(0)").attr("href").split("#")[1]).show();
        } else {
            //alert('else --');

            if (pusher.length > 0) {
                $.each(pusher, function(i, val) {
                    $(val).hide();
                });
            }
            $(".tabs ul li a.on").removeClass("on");
            //alert('tolte tutte !');
            $(".tabs ul li a[href='" + current + "']").addClass("on");
			//alert ($(current + " .menu ul li a:first").size());
            $(current).show();
        }

        $(".tabs ul li a").click(function() {
        
            $("#" + $("#hfCurrentTabId").val()).val($(this).attr("href").split("#")[1]);
            //$("#hfCurrentTab").val($(this).attr("href").split("#")[1]);
            $(".tabs ul li a.on").removeClass("on");
            $(this).addClass("on");
            var curr = "#" + $(this).attr("href").split("#")[1];
            if (pusher.length > 0) {
                $.each(pusher, function(i, val) {
                    $(val).hide();
                });
            }
            $(curr).show();
            $(curr).find(".menu ul:eq(0) li:eq(0) a:eq(0)").click();
            return false;
        });
    });
}


/* TOOLTIP
- - - - - - - - - - - - - - - - - - - - */
$.fn.tooltip = function() {
    return this.each(function() {
        $this = $(this);
        $this.next().hide();
        /*
        $this.hover(function(e){
        $(this).next().show().css({"top": "-" + ($(this).next().height() + 21) + "px", "right":"4px", "position":"absolute", "height":$(this).next().height() + "px"}).hide().fadeIn(500);
        },function(){
        $(this).next().fadeOut(500);
        });
        */
        $this.click(function() {
            $(this).next().toggle().css({ "top": "-" + ($(this).next().height() + 17) + "px", "left": "-10px", "position": "absolute", "height": $(this).next().height() + "px" });
        });
    });
}


/* SHOW MODULO
- - - - - - - - - - - - - - - - - - - - */
function show(obj) {
    $(".moduli .modulo").hide();
    if (obj != null) {
        $(obj).show();
    }
}



/* HIDE MENU.ON
- - - - - - - - - - - - - - - - - - - - */
function hideOn() {
    $(".menu a.on").removeClass("on");
}


/* LOADING
- - - - - - - - - - - - - - - - - - - - */
function loading(a) {
	if ($("#upProgress").size()) {
		$("#upProgress").appendTo($("body")).css({"width":$(document).width() + "px", "height":$(document).height() + "px","top":"0px","left":"0px"}).show();
		if ($.browser.msie) {
			$("body").css({"overflow":"hidden"});
		}
		if (a) {
			$("#upProgress img").css("background","url(img/attendi.png) no-repeat left top");
		}
	}
}


/* CHECK 3 STATE
- - - - - - - - - - - - - - - - - - - - */
function chk3State() {
    $(".check3").each(function() {

        var checkVal = $(this).find("input").val();
        var updateID = "FT_" + $(this).find("input").attr("id").split("HTxT_")[1];
        var background;
        if (checkVal == 0) {
            background = "url(img/Chk3State/checked.gif)";
        } else if (checkVal == 2) {
            background = "url(img/Chk3State/intermediate.gif)";
        } else {
            background = "url(img/Chk3State/unchecked.gif)";
        }
        checkDiv = '<div class="input3" style="background:' + background + ' no-repeat left top"></div>';
        $(this).append(checkDiv).click(function() {
            if (checkVal == 2) {
                $(this).find("div").css({ "background-image": "url(img/Chk3State/checked.gif)" });
                $(this).find("input").val("0");
            } else if (checkVal == 1) {
                $(this).find("div").css({ "background-image": "url(img/Chk3State/intermediate.gif)" });
                $(this).find("input").val("2");
            } else {
                $(this).find("div").css({ "background-image": "url(img/Chk3State/unchecked.gif)" });
                $(this).find("input").val("1");
            }
            checkVal = $(this).find("input").val();
            UpdateCfg(updateID);
        })
    });
}


// SCROLL ITEMS
function scrollItems() {
    $("div.scrollable").each(function(i) {
        // disabilitiamo i .disabled
        if ($(this).parent().is(".disabled")) {
            $(this).parent().append('<div style="position:absolute; right:0px; top:0px; width:240px; height:76px; z-index:10; background:#fff; opacity:0.5; -moz-opacity:0.5; filter:alpha(opacity=50);">&nbsp;</div>');
        }

        // preselezione al load
        $(this).find(".items div:eq(" + $(this).parent().find("input").val() + ")").click();
        $(this).append('<div class="scrollableName">' + $(this).find(".active img").attr("alt") + '</div>');

        $(this).find(".items img").mouseover(function() {
            $(this).parent().parent().next().html($(this).attr("alt"));
        }).mouseout(function() {
            $(this).parent().parent().next().html($(this).parent().parent().find(".active img").attr("alt"));
        });

        // click dell'item
        $(this).find(".items div").click(function() {
            $(this).parent().parent().parent().find("input").val($(this).parent().find("div").index($(this)));
            $(this).data("updateID", "FT_" + $(this).parent().parent().parent().find("input").attr("id").split("HTxT_")[1]);
            UpdateCfg($(this).data("updateID"));
        });
    });
}


// COLORI
function colori() {
    // fancy
    $(".apriColori").fancybox({
        'zoomOpacity': true,
        'overlayShow': true,
        'zoomSpeedIn': 500,
        'zoomSpeedOut': 500,
        'overlayOpacity': 0.7,
        'padding': 0,
        'frameWidth': 960,
        'frameHeight': $(window).height() - 100
    });

    // disabilito seleziona colore
    $(".selezionaColore").each(function() {
        if ($(this).hasClass("disabled")) {
            $(this).append('<div class="disabilitato"></div>');
        }
    });
}
function aggiornaColore(obj) {
    var nomeFeature = $(obj).parent().attr("rel").split("CL_")[1];
    var aggiornaInput = "#HTxT_" + nomeFeature;
    $(aggiornaInput).val($.trim($(obj).text()));
    UpdateCfg("FT_" + nomeFeature);
}


// IMMAGINE
function immagine() {
    $(".fancy").fancybox({
        'zoomOpacity': true,
        'overlayShow': true,
        'zoomSpeedIn': 500,
        'zoomSpeedOut': 500,
        'overlayOpacity': 0.7,
        'frameHeight': $(window).height() - 100
    });

    if ($("#hfImage").size()) {
        var currentImage = $("#hfImage").val().split(";")[1];
        $("#divImg .navi a").click(function() {
            thisClass = $(this).attr("class");
            if (thisClass.indexOf("_on") == -1) {
                $("#divImg .navi a[class$='on']").each(function() {
                    oldClass = $(this).attr("class");
                    $(this).removeClass(oldClass).addClass(oldClass.split("_on")[0]);
                });
                $(this).removeClass(thisClass).addClass(thisClass + "_on");
            }

        });
    }
}


/* INIT
- - - - - - - - - - - - - - - - - - - - */
function init() {
    $("body").focus();
    $(".fieldset").evenodd("li:not('li li')");
    $(".menu").menu();
    $(".tabs").tabs();
    loading("attendi");
    if ($.browser.msie) {
        //Su IE la funzione spegne le toolbar su IE ma in questo caso non cambio pagine e mi servono...
        $("body").css({ "overflow": "" });
    }    
    chk3State();
    $("div.scrollable").scrollable({ size: 3 });
    scrollItems();
    $(".tabs li a[href='#tabGR_InfoTec']").attr("id", "info");
    colori();
    immagine();
    $("a.tooltip").tooltip();
}

$(function() {
init();
    
});
