$(document).ready(function() {
    initInputs();
    initNavigation();
    initTopNavigation();
    initTabs(".tabset-years");
    initSlider();
    initPopups();
    initValidate();

    if ($(".shadow").length > 0) {
        $("div.shadow").galleryScroll({
            duration: 500,
            circleSlide: true,
            holderList: ".years",
            step: 5
        });
    }
    if ($(".tickets-gallery").length > 0) {
        $("div.tickets-gallery").galleryScroll({
            duration: 500,
            circleSlide: true,
            step: 1,
            scrollEl: ".item",
            scrollElParent: ".slider"
        });
    }
    if ($(".gallery").length > 0) {
        $("div.gallery").galleryScroll({
            duration: 500,
            circleSlide: true,
            holderList: ".gallery-holder div"
        });
    }
    if ($(".date-pick").length > 0)
        $(".date-pick").datePicker({ startDate: '01/01/2009' });

    setTimeout("VSA_initScrollbars()", 100);
});

function initAnchor(popup) {
    var currentPosition = 0;
    var position = 0;
    var duration = 13;
    $(".anchor").each(function(id, el) {
        $(el).click(function() {
            position = $("#" + $(el).attr("title")).position();
            currentPosition = $(popup).find(".scroll-content").scrollTop();

            setTimeout(function() {
                scrolling();
            }, duration);

            var _scrollHeight = $(popup).find(".vscroll-line").height();
            var _contentHeight = $(popup).find(".popup-content").height();
            var _sliderHeight = $(popup).find(".vscroll-slider").height();


            var _scrollTop = ((position.top) * (_scrollHeight)) / _contentHeight;
            $(popup).find(".vscroll-slider").animate({ "top": _scrollTop + 5 }, 700);

            return false;
        });
    });
    $(".to-top").each(function(id, el) {
        $(el).click(function() {
            $(popup).find(".vscroll-slider").css({ "top": 17 });
            $(popup).find(".scroll-content").scrollTop(0);
            return false;
        });
    });
    function scrolling() {
        if (currentPosition <= position.top + 320) {
            currentPosition += 30;
            $(popup).find(".scroll-content").scrollTop(currentPosition + 1);
            setTimeout(function() {
                scrolling();
            }, duration);
        }
    }
}
function initInputs() {
    $("#footer input, .form-search input").each(function(_ind, _el) {
        if (_el.type == "text") {
            _el.valueHtml = _el.value;
            $(_el).focus(function() {
                if (this.valueHtml == $(this).val())
                    $(this).val("");
                $(this).parent().addClass("focus");
            });
            $(_el).blur(function() {
                this.value != "" ? this.value = this.value : this.value = this.valueHtml;
                $(this).parent().removeClass("focus");
            });
        }
    });
}
function initNavigation() {
    $('#main-nav li').hover(function() {
        $(this).addClass("hover");
    },
	function() {
	    $(this).removeClass("hover");
	});
}

function initTopNavigation() {
    $('#tickets-img').supersleight({ shim: '/Content/images/transparent.gif' });
    $('#tickets-img').supersleight();
    $('#top-nav li').hover(function() {
        $(this).addClass("hover");
    },
	function() {
	    $(this).removeClass("hover");
	});
}

function initTabs(h_list) {
    $(h_list).each(function(_ind, _el) {
        var btn_h = $(_el);
        var _btn = $(_el).find('li');
        var _a = 0;
        _btn.each(function(_ind, _el) {
            this._box = $('#' + _el.title.substr(_el.title.indexOf("#") + 1));
            if ($(_el).hasClass('active')) {
                this._box.css({ "position": "static", "left": 0, "top": 0 });
                _a = _ind;
            }
            else {
                this._box.css({ "position": "absolute", "left": "-9999px", "top": "-9999px" });
            }
            _el.onmousedown = function() {
                if (!$(this).hasClass('active')) {
                    _btn.get(_a)._box.css({ "position": "absolute", "left": "-9999px", "top": "-9999px" });
                    _btn.eq(_a).removeClass('active');
                    this._box.css({ "position": "static", "left": 0, "top": 0 });
                    $(this).addClass('active');
                    _a = _ind;
                    VSA_handleResize();
                    $(".scroll-content").scrollTop(0);
                }
                return false;
            }
        });
    });
}
var u;

function is(browser) {
    if (u.indexOf(browser) != -1) return true;
}

