// Curved Corner Rendering function
function renderCorners(){
    $("#colLeft").cornerz({radius:10, borderWidth:1, borderColor: "#bbc3be"});
    $("#colRight").cornerz({radius:10, borderWidth:1, borderColor: "#bbc3be"});
}

//Capture the Flash Movie
function getFlashMovie(movieName) {
    var isIE = navigator.appName.indexOf("Microsoft") != -1;
    return (isIE) ? window[movieName] : document[movieName];
}

//Send Friendly URL to Flash
function showLarge(orn) {
    //trace( $("flashBlock") + " " + $("flashBlock") );
    getFlashMovie("flashBlock").sendAssetsFromJavaScript( orn );
}

//This Loads once the $(document).onReady has been fired
function initializeListeners(){

    // Get total number of assets
    var renditionTotal = $("div.rendCol").size();
    //Hide rendition navigation if only one image
    if (renditionTotal < 2) {
        $("#navRendition").css("display","none");
    }
    var newIdx;
    navState(0);

    // Gets the URL to be passed to the Flash Object
    function getRendition(idx) {
        if (idx >= 0 && idx < renditionTotal) {
            $('div.rendCol').removeClass("active");
            // Remove active styling for all and add to clicked
            var selectedObj = $('div.rendCol').eq(idx);
            selectedObj.addClass('active');
            //Display the ORN
            var assetSrc = selectedObj.find("a").attr("name");
            // Get the attribute of the image
            showLarge(assetSrc);
            //display ORN text for selected image
            var ornStr = selectedObj.find("img").attr("alt");
            $("#ornText").text(ornStr);
            //change altImage
            $("#altImage img").attr("src","http://digitallibrary.usc.edu/assetserver/controller/item/" + assetSrc.split("|")[0] + "/" + assetSrc.split("|")[1] + "?v=lr");
            $("#altImage img").attr("alt",ornStr);
        }
        navState(idx);
    }

    //Organize the Previous and Next Buttons based on the Rendition Selection
    function navState(idx) {
        if (idx <= 0) {
            $('a.prev').addClass("inactive");
            $('a.prev img').attr("src","/bmpix/images/gui/itemDetail/btn_prevInactive.gif");
        }
        else {
            $('a.prev').removeClass("inactive");
            $('a.prev img').attr("src","/bmpix/images/gui/itemDetail/btn_prev.gif");
        }

        if (idx >= renditionTotal - 1) {
            $('a.next').addClass("inactive");
            $('a.next img').attr("src","/bmpix/images/gui/itemDetail/btn_nextInactive.gif");
        }
        else {
            $('a.next').removeClass("inactive");
            $('a.next img').attr("src","/bmpix/images/gui/itemDetail/btn_next.gif");
        }
    }

    /////////////////////////////////////////////
    //  Event Listeners + Handlers
    /////////////////////////////////////////////
    //Preload 256px images
    $.each($(".rendition img"), renditionPreloadHandler);
    function renditionPreloadHandler( i, obj ){
        //trace( "renditionPreloadHandler: " + i + " evt: " + obj.id + "  " + obj.tagName + " " + obj.alt );
        var rendColl = $(this)[0].id.split("|")[0];
        var recId = $(this)[0].id.split("|")[1];
        var rendOrn = $(this)[0].id.split("|")[2];
        MM_preloadImages("http://digitallibrary.usc.edu/assetserver/controller/item/" + rendColl + "-m" + recId + "/" + rendOrn + "?v=lr");
    }

    //Handle when an Rendition Thumbnail has been Clicked
    $('div.rendCol').bind("click", onrendColClickHandler);
    function onrendColClickHandler( evt ){
        newIdx = $('div.rendCol').index(this);
        // Get the index of the item clicked
        getRendition(newIdx);
        return false;
    }

    //Handle when Previous or Next Buttons have been Clicked
    $('.prev, .next').bind("click", onPrevNextClickHandler);
    function onPrevNextClickHandler( evt ){
        oldIdx = $('div.rendCol').index($('.active')[0]);
        newIdx = ($(this)[0].className == "prev") ? oldIdx - 1 : oldIdx + 1;
        newIdx = ($(this)[0].className == "prev inactive") ? 0 : newIdx;
        getRendition(newIdx);
        return false;
    }

    //Handle Add and Remove Buttons once they've been Clicked
    $("li.btnAdd a").bind('click', onAddRemoveClickHandler);
    function onAddRemoveClickHandler( evt )
    {
        doMySelection( this.href );
        //	alert( e.target.id + "  "+ e.target.className + "  " + e.target.innerText  + " " + e.target.title );
        //Testing Purposes Only
        //trace('Bubble for ' + this.tagName + '#'+ this.id + ' target is ' + evt.target.id);
        //Toggle the Text with Every Click
        var text = $(this).find("img").attr("title");
        if(text == "Remove from My Selections") text = "Add to My Selections";
        else text = "Remove from My Selections";
        $(".rollText").html( text );

        return false;
    }

    //On RollOver + RollOut Listeners
    $(".btnAdd,.btnEmail,.btnEmailHelp,.btnEnlarge,.btnPrint").hover(onRollOverActions, onRollOutActions);
    //    $(".btnAdd,.btnEmail,.btnEmailHelp,.btnEnlarge,.btnPrint").bind("mouseover", onRollOverActions);
    function onRollOverActions( evt ){
        //Show the Text
        $(".rollText").html($(this).find("img").attr("title"));
        //Capture the SRC the user is currently hovering OVER
        /*
        var currentImgSrc = evt.target.src;
        //Strip out "Up.gif" and replace it with "Over.gif"
        var newImgSrc = currentImgSrc.substring( 0, currentImgSrc.lastIndexOf( "Up.gif" ) ) + "Over.gif";
        //Swap Images
        $(evt.target).attr("src", newImgSrc );
        */
    }
    function onRollOutActions( evt ){
        //Hide the Text
        $(".rollText").html("&nbsp;");
        //Capture the SRC the user is currently hovering OVER
        /*
        var currentImgSrc = evt.target.src;
        //Strip out "Up.gif" and replace it with "Over.gif"
        var newImgSrc = currentImgSrc.substring( 0, currentImgSrc.lastIndexOf( "Over.gif" ) ) + "Up.gif";
        //Swap Images
        $(evt.target).attr("src", newImgSrc );
        */
    }

    //Print Button Event Listener
    $("li.btnPrint a").bind('click', onPrintClickHandler);
    function onPrintClickHandler( event )
    {
        window.print();
    }

    //Large View Event Listener
    $("li.btnEnlarge a").bind('click', onEnlargeViewHandler);
    function onEnlargeViewHandler( event )
    {
        open1024( this.href );
        return false;
    }
}