//Runs all custom jQuery and functions
$(document).ready(function () {

    apply_paragraph_twills();
    apply_department_twills();
    apply_print_slider();
    apply_email_modal();
    $(".gallery_area a").colorbox({
        photo: 'true',
        opacity: '.9'
    });



    $("ul.sf-menu").superfish();

    if ($.browser.msie) {
        if (parseInt($.browser.version) < 7) {
            $('#mainNav').css('width', 920);
        }
    }
    $(".mouseOver").hover(function () {
        $(this).attr("src", $(this).attr("src").split("-nor.").join("-ovr."));
    }, function () {
        $(this).attr("src", $(this).attr("src").split("-ovr.").join("-nor."));
    });

    $('#searchOptions').hover(function () {
        $('#searchArrow').attr("src", $('#searchArrow').attr("src").split("-nor.").join("-ovr."));
        $('#moreSearch').css('color', '#FFF');
    }, function () {
        $('#searchArrow').attr("src", $('#searchArrow').attr("src").split("-ovr.").join("-nor."));
        $('#moreSearch').css('color', '#BCBF59');
    });

    //$('#moreSearch').hover(function () { $('#searchArrow').attr("src", $('#searchArrow').attr("src").split("-nor.").join("-ovr.")); }, function () { $('#searchArrow').attr("src", $('#searchArrow').attr("src").split("-ovr.").join("-nor.")); });
    //$('#moreSearch').hover(function () { $('#moreSearch').css('color', '#FFF'); }, function () { $('#moreSearch').css('color', '#BCBF59'); });
    //$('#searchArrow').hover(function () { $('#moreSearch').css('color', '#FFF'); }, function () { $('#moreSearch').css('color', '#BCBF59'); });

    if (!$.readCookie('textOnly')) {
        $('.homeNewsItem h2 a').hoverIntent(function () { Cufon.refresh(); }, function () { Cufon.refresh(); });
        $('.homeEventItem h4 a').hoverIntent(function () { Cufon.refresh(); }, function () { Cufon.refresh(); });
        $('.newsItem h1 a').hoverIntent(function () { Cufon.refresh(); }, function () { Cufon.refresh(); });
    }

    var padding = 0;
    var myWidth = 0;
    $('#mainNav ul li').each(function (i, val) {
        padding = padding + parseInt($(this).css('padding-left')) + parseInt($(this).css('padding-right')) + parseInt($(this).css('margin-left')) + parseInt($(this).css('margin-right'));
        myWidth = myWidth + parseInt($(this).width());
    });
    $('#mainNav').css('width', padding + myWidth + 10);

    courseDescriptions();

    // advanced search button
    $("#advanced_button").click(function (e) {
        e.preventDefault();
        if ($("#btnIWant").val() != 0) {
            if ($("#btnIWant").val() == "/catalog/index.html") {
                window.open($("#btnIWant").val(), 'catalog', '', '');
            } else {
                window.location = $("#btnIWant").val();
            }
        }
    });

    var done;
    var i = 0;
    var colCounter = 0;
    $('.sNav').each(function (i, val) {
        var thisID = $(this).parent().attr('id'); //get the parent ID so we can distinguish between columns later
        var listSize = parseInt($(this).children('li').size());
        if (listSize > 6) {
            var halfList = parseInt(listSize / 2) + 1;
            $(this).children('li').each(function (i2, val2) {
                if (i2 < halfList) {
                    $(this).addClass('col1');  //first half goes on the left
                } else {
                    $(this).addClass('col2');  //second half goes on the right
                }

                if (i2 == halfList) {

                }
                //console.log(i2);
                //var myObj = $(this);
                //console.log(val2);
            });

            $(this).children().unwrap();
            $('#' + thisID + ' .col1').wrapAll('<ul class="leftCol" id="ddlL' + colCounter + '"/>');
            $('#' + thisID + ' .col2').wrapAll('<ul class="rightCol" id="ddlR' + colCounter + '"/>');

            $('#ddlR' + colCounter).css('height', $('#ddlL' + colCounter).height());
            colCounter = colCounter + 1;
        }
    });

    /*For the footer nav*/
    padding = 0;
    myWidth = 0;
    $('.footerNav li').each(function (i, val) {
        padding = padding + parseInt($(this).css('padding-left')) + parseInt($(this).css('padding-right')) + parseInt($(this).css('margin-left')) + parseInt($(this).css('margin-right'));
        myWidth = myWidth + parseInt($(this).width());
    });

    if ($.browser.msie) {
        $('.footerNav').css('width', padding + myWidth + 30); //For IE's inability to conform
    } else {
        $('.footerNav').css('width', padding + myWidth + 10);
    }

    $('.homeEventItem').hoverIntent(function () {
        $(this).animate({ borderLeftWidth: 10, paddingRight: 3 }, 500);
    }, function () {
        $(this).animate({ borderLeftWidth: 3, paddingRight: 10 }, 500);
    });

    $('.moreNews li').hoverIntent(function () {
        $(this).animate({ left: "10px" }, 500);
    }, function () {
        $(this).animate({ left: "0px" }, 500);
    });

    $('.homeNewsItem img').hoverIntent(function () {
        $(this).animate({ opacity: .7 }, 500).animate({ opacity: 1 }, 700);
    }, function () {

    });

    $('#studentTypes div').hoverIntent(function () {
        $(this).animate({ left: "+=" + 10 }, 500);
    }, function () {
        $(this).animate({ left: "-=" + 10 }, 500);
    });

    if ($('#hidIsHome').val() != 'true') {
        $('#alertIcon').hide();
    }
    //showAlert(1000);

    $('.textOnly').click(function () {
        $('.textOnly').css('display', 'none');
        $('.viewOriginal').css('display', 'block');
        $.setCookie('textOnly', 'true', { duration: 1, path: '', domain: '', secure: false });
        textOnly();
    });

    $('.viewOriginal').click(function () {
        $('.textOnly').css('display', 'block');
        $('.viewOriginal').css('display', 'none');
        $.setCookie('textOnly', 'false', { duration: 1, path: '', domain: '', secure: false });
        window.location.reload();
    });

    $('a').each(function (i, val) {
        var thisLink = $(this);
        if ($(thisLink).attr('title').length < 1) {
            if ($(thisLink).text().length > 0) {
                $(thisLink).attr('title', $(thisLink).text());
            }
            if ($(thisLink).html().indexOf('<img') != -1) {
                $(thisLink).attr('title', 'Image');
                //$(thisLink).siblings('img:first').attr('alt', 'Image');
            }
        }

        var thisImage = $(thisLink).children('img:first');
        if ($(thisImage).attr('alt') != null) {
            if ($(thisImage).attr('alt').length < 1) {
                $(thisImage).attr('alt', 'Image');
            }
        }

        //if ($(thisImage).attr('alt').length < 1) {
        //    $(thisImage).attr('alt', 'Image');
        //}
    });
});

