// ***************************************************************
// ***************************************************************
// ***************************************************************
// Store Ajax menu

$(document).ready(function(){
						   
	//load product categories
	load_product_categories();
	//load the cart
	load_cart();
});


// ***************************************************************
// ***************************************************************
// load product categories and create the accordian menu
function load_product_categories()
{
	request = {}
	
	$.get( '/api/store.categoryList', request, function(data){
		if(data)
		{
			
			html = '';
			$.each( data.items, function( i, item ){
				if( item.product_active == "Active" )
				{
					html+='<div class="product-box">'
							+'<div class="product-list-header"><a href="javascript:void(0);" class="product-link" rel="'+item.category_id+'">'+item.category_name+'</a></div>'
							+'<div id="cat-'+item.category_id+'" class="product-list-content" style="display:none;"></div>'
						+'</div>';
				}
			});	
			
			html+= '<div id="product-content"></div>';
			
			$('#ajax-content').html(html);
			
			//add category link handlers
			$('a.product-link').click(function(){
				load_product_list( $(this), $(this).attr( 'rel' ) )		
			});
		}
	},'json');
}


// ***************************************************************
// ***************************************************************
// load the product list

function load_product_list( $obj, category_id )
{
	// add loading 
	$obj.append('<span class="product-loading"> Loading </span>');
	//close all
	$('.product-list-content').html('').slideUp();
		
	request = {
		'filter_category_id' : category_id,
		'image_width':50,
		'image_height':50
	};
	
	//start the overlay api
	$overlay = $('#product-details-overlay').overlay({
		api:true,
		left:'center',
		top:'center',
		closeOnClick:true,
		onBeforeLoad:function(){
			$('#product-details-overlay #overlay-content').html( '' );
		}
	});	
	$('#product-details-overlay').click(function(){ $overlay.close() });
	
	$.get('/api/store', request, function(data){
		$('span.product-loading').remove();
		if(data){
			
			html = '';
			if( data.items.length > 0 ){
				
				$.each( data.items, function(i, item){						
					html+='<div class="product-item" rel="'+item.product_id+'">'
							+'<div class="product-item-image">'
								+'<img src="'+item.image+'" border="0"/>'
							+'</div>'
							+'<div class="product-item-content">'
								+'<div class="product-item-title">'+crop( item.product_name, 40, true )+'</div>'
								+'<div class="product-item-price">$'+item.product_price_low+'</div>'
							+'</div>'
							+'<div style="clear:both;"></div>'
							+'<div class="product-action">'
								+'<div class="product-item-details"><a href="javascript:void(0)" class="details-link" rel="'+item.product_id+'"></a></div>'
								+'<div class="product-item-quantity">'
									+'<span class="qty">Qty.</span> <input type="text" size="2" id="qty-'+item.product_id+'" class="qty" value="5"/>'
									+'<a href="javascript:void(0)" class="qty-up" rel="'+item.product_id+'"></a>'
									+'<a href="javascript:void(0)" class="qty-down" rel="'+item.product_id+'"></a>'
								+'</div>'
								+'<div class="product-item-add"><a href="javascript:void(0)" class="add-link" rel="'+item.product_id+'"/></div>'
								+'<div style="clear:both;"></div>'
							+'</div>'
						+ '</div>';
				})
				
				html+= '<div style="clear:both;"></div>';
			}else{
				html+= '<p>No Product Enteries found</p>';	
			}
			$('#cat-'+category_id).html(html).slideDown('normal');
			
			//hover events
			$('.product-item').hover(
				function(){
					$(this).find('.qty:input').select();
				}
			);
			
			//quantity buttons
			add_quantity_buttons();
			
			//details
			$('.details-link').click(function(){
				product_id = $(this).attr('rel');
				load_product_overlay(product_id );
				
				//show the overlay
				$overlay.load();
			});
			
			//add
			$('.add-link').click(function(){
				product_id = $(this).attr('rel');
				quantity = $('#qty-'+product_id).val();
				
				add_cart_item( product_id, quantity );
			});
			
		}
	},'json');
}

// ***************************************************************
// ***************************************************************
// load product
// set overlay content

