$(document).ready(function() {

    //move this
    if ($('input[name=mobile_opt_in]').attr('checked')) {
        $('#extra_fields').show();
    }

    if ($('input[name=email_opt_in]').attr('checked')) {
        $('#extra_fields').show();
    }

    $('input[name=mobile_opt_in]').click(function() {
        if ($(this).attr('checked')) {
            $('#extra_fields').show();
        } else if ($('input[name=email_opt_in]').attr('checked') == false) {
            $('#extra_fields').hide();
        }
    });

    $('input[name=email_opt_in]').click(function() {
        if ($(this).attr('checked')) {
            $('#extra_fields').show();
        } else if ($('input[name=mobile_opt_in]').attr('checked') == false) {
            $('#extra_fields').hide();
        }
    });

    $('a[rel=external]').click(function() {
        alert("You are now leaving JimBeam.com. Please remember that we are not responsible for content outside of our site.")
        window.open(this.href);
        return false;
    });

    $('a[rel=new_window]').click(function() {
        window.open(this.href);
        return false;
    });


    //Brand Transitions

    $("a[rel=transition]").click(function(event) {
        set_cookie("fade_in", "true", 1);
        linkLocation = this.href;
        page = linkLocation.substring(linkLocation.lastIndexOf('/'));
        
        switch (page) {
            case "/":
                $("#transition_overlay").css("background-color", "#d1d0d0");
                break;
            case "/original":
                $("#transition_overlay").css("background-color", "#ffffff");
                break;
            case "/black":
                $("#transition_overlay").css("background-color", "#000000");
                break;
            case "/red-stag":
                $("#transition_overlay").css("background-color", "#5b1012");
                break;
            case "/other-bourbons":
            case "/jim-beam-rye":
            case "/jim-beam-7-year":
            case "/jim-beam-choice":
                $("#transition_overlay").css("background-color", "#f7e6b1");
                break;
        }
        $("#transition_overlay").fadeIn(600, redirectPage);
        return false;
    });

    function redirectPage() {
        window.location = linkLocation;
    }

    if ((fade_in != null && fade_in == "true") || $('#transition_overlay').is(':visible')) {
        $("#transition_overlay").fadeOut(1000);
        set_cookie("fade_in", "false", 0);
    }

    $("#main_content_container .more").click(function() {
        if ($('#' + this.id + '_full').is(':hidden')) {
            $('.fullDetail').slideUp();
            set_blkLinks('More &raquo;');
            $(this).html('Less &raquo;').val();
        } else {
            $(this).html('More &raquo;').val();
        }
        $('#' + this.id + '_full').slideToggle();
    });
});

var get_cookie = function(c_name)
{
    if (document.cookie.length > 0)
    {
        c_start=document.cookie.indexOf(c_name + "=");
        if(c_start!=-1)
        {
            c_start=c_start + c_name.length+1;
            c_end=document.cookie.indexOf(";",c_start);
            
            if (c_end==-1) c_end=document.cookie.length;
                return unescape(document.cookie.substring(c_start,c_end));
        }
      }
    return "";
}

var set_cookie = function(c_name, value, expiredays) 
{
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) +
    ((expiredays == null) ? "" : ";expires=" + exdate.toUTCString());
}


// clear global search form
var clear_search = function(searchBox)
{
	if (searchBox.value == 'Search')
		searchBox.value = '';
}

var set_blkLinks = function (content){
	$("#main_content_container .more").each(function(){
		$(this).html(content).val();
	});
}

var set_content = function(selector, content){
	$(selector).html(content);
	return false;
}

var do_pop_up = function(name, width, height) {
    window.open(name, "t", "menubar=0,width=" + width + ",height=" + height + ",toolbar=0,resizable=0,location=0,scrollbars=1");
    return false;
}

var show_modal = function(message_text, header_text, css_class) {
    if (message_text != null)
        $('#modal_message_text').html(message_text);
    if (header_text != null)
        $('#modal_header_text').html(header_text);
    if (css_class != null)
        $("#message_modal").addClass(css_class);

    $("#message_modal").overlay().load();
    $('#modal_overlay').bgiframe(); 
    return false;
}

var set_body_background = function() {
    if (!document.body || !document.body.style) return;
    var x, y, d = document, imagePath = '';
    if (self.innerHeight) { // all except IE
        x = self.innerWidth;
        y = self.innerHeight;
    } else {
        var dE = d.documentElement; // IE6+ in Standards mode
        if ((!dE || !dE.clientHeight) && d.body) dE = d.body; // other IE
        if (dE && dE.clientHeight) {
            x = dE.clientWidth;
            y = dE.clientHeight;
        }
    }

    if (x >= 1300) {
        if(window.location.pathname == '/')
            $('body').addClass("home_wide");
        else
            $('body').addClass("wide");
    }
    else
        $('body').removeClass("wide home_wide");
}

window.onresize = window.onload = set_body_background;
fade_in = get_cookie("fade_in");