function showAlert(to){
	setTimeout(function(){$('#alertContainer').slideDown('normal');},to);
	$('#alertIcon').fadeOut();
}

function hideAlert(){
	$('#alertContainer').slideUp('fast');
	$('#alertIcon').fadeIn();
}

function showSearch(){
	$('#advSearchContainer').slideDown('normal');
	return false;
}

function closeSearch(){
	$('#advSearchContainer').slideUp('fast');
	return false;
}

function courseDescriptions() {
    $(".course_twill").click(function () {
        $(this).parent().next().next().slideToggle();
    });
}

function load_iwantto() {
    var wantgroup = $("#btnIAm").val()
    if (wantgroup == 0) {
        $('#btnIWant').html("<option value='0'>I want to</option>");
    } else {
        $.get("/search_iwantto.xml?refresh=" + Math.floor(Math.random() * 1000), function (data) {
            var options;
            options += "<option value='0'>I want to</option>";
            $(data).find("wantgroup[id=" + wantgroup + "]").find("want").each(function () {
                options += "<option value='" + $(this).attr("url") + "'>" + $(this).text() + "</option>";
            });
            $('#btnIWant').html(options);
        });
    }
}

var closeCount = 4
var closeTimer
function apply_print_slider() {
    $("#printButton").click(function () {
        $("#printButton").hide();
        $("#printQuestion").show();
        $("#printPanel").animate({ height: '60px' }, 500);
    });

    $("#printPanel").mouseout(function () { 
       closeTimer = setInterval("reset_print_panel()",4000)
   })

   $("#printPanel").mouseover(function () {
       clearInterval(closeTimer);
       closeCount = 4;
   })
}



