/*
* global js for Skinnable Artist Pages
* include at BOTTOM of page
*/

// needed for UP, UF
var site_id = 2;

/**
* Global rollovers: to make any img a rollover, give it class "rollover" and
* put its rollover image src in the "id" attribute.

* If more rollovers need to be bound later (i.e. on AJAX load), use context
* to limit binding to just the new content.
*/
function ir_bind_rollovers(context)
{
    var rollIn, rollOut;

    rollIn = rollOut = function(event)
    {
        if (typeof rollIn.called == "undefined" && event.type == 'mouseout') {
            //element was under cursor while page loaded
            return false;
        }
        rollIn.called = true
        var temp = this.src;
        this.src = this.id;
        this.id = temp;
    }

    var rollovers = $('img.rollover', context);

    //preload rollover images
    rollovers.each(function() { new Image().src = this.id; });

    //bind image swapping
    rollovers.hover(rollIn, rollOut);

    location.search.match('rollover_debug') && alert('rollovers bound');
}

$(function()
{
    ir_bind_rollovers();

    /**
    * Pop up Privacy Policy, UF print view links
    */
    $('#footer a.privacy').bind('click', function()
    {
        window.open(this.href, 'Privacy',
            'width=720,height=540,scrollbars=yes,menubar=no,resizable=no,location=no,status=yes,toolbar=no'
        ).focus();
        return false;
    });
    $('div.user_tools a.print').bind('click', function()
    {
        window.open(this.href, 'printView', 'width=750,height=500,scrollbars=yes').focus();
        return false;
    });

    /**
    * Validate search form
    */
    $('#artistSearchForm').bind('submit', function()
    {
        var inp = $(this.artistSearch);
        inp.val(inp.val().replace(/^\s*|\s*$/g, ''));
        if (!inp.val()) {
            alert('Please enter an artist to search for...');
            return false;
        }
        return true;
    });

    /**
    * Bind hover control for artist dropdown
    */
    var nav = $('#aa_nav');
    //small layout adjustment for safari
    $.browser.safari && nav.css('top', '88px');
    var ir_nav_timeout;
    function startHide()
    {
        ir_nav_timeout = setTimeout(function(){nav.hide();}, 100);
    }
    function stopHide()
    {
        clearTimeout(ir_nav_timeout);
    }

    $('#label_nav_artist').hover(
        function()
        {
            stopHide();
            nav.show();
        },
        startHide);

    /**
    * Bind hover control for music/merch subnav
    */
    var store_subnav = $('#store_subnav');
    //small layout adjustment for safari
    $.browser.safari && store_subnav.css('top', '88px');
    var ir_store_subnav_timeout;
    function subnavStartHide()
    {
        ir_store_subnav_timeout = setTimeout(function(){store_subnav.hide();}, 100);
    }
    function subnavStopHide()
    {
        clearTimeout(ir_store_subnav_timeout);
    }

    $('#label_nav_store').hover(
        function()
        {
            subnavStopHide();
            store_subnav.show();
        },
        subnavStartHide);

    /**
    * Bind hover control for AV subnav
    */
    var label_av_subnav = $('#label_av_subnav');
    //small layout adjustment for safari
    $.browser.safari && store_subnav.css('top', '88px');
    var ir_av_subnav_timeout;
    function avSubnavStartHide()
    {
        ir_av_subnav_timeout = setTimeout(function(){label_av_subnav.hide();}, 100);
    }
    function avSubnavStopHide()
    {
        clearTimeout(ir_av_subnav_timeout);
    }

    $('#label_av_nav').hover(
        function()
        {
            avSubnavStopHide();
            label_av_subnav.show();
        },
        avSubnavStartHide);		
// End AV subnav binding		
		
    /**
    * Fix IE flash outlines.
    */
    $.browser.msie && $('embed').each(function(){
        this.outerHTML = this.outerHTML;
    });

    /**
    * Load cart status
    */
    if (document.cookie.search(/items=(units|upc)\d/i) > -1) {
        $.ajax({
            url:'_sap_cart_head.php' + location.search + '&ajax_parent='
                + encodeURIComponent(location.pathname.replace(/^([^\/]*\/)+/, '')),
            type: 'GET',
            dataType: 'html',
            success: function(html)
            {
                $('#cart').html(html);
                //force IE layout adjust
                $.browser.msie && $('body').hide().show();
            }
        });
    } //else { alert(document.cookie);}

    /**
    * Load one-click area (homepage only)
    */
    location.pathname.match('artist_home.php') &&
    $.ajax({
        url:'_sap_oneclick_signup.php?artist_id=' + location.search.match(/artist_id=(\d+)/)[1]
            + '&skin_path=' + encodeURIComponent(ir_sap_skin_path)
            + '&artist_name=' + encodeURIComponent(ir_sap_artist_name),
        type: 'GET',
        dataType: 'html',
        success: function(html)
        {
            $('#artist_links').prepend(html);
            // only allow one click
            $('#artist_mailing').bind('click', function()
            {
                this.onclick = function() { return false; }
                $(this).css('cursor', 'default');
            });
            //force IE layout adjust
            $.browser.msie && $('#right_column_370').hide().show();
        }
    });
});
