﻿$(document).ready(function() {

    // Fields
    var isIE6 = false;
    var equalHeightRowCss = ".row_eq_height";
    var equalHeightColCss = ".col_eq_height";
    // Check for IE6 
    if (jQuery.browser.msie && jQuery.browser.version < 7) {
        isIE6 = true;
    }
    // Set the heights of columns within rows
    $(equalHeightRowCss).each(function() {
        setColumnHeights($(this));
    });
    function setColumnHeights(container) {
        equalHeight($(equalHeightColCss, container));
    }
    function equalHeight(group) {
        var tallest = 0;
        group.each(function() {
            thisHeight = $(this).height();
            if (thisHeight > tallest) {
                tallest = thisHeight;
            }
        });
        // IE6 does not support the min-height property so apply standard height
        if (isIE6) {
            group.css("height", tallest.toString() + "px");
        } else {
            group.css("min-height", tallest.toString() + "px");
        }
    }

    // only build the bookmarks box if present on the page
    if ($("#bookmarksites_box").length > 0) {
        $("#bookmarksites_box").transparentFrame({ popupClass: 'popup jqmWindow' });
        $("#bookmarksites_box").jqm({
            trigger: 'a[rel$=\'share\']',
            modal: false,
            onShow: function(h) {
                /* get position of trigger */
                var sharePos = $('.tb_socialmedia_thick a[rel$=\'share\']').position();
                window.location = $(".tb_socialmedia_thick a[rel$=\'share\']").attr("href");
                var posTop = sharePos.top - 161;
                var posLeft = sharePos.left + 178;
                h.w.css("top", posTop);
                h.w.css("left", posLeft);
                h.w.show();
            },
            overlay: 1
        });
    }

    // forward to a friend
    if ($("#forward_form").length > 0) {
        // if we have DO NOT have an error, hide the form
        if ($("#forward_form .forward_form_validationsummary").html().trim().length <= 0) {
            ShowHideElement('forward_form');
        }
        $("a[rel$='forwardform']").click(function() {
            ShowHideElement('forward_form');
        });
    }


    // Mark some navigation links as open in new window
    $('div.navigation_inner a').each(function() {
    if ($(this).html().trim() == 'Video' || $(this).html().trim() == 'Competitions') {

            $(this).attr("target", "_blank");
        
        }
        
    });
});

function ShowHideElement(objElement) {
    var element = document.getElementById(objElement);

    if (element.style.display != 'none') {
        element.style.display = 'none';
    }
    else {
        element.style.display = '';
    }
}
function MaxLength(field, maxLength) {
    if (field.value.length >= (maxLength - 1)) {
        field.value = field.value.substr(0, (maxLength - 1));
    }
}

//	Transparent frame plugin
// -- NOTE: you must specify the width of the content section in the css
(function($) {
    $.fn.extend({
        transparentFrame: function(options, callbackFunction) {

            var defaults = {
                borderWidth: 20,
                topBorderWidth: 310,
                contentWidth: 310,
                bottomBorderWidth: 310,
                popupClass: "popup"
            };
            var options = $.extend(defaults, options);

            return this.each(function() {
                var o = options;
                var obj = $(this);

                var $topBorder, $leftBorder, $rightBorder, $bottomBorder, $middleContainer;

                function $div(className) {
                    return $('<div class="' + className + '" />');
                }

                $wrap = $div(o.popupClass);
                $wrap.attr("id", obj.attr("id"));
                $content = $div("content_section");
                $content.append(obj.html());

                var interfaceWidth = o.contentWidth + o.borderWidth;

                $wrap.css("width", interfaceWidth);

                $wrap.append(
					$('<div class="top_container clearfix" />').append(
						$div("top_left"),
						$topBorder = $div("top_middle"),
						$div("top_right")
					),
					$middleContainer = $('<div class="middle_container clearfix" />').append(
						$leftBorder = $div("middle_left"),
						$content,
						$rightBorder = $div("middle_right")
					),
					$('<div class="bottom_container clearfix" />').append(
						$div("bottom_left"),
						$bottomBorder = $div("bottom_middle"),
						$div("bottom_right")
					)
				);

                $topBorder.css("width", o.topBorderWidth);
                $content.css("width", o.contentWidth);
                $bottomBorder.css("width", o.bottomBorderWidth);

                // Check for IE6 
                if (jQuery.browser.msie && jQuery.browser.version < 7) {
                    $leftBorder.css("height", obj.outerHeight(true));
                    $middleContainer.css("height", obj.outerHeight(true));
                    $content.css("height", obj.outerHeight(true));
                    $rightBorder.css("height", obj.outerHeight(true));
                } else {
                    $leftBorder.css("min-height", obj.outerHeight(true));
                    $middleContainer.css("min-height", obj.outerHeight(true));
                    $content.css("min-height", obj.outerHeight(true));
                    $rightBorder.css("min-height", obj.outerHeight(true));
                }

                // Workaround for Firefox Mac - 1 pixel height gap on left and right border
                if (obj.hasClass("height_fix")) {
                    jQuery.each(jQuery.browser, function(i, val) {
                        if (i == "mozilla" && navigator.appVersion.indexOf("Mac") != -1) {
                            $leftBorder.height(obj.outerHeight(true) + 1);
                            $rightBorder.height(obj.outerHeight(true) + 1);
                        }
                    });
                }

                $(this).replaceWith($wrap);

                // now call a callback function
                if (typeof callbackFunction == 'function') {
                    callbackFunction.call(this);
                }
            });
        }
    });
})(jQuery);

// Functions for search box
function clearText(fieldid) {
    if (fieldid != null) {
        $(".tip_greyed_out").each(function() {
            if ($(this).attr('id') == fieldid) {
                searchTip = $(this).val();
                $(this).removeClass('tip_greyed_out');
                $(this).removeClass('error');
                $(this).val('');
            }
        });
    }
}

function repopulateText(fieldid, text) {
    if (fieldid != null) {
        $("input").each(function() {
            if ($(this).attr('id') == fieldid) {
                if ($(this).val() == '') {
                    $(this).addClass('tip_greyed_out');
                    $(this).val(text);
                }
            }
        });
    }
}
