$(document).ready(function() {
						   
	$(".mm").hover(function() {
		$(this).addClass("mmhover");	
		$(".megamenu").fadeIn(300);		
	});
	
	$(".megamenu").mouseleave(function() {
		$(".mm").removeClass("mmhover");	
		$(".megamenu").fadeOut(300);		
	});	
	
	//twitter
	 $("#tweet").tweet({
            username: "ebonyIvoryHAIR1",
            count: 1,
            loading_text: "loading tweets..."
     });
	
	
	//ipad and iphone fix
    $(".mm").click(function(){
       	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {	
			if (!$(".megamenu").hasClass('active')) {
				$(this).addClass("mmhover");
       			$(".megamenu").fadeIn(300).addClass('active');	
			} else {
				$(this).removeClass("mmhover");
       			$(".megamenu").fadeOut(300).removeClass('active');					
			}
		}
   	 });
	
	$("header nav a.item").hover(function() {
		$(".mm").removeClass("mmhover");	
		$(".megamenu").fadeOut(300);		
	});
	
	$("#ccdetails").submit(function() {
		$("#processing").html("<p>Your Order is being processed. This could take a couple of minutes, please do not close your browser.</p>");
	});
	
	
	$('#feature .slider') 
		.cycle({ 
    		fx:     'scrollHorz', 
    		speed:  '2000', 
    		timeout: 6000, 
    	pager:  '#feature nav,.brand nav' 
	});
		
		
	// brands
	$(function() {
    $('.brand .slider').each(function(i) {
        $(this).after('<div class="imgSelect imgSelect'+i+'">').cycle({
            fx:     'fade',
            speed:  'fast',
            timeout: 5000,
            pager:  '.imgSelect' + i
            });
        });
    });

	
	// twitter/facebook logos
	$(".socialmedia").hover(function() {

		$("img",this).stop().animate({
			"opacity": 1
			},500, 'linear')	
		
		},function() {
	
		$("img",this).stop().animate({
			"opacity": 0.25
			}, 500);
	
	});
	
	// CLEAR FORMS
	$('input[type=text], textarea').each(function() {
		$(this).attr('data-default',$(this).val());
	});
	
	$('input.info, textarea').live('focus',function() {
		if ($(this).val() == $(this).attr('data-default')) {
			$(this).val('');
		}
	});
	
	
	$('input.info, textarea').live('blur',function() {
		if ($(this).val() == '') {
			$(this).val($(this).attr('data-default'));
		}
	});	


	// Product List
	
	$("#sort_rows, #sort-data, #filter-brand, #filter-category").live('change',function(e) {
			productDisplay(1,$("#sort-data").val(),$("#filter-brand").val());
			//return false;
	});
	
	$("#pagination a").live('click',function(e) {
			$("#current_page").val($(this).attr('data-pageno'));
			productDisplay($(this).attr('data-pageno'),$("#order").val(),$("#filter-brand").val());
			return false;
	});
	
	$("#order").live('click',function(e) {
			
			productDisplay(1,$("#sort-data").val(),$("select#filter-brand").val());
			
			if ($("#order").attr('data-order') == 'DESC') {
				$("#order").attr('data-order','ASC');
				$("#order img").attr('src','/img/up.gif');
			} else {
				$("#order").attr('data-order','DESC');
				$("#order img").attr('src','/img/down.gif');
			}
			
			return false;
	});
	
	// copy billing info to shipping info
	$("#copy").live('click',function(e) {
		 if ($("#copy").is(":checked")) {							 
			$("#shipping_name").val($("#billing_name").val());	
			$("#shipping_address").val($("#billing_address").val());
			$("#shipping_city").val($("#billing_city").val());
			$("#shipping_state").val($("#billing_state").val());
			$("#shipping_postcode").val($("#billing_postcode").val());
			$("#shipping_phone").val($("#billing_phone").val());
		 } else {
			$("#shipping_name").val($("#shipping_name").attr('data-default'));	
			$("#shipping_address").val($("#shipping_address").attr('data-default'));
			$("#shipping_city").val($("#shipping_city").attr('data-default'));
			$("#shipping_state").val('WA');
			$("#shipping_postcode").val($("#shipping_postcode").attr('data-default'));
			$("#shipping_phone").val($("#shipping_phone").attr('data-default')); 
		 }
	});
	
	// update shipping cost
	/*$("#shipping_postcode").live('keyup',function(e) {
			surcharge=0;
			if ($("#shipping_state").val() == "NT") { // northern territory surcharge
				surcharge=1;
			}
			if ($("#shipping_postcode").val() >= 6000 && $("#shipping_postcode").val() <= 6199) {
				calcShipping(1,1,surcharge);
				calcShipping(2,1,surcharge);
			} else if ($("#total_weight").val() <= 1) { 
				calcShipping(1,2,surcharge);
				calcShipping(2,2,surcharge);
			} else if ($("#total_weight").val() <= 3){ 
				calcShipping(1,3,surcharge);
				calcShipping(2,3,surcharge);
			} else if ($("#total_weight").val() >= 5){
				calcShipping(1,4,surcharge); 
				calcShipping(2,4,surcharge); 
			}
		
	});*/
	
	/*$("#wa_shipping").live('change',function(e) {
		
		if ($(this).val() == 1) { // perth
				calcShipping(1,1,0,1);
				calcShipping(2,1,0,1);
		} else if ($(this).val() == 2 && $("#total_weight").val() <= 1) {
				calcShipping(1,2,0,2);
				calcShipping(2,2,0,2);
		} else if ($(this).val() == 2 && $("#total_weight").val() <= 3) {
				calcShipping(1,3,0,2);
				calcShipping(2,3,0,2);
		} else if ($(this).val() == 2 && $("#total_weight").val() >= 5) {
				calcShipping(1,4,0,2);
				calcShipping(2,4,0,2);
		} 
	});
	
	$("#shipping_state").live('change',function(e) {
			if ($("#total_price").val() < 100) {
				if ($(this).val() == 'WA') {
					calcShipping(1,1,0);
					calcShipping(2,1,0);
				} else if ($(this).val() == 'NT' && $("#total_weight").val() <= 1) {
					calcShipping(1,2,1,0);
					calcShipping(2,2,1,0);
				} else if ($(this).val() == 'NT' && $("#total_weight").val() <= 3) {
					calcShipping(1,3,1,0);
					calcShipping(2,3,1,0);
				} else if ($(this).val() == 'NT' && $("#total_weight").val() >= 5) {
					calcShipping(1,4,1,0);
					calcShipping(2,4,1,0);
				} else if ($(this).val() != 'NT' && $("#total_weight").val() <= 1) {
					calcShipping(1,2,0,0);
					calcShipping(2,2,0,0);
				} else if ($(this).val() != 'NT' && $("#total_weight").val() <= 3) {
					calcShipping(1,3,0,0);
					calcShipping(2,3,0,0);
				} else if ($(this).val() != 'NT' && $("#total_weight").val() >= 5) {
					calcShipping(1,4,0,0);
					calcShipping(2,4,0,0);
				} 
			}
	 });
	
	var calcShipping = function(section,shipping_id,surcharge,wa) {
		$.ajax( 
				{ 
					type: "POST", 
					url: "/includes/update_shipping.inc.php", 
					data: "section="+section+"&shipping="+shipping_id+"&surcharge="+surcharge+"&wa="+wa,
					success: 
						function(data) 
						{ 
							if (section == 1) {
								$("#shipping_method").html(data);
							} else if (section == 2) {
								$("#shipping").html("$"+data);
								$("#shipping_cost").val(data);
								total = (parseInt($("#total_price").val())+parseInt(data)).toFixed(2);
								$("#grand_total").html("$"+total);
								$("#payment_amount").val(total);
							}
						}
				});
	}*/
	
	// checkout ul background
	$("div ul.review:nth-child(even)").attr('class','review bg');
	$("div ul.summary-review:nth-child(odd)").attr('class','summary-review bg');
	
});

var productDisplay = function(page,order,brand) {
	
		var orderby = order;
		var sortData = $("#order").attr('data-order');
		var filterBrand = brand;
		var rows = $("select#sort_rows").val();
		var section = $("input#section").val();
		var category = $("#filter-category").val();
		var brand = $("input#brand").val();
		var page = page;

		$.ajax( 
				{ 
					type: "POST", 
					url: "/includes/update_product_list.inc.php", 
					data: "section="+section+"&brand="+filterBrand+"&category="+category+"&rows="+rows+"&order="+orderby+"&sort="+sortData+"&page="+page,
					success: 
						function(data) 
						{ 
							$("#wrapper").html(data);
						}
		});	
		
}