function load_product_overlay( product_id, $overlay )
{
	request = {
		'id':product_id,
		'image_maxwidth':280
	};
	$.get('/api/store.getProduct', request, function(data){
		if(data){
			html = '';
			var item = data.entry;
			if( item.product_id != null && item.product_active=="Active" )
			{
				html += '<img src="'+item.image+'" style="margin:0px 5px 5px 0px;" align="left"/>';		
				html += '<h3>'+item.product_name+'</h3>'
				html += '<p>'+item.product_description+'</p>';
				html += '<p>$'+item.product_price_low+'</p>';
			}
			//set content
			$('#overlay-content').html( html );
			
		}
	},'json');	
}



// ***************************************************************
// ***************************************************************
// Add product to cart

function add_cart_item( product_id, quantity )
{	
	request = {
		product_id:product_id,
		quantity:quantity
	}	
	
	$.post('/api/store.addCartItem', request, function(data){
		if(data){
			if( data.entry.success == true ){
				load_cart();	
			}
		}
	},'json');
}


// ***************************************************************
// ***************************************************************
// Load Cart
// load the cart items and build the cart summary


function load_cart()
{
	request = {
		'image_width':20,
		'image_height':20
	};
	$.get('/api/store.cartList', request, function(data){
		if(data){
			html = '';
			if( data.items.length > 0 ){
				//load cart totals
				load_cart_details();
				
				html+='<table class="cart-summary">';
				
				$.each( data.items, function(i, item){	
					alt =( i%2==1 ? '' : 'alt' );
					html+='<tr class="'+alt+'">'
							+ '<td><img src="'+item.image+'" border="0"/></td>'
							+ '<td>'+item.item_name+'</td>'
							+ '<td style="white-space:nowrap;">$'+item.item_price+' x '+Math.round( item.item_quantity )+'</td>'
							+ '<td><a href="javascript:void(0);" class="delete-cart-item" rel="'+item.item_id+'" title="Remove item from cart"/></td>'
						+ '<tr>';
				});
				
				html+='</table>';
			}
			else
			{
				html+='<p>Your Cart is currently empty</p>';
			}
			
			$('#cart-items-content').html(html);
			
			//add delete actions
			$('.delete-cart-item').click(function(){
				id = $(this).attr('rel');
				remove_cart_item( id );
			});
			
		}
	},'json');
}

// ***************************************************************
// ***************************************************************
// load cart details
function load_cart_details()
{
	$.get('/api/store.cartDetail', {}, function(data){
		if(data){
			item = data.entry;			
			if(item.cart_id != null){
				
				html = '<div id="cart-detail-item">Total : $'+item.cart_total+'</div>';
				html += '<div id="cart-detail-notice">'+item.cart_notice+'</div>';
				
				$('#cart-details-content').html(html);
			}
		}
	},'json');
}

// ***************************************************************
// ***************************************************************
function remove_cart_item( id )
{
	if( confirm( 'Are you sure you want to remove this item from your cart' ) )	{
		$.ajax({
			type: "DELETE",
			url: '/api/store.removeCartItem?id='+id,
			dataType: 'json',
			success: function(data){
				if(data){
					if( data.entry.success == true ){
						load_cart();
						load_cart_details();	
					}
					
				}				
			}
						
		});
	}
}

// ***************************************************************
// ***************************************************************
// Add quantity button events

function add_quantity_buttons()
{
	//up
	$('a.qty-up').click(function(){
		id = $(this).attr('rel');
		val = $('#qty-'+id).val();
		val++;
		$('#qty-'+id).val(val).select();
	});
	
	//down
	$('a.qty-down').click(function(){
		id = $(this).attr('rel');
		val = $('#qty-'+id).val();
		val--;
		if( val <= 0 ) val = 1;
		$('#qty-'+id).val(val).select();
	});
	
}

// ***************************************************************
// ***************************************************************
// crop text

function crop(str, len, tip)
{
	if( tip == null ) tip = false;
	full_str = str;
	str = str.replace(/<\/?[^>]+(>|$)/g, '');
	if( str.length > len )
	{
		str = str.substr(0,len)+'...';
		if(tip==true){
			str = '<a title="'+full_str+'">'+str+'</a>';	
		}
	}
	return str;	
}

function date_full( time )
{
	date = new Date();
	date.setTime( time );
	
	month = new Array();
	month[0]="January";
	month[1]="February";
	month[2]="March";
	month[3]="April";
	month[4]="May";
	month[5]="June";
	month[6]="July";
	month[7]="August";
	month[8]="September";
	month[9]="October";
	month[10]="November";
	month[11]="December";
	
	
	return month[date.getMonth()]+' '+date.getDate()+', '+date.getFullYear();
}