﻿/* Things to run Onload */
$(document).ready(function() {
    globalEvents();

    // calculate the width of the social media badges
    var socialWidth = 0;
    $('#social li').each(function() {
        socialWidth += $(this).outerWidth(true);
    });
    $('#socialInfo .floatCentre').css('width', socialWidth + 10);


    if ($("#homeTheaterDivWrapper").length > 0) {
        utils.startCycle();
    }

    // set up tabs if page has them
    if ($('#tabs').length > 0) {
        $.address.tracker(0);
        // make the first tab the default tab
        var defaultTab = "#" + $("#tabContent_tabs > div").attr("id");
        if ($.address.value() == '/') {
            $("#tabs").tabs("select", defaultTab);
            $.address.value('');
        }
        $.address.init(function(event) {
            $("#tabs").tabs();
        }).change(function(event) {
            var value = $.address.value();
        }).externalChange(function(event) {
            var value = $.address.value();
            // remove leading slash
            var mainTab = value.substring(1, value.length);
            $("#tabs").tabs("select", mainTab);
            // for going back to the default tab
            if ($.address.value() == '/') {
                $("#tabs").tabs("select", defaultTab);
                $.address.value('');
            }
            else {
                $.address.value(value);
            }
        }).history(true);
        $('#tabs_nav li a').bind('click', function() {
            var value = $(this).attr('href').substring(1);
            $.address.value(value);
        });
    }

    // Initialize Superfish menu, center nav
    if ($("ul.sf-menu").length > 0) {
        if ($.browser.msie) {  //Removes fade in from IE to get rid of opacity jump
            $("ul.sf-menu").superfish({
                'autoArrows': false,
                'speed': 0,
                onShow: function() { this[0].style.removeAttribute("filter"); }
            });
            //if ($.browser.version.substr(0, 1) < 7) { $("#nav li li:first-child").css('padding-top', '10px'); }
            //$("#nav li li:last-child").css({ 'padding-bottom': '10px', 'border-width': '0px 1px 1px' });
        } else {
            $("ul.sf-menu").superfish({
                'autoArrows': false,
                'speed': 400
            });
        }
    }

    // check to see if there's any select menus that need to be styled
    $('.styledSelect').each(function() {
        createDropDown($(this).attr('class').split(' ')[1]);
    });

    // make the whole area of a global promo clickable
    $('.promo').click(function(e) {
        e.preventDefault();
        var linkUrl = $(this).find('a').attr('href');
        document.location.href = linkUrl;
    });


    // Tell webpage JS is available
    $("html").removeClass("no-js").addClass("JS");

    // Video Handler..
    $(".colorbox").click(function(e) {
        e.preventDefault();
        var videoHref = $(this).attr("href");
        // parse the querystring of the video link
        var vars = [], hash;
        var hashes = videoHref.slice(videoHref.indexOf('?') + 1).split('&');
        for (var i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        // pull out the width and height parameters, then add some pixels for the video player chrome
        var contentWidth = parseInt(vars["width"]) + 58;
        var contentHeight = parseInt(vars["height"]) + 106;

        $.colorbox({
            href: videoHref,
            iframe: true,
            width: contentWidth,
            height: contentHeight,
            transition: "none"
        });
    });

    // set classes on alternating rows of license numbers table
    $('#licenseTable tr:even').addClass("even");

});

var utils = {
    startCycle: function() {
        $('#homeTheaterDivWrapper').cycle({
            fx: 'fade',
            speed: 'fast',
            timeout: 10000,
            pager: '#theaterNav',
            pause: 1
        });
    }
}

/****************************************************************
globalEvents sets up events that are global to the entire site
Params: None
*****************************************************************/
function globalEvents() {
    // open all links with rel='ext' in a new window
    //$("a[rel='ext']").attr('target', '_blank');
	
	//Remove outline from links
	$("a").click(function(){
		$(this).blur();
	});

	// surround reg marks with <sup> tags
	$('p, li').each(function(i, elem) {
	    $(elem).html(function(i, html) {
	        return html.replace(/(®)/g, "<sup>$1</sup>");
	    });
	});
}

/****************************************************************
// createDropDown creates a styled select menu by replacing the select menu with a dl and nested ul
// the select element requires 2 classes: "styledSelect" + any unique name
*****************************************************************/
function createDropDown(selectClass) {
    var source = $("." + selectClass);
    var selected = source.find("option:selected");
    source.addClass("hideSelect");
    var options = $("option", source);

    var target = "target-" + selectClass;
    source.after('<dl id="' + target + '" class="dropdown"></dl>')
    $("#" + target).append('<dt><a href="#">' + selected.text() +
		'<span class="value">' + selected.val() +
		'</span></a></dt>')
    $("#" + target).append('<dd><ul></ul></dd>')

    options.each(function() {
        $("#" + target + " dd ul").append('<li><a href="#">' +
			$(this).text() + '<span class="value">' +
			$(this).val() + '</span></a></li>');
    });

    $("#" + target + ".dropdown dt a").click(function(e) {
        e.preventDefault();
        $("#" + target + ".dropdown dd ul").toggle();
    });
    $(document).bind('click', function(e) {
        var $clicked = $(e.target);
        if (!$clicked.parents().hasClass("dropdown"))
            $("#" + target + ".dropdown dd ul").hide();
    });
    $("#" + target + ".dropdown dd ul li a").click(function(e) {
        e.preventDefault();
        var text = $(this).html();
        $("#" + target + ".dropdown dt a").html(text);
        $("#" + target + ".dropdown dd ul").hide();
        source.val($(this).find("span.value").html());
    });
}

// Clear the input box on click if default value is present
function conditionalClear(current, field) {
    if (current == "Search" || current == "Enter Keywords" || current == "Enter City or ZIP" || current == "Enter name" || current == "Enter Email Address") {
        field.value = "";
    } 
}


