$(document).ready(function() {
 $('.menu_home a').hover(function(){
    $('.menu_home a').addClass("home_hover");
	$('#sub_menu').slideUp('Slow').hide();
	$('.sub_mn_coupon').slideUp('Slow').hide();
    }, 
    function () {
     $('.menu_home a').removeClass("home_hover");
 });
 $('.menu_browsecatalog a').hover(function(){
    $('.menu_browsecatalog').addClass("browse_hover");
	$('.sub_mn_coupon').slideUp('Slow').hide();
    }, 
    function () {
     $('.menu_browsecatalog').removeClass("browse_hover");
	 $('.menu_browsecatalog').removeClass("browse_visited");
 });
 $('.menu_create a').hover(function(){
    $('.menu_create a').addClass("create_hover");
	$('#sub_menu').slideUp('Slow').hide();
	$('.sub_mn_coupon').slideUp('Slow').hide();
    }, 
    function () {
     $('.menu_create a').removeClass("create_hover");
	 $('.menu_create a').removeClass("browse_visited");
 });
 
 $('#redeemcert').hover(function() {
	$('#redeemcert').addClass("browse_hover");
	},function() {
	$('#redeemcert').removeClass("browse_hover"); 
 });

 
$('#sub_menu').hover(function(){
	$('.menu_browsecatalog').addClass("browse_visited");
    }, 
    function () {
     $('#sub_menu').slideUp('Slow').show();
	 $('.menu_browsecatalog').removeClass("browse_visited");
});

$('.sub_mn_coupon').hover(function(){
    }, 
    function () {
     $('.sub_mn_coupon').slideUp('Slow').show();
	 $('#redeemcert').removeClass("browse_visited");
});
 
 $('.menu_home a').click(function() {
   $('.menu_home a').addClass("home_visited");
   $('.menu_browsecatalog').removeClass("browse_visited");
	$('.menu_create a').removeClass("create_visited");
	$('#redeemcert').removeClass("browse_visited");
});
$('.menu_browsecatalog a').click(function() {
   $('.menu_browsecatalog').addClass("browse_visited");
   $('.menu_home a').removeClass("home_visited");
	$('.menu_create a').removeClass("create_visited");
	$('#sub_menu').slideDown('Slow').show();
	$('#redeemcert').removeClass("browse_visited");
});
$('.menu_create a').click(function() {
   $('.menu_create a').addClass("create_visited");
   $('.menu_browsecatalog').removeClass("browse_visited");
	$('.menu_home a').removeClass("home_visited");
	$('.#redeemcert').removeClass("browse_visited");
});
$('#redeemcert').click(function() {
   $('#redeemcert').addClass("browse_visited");
   $('#coupon_input').focus();
   $('.menu_home a').removeClass("home_visited");
	$('.menu_create a').removeClass("create_visited");
	$('.menu_browsecatalog').removeClass("browse_visited");
	$('.sub_mn_coupon').slideDown('Slow').show();
});

$('form[name=frm_subscribe]').submit(function(){
	$.post('/includes/processes.php',{checked: 'true', email: $('#email_register').val(), action:'update_subscription'},function(r){
		if(r!=''){
			$('#ty_sub').text('Thank you for subscribing!');
			$('#ty_sub').fadeIn('slow');
			$('#ty_sub').fadeOut(5000);
		}
	});
	return false;
});


$('.expand_links').click(function() {
	var currentId = $(this).attr('id');
	if($(this).text().toUpperCase()=="CLICK TO EXPAND"){
	   $('#wall_pic_'+currentId).slideDown('Slow').show();
	   $(this).text('Click to hidden');
	}else{
	   $('#wall_pic_'+currentId).slideUp('Slow').show();
	   $(this).text('Click to expand');
	}
});

$('#coupon_form_top').submit(function(){
	var cert = $('#coupon_input').val(), subtotalval = ($('#cert').length>0 ? $('#totalsubtotal').text() : 0);
	$('#coupon_apply_btnnormal').removeClass('btn_75_25'); $('#coupon_apply_btnnormal').addClass('btn_75_25_click');
	$.post('/includes/processes.php',{coupon:cert,action:'certificate_redeem'},function(data){
		$('#result_status').html(data);
		if(data!=''){
			$('#result_img').html('<img id="apply_icon" src="/images/apply_icon.png" alt="apply" />');
			$('#result_status').html('<span class="statusapply">This certificate has been applied!</span>');
			
			setCookie('cert',data,7);
			if($('#cert').length>0){
				result = data.split('|'); code = result[0]; msg = result[1]; amount = result[2]*1; type = result[3];
				
				if(amount > 0){
					if(type=='d'){
						amt=amount;
					}else{
						amt=(subtotalval*1) * (amount/100);
					}
					$('#cert').val(amt);
					$('#cert_info').val(code);
					$('#cert_con').show();
					$('#cert_code').show();
					$('#cert_code').html(code);
					$('#cert_desc').html(msg);
				}else{
					$('#cert').val(0);
					$('#cert_info').val('');
					$('#cert_con').hide();
					$('#cert_code').hide();
					$('#cert_code').html('');
					$('#cert_desc').html('');
				}
				updateTotals();
			}
		}else{
			$('#result_img').html('');
			$('#result_status').html('<span>This certificate is invalid.</span>');
		}
		$('#coupon_apply_btnnormal').removeClass('btn_75_25_click'); $('#coupon_apply_btnnormal').addClass('btn_75_25');
	});
	return false;
});


$('#coupon_cart_submit').click(function(){
		var coupon_code = $('#coupon_input_cart').val(), result='', amt=0;
		$.post('/includes/processes.php',{coupon:coupon_code,subtotal:$('#totalsubtotal').text()*1,action:'coupon_redeem'},
		function(data){
			$('#coupon_status').html(data);
			result = data.split('|'); code=result[0]; msg=result[1]; amount=result[2], type=result[3];
			
			if(msg!=''){
				setCookie('coup',data,7);
				if(type=='d'){
					amt=amount;
				}else{
					amt=($('#totalsubtotal').text()*1) * (amount/100);
				}
				$('#coupon_status').html('<span class="statusapply">'+msg+'</span>');
				$('#coupon_cart_container').val('<span class="cart_total">'+code+'</span>');
				$('#discount').val(amt);
				
				updateTotals();
			}else{
				$('#coupon_status').html('<span>This code is invalid.</span>');
			}
		});
		return false;
});
 
});


