function application()
{
  this.myshoppingcart = new myshoppingcart();
  return this;
}
application.prototype.init = function()
{
	var arr = $("#menu > ul > li");
	$.each(
		arr,
		function(key,val)
		{
			app.initMenu(val);
			$("ul>li",val).mouseover(
				function()
				{
					$(this).css("background","#ffd9e2");
				}
			).mouseout(
				function()
					{
						$(this).css("background","#ffffff");
					}
			);
		}
	);
	
	
	  if($.browser.msie && $.browser.version<7)
  {
  	//init js 4 mnu
  }
  
}
	

application.prototype.initMenu= function(it)
{
	$(it).click( function()
					  {

$("ul",this).slideToggle( "fast" );

					  }
					  ).mouseout( function()
					  {
					  	$("a:first",this).css("color","#000000");
					  	//$("ul",this).css( "display", "none" );
					  }
					  );
}

application.prototype.toString = function()
{
 return "[application Object]";
}

/**
* @auth: blue
* @desc: shopping cart class
*/
function myshoppingcart()
{
	this.products = [];
	this.price = 0;
	this.shipping = 0;
	this.total = 0;
	this.count = 0;
	this.api = {};
   return this;

}

myshoppingcart.prototype.addProductById = function( id )
{
	if( null != id && this.products[ id ] )
	{
		this.products[ id ].quantity++;
		this.update();
	}else{
      // alert( 'Product id:'+id+' is not defined.');
    }
}

myshoppingcart.prototype.update = function()
{
	this.price = 0;
	this.total = 0;
	this.count = 0;
	if( this.products && this.products.length > 0 )
	{
        var n = this.products.length;
        for(var i=0;i<n;i++)
		{
               var item = this.products[ i ];
               if( item )
               {
					this.price += item.quantity * item.product.price;
					this.count += item.quantity;
               }
		}
	}
	this.total = this.price + this.shipping;
}

myshoppingcart.prototype.command = function( params, callback )
{
     var url = "http://biostaseparis.com/index-ajax.php";
     params.q = 'assets/snippets/myshoppingcart/myshoppingcart.php';
     $.get( url, $.param( params ), callback ? callback : this.callback, 'json' );
	$('#load').show();
}

myshoppingcart.prototype.getForm = function()
{
     $('#pp_btn').html( '<img src="http://biostaseparis.com/images/loading.gif" />' ).show();
     var url = "http://biostaseparis.com/index-ajax.php";
	var params = {};
     params.q = 'assets/snippets/myshoppingcart/myshoppingcart.php';
	params.action = 'form';
     $.get( url, $.param( params ), this.loadForm );
}

myshoppingcart.prototype.loadForm = function( response )
{
  $('#pp_btn').html( response );
}
myshoppingcart.prototype.callback = function( response )
{
	//alert( response );
    $('#prod_count').html( response.count );
	$('#load').hide();
}
/**
* @auth: blue
* @desc: product class
*/
function product( params )
{
	this.id = params.id;
	this.src = params.src;
	this.pic = params.pic;
	this.price = params.price;
	return this;
}

app = new application();
$(document).ready(function(){

var o = $(".lightbox");
   if( o )
{
	if ( o.lightbox ) o.lightbox();
}
   app.init();
   $("#searchbox").click(
        function()
		{
			if($(this).val() == 'Search') $(this).val('');
		}
    ).blur(
		function()
		{
			if($(this).val() == '') $(this).val('Search');
		}
	);


   $("a").focus( 
					function() {
					  				$(this).get(0).blur();
					  				return false;
					  				//alert($(this).html());
					  			}
				);
     //app.myshoppingcart.command( { action:'add',id:11 } );

 });

function increase( response )
{
    var val = parseInt( $( '#o'+response.id ).html() );
    $( '#o'+response.id ).html( 1+val );
    updateCount( response.count );
	updateTotal( response.total );
}

function decrease( response )
{
    var val = parseInt( $( '#o'+response.id ).html() );
    $( '#o'+response.id ).html( -1+val );
    updateCount( response.count );
	updateTotal( response.total );
}

function updateCount( c )
{
     $('#prod_count').html( c );
}
function updateTotal( c )
{
     $('#cart_total').html( c );
}
function clearCart( response )
{
    $('#cart_table').hide();
    updateCount( 0 );    
}
