var CaseNums = 0;
var CurrCase = 1;
var CaseInterval = "";

$(document).ready(function() {
    $("#menu li a").hover(function() {
        var ThisMenu = $(this).children("b").text();
        $("#foot-menu li a b").each(function() {
            if ($(this).text() == ThisMenu) {
                $(this).parent("a").parent("li").addClass("hover");
            } else {
                $(this).parent("a").parent("li").removeClass("hover");
            }

        });
    }, function() {
        $("#foot-menu li").removeClass("hover");
    });


    var CaseHeight = $("#case-thumbs ul").height();
    $("#case-filter a").click(function() {
        if (!$(this).hasClass("selected")) {
            var cLink = this;
            $("#case-thumbs ul").animate({ opacity: "0" }, 500, function() {
                var SelVal = $(cLink).text();
                $("#case-filter a").removeClass("selected");
                $(cLink).addClass("selected");
                if (SelVal != "All") {
                    $("#case-thumbs ul li").each(function() {
                        if ($(this).hasClass(SelVal)) {
                            $(this).show();
                        } else {
                            $(this).hide();
                        }
                    });
                } else {
                    $("#case-thumbs ul li").show();
                }
                SetThird();
                $("#case-thumbs ul").animate({ opacity: "1" }, 500);
            });
        }
        return false;
    });

    $("#foot-menu li a").hover(function() {
        var ThisMenu = $(this).parent("li").children("a:first").children("b").text();
        $(this).parent("li").addClass("hover");
        $("#menu li a b").each(function() {
            if ($(this).text() == ThisMenu) {
                $(this).parent("a").parent("li").addClass("hover");
            } else {
                $(this).parent("a").parent("li").removeClass("hover");
            }

        });
    }, function() {
        $(this).parent("li").removeClass("hover");
        $("#menu li").removeClass("hover");
    });


    $("#case-imgthumbs a").click(function() {
        $("#case-imgthumbs a").removeClass("selected");
        $(this).addClass("selected");
        var GotoImg = $("#case-imgthumbs a").index($(this));
        var imgHeight = 365;
        var GotoPx = GotoImg * imgHeight;
        $("#case-imgwrapper div").animate({ top: "-" + GotoPx + "px" }, 500);
        return false;
    })

    $("#case-thumbs ul li").hover(function() {
        $(this).addClass("cview");
        $("#case-thumbs ul li").each(function() {
            if (!$(this).hasClass("cview")) {
                $(this).children("h4").children("span").css("color", "#000");
                $(this).css("opacity", "0.2");
            }
        });
    }, function() {
        $(this).removeClass("cview");
        $("#case-thumbs ul li").each(function() {
            if (!$(this).hasClass("cview")) {
                $(this).children("h4").children("span").css("color", "#797979");
                $(this).css("opacity", "1");
            }
        });
    });

    if (location.href.indexOf("/work/case/") != -1) {
        var NextL = "";
        var PrevL = "";
        var thisUrl = "/work/case/" + location.href.split("/work/case/")[1];
        $("#case-titles ul li a").each(function() {
            if ($(this).attr("href") == thisUrl) {
                NextL = $(this).parent("li").next("li").children("a").attr("href");
                PrevL = $(this).parent("li").prev("li").children("a").attr("href");

                $("#case-nav a:nth-child(1)").attr("href", NextL);
                $("#case-nav a:nth-child(2)").attr("href", PrevL);
            }
        });
        if ($("#case-nav a:nth-child(1)").attr("href").length < 3)
            $("#case-nav a:nth-child(1)").attr("href", $("#case-titles ul li:nth-child(1) a").attr("href"));

        if ($("#case-nav a:nth-child(2)").attr("href").length < 3)
            $("#case-nav a:nth-child(2)").attr("href", $("#case-titles ul li:last a").attr("href"))

        $("#case-nav span.rodeopark a").attr("href", "http://www.rodeopark.se");

    }

    $("#case-thumbs ul li p").mouseover(function() { $(this).hide(); });

    CaseNums = $("#case-images img").size();
    for (i = 1; i <= CaseNums; i++) {
        if (i == 1) {
            $("#case-counter").html($("#case-counter").html() + "<img onclick=\"SwapCase(" + i + "); clearInterval(CaseInterval);\" src=\"Images/case-sell.gif\" alt=\"\" />");
        } else {
            $("#case-counter").html($("#case-counter").html() + "<img onclick=\"SwapCase(" + i + "); clearInterval(CaseInterval);\" src=\"Images/case-null.gif\" alt=\"\" />");
        }
    }

    CaseInterval = setInterval(function() {
        CurrCase++;
        if (CurrCase > CaseNums)
            CurrCase = 1;

        SwapCase(CurrCase);
    }, 5000);
    $("#case-images img").hide().css("top", "310px");
    $("#case-images img:nth-child(1)").show().css("top", "26px");

    $("#case-texts div").hide();
    $("#case-texts div:nth-child(1)").show();

    SetThird();


    $("#cimg-next").click(function() {
        CurrCase++;
        if (CurrCase > CaseNums)
            CurrCase = 1;

        SwapCase(CurrCase);
        clearInterval(CaseInterval);
    });

    $("#cimg-prev").click(function() {
        CurrCase = CurrCase - 1;
        if (CurrCase == 0)
            CurrCase = CaseNums;

        SwapCase(CurrCase);
        clearInterval(CaseInterval);
    });

    NameVal = $("input[name='name']").val();
    EmailVal = $("input[name='email']").val();
    SetEnable();

    $("input[name='name'], input[name='email']").keyup(function() {
        NameVal = $("input[name='name']").val();
        EmailVal = $("input[name='email']").val();
        SetEnable();
    });


});

var NameVal = "";
var EmailVal = "";

function SetThird() {
	var cCount = 0;
	$("#case-thumbs ul li").removeClass("third");
	$("#case-thumbs ul li").each(function() {
		if($(this).css("display") != "none") {
			cCount++;
			if(cCount == 3) {
				$(this).addClass("third");
				cCount = 0;
			}
		}
	});
}

function SetEnable() {
    if (NameVal.length > 2 && EmailVal.length > 2) {
        $(".submit").removeAttr("disabled").css("opacity", "1")
    } else {
        $(".submit").attr("disabled", "disabled").css("opacity", "0.2")
    }
}


var DelayInt = "no";
function SwapCase(cNum) {

	if(DelayInt == "no") {
		
		$("#case-images img:nth-child(" + cNum + ")").css("z-index", "10").show().animate({ top: "26px" }, 500, function(){ $("#case-images img").css("z-index","5").hide().css("top","310px"); $("#case-images img:nth-child(" + cNum + ")").show().css("top","26px"); });
		
		$("#case-texts div").hide();
		$("#case-texts div:nth-child(" + cNum + ")").fadeIn(950);
		
		$("#case-counter").html("");
		for (i=1;i<=CaseNums;i++)
		{
			if(i == cNum) {
				$("#case-counter").html($("#case-counter").html() + "<img onclick=\"SwapCase(" + i + "); clearInterval(CaseInterval);\" src=\"Images/case-sell.gif\" alt=\"\" />");
			} else {
				$("#case-counter").html($("#case-counter").html() + "<img onclick=\"SwapCase(" + i + "); clearInterval(CaseInterval);\" src=\"Images/case-null.gif\" alt=\"\" />");
			}
		}

		DelayInt = setInterval(function() { clearInterval(DelayInt); DelayInt = "no"; }, 1050);	

	}
}
