﻿var CurrTour = 1;
var TourNums = 0;
var CurrBg = 1;
var winHeight = 0;
var winCompHeight = 0;
window.onload = function() {
    getWinHeight();
}

$(window).bind("resize", function() {
    getWinHeight();
});

function getWinHeight() {
    if (jQuery.browser.msie) {
        winHeight = document.documentElement.offsetHeight;
    } else {
        winHeight = window.innerHeight;
    }

    winCompHeight = winHeight;

    winHeight = winHeight - 100;

    SetImageNav();
}

var LogoTimer = "";

function HideLogo() {

    if ($.browser.msie) {
        $("#big-logo").hide();
        $("#imgnav").show();
    } else {
        $("#big-logo").fadeOut(1000, function() { $("#imgnav").fadeIn(1000); });
    }
    clearInterval(LogoTimer);
}

function SetImageNav() {
    $("#imgnav").css("top", Math.round(winHeight / 2) - 100 + "px");
    
    $("#big-logo").css("top", Math.round(winHeight / 2) - 100 + "px");
    if ($.browser.msie) {
        $("#big-logo").show();
    } else {
        $("#big-logo").fadeIn();
    }

    clearInterval(LogoTimer);
    LogoTimer = setInterval(function() { HideLogo(); }, 4000);
    

    $("#bgcontainer div:nth-child(1)").css("height", winHeight + "px");
    SetScrollHeight();
    if ($("#content-wrapper").html().length > 3) {
        $("#content-wrapper").css("height", winHeight + "px");
    } else {
        $("#content-wrapper").css("height", "0px");
    }
}

function SetScrollHeight() {
    $("#singlecol, #twocol, .jScrollPaneContainer").css("height", (winHeight - 150) + "px");
}

var TourTimer = setInterval(function() { NextTour(); }, 3000);
var ImageTimer = setInterval(function() { NextImg(); }, 5000);

$(document).ready(function() {

    $("#menu ul li.about ul li a").click(function() { var eThis = this; $("#content-wrapper").animate({ height: "0px" }, 400, function() { BindLoad(eThis); return false; }); });
    $("#menu ul li.about ul").css("height", "0px").css("overflow", "hidden");

    $("a.lightbox").lightBox();

    $("input[type='text']").focus(function() {
        if (this.value == this.defaultValue) this.value = "";
    });
    $("input[type='text']").blur(function() {
        if (this.value == "") this.value = (this.defaultValue ? this.defaultValue : "");
    });

    $(document).keyup(function(eh) {
        var keycode = "";
        var keyp = "";
        if (eh == null) {
            keycode = event.keyCode;
        } else {
            keycode = eh.keyCode;
        }
        keyp = String.fromCharCode(keycode).toLowerCase();
        
        if ($("#imgnav").css("display") != "none") {
            if (keyp == "'") { NextImg(); clearInterval(ImageTimer); }
            if (keyp == "%") { PrevImg(); clearInterval(ImageTimer); }
        }
    });


    function getKey(key) {
        if (key == null) {
            keycode = event.keyCode;
        } else {
            keycode = key.keyCode;
        }
        return String.fromCharCode(keycode).toLowerCase();
    }

    $("#nextdate").click(function() { NextTour(); clearInterval(TourTimer); });
    $("#prevdate").click(function() { PrevTour(); clearInterval(TourTimer); });

    $("#nextimg").click(function() { NextImg(); clearInterval(ImageTimer); });
    $("#previmg").click(function() { PrevImg(); clearInterval(ImageTimer); });

    $("#menu ul li.about a").click(function() {
        if ($(this).parent("li").children("ul").css("height") == "0px") {
            $(this).parent("li").children("ul").animate({ height: "140px" }, 300);
        } else {
            $(this).parent("li").children("ul").animate({ height: "0px" }, 300);
        }
        return false;
    });
    BindLinks();

    $("#bgcontainer, #content-wrapper, h1, #menu ul li.about ul li a").click(function() { $("#menu ul li.about ul").animate({ height: "0px" }, 300); });

    TourNums = $("#tourdates ul li").size();

    var zIn = 20;
    $("#bgcontainer div").each(function() {
        $(this).css("z-index", zIn);
        zIn = zIn - 1;
    });

});


