var openSubcat = 0;
function loadCategory(category) {
	$('div#backstretch img').attr('src', '/img/bg-'+category+'.jpg');
	$('#content').hide();
	$('#looks-container').hide();
	window.location.hash = category;
	$.get('/'+category, function(data) {
		$('.panel').html(data);
	});

}

function toggle(id) {
	$(id).toggle('fast');
}

function loadSubCategory(id) {
	openSubcat = id;
	if ( $('#subcat-'+id+' > a').is( '.subcategoryActive' ) )
	{
		$('#content-'+id).stop().slideUp('fast');
	}
	else
	{
		$.get('/product_subcategories/view/'+id, function(data){
			$('#content-'+id).html(data);
		});
	}
	$('#backstretch').cycle('stop');
}

function loadLCCategory(id) {
	window.location.hash += '/'+id;
	$.get('/product_subcategories/view/'+id, function(data){
		$('.accordionContent').html(data);
	});
	category = category.replace(" ", "_");
	$('div#backstretch img').attr('src', '/img/bg-'+category+'.jpg');
	$('#backstretch').cycle('stop');
}

function loadLCSubCategory(id) {
	openSubcat = id;
	if ( $('#subcat-'+id+' > a').is( '.subcategoryActive' ) )
	{
		$('div.accordionContent').slideUp('fast');
	}
	else
	{
		$.get('/product_subcategories/lastchance/'+id, function(data){
			$('#content-'+id).html(data);
		});
	}
	$('#backstretch').cycle('stop');
}

function loadProduct(id) {
	$.get('/products/view/'+id, function(data){
		$('.panelThree').html(data);
	});
}

function loadProductPage(category, subcategory) {
	$(".panel").toggle(true);
	category = category.replace(" ", "_");
	openSubcat = subcategory;
	$.get('/'+category, function(data) {
		$('.panel').html(data);
	});
	$.get('/product_subcategories/view/'+subcategory, function(subcat_data){
		$('#content-'+subcategory).html(subcat_data);
	});
	$('div#backstretch img').attr('src', '/img/bg-'+category+'.jpg');
}

function loadLCProductPage(category, subcategory) {
	$(".panel").toggle(true);
	openSubcat = subcategory;
	category = category.replace(" ", "_");
	$.get('/'+category, function(data) {
		$('.panel').html(data);
	});
	$.get('/product_subcategories/lastchance/'+subcategory, function(subcat_data){
		$('#content-'+subcategory).html(subcat_data);
	});
	$('div#backstretch img').attr('src', '/img/bg-'+category+'.jpg');
}

	// PageLoad function
	// This function is called when:
	// 1. after calling jQuery.historyInit();
	// 2. after calling jQuery.historyLoad();
	// 3. after pushing "Go Back" button of a browser
	//jQuery.noConflict();
function pageload(hash) {
	// hash doesn't contain the first # character.
	if(hash)
	{
		if( hash != 'lc_eyes' && hash != 'lc_lips' && hash != 'lc_nails' && hash != 'lc_face' && hash != 'lc_accessories' && hash !='lc_brush_sleeves' && hash != '' )
		{
			$("#products").parent().next('.toggle').show();
			$("#lastchance").parent().next('.toggle').hide();
		}
		else
		{
			$("#products").parent().next('.toggle').hide();
			$("#lastchance").parent().next('.toggle').show();
		}
		$(".active").toggleClass("active");
		$("."+hash+"").toggleClass("active");
		$(".panel").toggle(true);
    	loadCategory(hash);
    }
}

//Checkout functions
function completeStep1(direction) {
	if ( direction == 'forward' )
	{
		$("#step1").toggle("slide", {direction: "left"}, 1000);
		$("#step2").toggle("slide", {direction: "right"}, 1000);
		$(".stepOne").removeClass('stepOneActive');
		$(".stepTwo").addClass('stepTwoActive');
		$.ajax({
			type: "POST",
			url: window.location.pathname,
			data: $("#OrderCheckoutForm").serialize(),
			success: function(data){
				$("#response").html(data);
			}
		});
	}
	else
	{
		$("#step1").toggle("slide", {direction: "left"}, 1000);
		$("#step2").toggle("slide", {direction: "right"}, 1000);
		$(".stepOne").addClass('stepOneActive');
		$(".stepTwo").removeClass('stepTwoActive');
	}
}

function completeStep2( order_id, direction ) {
	if ( direction == 'forward' )
	{
		$('#completeOrderBtn').toggle();
		$.ajax({
			type: "POST",
			url: '/orders/process/'+order_id,
			data: $("#OrderCheckoutForm").serialize(),
			success: function(data){
				$("#response_message").html(data);
				$("#step2").toggle("slide", {direction: "left"}, 1000);
				$("#step3").toggle("slide", {direction: "right"}, 1000);
				$(".stepTwo").removeClass('stepTwoActive');
				$(".stepThree").addClass('stepThreeActive');
			}
		});
	}
	else
	{
		$('#completeOrderBtn').toggle();
		$("#step2").toggle("slide", {direction: "right"}, 1000);
		$("#step3").toggle("slide", {direction: "left"}, 1000);
		$(".stepTwo").addClass('stepTwoActive');
		$(".stepThree").removeClass('stepThreeActive');
	}
	return true;
}

function changePaymentType() {
	type = $('#payment_type').val();
	if ( type == 'Credit' )
	{
		$('#cancelOrderBtn').hide();
		$('#completeOrderBtn').show();
		$('#cc_payment_info').show();
		$('#pp_payment_info').hide();
		$('#mo_payment_info').hide();
		$('#addl_payment_info').toggle();
	}
	else if ( type == 'Paypal')
	{
		$('#cancelOrderBtn').hide();
		$('#completeOrderBtn').hide();
		$('#pp_payment_info').show();
		$('#cc_payment_info').hide();
		$('#mo_payment_info').hide();
		$('#addl_payment_info').toggle();
	}
	else if ( type == 'MO' )
	{
		$('#cancelOrderBtn').hide();
		$('#completeOrderBtn').show();
		$('#mo_payment_info').show();
		$('#pp_payment_info').hide();
		$('#cc_payment_info').hide();
		$('#addl_payment_info').toggle();
	}
	else
	{
		$('#completeOrderBtn').hide();
		$('#cancelOrderBtn').show();
	}
}

function filterStatus() {
	window.location = '/orders/index/filter:'+$('#status_filter').val();
}