function reset_print_panel() {
  
        $("#printButton").show();
        $("#printQuestion").hide();
        $("#printPanel").animate({ height: '30px' }, 500);

}


function apply_paragraph_twills() {
    $(".expander_twill").click(function () {
        $(this).next().slideToggle();
    });
}

function apply_department_twills() {
    $(".department_twill").click(function () {
        $(this).next().slideToggle();
    });
}

/*TEXT ONLY VIEW*/
function textOnly() {
    $('html').css({'background':'none', 'color':'#000'});
    $('body').css({ 'background': 'none', 'color': '#000','font-size':'12px','line-height':'3ex' });
    $('a').css({ 'color': '#000', 'text-shadow': 'none', 'font-family': 'Arial', 'font-style': 'normal' });
    $('#container, #header').css({'margin':'0'});
    $('#logo').hide();
    $('#header').css('height', 'auto');
    $('#flashArea').remove();
    $('#mainNav').css({ 'position': 'static', 'width': '200px', 'margin': '0px','height':'auto','z-index':'0'});
    $('#mainNav').append('<div class="clear"></div>');
    $('#mainNav ul').removeClass('sf-menu');
    $('#mainNav ul').removeClass('sf-js-enabled');
    $('#mainNav ul').css({'position':'static','margin':'0px'});
    $('#mainNav ul li').css({ 'float': 'none', 'text-align': 'left', 'list-style': 'disc','width':'inherit','padding':'0'});
    $('#mainNav ul li ul').addClass('plainNav');
    $('#mainNav ul li ul').removeClass('sNav');
    $('#mainNav ul li ul').css({ 'display': 'block', 'visibility': 'visible', 'background': 'none','margin-left':'20px'});
    $('.sep').remove();
    $('#studentTypes').unwrap().css({ 'position': 'static', 'top': '0', 'left': '0', 'text-align': 'left', 'margin': '0', 'padding': '0' });
    $('#search').css({ 'float': 'none', 'padding': '0' });
    $('.leftColBlueCont').siblings().remove();
    $('.leftColBlueCont').unwrap().removeClass();
    $('#leftColumn,#rightColumn').css({ 'float': 'none', 'width': 'auto', 'padding': '0' });
    $('.homeNewsItem').unwrap().removeClass();
    $('.homeEventItem').unwrap().removeClass();

    $('canvas').remove();
    $('cufontext').unwrap();
    $('#footer').css({ 'background': 'none' });
    $('#footerContent').css({ 'margin': '0' });
    $('#footerButtons,#footerLogos,.footerLogos,#footerMap').attr('id', '').attr('class', '');

    $('.footerNav').attr('style', '');
    $('.footerNav').removeClass();
}
function printer() {
   setTimeout("printDelay()",2000)
}

function apply_email_modal() {

    $('a[href*="email.aspx"]').colorbox({
        opacity: '.9',
        iframe: true,
        inline: false,
        width: 600,
        height: 480
    });

    $(document).bind('cbox_complete', function () {

        $("#cboxTitle").html('');

    });

}