function NextImg() {
    $("#imgnav").hide();
    $("#bgcontainer div:nth-child(2)").animate({ height: winHeight + "px" }, 500);
    $("#bgcontainer div:nth-child(1)").animate({ height: "0px" }, 500, function() {
        $("#bgcontainer div:nth-child(1)").clone().appendTo("#bgcontainer");
        $("#bgcontainer div:nth-child(1)").remove();

        $("#bgcontainer div:nth-child(1)").css("z-index", "20");
        $("#bgcontainer div:nth-child(2)").css("z-index", "19").css("height", winCompHeight + "px");
        $("#bgcontainer div:nth-child(3)").css("z-index", "18").css("height", winCompHeight + "px");
        $("#bgcontainer div:nth-child(4)").css("z-index", "17").css("height", winCompHeight + "px");
        $("#bgcontainer div:nth-child(5)").css("z-index", "16").css("height", winCompHeight + "px");
        $("#bgcontainer div:nth-child(6)").css("z-index", "15").css("height", winCompHeight + "px");
        $("#bgcontainer div:nth-child(7)").css("z-index", "14").css("height", winCompHeight + "px");
        $("#bgcontainer div:nth-child(8)").css("z-index", "13").css("height", winCompHeight + "px");
        $("#bgcontainer div:nth-child(9)").css("z-index", "12").css("height", winCompHeight + "px");
        $("#bgcontainer div:nth-child(10)").css("z-index", "11").css("height", winCompHeight + "px");
        $("#bgcontainer div:nth-child(11)").css("z-index", "10").css("height", winCompHeight + "px");
        $("#bgcontainer div:nth-child(12)").css("z-index", "9").css("height", winCompHeight + "px");

        if ($.browser.msie) {
            $("#imgnav").show();
        } else {
            $("#imgnav").fadeIn();
        }
    });
}

function PrevImg() {
    $("#imgnav").hide();
    $("#bgcontainer div:nth-child(12)").css("z-index","19").animate({ height: winHeight + "px" }, 500);
    $("#bgcontainer div:nth-child(1)").animate({ height: "0px" }, 500, function() {
        $("#bgcontainer div:nth-child(12)").clone().prependTo("#bgcontainer");
        $("#bgcontainer div:nth-child(13)").remove();
        
        $("#bgcontainer div:nth-child(1)").css("z-index", "20");
        $("#bgcontainer div:nth-child(2)").css("z-index", "19").css("height", winCompHeight + "px");
        $("#bgcontainer div:nth-child(3)").css("z-index", "18").css("height", winCompHeight + "px");
        $("#bgcontainer div:nth-child(4)").css("z-index", "17").css("height", winCompHeight + "px");
        $("#bgcontainer div:nth-child(5)").css("z-index", "16").css("height", winCompHeight + "px");
        $("#bgcontainer div:nth-child(6)").css("z-index", "15").css("height", winCompHeight + "px");
        $("#bgcontainer div:nth-child(7)").css("z-index", "14").css("height", winCompHeight + "px");
        $("#bgcontainer div:nth-child(8)").css("z-index", "13").css("height", winCompHeight + "px");
        $("#bgcontainer div:nth-child(9)").css("z-index", "12").css("height", winCompHeight + "px");
        $("#bgcontainer div:nth-child(10)").css("z-index", "11").css("height", winCompHeight + "px");
        $("#bgcontainer div:nth-child(11)").css("z-index", "10").css("height", winCompHeight + "px");
        $("#bgcontainer div:nth-child(12)").css("z-index", "9").css("height", winCompHeight + "px");

        if ($.browser.msie) {
            $("#imgnav").show();
        } else {
            $("#imgnav").fadeIn();
        }
    });
}


function NextTour() {
    $("#tourdates ul li:nth-child(" + CurrTour + ")").fadeOut(500, function() {
        $("#tourdates ul li:nth-child(" + CurrTour + ")").addClass("noshow");
        if ((CurrTour + 1) > TourNums)
            CurrTour = 1;
        else
            CurrTour++;

        $("#tourdates ul li:nth-child(" + CurrTour + ")").removeClass("noshow").fadeIn();
    });
}

function PrevTour() {
    $("#tourdates ul li:nth-child(" + CurrTour + ")").fadeOut(500, function() {
        $("#tourdates ul li:nth-child(" + CurrTour + ")").addClass("noshow");
        if ((CurrTour - 1) == 0)
            CurrTour = TourNums;
        else
            CurrTour = CurrTour - 1;

        $("#tourdates ul li:nth-child(" + CurrTour + ")").removeClass("noshow").fadeIn();
    });
}

function IfEmpty() {
    if ($("#content-wrapper").html().length < 3) {
        $("#content-wrapper").hide();
    } else {
    $("#content-wrapper").animate({ height: winHeight + "px" }, 400);
    }
}

