/**************************************************
CHANGELOG
2011-05-17 - A Conley (DSI)
    Added processing to connect the course shown in All Courses (if shown) and the one shown on the course tab
***************************************************/
$(document).ready(function() {
    //initialize tabs
    $('#booklisttabs').tabs();

    //enable course selection button if a section is selected
    $('#sectionlb').click(function() { $('#Button1').removeAttr('disabled'); $('#Button1').removeAttr('class'); });

    //gray backdrop for 2nd row of tabs, only show if there's a second row.
    if ($('#booklisttabs ul.tabs-nav').length > 0) {
        if ($('#booklisttabs ul.tabs-nav').height() > 30)
            $('#booklisttabs ul.tabs-nav').attr("style", "background: transparent url(skins/skin_1/images/booklist/tab2ndrowbg.gif) repeat-x right bottom;");
    }

    //Highlight row when New/Used/eBook clicked
    $('.addcartform input').click(function() {
        selectRow(this);
    });
    //Highlight row when New/Used/eBook preselected.
    $('.addcartform input:checked').each(function() {
        selectRow(this);
    });

    //Row remove link
    $('.unselect a').click(function() {
        var row = $(this).closest('tr');
        var altrow = $('#' + getAltID(row.attr('id')));
        $(row).find('.addcartform input').removeAttr('checked');
        $(row).find('.subs input').removeAttr('disabled');
        $(row).attr('class', $(row).attr("class").replace(" selected", ""));
        if ($(altrow).length > 0) {
            $(altrow).find('.addcartform input').removeAttr('checked');
            $(altrow).find('.subs input').removeAttr('disabled');
            $(altrow).attr('class', $(altrow).attr("class").replace(" selected", ""));
        }
        return false;
    });

    //details rollover
    $('#bodyWrapper .info').hover(
		function() {
		    $(this).addClass("infobackdrop");
		    $(this).find('.productoverlay').fadeIn('fast');
		},
		function() {
		    var info = this;
		    $(this).find('.productoverlay').fadeOut('fast', function() { $(info).removeClass("infobackdrop"); });
		}

	);

    //rollover close link
    $('.productoverlay a.close').click(function() {
        $(this).closest('.productoverlay').fadeOut('fast', function() {
            $(this).closest(".info").removeClass("infobackdrop");
        });
        return false;
    });

    //Automatically show subs overlay if the link to it is tagged with the "autoload" css class.
    if ($('a#ShowOverlay.autoload').length > 0) {

        if (imgLoader == undefined) //just in case thickbox.js isn't initialized.
        {
            imgLoader = new Image(); // preload image
            imgLoader.src = tb_pathToImage;
        }
        tb_show("", $('a#ShowOverlay.autoload').attr("href"), "");

    }



    //Select All links
    $('a.selectall').click(function() {
        var type = $(this).attr("type"); // attribute type="new" or type="used" in the link.

        $(this).closest('table').find(".addcartform input." + type).click();
        return false;
    });

    $('.booklistright #pnlStudentLogin .title').click(function() {
        $(this).next('.inner').slideDown("fast");
    });


});

function selectRow(radio)
{
	
	var name = $(radio).attr('name');
	$(".addcartform input[name='"+name+"']").closest('tr').removeClass('selected');
	$(radio).closest('tr').addClass('selected');
}

function setSub(value)
{
	tb_remove();
	if (value=="1" || value=="0")
	{
		$('.subs input').attr("checked","");
		$('.subs input[value='+value+']').attr("checked","checked");
	}
	return false;
}

function selectedVariant(prodid, suffix, prodtype, obj) {
    var sub0 = $('#sub_' + prodid + '_0' + suffix);
    var sub1 = $('#sub_' + prodid + '_1' + suffix);
    var altvar = $('#' + getAltID(obj.id));

    if (sub0.attr("type") == 'radio') {
        if (prodtype == 'used' || prodtype == 'new') {
            sub1.attr("disabled", false);
        } else {
            sub0.click();
            sub1.attr("disabled", true);
        }
    }
    if (altvar.length > 0 && altvar.attr("type") == 'radio' && altvar.attr("checked") != obj.checked) {
        altvar.click();
    }

}

function selectedSub(obj) {
    var sub = null;
    if (obj != null) {
        sub = $('#' + getAltID(obj.id));
        if (sub.length > 0 && sub.attr("type") == 'radio' && sub.attr("checked") != obj.checked) {
            sub.click();
        }
    }
}

function getAltID(id) {
    if (id.substr(id.length - 4) == '_all') {
        return id.substr(0, id.length - 4);
    }
    else {
        return id + '_all';
    }
}

