$(function () {

    //Math.floor(Math.random()*11)
    var defstore = location.href.split("?");
    defstore = defstore.length > 1 ? defstore[1] : null;

    if (defstore != null) {
        defstore = defstore.split("sId=");
        defstore = defstore.length > 1 && defstore[1] != "" ? parseInt(defstore[1]) : null;
    }

    if (defstore != null)
    //$("#store"+defstore+".storedetails").appendTo("#storedetails").show();
        ShowStore("store" + defstore);
    else {
        var store = $(".storedetails").eq(Math.floor(Math.random() * $(".storedetails").size()));
        if (store.size() == 1) ShowStore(store.attr("id"));
    }

    $("#content").css("min-height", $("#navigation").outerHeight(true));

    $("a.register").click(function (evt) {

        if (!$(this).hasClass("disabled")) {
            var val = $(this).siblings("table").find(":radio:checked").val();

            if (val != undefined) {
                var loc = location.href.indexOf("#") > -1 ? location.href.substring(0, location.href.indexOf("#")) : location.href;
                location.href = "http://www.fcpevents.com/Register.aspx?etId=" + val + "&r=" + escape(loc + "#e_anchor" + $(this).attr("rel"));
            }
            else
                alert("You have not selected the time you wish to attend for this event.");
        }

        return false;

    });

    $("a.expand").click(function (evt) {

        if ($(this).hasClass("open")) {
            $(this).siblings(".hide").slideUp("slow");
            $(this).removeClass("open");
        } else {
            $(this).siblings(".hide").slideDown("slow");
            $(this).addClass("open");
        }

        return false;

    });

    $(".rotate").each(function (idx) {

        var rotate = $(this);

        rotate.children(".rotateitem:first").addClass("active");

        if (rotate.children(".rotateitem").size() > 1) {

            rotate.everyTime(8000, "rotate", function () {

                rotate.children(".active").fadeOut("slow", function () {

                    $(this).removeClass("active");

                    if ($(this).next().size() > 0)
                        $(this).next().fadeIn("slow", function () { $(this).addClass("active"); });
                    else
                        $(this).prevAll(".rotateitem:last").fadeIn("slow", function () { $(this).addClass("active"); });

                });

            });

        }

    });

    $(".crosslink").click(function () {

        var split = $(this).attr("href").split("#");
        var panel = split[1];
        var panelId = panel.split("-")[0];
        var panelNum = panel.split("-")[1];

        if ($($(this).attr("href")).size() > 0) {
            panel = $($(this).attr("href"));
        } else if (isNaN(panelId)) {
            panel = $("#" + panelId + ">.panelswrapper>.panel").eq(parseInt(panelNum));
        } else {
            panel = $(".panels:eq(" + panelNum + ">.panelswrapper>.panel").eq(parseInt(panelNum));
        }

        if (!panel.is(".active") || split[0] == "") {

            var move = panel.parent().children(":first").offset().left - panel.offset().left;

            panel.siblings(".active").removeClass("active");
            panel.addClass("active");

            $(this).siblings(".active").removeClass("active");
            $(this).addClass("active");

            panel.parent().animate({
                left: move,
                easing: "easeInOut"
            }, 750);

            return false;

        }

    });

    $(".panels").wrapInner("<div class='panelswrapper'></div>");

    $(".panels").each(function (idx) {

        var width = 0;
        var height = 0;
        var start = 0;

        var oldwidth = $(this).width();

        $(this).find(">.panelswrapper>.panel").each(function (idx) {

            if ($(this).outerHeight(true) > height)
                height = $(this).outerHeight(true);

            $(this).css("left", width);
            $(this).width(oldwidth);
            if ($(this).hasClass(".active"))
                start = width;

            width += $(this).outerWidth(true);

        });


        $(this).find(">.panelswrapper").width(width);
        $(this).find(">.panelswrapper").height(height);
        $(this).find(">.panelswrapper").css("left", -start);
        $(this).find(">.panelswrapper>.panel").css("position", "absolute");
        $(this).width(oldwidth);

        //if ($(this).find(".panelswrapper .panel.active").size() == 0)
        //    $(this).find(".panelswrapper .panel").eq(0).addClass("active");
        $(this).find(">.panelswrapper>.panel").eq(0).addClass("active");

        $(".crosslink.active").click();

    });

    $(".panels").each(function (idx) {

        var height = 0;

        $(this).find(">.panelswrapper>.panel").each(function (idx) {

            if ($(this).outerHeight(true) > height)
                height = $(this).outerHeight({ margin: true });

        });

        $(this).find(">.panelswrapper").height(height);

    });

    $(".module-container").each(function (idx) {

        var $container = $(this);
        var $itemContainer = $container.find(".items");
        var $items = $itemContainer.children(".item");
        var $leader = $container.find(".leader");
        var $selector = $container.find(".group-selector");

        $items.find("a.extended-show").click(function () {

            var $item = $(this).closest(".item");
            var $extended = $item.children(".extended");

            $extended.stop().show().css({

                top: -$extended.outerHeight()

            }).animate({

                top: 0

            }, 500);

        });

        $items.find("a.extended-close").click(function () {

            var $item = $(this).closest(".item");
            var $extended = $item.children(".extended");

            $extended.stop().animate({

                top: -$extended.outerHeight()

            }, 500);

        });

        $selector.click(function () {

            $selector.addClass("open");

        }).find("li>a").bind("click autoclick", function (evt) {

            if ($selector.is(".open") || evt.type == "autoclick") {

                var $link = $(this);
                var $parent = $link.closest("li");

                if (!$link.is(".active")) {

                    $parent.addClass("active").siblings(".active").removeClass("active");
                    $selector.find(".selected>span").text($link.text());

                    if ($link.attr("rel")) {

                        $items.filter(":not(." + $link.attr("rel") + ")").hide();
                        $items.filter("." + $link.attr("rel")).show();

                    } else {

                        $items.show();

                    }

                    $itemContainer.find(".h-spacer").remove();

                    var $visibleItems = $items.filter(":visible");

                    for (var i = 2; i < $visibleItems.length; i += 3)
                        $visibleItems.eq(i).after("<div class='spacer h-spacer'/>");

                    $leader.find(".title").html($parent.find(".title").html());
                    $leader.find(".content").html($parent.find(".content").html());

                    $selector.removeClass("open");

                    return false;

                }

            } else {

                $selector.addClass("open");
                return false;

            }

        });

        if (window.location.hash)
        //$selector.find("a[rel='" + window.location.hash.substring(1) + "']").click();
            $selector.find("li>a[rel='" + window.location.hash.substring(1) + "']").trigger("autoclick");

    });

    // Text box masks

    $("input[type=text].masked").each(function (idx) {

        $(this).val($(this).attr("title"));

        $(this).bind("focus", function () {

            if ($(this).val() == $(this).attr("title")) {
                $(this).val("");
                $(this).addClass("active");
            }

        });

        $(this).bind("blur", function () {

            if ($(this).val() == "") {
                $(this).val($(this).attr("title"));
                $(this).removeClass("active");
            }

        });

    });

    function CreateLightbox() {

        if ($("#lightboxwrapper").size() == 0) {
            $("body").prepend("<div id='lightboxwrapper'></div><div id='lightboxholder'><a href='javascript:void(0)' id='lightboxclose'>close</a><div id='lightboxcontent'></div></div>");

            $("#lightboxwrapper, #lightboxclose").click(function () {
                $("#lightboxwrapper").hide();
                $("#lightboxholder").hide();

                if ($("#lightboxholder").attr("rel") == "")
                    $("#lightboxcontent").empty();
                else
                    $("#lightboxcontent").children().appendTo($("body")).hide();

                $(".flash, select").css("visibility", "visible");

            });

            $("#lightboxwrapper").css("opacity", 0.5);
        }

        //$("#lightboxwrapper").width($(document).width());
        $("#lightboxwrapper").height($(document).height());

        $("#lightboxcontent").empty();
        $("#lightboxholder").width("auto");
        $("#lightboxholder").height("auto");

    }

    $(".lightbox").click(function () {

        CreateLightbox();

        var lbw = $("#lightboxwrapper");
        var lbh = $("#lightboxholder");
        var lbc = $("#lightboxcontent");

        function ShowContent() {

            $(".flash, select").css("visibility", "hidden");

            var marginw = parseInt(lbh.css("margin-left")) + parseInt(lbh.css("margin-right"));
            var marginh = parseInt(lbh.css("margin-top")) + parseInt(lbh.css("margin-bottom"));
            var borderw = parseInt(lbh.css("border-left-width")) + parseInt(lbh.css("border-right-width"));
            var borderh = parseInt(lbh.css("border-top-width")) + parseInt(lbh.css("border-bottom-width"));
            var endw = lbh.width();
            var endh = lbh.height();

            var top = params['y'] == null ? Math.floor(($(window).height() / 2) - ((endh + borderh) / 2)) + $(window).scrollTop() : parseInt(params['y']);
            var left = Math.floor(($("body").width() / 2) - ((endw + borderw) / 2));

            if (top < 30) top = 30;

            if (params['noanim'] == null) {

                lbh.width(250);
                lbh.height(100);
                lbh.css("left", Math.ceil(($("body").width() / 2) - 125 - borderw));
                lbh.css("top", Math.ceil(($(window).height() / 2) - 50 - borderh) + $(window).scrollTop());
                lbh.css("opacity", 0).show();

                lbh.stop(true).animate({
                    opacity: 1,
                    left: left,
                    width: endw,
                    top: top,
                    height: endh
                }, {

                    duration: 600,
                    complete: function (evt) { $(this).css("opacity", ""); }

                });

            } else {

                lbh.css({
                    opacity: "",
                    left: left,
                    width: endw,
                    top: top,
                    height: endh
                });

                lbh.show();

            }

        }

        var paramsArr = $(this).attr("rel") ? $(this).attr("rel").split(",") : [];
        var params = new Array();

        for (var i = 0; i < paramsArr.length; i++) {
            var param = paramsArr[i].split("=");
            params[param[0]] = param[1];
        }

        if (params['hideholder'] == null)
            lbw.show();
        else
            lbw.hide();
        lbh.hide();

        if (params['clear'] != null) { lbw.addClass("clear"); lbh.addClass("clear"); }
        else { lbw.removeClass("clear"); lbh.removeClass("clear"); }

        if ($(this).attr("href").indexOf("#") != 0 &&
			$(this).attr("href").toLowerCase().indexOf(".jpg") < 0 &&
			$(this).attr("href").toLowerCase().indexOf(".gif") < 0 &&
			$(this).attr("href").toLowerCase().indexOf(".png") < 0) {

            lbh.attr("rel", "");
            lbh.width(params["width"] != null ? parseInt(params["width"]) : 940);
            lbh.height(params["height"] != null ? parseInt(params["height"]) : $(window).height() * .85);
            lbc.html("<iframe src='" + $(this).attr("href") + "' width='" + lbh.width() + "' height='" + lbh.height() + "' style='border-width: 0' frameborder='0'></iframe>");

            ShowContent();
        } else if ($(this).attr("href").toLowerCase().indexOf(".jpg") > -1 ||
			$(this).attr("href").toLowerCase().indexOf(".gif") > -1 ||
			$(this).attr("href").toLowerCase().indexOf(".png") > -1) {

            lbh.attr("rel", "");

            var img = new Image();
            $(img).hide().appendTo(lbc).load(function () {
                ShowContent();
            }).attr("src", $(this).attr("href")).css("display", "block"); ;


        } else {

            lbh.attr("rel", "save");
            $($(this).attr("href")).appendTo(lbc).show();

            ShowContent();

        }

        return false;

    });

    var deflightbox = location.href.split("#");

    if (deflightbox.length > 1 && deflightbox[1].length) {

        deflightbox = deflightbox[1];
        $("a.lightbox[href$=" + deflightbox + "]").first().click();

    }

    $("#contestslegend a").each(function (idx) {
        $(this).click(function () { ShowContest(idx); return false; });
    });

    function ShowContest(idx) {
        $("#contestslist .item").hide();
        $("#contestslist .item:eq(" + idx + ")").show();
    }

    if ($("#contestslegend a:not(.contest-no-lightbox)").size() > 0) {

        $("#contestslegend a:not(.contest-no-lightbox)").eq(Math.floor(Math.random() * $("#contestslegend a:not(.contest-no-lightbox)").size())).click();

        if ($("#lightboxwrapper").size() == 0 && location.href.indexOf("fcpfirst.com") > -1 && $.cookie("autoshowcontest") == null) {

            var item = $("#contestslist .item:eq(" + Math.floor(Math.random() * $("#contestslist .item a").size()) + ") a");
            item.attr("rel", item.attr("rel") + ",y=150,clear=true,noanim=true");
            item.click();
            item.attr("rel", item.attr("rel").replace(",y=150,clear=true,noanim=true"));

            $.cookie("autoshowcontest", "false", { domain: 'fcpfirst.com' })

        }

    }

    var $window = $(window);
    var $backToTop = $("#back-to-top");

    $backToTop.click(function () {

        var $target = $($backToTop.attr("href"));

        if ($window.scrollTop() > $target.offset().top) {

            $.scrollTo($target, 500);

        }

        return false;

    });

});