function BindLinks() {
    IfEmpty();
    $("#imgthumbs a").click(function() {
        var ImgSrc = $(this).attr("href");
        $("#imgviewer").html("<img height=\"261\" width=\"250\" src=\"" + ImgSrc + "\" alt=\"\" />");
        return false;
    });
 
    var numFanImgs = $(".fan-cont").size();
    var currFanImg = 1;
    $("#img-nums").html(currFanImg + " / " + numFanImgs);
    $("a#next-fan-img").click(function() {
        currFanImg++;
        if (currFanImg > numFanImgs) {
            currFanImg = 1;
        }
        $(".fan-cont").hide();
		 
        $(".fan-cont:nth-child(" + currFanImg + ")").show();
	
        $("#img-nums").html(currFanImg + " / " + numFanImgs);
        return false;
    });
    $("a#prev-fan-img").click(function() {
        currFanImg = currFanImg - 1;
        if (currFanImg < 1) {
            currFanImg = numFanImgs;
        }
        $(".fan-cont").hide();
		
        $(".fan-cont:nth-child(" + currFanImg + ")").show();
		
        $("#img-nums").html(currFanImg + " / " + numFanImgs);
        return false;
    });
    

    $(".team-sub, .media-sub").hide();
    $(".team-sub:nth-child(1), .media-sub:nth-child(1)").show();

    $("#sub-team ul li a, #sub-media ul li a").click(function() {
        var UrlID = "#" + $(this).attr("href").split("#")[1];
        $(".team-sub, .media-sub").hide();
        $(UrlID).fadeIn();
        $("#sub-team ul li, #sub-media ul li").removeClass("selected");
        $(this).parent("li").addClass("selected");
        if ($(this).hasClass("photocatlink")) {
            LoadGalImages($(this).attr("rel"));
        }
        $("#singlecol, #twocol, #sub-scroll").jScrollPane({ showArrows: true, scrollbarWidth: 13 });
        return false;
    });

    $("#close").css("display", "inline-block");

    //$("a#close, h1 a").click(function() { var eThis = this; $("#content-wrapper").animate({ height: "0px" }, 400, function() { BindLoad(eThis); return false; }); return false; });
    $("a#close, h1 a").click(function() { CloseWindow(); return false; });

    $("#singlecol, #twocol, #sub-scroll").css("height", (winHeight - 365) + "px");
    $("#singlecol, #twocol, #sub-scroll").jScrollPane({ showArrows: true, scrollbarWidth: 13 });

    $("#form-contact-submit").click(function() {
        SendMail();
        return false;
    });
}

function BindLoad(element) {
    var lTarget = $(element).attr("href");
    $("#content-wrapper").load(lTarget + " #content", function() { BindLinks(); });
    return false;
}

function ToFanPhotos() {
    var lTarget = "/Media";
    $("#content-wrapper").load(lTarget + " #content", function() { BindLinks(); });
    return false;
}

function ToPage(whatpage) {
    if (whatpage.substring(0, 1) != "index.html") { whatpage = "/" + whatpage; }
    $("#content-wrapper").animate({ height: "0px" }, 400, function() { $("#content-wrapper").load(whatpage + " #content", function() { BindLinks(); }); return false; });    
    return false;
}


function CloseWindow() {
    $("#content-wrapper").animate({ height: "0px" }, 400, function() { $("#content-wrapper").html(""); });
}

function LoadGalImages(catID) {
    $.ajax({
        url: "/Gallery/" + catID,
        success: function(data) {
            $("#PhotosList").html(data.split("<div id=\"theimages\">")[1].split("</div>")[0]);
            $("#PhotosList a.lightPop").lightBox();
        }
    });    
    return false;
}

function PopUP(nhref, ntitle, nwidth, nheight) {
    window.open(nhref, ntitle, "location=1,status=1,scrollbars=1,width=" + nwidth + ",height=" + nheight);
}


function SendMail() {
    
    $.ajax({
        url: "/SendContact",
        type: "POST",
        data: {
            subject: $("select[name='subject']").val(),
            email: $("input[name='email']").val(),
            name: $("input[name='name']").val(),
            message: $("textarea[name='message']").val().replace(/\n/g, "[br]")
        },
        success: function(feedback) {
            var rPon = "";
            if (feedback == "Sent") {
                rPon = "Your message has been sent!";
                $("<div class=\"message\">" + rPon + "</div>").insertAfter("form");
            } else {
                rPon = "Unable to send message. Please try again.";
                $("<div class=\"message err\">" + rPon + "</div>").insertAfter("form");
            }            
            $("form").remove();
        },
        error: function(xhr, ajaxOptions, thrownError) {
            $("<div class=\"message err\">" + xhr.status + "</div>").insertAfter("form");
            $("form").remove();
        }
    });
}