function doPopup(popup, overlayer, hide_fade) {
    var _top = 0;
    if (document.documentElement.scrollTop > document.body.scrollTop)
        _top = document.documentElement.scrollTop;
    else
        _top = document.body.scrollTop;

    if (hide_fade) {
        $(popup).css({ "display": "block", "opacity": 1, "top": 50 + _top, "margin-left": -$(popup).width() / 2 });
        $(overlayer).css({ "display": "block", "opacity": .5, "height": document.documentElement.scrollHeight });
    }
    else {
        $(popup).css({ "display": "block", "top": 50 + _top, "margin-left": -$(popup).width() / 2 }).animate({ opacity: 1 }, { duration: 400 });
        $(overlayer).css({ "display": "block", "height": document.documentElement.scrollHeight }).animate({ opacity: 0.5 }, { duration: 400, queue: false });
    }
    if (typeof VSA_handleResize == "function") {
        setTimeout(function() { VSA_handleResize(); $(".scroll-content").scrollTop(0); }, 100);
    }
    initAnchor(popup);
}

function initPopups() {

    var overlayer = $(".overlayer");
    var popups = $(".popup");
    $(popups).css({ "display": "none" }).css({ "opacity": 0 });
    $("a.popup-opener").unbind();
    $("a.popup-opener").click(function() {

        $(overlayer).css({ "opacity": 0 });
        var popup = $("#" + this.rel);
        var popup_content = $("#" + this.rel + "-content");

        var loadurl = "";

        if (this.href.substring(this.href.length - 1) == "/") {
            loadurl = this.href + "true";
        }
        else {
            loadurl = this.href + "/true";
        }

        var is_sync_popup = false;

        if (is_sync_popup) {
            var popup = $("#" + this.rel);
            doPopup(popup, overlayer, false);
            return false;
        }
        else {
            popup_content.load(loadurl, null, function() {
                doPopup(popup, overlayer, false);
            });
        }
        return false;
    });

    $("a[title='popup-video']").unbind();
    $("a[title='popup-video']").click(function() {

        $(overlayer).css({ "opacity": 0 });
        var popup = $("#popup-video");
        var popup_content = $("#popup-video-content");

        var loadurl = "";

        if (this.href.substring(this.href.length - 1) == "/") {
            loadurl = this.href + "true";
        }
        else {
            loadurl = this.href + "/true";
        }

        var is_sync_popup = false;

        if (is_sync_popup) {
            var popup = $("#popup-video");
            doPopup(popup, overlayer, false);
            return false;
        }
        else {
            popup_content.load(loadurl, null, function() {
                doPopup(popup, overlayer, false);
            });
        }
        return false;
    });

    $("a.btn-close").unbind();
    $("a.btn-close").click(function() {
        $(popups).css({ "display": "none" }).animate({ "opacity": 0 });
        $(overlayer).animate({ "opacity": 0 }, 400, function() { $(overlayer).css({ "display": "none" }); });
        return false;
    });
}


function initSlider()
{
    $("div.range-box").each(function() 
	{
	    var d = new Date();
	    var curr_year = d.getFullYear();


		var _holder = $(this);
		var _slider = _holder.find('div.slider-holder');

		var _valMin = _holder.find('.val-min');
		var _valMax = _holder.find('.val-max');

		var _curValMin = parseInt(_valMin.html());
		var _curValMax = parseInt(_valMax.html());

		var _inputMin = _holder.find('input.input-min');
		var _inputMax = _holder.find('input.input-max');
		
		_slider.slider({
			range: true,
			min: 1959,
			max: curr_year,
			values: [1959, curr_year],
			slide: function(event, ui) {
				$(".ui-slider-handle").eq(0).html(ui.values[0]);
				$(".ui-slider-handle").eq(1).html(ui.values[1]);
				_valMax.html(ui.values[1]);

				_inputMin.val(ui.values[0]);
				_inputMax.val(ui.values[1]);
			}
		});
		
		$(".ui-slider-handle").eq(0).html("1959");
		$(".ui-slider-handle").eq(1).html(curr_year);
	});
}
function initValidate() {
    if ($(".validate-form").length > 0) {
        $(".validate-form").validate({
            invalidHandler: function(e, validator) {
                var errors = validator.numberOfInvalids();
                if (errors > 0)
                    $("div.errors-holder").show();
                else
                    $("div.errors-holder").hide();
            },
            onkeyup: false,
            submitHandler: function() {
                $("div.errors-holder").hide();
                //define this method on any form submission pages...
                submitForm();
               
            }
        });
        $("input.phone").mask("999-999-9999");
    }
}
