﻿function toggleit(elementToAddToggleFunctionTo, elementToShowAndHide) {
    $(elementToAddToggleFunctionTo).click(function() {
        $(elementToShowAndHide).slideToggle("slow");
        return false;
    });
    $(elementToShowAndHide).hide();
    
}
function hideit(elementToAddHideFunctionTo, elementToHide) {
    $(elementToAddHideFunctionTo).click(function() {
        $(elementToHide).hide();
        return false;
    });

}

function showit(elementToAddShowFunctionTo, elementToShow) {
    $(elementToAddShowFunctionTo).click(function() {
        $(elementToShow).show();
        return false;
    });

}

function makePopUp(elementToAddPopupTo, divWithContents) {
    $(elementToAddPopupTo).click(function() {
        $.fancybox({
            //'orig'			: $(this),
            //'padding': 0,
            //'href': 'http://farm3.static.flickr.com/2687/4220681515_cc4f42d6b9.jpg',
            //'title': $(elementToAddPopupTo).attr('title'),
            'transitionIn': 'elastic',
            'transitionOut': 'elastic',
            'content': $(divWithContents).html()
        });

        return false;
    });
    $(divWithContents).hide();
}

function makeSwapOut(elementToLink, elementWithContents)
{
    swaptext('.swaptarget', elementWithContents, elementToLink);
}

$(document).ready(function() {  // all the jQuery on load stuff :)
    // make all outside links open in a new window/tab
    $('a[href^="http"]').click(function() { //find the links pointing to a URL 
        if (this.href.indexOf(location.hostname) == -1) { //check if they point outside
            window.open($(this).attr('href')); //force open the new window
            return false;
        }
    });

    $('a[href^="#popup"]').each(function(index) {
        
        makePopUp(this, ('.' + $(this).attr('href').substring(1)));
    });

    $('a[href^="#swapout"]').each(function(index) {

        makeSwapOut(this, ('.' + $(this).attr('href').substring(1)));
    });
    /*
    *   Zebra-stripping table
    */
    $("table.options tr:nth-child(even)").addClass('even');

    showit('#l_navigation', '#center_hidden_nav');
    hideit('#l_navigation2', '#center_hidden_nav');


    // for the login and search pages, tweak it a bit.
    reworkLogin();
    reworkSearch();
});

function reworkLogin() {

    //Some minor edits to the Login Page:

    //Change Legend to indicate that the user needs to sign in with their eID
    var signInDiv = document.getElementById('ctl00_mainContent_pnlLogin');
    if (signInDiv) {
        var legendArr = signInDiv.getElementsByTagName("legend");
        legendArr[0].innerHTML = "Please Sign In With Your CSU eID. NOT YOUR CASA ACCOUNT!";
    }

    //Change User ID to say eID 
    var loginDiv = document.getElementById('ctl00_mainContent_LoginCtrl');
    if (loginDiv) {
        var loginLabelArr = loginDiv.getElementsByTagName("label");
        for (var i = 0; i < 1; i++)
            loginLabelArr[0].innerHTML = "Username";
    }

    //Ensure that the browser's autoSave password feature is turned off
    var autoComplete = document.getElementById('ctl00_mainContent_LoginCtrl_Password');
    if (autoComplete) {
        var autoCompleteAttr = document.createAttribute("autocomplete");
        autoCompleteAttr.value = "off";
        autoComplete.setAttributeNode(autoCompleteAttr);

    }

    //Hide Remember Me Text
    var rememberMe = document.getElementById('ctl00_mainContent_LoginCtrl');
        if (rememberMe) {
        var labelArr = rememberMe.getElementsByTagName("div");
        labelArr[3].style.visibility = 'hidden';
        labelArr[3].style.display = 'none';
    }

}

function reworkSearch() {
    //Get value from textbox for CSU Search on SearchResults.aspx page
    var strSearch = document.getElementById('ctl00_mainContent_txtSearchInput');
    if (strSearch) {
        var strDuration = document.getElementById('ctl00_mainContent_lblMessage');
        if (strDuration) {
            strDuration.innerHTML = "<br />Not what you're looking for? Try <a href='http://search.colostate.edu/search-csu.aspx?cx=001074770120494240330%3Annxeddf6qe0&sa=Search&cof=FORID%3A11&q=" + strSearch + "'>searching the entire CSU system</a>...<br /><br />";
            strDuration.style.visibility = 'visible';
            strDuration.style.display = 'block';
        } 
    }
}

function swaptext(elementToReplaceContentsof, elementWithContents, elementToLink) {
    $(elementToLink).click(function() {
        $(elementToReplaceContentsof).after('<div class="swaptarget">' + $(elementWithContents).html() + '<\/div>').remove();
        return false;
    });
    $(elementWithContents).hide();

}