//	pre-render scripts
$(document).ready(function () {
    //Text Tile
    $('.texttile').each(function () {
        setTileHeight($(this));
    });

    $('.xForm').each(function () {
        setTileHeight($(this));
    });
    //Remove height from xForm comment
    $('.comment-form .xForm').css({ height: "auto" });

    var searchMap = $(".search-map");
    if (searchMap.length == 0) {
        $("#map_canvas").hide();
    }
    else if (searchMap.length > 0) {
        var footer = $("#footer");
        footer.css("position", "fixed");
        footer.css("bottom", "0");
    }

    //Special case for blog detail page
    var eightColumn = $(".row > .vt-ten > .row > .vt-eight")
    //If we have an 8-2 sublayout inside a 10-wide column
    eightColumn.each(function (index) {
        var row = eightColumn.closest(".vt-ten");
        row.addClass('cell-bg');
    });

    //xForm
    var xformInput = $(".xForm input[type='submit']");
    if (xformInput)
        xformInput.addClass("xSubmit");
    var xformValidator = $(".xForm .xformvalidator");
    if (xformValidator)
        xformValidator.prevAll().addClass("xformvalidator");

    // Print
    $('.printpage').click(function () {
        window.print();
    });


    // Club profile
    $('.club-profile').find('#btnEdit').click(function (e) {
        e.preventDefault();
        $('.club-profile-input').find('input').removeAttr('disabled');
        $('.club-profile-input').find('select').removeAttr('disabled');

        $(this).hide();
        $('.club-profile').find('.btnSave').show();
        $('.club-profile').find('.btnReset').show().click(function () { window.location.reload(); });

        $('.club-change-password-link').show();
    });

    // Club close account
    $(".club-close-account-link").fancybox({
        'overlayColor': '#000',
        'href': '#club-close-account-popout',
        'onStart': function () {
            $("#club-close-account-popout-input").show();
            $("#btnSave").attr("disabled", "disabled");
        },
        'onClosed': function () {
            $("#btnSave").removeAttr("disabled");
        }
    });

    // Club change password
    $(".club-change-password-link").fancybox({
        'overlayColor': '#000',
        'href': '#club-change-password-popout',
        'onStart': function () {
            $("#club-change-password-popout-input").show();
            $("#btnSave").attr("disabled", "disabled");
        },
        'onClosed': function () {
            $("#btnSave").removeAttr("disabled");
        }
    });
});

function setTileHeight(tile) {
    var parent = tile.parent('#lookbookgrid');
    if (parent.length == 0) {
        var height = tile.height();
        var tileHeight = 265;
        var diff = height / tileHeight;

        var diffRounded = Math.round(diff * 100) / 100;

        if (diffRounded != 1) {
            var newHeight = height;
            if (diff < 1) {
                var add = Math.ceil(((1 - (height / tileHeight))) * (tileHeight / 100) * 100)
                newHeight = height + add;
            }
            else {
                if (diffRounded > 2) {
                    tile.css('overflow-y', 'auto');
                    tile.css('overflow-x', 'hidden');
                    tile.css('padding-right', '20px');
                    newHeight = tileHeight * 2;
                }
                else

                    newHeight = Math.ceil(diff) * tileHeight + 1;
            }
            tile.height(newHeight);

        }
    }
}

function RemoveKeyFromUrl(key) {
    var currentFilter = window.location.hash;
    var newFilter = "";
    key = key.replace(/ /gi, '');
    if (currentFilter && currentFilter.length > 1) {
        var filters = currentFilter.substring(1, currentFilter.length).split('&');
        for (var i = 0; i < filters.length; i++) {
            var filter = filters[i].split('=');
            if (filter && filter.length == 2 && filter[0] != key) {
                newFilter += filter[0] + "=" + filter[1] + "&";
            }
        }
    }

    if (newFilter.substring(newFilter.length - 1) == "&")
        newFilter = newFilter.substring(0, newFilter.length - 1)

    window.location.hash = newFilter;
}

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\#&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}

var companys = companys || {};

companys.dropdown = (function () {

    var contSelector = '.navigation ul.hnav';

    // private vars
    var $cont,
		t;

    var init = function () {
        $cont = $(contSelector);

        $('.navigation > ul.hnav').css("display", "block");
        $('ul.hnav > li > ul').css({
            display: 'block',
            position: 'absolute',
            left: '0px',
            top: '220px',
            width: '472px',
            height: '135px',
            backgroundColor: 'black',
            float: 'left',
            opacity: 0.9
        });

        $("ul.hnav > li > ul", $cont).each(function () {
            var h = $(this).height();
            $(this).data('height', h);
        });

        $("ul.hnav > li > ul").css({
            display: 'none',
            height: 0
        });

        $("ul.hnav > li > ul > li > a > span").each(function () {
            var pos = $(this).parent().parent().parent().parent().position(),
                posleft = Math.round(pos.left - 10) + 1,
                postop = /msie|MSIE 8/.test(navigator.userAgent) ? -5 : 10;

            $(this).css("margin", postop + "px 0px 0px " + posleft + "px");
        });

        $(contSelector + " > li > a")
			.mouseover(function () {
			    if ($(this).hasClass('active')) return;
			    closeActive();
			    var ul = $(this)
					.addClass('active')
					.siblings("ul.hnav > li > ul");
			    if (/msie|MSIE 6/.test(navigator.userAgent)) {
			        ul
					.css({
					    display: 'block',
					    height: '135px'
					})
			    }
			    else if (/msie|MSIE 7/.test(navigator.userAgent) || /msie|MSIE 8/.test(navigator.userAgent)) {
			        ul
					.css({
					    display: 'block'
					})
					.stop()
					.animate({
					    height: '135px'
					}, {
					    duration: 250
					});
			    }
			    else {
			        ul
					.css({
					    opacity: 0,
					    display: 'block'
					})
					.stop()
					.animate({
					    height: '135px',
					    opacity: 0.9
					}, {
					    duration: 250
					});
			    }

			});
        $cont
			.mouseover(function () {
			    clearTimeout(t);
			})
			.mouseout(function () {
			    //closeActive();
			    t = setTimeout(closeActive, 250);
			});
    };


    var closeActive = function () {
        var $el = $(contSelector + " > li > a.active");
        var ul = $el.removeClass('active').siblings("ul.hnav > li > ul");
        if (/msie|MSIE 6/.test(navigator.userAgent)) {
            ul
			.css({
			    display: 'none',
			    height: 0
			})
        }
        else if (/msie|MSIE 7/.test(navigator.userAgent) || /msie|MSIE 8/.test(navigator.userAgent)) {
            ul.css({
                display: 'inline-block'
            }).stop().animate({
                height: 0
            }, {
                duration: 250,
                complete: function () {
                    $(this).hide();
                }
            });
        }
        else {
            ul.css({
                display: 'inline-block'
            }).stop().animate({
                height: 0,
                opacity: 0
            }, {
                duration: 250,
                complete: function () {
                    $(this).hide();
                }
            });
        }
    };


    return {
        init: init
    }
})();

$(document).ready(companys.dropdown.init);
