/* Things to run Onload */
$(function() {
    globalEvents();

    // calculate the width of the affiliation badges
 //   var badgesWidth = 0;
 //   $('#badges img').each(function() {
 //       badgesWidth += $(this).outerWidth(true);
 //   });
 //   $('#badges .floatCentre').css('width', badgesWidth);

    // calculate the width of the social media badges
    var socialWidth = 0;
    $('#social li').each(function() {
        socialWidth += $(this).outerWidth(true);
    });
    $('#socialInfo .floatCentre').css('width', socialWidth);

    // find out the height of the tallest tab, then fix the tab height to that height for all tabs
    var maxHeight = 0;
    $('#slides div div, #about, #slideshow div, #offers').each(function() { maxHeight = Math.max(maxHeight, $(this).height()); });
    $('#tabContent_tabs').height(maxHeight);

    // tab setup, with 2 level deep-linking
    //$.address.strict(0);
    $.address.tracker(0);
    // get the default tab and switch (installation subnav)
    var defaultTab = "#" + $("#tabContent_tabs > div").attr("id");
    var defaultSwitch = $("#switches > li a").attr("href");
    if ($.address.value() == '/') {
        $(".tabs1").tabs("select", defaultTab);
        $(".tabs2").tabs("select", defaultSwitch);
        $.address.value('');
    }
    $.address.init(function(event) {
        $(".tabs1").tabs();
        $(".tabs2").tabs();
    }).change(function(event) {
        var value = $.address.value();
    }).externalChange(function(event) {
        var value = $.address.value();
        var value_array = value.split('-');
        // remove leading slash
        var mainTab = value_array[0].substring(1, value_array[0].length);
        //var subTab = value_array[1].substring(1, value_array[1].length);
        $(".tabs1").tabs("select", mainTab);
        $(".tabs2").tabs("select", value_array[1]);
        // for going back to the default tab
        if ($.address.value() == '/') {
            $(".tabs1").tabs("select", defaultTab);
            $(".tabs2").tabs("select", defaultSwitch);
            $.address.value('');
        }
        else {
            $.address.value(value);
        }
    }).history(true);
    $('ul li a').bind('click', function() {
        var value = $(this).attr('href').substring(1);
        $.address.value(value);
    });

    // slideshow for gallery tab
    $('#slideshow').cycle({
        prev: '#prev',
        next: '#next',
        speed: 0,
        timeout: 0
    });

    // show the change zip panel if we're in the "local installer not found" state
    if ($('#ctl00_ContentPlaceHolder1_divisionLeftColumn1_divisionInfo1_notFoundMessage p').length > 0) {
        $("#changeLocation a").toggle();
        $("#changeZip").css("display", "block");
    }

    $("#closeChangeZip").click(function(e) {
        e.preventDefault();
        $("#changeLocation a").toggle();
        $("#changeZip").css("display", "none");
    });

    $("#closeNoInstaller").click(function(e) {
        e.preventDefault();
        $("#noInstaller").css("display", "none");
        // need to bring back change location link?
    });

    $("#changeLocation a").click(function(e) {
        e.preventDefault();
        // get current state, so that the contractor area can extend
        var leftColBtmTop = $('#leftColBtm').offset().top;
        var leftColHeight = $('#leftCol').height();
        // hide the link
        $(this).toggle();
        // switch the bottom of the left col to absolute position, so the contract area can extend over it
        $("#leftCol").css("height", leftColHeight);
        $("#leftColBtm").css({ "position": "absolute", "display": "block", "top": leftColBtmTop, "left": "0" });
        $("#changeZip").css("display", "block");
    });

    // check to see if there's any select menus that need to be styled
    $('.styledSelect').each(function() {
        createDropDown($(this).attr('class').split(' ')[1]);
    });

    // Tell webpage JS is available
    $("html").removeClass("no-js").addClass("JS");

    $(".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"
        });
    });
});


/****************************************************************
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 after click - mainly/only for firefox
	$("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 = "";
    }
}