function checkState(state){
	var tax = 0, totalval = $('#total').text()*1;
	if(totalval > 0){
		if(state != 'IL'){
			$('input[name="tax"]').val(0);
			$('#taxval').text('0.00');
			updateTotals();
		}else{
			var newtotal = (totalval - $('input[name="tax"]').val());
			tax = totalval * .07;
			$('input[name="tax"]').val(tax);
			$('#taxval').text(tax.toFixed(2));
			newtotal = tax + newtotal;
			$('#total').text(newtotal.toFixed(2));
		}
	}else{
		$('input[name="tax"]').val('0.00');
		$('#taxval').text('0.00');
	}
}



function updateTotals(){
	var totalsubtotal=0.00, total=0.00, taxval=0.00, shipping=0.00, 
	discount=$('#discount').val()*1, certificate=$('#cert').val()*1, promo=$('#promo').val()*1,
	tax=$('#tax').val()*1;
			// Run subtotal update on load
	$('div.cart_item').each(function(){
			var subtotal=0.00;
			itemID=$(this).attr('id');
		$('div#'+itemID+'prices input.quan_input').each(function(i){
			qty=$(this).val()*1;
			id=$(this).attr('id');
			hval=$(this).attr('hval');
			cid=$(this).attr('cid');
			if(qty=='') $(this).val('0');
			
			subtotal+=($('div#'+itemID+'prices #'+id+'price').val()*1)*qty;

			$.post('/includes/processes.php',{heightval: hval, cartval: cid, qtyval: qty, action:'update_qty'});
		});
		$('#'+itemID+'subtotal').text(subtotal.toFixed(2));
		totalsubtotal+=subtotal;
	});
	
	if($('#shipping').length > 0){
		ship=$('#shipping').val();
		shipping = $('#shipping'+ship).val()*1;
	}else{
		shipping=0;
	}
	total+=totalsubtotal+shipping - (discount+certificate+promo);
	
	total+= tax;
	
	if(totalsubtotal > 0){
		$('#totalsubtotal').text(totalsubtotal.toFixed(2));
		$('#total').text(total.toFixed(2));
		if($('#checkout_details').length > 0){
			$('#checkout_details').slideDown('slow').show();
			$('#totals_details').slideDown('slow').show();
			$('#notice').hide();
		}
	}else{
		if($('#checkout_details').length > 0){
			$('#checkout_details').hide();
			$('#totals_details').hide();
			$('#notice').show();
		}
		$('#total').text('0.00');
	}

	return false;
}


function getCookie(c_name)
{ var c_start,c_end;
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + '=');
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(';',c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return '';
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ '=' +escape(value)+
((expiredays==null) ? '' : ';expires='+exdate.toUTCString())+'; path=/';
}


function checkCert()
{
	var data =getCookie('cert'), result='', code='', msg='', amount='', type='';
	if (data!=null && data!=''){
	  	result = data.split('|'); code = result[0]; msg = result[1]; amount = result[2]*1; type = result[3];
		
		$.post('/includes/processes.php',{code: code,action:'check_cert'},
		function(r){
			if(r == 1 && amount > 0){
				if(type=='d'){
					amt=amount;
				}else{
					amt=($('#totalsubtotal').text()*1) * (amount/100);
				}
				$('#cert').val(amt);
				$('#cert_con').show();
				$('#cert_code').show();
				$('#cert_code').html(code);
				$('#cert_desc').html(msg);
				if($('#cert_info').length > 0){
					$('#cert_info').val(data);
				}
				updateTotals();
			}
			if(data == '' || data == 0 || amount == 0){
				setCookie('cert','',-1);
				$('#cert').val(0);
				$('#cert_con').hide();
				$('#cert_code').hide();
				$('#cert_code').html('');
				$('#cert_desc').html('');
				if($('#cert_info').length > 0){
					$('#cert_info').val('');
				}
			}
		});
	}
}

function checkCoup()
{
	var data =getCookie('coup'), result='', code='', msg='', amt=0;
	if (data!=null && data!=''){
	  	result = data.split('|'); code = result[0]; msg = result[1]; amount = result[2]*1, type = result[3];
		
		$.post('/includes/processes.php',{code: code,action:'check_coup'},
		function(r){
			if(r == 1 && amount > 0){
				if(type=='d'){
					amt=amount;
				}else{
					amt=($('#totalsubtotal').text()*1) * (amount/100);
				}
				$('#coupon_status').html('<span class="statusapply">'+msg+'</span>');
				$('#coupon_input_cart').val(code);
				$('#discount').val(amt);
				updateTotals();
			}else{
				setCookie('coup','',-1);
				$('#coupon_status').html('');
				$('#coupon_input_cart').val('');
				$('#discount').val('0.00');
			}
		});
	}
}

function remCo(){
	var coup=getCookie('coup'), cert=getCookie('cert');
	if (coup!=null && coup!=''){
		setCookie('coup','',-1);
	}
	if (cert!=null && cert!=''){
		setCookie('cert','',-1);
	}
}