function eventsRotate(timeout, myFunction) {

	clearTimeout(eventRotateTimeout);

	if (isNaN(timeout) || timeout == 0)
		timeout = eventsTime;

	eventRotateTimeout = setTimeout(myFunction, timeout);

}

function eventsPrevious() {

	clearTimeout(eventRotateTimeout);

	var current = $("ul#eventsflash li.item.active");
	var prev = current.prev("li.item").size() == 0 ? current.siblings("ul#eventsflash li.item:last") : current.prev("li.item");

	prev.show().css("opacity", "").addClass("next");

	if ($.browser.msie && $.browser.version < 7) {
		current.removeClass("active").hide();
		prev.addClass("active");
		eventsRotate(parseInt(prev.attr("rel")), eventsPrevious);
	} else {
		current.addClass("animating").fadeOut(1250, function () { $(this).removeClass("animating").removeClass("active").hide(); eventsRotate(parseInt(prev.attr("rel")), eventsPrevious); });
		prev.removeClass("next").addClass("active");
	}

	return false;

}

function eventsNext() {

	clearTimeout(eventRotateTimeout);

	var current = $("ul#eventsflash li.item.active");
	var next = current.next("li.item").size() == 0 ? current.siblings("ul#eventsflash li.item:eq(0)") : current.next("li.item");

	next.show().css("opacity", "").addClass("next");

	if ($.browser.msie && $.browser.version < 7) {
		current.removeClass("active").hide();
		next.addClass("active");
		eventsRotate(parseInt(next.attr("rel")), eventsNext);
	} else {
		current.addClass("animating").fadeOut(1250, function () { $(this).removeClass("animating").removeClass("active").hide(); eventsRotate(parseInt(next.attr("rel")), eventsNext); });
		next.removeClass("next").addClass("active");
	}

	return false;

}

function eventsPause() {
	clearTimeout(eventRotateTimeout);	
	
	return false;
}
