var courses_per_page = 15;
var original_total = '';
var price_sorting = [[3,0]]; 
var hours_sorting = [[2,0]];

$(document).ready(function(){
    $('.pagesize').val(courses_per_page);
	$("label.overlabel").overlabel();
	$("#quote-rotator").cycle({ 
		fx:    'fade',
		speed:  1000,
		timeout: 6000,
		pause: 1,
		random: 1
	});
	tooltip();

	$.tablesorter.defaults.sortList = [[0,0]]; 



	function getPriceSort() {
		if (price_sorting == "3,0") {
			price_sorting = [[3,1]];
		}        
		else {
			price_sorting = [[3,0]];
		}
	}

	function getHoursSort() {
		if (hours_sorting == "2,0") {
			hours_sorting = [[2,1]];
		}        
		else {
			hours_sorting = [[2,0]];
		}
	}

	$("#sort-price").click(function() { 
		getPriceSort();
		$("#table-course-list-all").trigger("sorton",[price_sorting]); 
		return false; 
	}); 
	$("#sort-hours").click(function() { 
		getHoursSort();
		$("#table-course-list-all").trigger("sorton",[hours_sorting]); 
		return false; 
	}); 






    $("#table-account-summary").tablesorter({ 
        headers: { 
           3: { 
                sorter: false 
            }, 
            6: { 
                sorter: false 
            } 
        }
    });  

	$("#member-login-module-sidebar").hide();
	$("p#member-login a").click(function () {
		$("#member-login-module-sidebar").slideToggle("medium");
		return false;
	});   

if ($("#table-course-list").find('td:first').attr('colspan') != '5')
{
    $("#table-course-list").tablesorter({
		sortList: [[4,1]],
		headers: { 
            3: { 
                sorter: false
            }, 
            5: { 
                sorter: false 
            }, 
            6: { 
                sorter: false 
            }, 
            7: { 
                sorter: false 
            } 
        }
    });
}
    $("#table-course-list-all").tablesorter({ 
		sortList: [[0,0],[1,0],[4,1]],
        headers: { 
            5: { 
                sorter: false 
            }, 
            6: { 
                sorter: false 
            }, 
            7: { 
                sorter: false 
            } 
        }

    }).tablesorterPager({
		container: $(".course-pagination-both"),
		cssPrev: '.previous-course',
		cssNext: '.next-course',
		size: courses_per_page,
		positionFixed: false,
		hidePagerLinks: true
	});
    
    original_total = $('#cart-total').text();
    
	$('#coupon-button').click(function(){
		apply_coupon_code();
		return false;
	});
	$('#coupon-codes').keydown(function(e){
		var code = (e.keyCode ? e.keyCode : e.which);
		if (code == 13)
		{
			apply_coupon_code();
			return false;
		}
	});
    
    $('a[rel="external"]').attr('target', '_blank');
    
    if ($('#table-course-list-all tbody tr').length < courses_per_page)
    {$('.course-pagination-both').hide();}

    $('.course-pagination-both .previous-course').hide();

    $("#zip").mask("99999",{placeholder:" "});
    $("#phone").mask("999-999-9999",{placeholder:" "});


});



function apply_coupon_code()
{
	var code = $('#coupon-codes').val();
	if (code != '')
	{
		$('#coupon-button').hide();
		$('#coupon-button').after('<img src="/images/icons/ajax-loader.gif" id="coupon-loading" />');

		$.post(
			'/cart/.ajax_validate_coupon_code/',
			{'coupon_code':code},
			function(data) {
				$('#coupon-loading').remove();
                                $('#coupon-message').html(data.msg);//.slideDown();
				if (data.error == 0)
				{
                                        $('#cart-total').html('<span id="original-total"><span style="color:#666;text-decoration:line-through;">'+original_total+'</span>&nbsp;</span>'+data.output);
				}
				else
				{$('#coupon-codes').focus();}
				$('#coupon-button').show();
			},
			'json'
/*
			function(data) {alert(data);},
			'test'
*/
		);
	}
}

function refresh_cart()
{
    $('#utility-cart a').html('View Cart (Refreshing...)').load('/cart/.ajax_refresh_cart/');
}