// MOSNE FUNCTIONS
$(document).ready(function () {
    var ea = 'easeInOutQuad';
    var speed = 300;
    var speed2 = 350;
    var mydomain = 'http://www.antoniovirgaarchitecte.com';
    var dln = mydomain.length;
    var turn_delay = 300;
  
    function menu_move(m_who, m_speed, m_delay, m_left, m_width, m_active) {
        $(m_who).stop().animate({
            width: m_width,
            left: m_left,
            easing: ea
        }, 3 * m_speed);
        if (m_active) {
            if ($(m_who).find("a.inside").hasClass("active")) {} else {
                $(".innermenu ul ul,.deco,.deco2").stop().fadeOut(m_speed);
                $(".pagecont").hide();
                $("a.inside").removeClass("active")
            }
            $(m_who).addClass("m_active").find(".innermenu").stop().delay(2 * m_speed).animate({
                width: m_width,
                backgroundColor: "#232323"
            }, (2 * m_speed));
        } else {
            $(m_who).find(".mainlink:hidden").show();
            $(m_who).removeClass("m_active").find(".innermenu").animate({
                width: 0,
                backgroundColor: "#666666"
            }, 2 * m_speed);
        }
    }

    function menu_move_lt(m_who, m_speed, m_delay, m_left, m_width, m_active) {
        $(m_who).css({
            width: m_width,
            left: m_left
        });
        $(m_who).find(".innermenu").hide();
    };

    function menu_showsub(el) {
        if ($(el).find("a.inside").hasClass("active")) {} else {
            var dist = 40;
            var wel = $(el).find(".innermenu ul a").eq(0).width();
            $(el).find(".deco").css({
                opacity: 0,
                left: wel
            }).delay(1200).show().animate({
                opacity: 1,
                left: (wel + dist)
            }, 600);
            $(el).find(".innermenu ul ul").css({
                opacity: 0,
                left: (wel + dist + 14 + dist)
            }).delay(1700).show().animate({
                opacity: 1
            }, 600);
            $(el).find("a.inside").addClass("active");
        }
    }
    $("#m_uno").click(function () {
        menu_move($("#m_uno"), speed, 0, -5, 675, true);
        menu_move($("#m_due"), speed, 0, 676, 675, false);
        menu_move($("#m_tre"), speed, 0, 838, 675, false);
        menu_showsub($("#m_uno"));
    });
    $("#m_due").click(function () {
        menu_move($("#m_uno"), speed, 0, -5, 675, false);
        menu_move($("#m_due"), speed, 0, 157, 675, true);
        menu_move($("#m_tre"), speed, 0, 838, 675, false);
        menu_showsub($("#m_due"));
    });
    $("#m_tre").click(function () {
        menu_move($("#m_uno"), speed, 0, -5, 675, false);
        menu_move($("#m_due"), speed, 0, 157, 675, false);
        menu_move($("#m_tre"), speed, 0, 320, 675, true);
        menu_showsub($("#m_tre"));
    });


    $("#menu").bind("open_menu", function () {
        menu_move($("#m_uno"), 200, 200, -5, 675, false);
        menu_move($("#m_due"), 300, 350, 330, 675, false);
        menu_move($("#m_tre"), 300, 450, 665, 675, false);
    });
    
    //mup
    $("#menu").bind("update", function () {
        setTimeout(function () {
            setTimeout(function () {
                $("#menu").find('.scroll-pane').jScrollPane({
                    scrollbarWidth: 12,
                    scrollbarMargin: 0,
                    dragMinHeight: 83,
                    dragMaxHeight: 83
                });
            }, 300);
            $("#aj").show("blind", {
                direction: "right"
            }, 600);
        }, 800);
    });
    $("#menu").bind("pretty", function () {
        $("a[rel^='prettyPhoto']").prettyPhoto({
            theme: 'dark_square',
            show_title: false,
            overlay_gallery: false
        });
        $("a.opengallery").click(function () {
            $(this).parent().parent().find(".pics").find("a").eq(0).trigger("click");
        });
    });
    $(".archivio a").live("click", function (event) {
        event.preventDefault();
        var link = $(this).attr("href");
        var el = $(this);
        $(".archivio .current, .subcatnavi .current").removeClass("current");
        $(".archivio .active_page").removeClass("active_page");
        $(".subcatnavi .current-cat").removeClass("current-cat");
        setTimeout(function () {
            $(el).addClass("current");
        }, 100);
        $("#aj .scroll-pane").empty().load(link + " .bodytext", function () {
           // alert("archivio");
            $("#menu").find('.scroll-pane').jScrollPane({
                scrollbarWidth: 12,
                scrollbarMargin: 0,
                dragMinHeight: 83,
                dragMaxHeight: 83
            }).hide().fadeIn(300);
            $("#menu").trigger("pretty");
            $("#menu").trigger("prj");
            location = "#"+link.substr(dln);
        });
    });
    //sub
    $(".subcatnavi a").live("click", function (event) {
        event.preventDefault();
        var link = $(this).attr("href");
        var el = $(this);
        $(".subcatnavi .current").removeClass("current");
        $(".subcatnavi .current-cat").removeClass("current-cat");
        //$("#schedaprj").empty();
        setTimeout(function () {
            $(el).addClass("current");
        }, 100);
        $("#ajprfull").empty().hide().load(link + " #ajprfull", function () {
            //alert("cat");
            $("#sprj a").eq(1).addClass("current");
            $(this).fadeIn("slow");
            $("#menu").trigger("pretty");
            $("#menu").trigger("prj");
            location = "#"+link.substr(dln);
        });
    });
    
    $("#sprj a").live("click", function (event) {
        event.preventDefault();
        var link = $(this).attr("href");
        var el = $(this);
        $("#sprj .current").removeClass("current");
        setTimeout(function () {
            $(el).addClass("current");
        }, 100);
        $("#aprj").empty().load(link + " #schedaprj", function () {
            //alert("prj");
            $("#selectprj").hide();
            $("#schedaprj").show("blind", {
                direction: "right"
            }, 600);
            $("#menu").trigger("pretty");
            $("#menu").trigger("prj");
            location = "#"+link.substr(dln);
        });
    });
    
    

    $(".hovermenu").mouseover(function () {
        $(this).oneTime(150, "fadbkg", function () {
            $(this).stop().animate({
                backgroundColor: "#C0BFBD",
                easing: ea
            }, 3 * speed);
        });
    }).mouseout(function () {
        $(this).stopTime("fadbkg");
        $(this).stop().animate({
            backgroundColor: "#E1E0DD",
            easing: ea
        }, 2 * speed);
    });
    
    $("#menu ul.subcat li.page_item a").each(function () {
        var link = $(this).attr("href");
        var el = $(this);
        $(this).click(function (event) {
        		event.preventDefault();
            $("#menu .current-cat").removeClass("current-cat");
            $(el).parents(".innermenu").find(".deco2").css({
                opacity: 0,
                left: 200
            });
            setTimeout(function () {
                $(el).addClass("current-cat");
                $(el).parents(".innermenu").find(".deco2").delay(speed).show().animate({
                    opacity: 1,
                    left: 250
                }, 600);
                $(el).parents(".innermenu").find(".pagecont").hide().load(link + " .wrapper", function () {
                    //pages hide0
                    $(this).fadeIn("slow");
                    location = "#"+link.substr(dln);
                });
            }, 100);
        });
    });
    //prj selected
    $("#menu ul.subcat li.cat-item-5 a").each(function () {
        var link = $(this).attr("href");
        var el = $(this);
        $(this).click(function (event) {
            // alert("hide1");
            event.preventDefault();
            $("#menu .current-cat").removeClass("current-cat");
            $(el).parents(".innermenu").find(".deco2").css({
                opacity: 0,
                left: 200
            });
            setTimeout(function () {
                $(el).addClass("current-cat");
                $(el).parents(".innermenu").find(".deco2").delay(speed).show().animate({
                    opacity: 1,
                    left: 273
                }, 600);
                $(el).parents(".innermenu").find(".pagecont_plist").hide();
                $(el).parents(".innermenu").find(".pagecont_sub").hide().delay(2 * speed).fadeIn("slow");
            }, 100);
        });
    });
    $("#menu ul.subcat li.cat-item-9 a").each(function () {
        var link = $(this).attr("href");
        var el = $(this);
        $(this).click(function (event) {
            //alert("hide3");
            event.preventDefault();
            $("#menu .current-cat").removeClass("current-cat");
            $(el).parents(".innermenu").find(".deco2").css({
                opacity: 0,
                left: 200
            });
            setTimeout(function () {
                $(el).addClass("current-cat");
                $(el).parents(".innermenu").find(".deco2").delay(speed).show().animate({
                    opacity: 1,
                    left: 273
                }, 600);
                $(el).parents(".innermenu").find(".pagecont").hide();
                $(el).parents(".innermenu").find(".pagecont_plist").hide().delay(2 * speed).fadeIn("slow");
            }, 100);
        });
    });
    $("#menu ul.subcat li.cat-item").not(".cat-item-5,.cat-item-9").find("a").each(function () {
        var link = $(this).attr("href");
        var el = $(this);
        $(this).click(function (event) {
        		event.preventDefault();
            $("#menu .current-cat").removeClass("current-cat");
            $(el).parents(".innermenu").find(".deco2").css({
                opacity: 0,
                left: 200
            });
            $(el).parents(".innermenu").find(".pagecont").hide();
            //alert("hide4");
            $("#menu .current-cat").removeClass("current-cat");
            setTimeout(function () {
                $(el).addClass("current-cat");
            }, 100);
      
            $("#m_cin").empty().load(link + " #aj", function () {
                $("#menu").trigger("pretty");
                $("#menu").trigger("prj");
                location = "#"+link.substr(dln);
                setTimeout(function () {$("#ajaxwrap").cycle(1);}, turn_delay);
            });
            $("#aj").hide();
        });
    });
    

    $("ul.subcat li a").click(function (event) {
        event.preventDefault();
        var h = 18;
        var idx = $(this).parent().parent().find("li a").index($(this));
        var the_top = (0 - idx * h);
        $(this).parent().parent().animate({
            top: the_top,
            easing: ea
        }, speed2);
    });
    $("ul.subsubcat li a").each(function () {
        var link = $(this).attr("href");
        var el = $(this);
        var h = 18;
        var idx = $(this).parent().parent().find("li a").index($(this));
        var the_top = 274 + (0 - idx * h);
        $(this).click(function (event) {
        		//alert("subc2");
        		event.preventDefault();
            $("#menu ul.subsubcat li").removeClass("current-cat");
            setTimeout(function () {
                $(el).parent().addClass("current-cat");
            }, 10);
            $(this).parent().parent().animate({
                top: the_top,
                easing: ea
            }, speed2);
            //alert("hide6");
            $("#m_cin").empty().load(link + " #aj", function () {
                $("#sprj a").eq(1).addClass("current");
                $("#menu").trigger("pretty");
                $("#menu").trigger("prj");
                location = "#"+link.substr(dln);
                $("#aj").hide();
                setTimeout(function () {$("#ajaxwrap").cycle(1);}, turn_delay);
            });
            
        });
    });
    //cose   
    $("#menu").bind("prj", function () {
        
        $("#sprj").mouseenter(function () {
                $("#selectprj").fadeIn(550);
        }).mouseleave(function () {
        				$("#selectprj").fadeOut(550);
        });
        
        $("#selectprj .cycle").cycle({
            fx: 'fade',
            speed: 550,
            timeout: 0
        });
        
        $("#sprj a").hover(function () {
            var idx = $("#sprj a").index($(this));
            $("#selectprj .cycle").cycle(idx);
            $("#selectprj:hidden").fadeIn(550);
         },function () {
            $("#selectprj .cycle").cycle(0);
         });
        
        $("#schedaprj .pics").cycle({
            fx: 'fade',
            speed: 550,
            timeout: 0,
            next: '#m_next',
            prev: '#m_previous',
            before: onBefore
        });
        
        $('#tendina').hide();
        $("#showall").toggle(function () {
            $('#tendina').slideUp({
                duration: 500
            });
        }, function () {
            $('#tendina').css({
                opacity: 1
            }).hide().slideDown({
                duration: 500
            });
        });
        setTimeout(function () {
            $("#showall").trigger("click");
        }, 100);
    });
    $(".piclist a").live("click", function () {
        $('#showall').trigger('click');
        var el = $(this);
        var idx = $(".piclist a").index($(this));
        if (idx >= 0) {
            $("#schedaprj .pics").cycle(idx);
        }
    });

    function onBefore() {
        var total = $('#schedaprj .pics').find("img").length;
        var idxo = $("#schedaprj .pics a").index(this);
        var idx = idxo + 1;
        $('#counter').html(idx + " / " + total);
    }

    function m_update() {
        $("#menu").trigger("update");
    }
    $("#ajaxwrap").cycle({
        fx: 'scrollHorz',
        speed: 1000,
        easing: ea,
        timeout: 0,
        after: m_update
    });
    $("#goback").live("click", function () {
        $("#aj").empty().hide();
        $("#ajaxwrap").cycle(0);
    });
    $("body").mousemove(function (e) {
        $('#follow').css({
            left: (e.pageX + 16),
            top: (e.pageY + 16)
        });
    });
    $("#menu").ajaxStart(function () {
        $('#follow').show();
    }).ajaxStop(function () {
        $('#follow').hide();
    });
    
    $("#snews").click(function(){
        $('a[href$="/category/atelier/0-news/"]').trigger("click");
    });
    
    
    $("#menu").trigger("open_menu");
 		$("#menu").trigger("pretty");
    $("#menu").trigger("prj");
    
    if (window.location.hash.length > 1) {
			page = window.location.hash.split("#")[1];
			if ($('a[href="'+page+'"]').length) {
				setTimeout(function(){
					$('a[href="'+page+'"]').trigger("click");
				},1500);
			}else{
				$("#m_cin").empty().load(page + " #aj", function () {
            ck = page.indexOf("category");
            
            if (ck>0){
            	$("#sprj a").eq(1).addClass("current");
            	$("#showall").trigger("click");
            };
            
            $("#menu").trigger("pretty");
            $("#menu").trigger("prj");
            location = "#"+page;
            setTimeout(function () { $("#ajaxwrap").cycle(1); },turn_delay);
        });
			}
		}	
    
}); //scope
