$(document).ready(function() {

	$('.left_menu_cat_do_not_display').each(function(i){
		//alert();
		$('#prod_cat_' + $(this).html()).css('display', 'none');
	});

});

function format(prod) {
	return prod.name;
}

function forgot_password(){
	
	email_add = prompt("Forgot your password? \n\n Please enter your email and your password will be sent to you in an email.", "Enter your email address here.");
	 $.ajax({
		   type: "POST",
		   url: node+'ajax/account.php',
		   data: {
					action_do : 'forgot_password',
					email : email_add
		   },
		   cache: false,
		   dataType: 'json',
		   beforeSend : function(){
			   //displayLoader(true);
		   },
		   complete : function(){
			   //displayLoader(false);
		   },
		   success: function(msg){
				if(msg.success == true){
					alert(msg.msg);
				}else{
					alert(msg.msg);
				}
		  }
	});
	
}

function searchProducts(){
	$("#search_prods").autocomplete(node+'ajax/search_prods.php', {
		multiple: false,
		minChars : 3,
		parse : function(data) {
			return $.map(eval(data), function(row) {
				return {
					data: row,
					value: row.name,
					result: row.name // selected and displayed in input text
				}
			});
		},
		formatItem: function(item) {
			return format(item);
		}
	}).result(function(e, item) {
		location.href = 'http://www.rockwarehouse.co.uk/product/?id=' + item.id;
	});
}

function getWindowSize() {
  var myWidth = 0, myHeight = 0;
  
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
    
  return [myWidth, myHeight];

}

function init(){
	s = getWindowSize();
	if(s[0] >= 1024){
		$('#frame').css('margin-left', ((s[0] - 1024) / 2) + 'px') 
	}else{
		$('#frame').css('margin-left', '0px') 
	}
}

function selectGalleryItem(item_index){	
	$('.gallery_item_gen').css('borderLeft', '5px solid #FFF');
	$('.gallery_item_' + item_index).css('borderLeft', '5px solid #900');
	
	$('.gallery_img').css('display', 'none');
	$('.gallery_img_' + item_index).css('display', 'block');	
}

function switchTab(index){
	$('.tab_products').css('display', 'none');
	$('.tab_products_' + index).css('display', 'block');
}

function shop(action, product_id){
	
	if(product_id == undefined){
		product_id = 0;	
	}
	
	if(action == 'delete_product'){
		if(confirm('Would you like to delete this product?') == false){
			return;
		}
	}
	
	 $.ajax({
		   type: "POST",
		   url: node+'ajax/shop.php',
		   data: {
					action : action,
					id : product_id
		   },
		   cache: false,
		   dataType: 'json',
		   beforeSend : function(){
			   //displayLoader(true);
		   },
		   complete : function(){
			   //displayLoader(false);
		   },
		   success: function(msg){
				if(msg.success == true){
					
					if(action == 'add_product'){
						$('.add_do_cart_' + product_id).empty();
						$('.add_do_cart_' + product_id).html('<img src="'+node+'rock_admin/pics/icons/ok_16.png" alt="Product in cart"> <span style="font-size:11px; color:#999">Product in cart, edit quantity at checkout</span>');
						
						
						$.jGrowl(msg.msg, {header:'Online Shop'});
						shop('get_cart');
					}else if(action == 'delete_product'){
						$('.cart_item_id_' + product_id).hide(500);

						$('#delivery_pa_charges').html(msg.delivery_pa_charges);
						$('#delivery_charges_only').html(msg.delivery_charges_only);
						$('#delivery_discount').html(msg.delivery_discount);						
						$('#delivery_total').html(msg.delivery_total);						
						
						setTimeout(
								   function(){
										$('.cart_item_id_' + product_id).empty();
										if(msg.products_left == 0){
											location.href = '';	
										}
								   }, 1000);
						$.jGrowl(msg.msg, {header:'Online Shop'});
						shop('get_cart');
					}else if(action == 'get_cart'){
						////////////
						////////////
						s = new String();
						
						if(msg.products.length > 0){
							$('#header_check_out_total').html('CHECKOUT ('+msg.products.length+')');
							for(i=0; i < msg.products.length; i++){
								
								if( msg.products[i].price_discount != '0.00' ){
									$('#message_discount_product_' + msg.products[i].id).html('(&pound; '+ msg.products[i].price_discount +' discount per unit)');
								}else{
									$('#message_discount_product_' + msg.products[i].id).html('');
								}
								
								
								$('#product_price_vat_' + msg.products[i].id).html(msg.products[i].price_vat);
								$('#check_out_price_total_' + msg.products[i].id).html(msg.products[i].price_total);
								
								s += '<div class="panel_item_right cart_item cart_item_id_'+msg.products[i].id+'" align="justify" style="margin-top:8px; margin-right:5px;"><span><a href="'+node+'check-out/"><em>'+msg.products[i].manu_name+' '+msg.products[i].name+'</em> <br/> <b>(&pound; '+ msg.products[i].price_total +' x '+msg.products[i].qty+')</b></a></span></div>';
							
								if(msg.products[i].message != ''){
									alert(msg.products[i].message);
									$('#qty_' + i).val( msg.products[i].qty );
								}
							
							} // end for
							
							s += '<div class="panel_item_right" style="margin-top:10px; margin-right:5px;"><span><a href="'+node+'check-out/"><b>TOTAL &pound; '+msg.total+'</b> </a></span></div>';
							
							
							$('#check_out_grand_total').html(msg.grand_total);
							
							del_msg = '';
							if(msg.delivery_discount > 0){
								del_msg = ' - <span style="color:#900"><b>Discounted amount on delivery &pound;' + msg.delivery_discount;
							}

							$('#delivery_pa_charges').html(msg.delivery_pa_charges);
							$('#delivery_charges_only').html(msg.delivery_charges_only);
							$('#delivery_total_no_vat').html(msg.delivery_charges_only_no_vat);
							$('#delivery_discount').html(msg.delivery_discount);						
							$('#delivery_total').html(msg.delivery_total);						
						
							//$('#check_out_delivery_total').html(msg.delivery_total);
							
							// check out page
							// check out page

							$('#online_shop').html(s);
						}else{
							$('#online_shop').html('<div class="panel_item_right" style="margin-top:5px; margin-right:5px;"><span>Your cart is empty</span></div>');
						}
						////////////
						////////////						
					}
				}else{
					$.jGrowl(msg.msg, {header:'Online Shop'});
				}
		   }
	 });
	
}

