///<reference path="/_js/jQuery/1.4.1/jquery-1.4.1-vsdoc.js"/>

//this is because sitecore uses another library called Prototype, which gets loaded first when going into edit mode and conflicts with the $() syntax of jQuery.
//doing this will let us use j$() instead.
j$ = $.noConflict();

//widget array
var m_Widgets = Array();

j$(document).ready(function () {
    //activate all widgets
    ActivateWidgets();

    //make sure the footer is at the bottom of the page.
    j$(window).resize();

    j$("#scroller").scrollable();

    // remove right border, add 22px to width of last scroll set item in each set
    j$('.scrollSet').each(function () {
        var obj = j$('.scrollSetItem:last', j$(this));
        var width = obj.width();
        obj.css('width', (width + 22) + 'px');
        obj.css('border-right', 'none');
    });

    //new nav arrow fade effects
    j$("#footer").hover(function () {
        j$(".scrollerLink").each(function () {
            if (j$(this).css('display') == 'none') {
                j$(this).fadeIn("slow");
            }
        });
    }, function () {
        j$('.scrollerLink').fadeOut("fast");
    });

    //any accordions on the page
    j$("#accordion").accordion();

    var windowLocation = window.location.href;
    j$("#contactUsLightbox .contactForumSubmit").click(function () {
        j$.post("/Feedback.aspx",
            { "pageTitle": j$("#contactUsLightbox [id$='pageTitle']").val(),
                "pageLocation": windowLocation,
                "pageEmails": j$("#contactUsLightbox [id$='pageEmails']").val(),
                "pageType": j$("#contactUsLightbox [id$='pageType']").val(),
                "userFirst": j$("#contactUsLightbox #txtFirstName").val(),
                "userLast": j$("#contactUsLightbox #txtLastName").val(),
                "userEmail": j$("#contactUsLightbox #txtEmail").val(),
                "userComments": j$("#contactUsLightbox #txtComment").val()
            }, function (responseText, textStatus) {
                //triggered when call is done
                //alert(responseText);
            });
    });

    //this is for the search form text box enter key submition
    j$("#searchForm input[name='q']").keydown(function (event) {
        if (event.keyCode == 13) {
            j$("#searchButton").click();
            return false;
        }
    });

    j$("#searchForm input[name='q']").focus(function () {
        if (j$(this).val().length == 0)
            j$(this).animate({
                width: '+=10em'
            }, 500);
    });

    j$("#searchForm input[name='q']").blur(function () {
        if (j$(this).val() == "SEARCH")
            j$(this).animate({
                width: '-=10em'
            }, 500);
    });


});       //end the main document load

//function CustomWidthAnimation(textBox, newWidth, duration) {
//    var xStart = j$(textBox).width();
//    var xStop = newWidth;

//    if (xStart > xStop) {
//        //if the starting size is greater than the stopping size, we're shrinking



//        setInterval("CustomWidthAnimationWorker('" + textBox + "', 15", 124);
//    } else {
//        //otherwise we're expanding
//    }

//}

//function CustomWidthAnimationWorker(textBox, amount) {
//    var curWidth = j$(textBox).width();
//    j$(textBox).width(curWidth + amount);
//}

//contact form thing
function ShowContactForm(currentPage) {
    j$.fn.colorbox({ href: "/_lightbox/exed/Contact.aspx?type=1&pageGuid=" + currentPage, width: "750px", height: "500px", iframe: true });
}

function doSearch() {
//    var cx = j$("#searchForm input[name='cx']").val();
//    var cof = j$("#searchForm input[name='cof']").val();
//    var ie = j$("#searchForm input[name='ie']").val();
    var q = j$("#searchForm input[name='q']").val();
//    var sa = j$("#searchForm input[name='sa']").val();
//    var siteurl = j$("#searchForm input[name='siteurl']").val();

    //    window.location = "/search/?cx=" + cx + "&cof=" + cof + "&ie=" + ie + "&q=" + q + "&sa=" + sa + "&siteurl=" + siteurl;
    window.location = "/Results?q=" + q;
}

function doExedSearch() {
    var cx = j$("#searchForm input[name='cx']").val();
    var cof = j$("#searchForm input[name='cof']").val();
    var ie = j$("#searchForm input[name='ie']").val();
    var q = j$("#searchForm input[name='q']").val();
    var sa = j$("#searchForm input[name='sa']").val();
    var siteurl = j$("#searchForm input[name='siteurl']").val();

    window.location = "/search/?cx=" + cx + "&cof=" + cof + "&ie=" + ie + "&q=" + q + "&sa=" + sa + "&siteurl=" + siteurl;
}

/*IE footer check*/
function footerCheck() {
    if (j$.browser.msie)
        j$("#footer").html(j$("#footer").html());
}

function playVideo(guid) {
    //j$.fn.colorbox({ href: "/_lightbox/global/video.aspx?videoGuid=" + guid, width: "700px", height: "470px", iframe: true });
    //play video in new window w/o nav bar
    newwindow = window.open('/_lightbox/global/video.aspx?videoGuid=' + guid, '_blank', 'height=380, width=660');
    //if (window.focus) { newwindow.focus() }
    //    return false;
}

function expandNav(navItem) {
    j$(navItem).prev("a").click();
    return false;
}

function navLinkOverride(newURL) {
    location.href = newURL;
    return false;
}

function navLinkNewWindow(url) {
    window.open(url);
    return false;
}

function visitBuckyIframe() {
//    document.domain = "wisc.edu";
    j$.fn.colorbox({ iframe: true, width: "825px", height: "730px", name: "innerframe", href: "http://visitbucky.wisc.edu/browse.aspx" });
    return false;
}

function launchLightbox(url, _width, _height) {
    j$.fn.colorbox({ iframe: true, width: _width, height: _height, href: url });
}

function launchSlideshow(guid) {
    j$.fn.colorbox({ iframe: true, width: "793px", height: "583px", href: "/_lightbox/global/Slideshow.aspx?guid=" + guid });
}

//Adds a widgetFunction(options) and its wrapper div id to an array to process later
function RegisterWidget(objDiv, pluginName, pluginOptions) {
    var obj = { divWrapper: objDiv, plugin: pluginName, options: pluginOptions };

    //is it already registered?
    if (j$.inArray(obj, m_Widgets) == -1) {
        //if it isn't, add it
        m_Widgets.push(obj);
    }
}

//Loops through widgets to enable them
function ActivateWidgets() {
    for (var i = 0; i < m_Widgets.length; i++) {
        var temp = "j$('#" + m_Widgets[i].divWrapper + "')." + m_Widgets[i].plugin + "(" + m_Widgets[i].options + ");";
        eval(temp);
    }
}
