var COOKIE_OPTIONS = { path: '/' };

$(document).ready(function(){
    toggleLinks();
    $('a').focus(function(){$(this).addClass('focus')});
    $('a').blur(function(){$(this).removeClass('focus')});
    $('#q').val("Search").css('color','#AAA');
    $('#q').focus(function(){ if($(this).val()=='Search'){$(this).val('').css('color','#000');}});
    equalHeights();
    var spotlightLink = $('body#home a.header').attr("href");
    $('body#home #content .yui-u.first').click(function(){ window.open(spotlightLink); });
    $('body#home #content a.header').hover(function(){ $('body#home #content .yui-u.first').unbind('click'); }, function(){ $('body#home #content .yui-u.first').click(function(){ window.open(spotlightLink); }); } );    
    $("body#home #content .yui-u.first").hover(function(){ $(this).css('cursor','pointer'); });
});

toggleLinks = function() {
    var isResourcePage = $('#isResourcePage');
    if (isResourcePage) {
        isResourcePage.replaceWith('<a id="toggleDescriptions" class="hide">Hide Descriptions</a>')
        $('#toggleDescriptions').click(function(){
            $('.resource .description').toggle();
            $(this).toggleClass('hide');
            if ($(this).hasClass('hide')) {
                $(this).text('Hide Descriptions');
            } else {
                $(this).text('Show Descriptions');
            }
        });
    }
}

setPageTextSize = function() {
    var base = '13px'; var larger = '15px'; var largest = '17px';
    var COOKIE_NAME = 'textsize';
    var current = $.cookie(COOKIE_NAME) || base;

    if (current == larger || current == largest) {
        $('#slideshow .about_us').css({'right':'0','left':'auto'});
    }
    $('body').css('font-size', current);

    $('#baseFontSize').click(function(){
        $('body').css('font-size', base);
        $.cookie(COOKIE_NAME, base, COOKIE_OPTIONS);
        $('#slideshow .about_us').css({'left':'560px','right':'auto'});
        equalHeights();
    })
    $('#largerFontSize').click(function(){
        $('body').css('font-size', larger);
        $.cookie(COOKIE_NAME, larger, COOKIE_OPTIONS);
        $('#slideshow .about_us').css({'right':'0','left':'auto'});
        equalHeights();
    })
    $('#largestFontSize').click(function(){
        $('body').css('font-size', largest);        
        $.cookie(COOKIE_NAME, largest, COOKIE_OPTIONS);
        $('#slideshow .about_us').css({'right':'0','left':'auto'});
        equalHeights();
    })
}

/*
Selector target - A select for the block of HTML to turn into a group of tabs
String tabClass - the className that will be used for the list of tabs. Defaults to 'tabList'.
 */
buildTabs = function(target, tabClass) {
    if (target) {
        var tabClass = tabClass || 'tabList';

        // Create an unordered list to hold the tabs
        $(target).prepend('<ul class="'+tabClass+'"></ul>');

        // Build out the list of tabs based on the content
        $(target + ' > div').hide().each(function(i){
          var tab_txt = $('h2', this).hide().text();
          $('.'+tabClass).append('<li class="tab '+this.id+'"><span class="left"></span>'+tab_txt+'<span class="right"></span></li>');
          if (i == 0) { $(this).show(); $('.'+tabClass+' li:last').addClass('active'); }
        });


        // Show/hide the content of each tab when it's clicked
        /* see US1133
        $('.'+tabClass+' .tab').click(function(){
          if ( ! $(this).hasClass('active')) {
            $('.'+tabClass+' .tab').toggleClass('active');
            $(target+' > div').toggle();
          }
        })
        */
    }
}


/**
 * Page Style controls what stylesheet is applied to the page.
 */
var pageStyle = {
    COOKIE_NAME: 'style',

    init: function() {
        var title = pageStyle.getPageStyle();
        pageStyle.setPageStyle(title);
        $('#VisualPreference select').val(title);
    },
    setPageStyle: function(title) {
        pageStyle.setActiveStyleSheet(title);
        $.cookie(pageStyle.COOKIE_NAME, title, COOKIE_OPTIONS);
    },
    setPageStyleFromDropDown: function() {
        var title = title || $('#VisualPreference :selected').val();
        $.cookie(pageStyle.COOKIE_NAME, title, COOKIE_OPTIONS);
        window.location.href=window.location.href;
    },
    getPageStyle: function(){
        return $.cookie(pageStyle.COOKIE_NAME) || pageStyle.getPreferredStyleSheet();
    },
    setActiveStyleSheet: function(title) {
        $('link[rel*=stylesheet][title!=]').attr('disabled',true);
        $('link[rel*=stylesheet][title='+title+']').attr('disabled',false);
        equalHeights();
    },
    getActiveStyleSheet: function() {
        return $('link[rel*=stylesheet][title!=]:not(:disabled)').attr('title') || null;
    },
    getPreferredStyleSheet: function() {
        return $('link[rel=stylesheet][title!=]').attr('title') || null;
    }
}

/**
 *  Set left column to height of body if content is longer so the background image does not get cut off
 *  Also adjust height of content section on home page so background does not appear cut off
 */
equalHeights = function() {
    $("#InfoByState").css({height:"auto"});
    $("body#home #content").css({height:"auto"});
    $("#InfoByState").height($("#bd").height()-$("#InfoByTopic").height()-$("#AdminNavigation").height());
    $("body#home #content").height($("#bd").height()-$(".yui-gc").height());
}