function shop_edit_quantities(){		
	$('#check_out').ajaxForm({
			dataType : 'json',
			url:'../ajax/shop.php',
			success: function(data) {				
				
				if(data.success == false){
					$.jGrowl(data.msg, {header:'Online Shop'});
				}else{
					$.jGrowl(data.msg, {header:'Online Shop'});
					//	$('#delivery_price').html(data.delivery_price);
					//	$('#delivery_discount').html(data.delivery_discount);
					//	$('#delivery_total').html(data.delivery_discount);						
					
					shop('get_cart');
				}
				
			}
    });
}

function account(action){		
	$('#'+action).ajaxForm({
			dataType : 'json',
			url:'../ajax/account.php',
			success: function(data) {				
				
				if(data.success == false){
					alert(data.msg);
				}else{
					alert(data.msg);
					location.href = '/check-out/';
				}
				
			}
    });
}

function editAccount(){		
	$('#edit_account').ajaxForm({
			dataType : 'json',
			url:'../ajax/account.php',
			success: function(data) {				

				if(data.success == false){
					alert(data.msg);
				}else{
					alert(data.msg);
				}
				
			}
    });
}


function sendContactUsForm(){	
	
	if(!confirm('You are about to send this form. Proceed?')){
		return;	
	}

	$('#contact_us').ajaxForm({
			dataType : 'json',
			url:'/ajax/forms.php',
			success: function(data) {				

				if(data.success == false){
					alert(data.msg);
					
				}else{
					alert(data.msg);
					location.href = 'http://www.rockwarehouse.co.uk/';
				}
				
			}
    });
}

function switchForms(){
	if($('#register').css('display') == 'none'){
		$('#register').css('display', 'block');
		$('#login').css('display', 'none');
	}else{
		$('#register').css('display', 'none');
		$('#login').css('display', 'block');
	}
}

function gallerySwitcher(){

	if(hide >= g_length){
		hide = 0;
	}

	if(show >= g_length){
		show = 0;
	}

	$('.gallery_item_' + hide).hide(500);
	$('.gallery_item_' + show).show(500);
	selectGalleryItem(show);
	
	show++;
	hide++
}

function playGallery(){
	
	playGallery = setInterval( function(){ gallerySwitcher(); }, 5000);	
}

function pauseInterval(){
	clearInterval(playGallery);
}

function productSorter(sortBy, asc){
	$('#product_listings>div').tsort('span.' + sortBy, {order:asc});
}

