
ShopClass = {};
ShopClass.product = {};
ShopClass.cart = [];

ShopClass.init = function(){
	
	$('#shop .shop .shop-quant select').change(function(){
		var id = parseInt( $(this).attr('id').replace('Product','') );
		var amount = $(this).val();		
		var anchorPos = window.location.href.indexOf('#');		
		window.location = anchorPos == -1 ? window.location.href + '#CartBlock' : window.location.href.substring(0, anchorPos )+'#CartBlock';
		if( amount == 0 ) {
			ShopClass.removeCart( id );
		} else {
			ShopClass.addCart( id, amount );
		}			
	});
	
	if( $.cookie('neptunas_cart') != null ) {
		var cart = $.cookie('neptunas_cart');
		cart = cart.split('&');		
		for( var i=0; i<cart.length; i++ ) {
			var item = cart[i].split('=');
			if( item[0].length > 0 ) { 			
				ShopClass.addCart( item[0], item[1] );
			}			
		}		
	}
	
	$('#user-login form .submit a ').click(function(){
		$('#user-login form').submit();	
		return false;
	});
		
	$('#user-login form').submit( function() {		
		var url = $(this).attr('action');		
		$.post( url, $(this).serialize(), function( data ) {
			if( data['success'] ) {
				if( data['redirect'] != undefined ) {
					window.location = data['redirect'];
				} else {							
					$('#user-form').html( data['message'] );		
					ShopClass.showTab('form');
				}				
			} else {
				$('#user-login .field').addClass('error');				
			}			
		},'json');		
		return false;
	});	
	
	$('#user-remind form .submit a ').click(function(){
		$('#user-remind form').submit();	
		return false;
	});
		
	$('#user-remind form').submit( function() {		
		var url = $(this).attr('action');
		$.post( url, $(this).serialize(), function( data ) {
			if( data['success'] ) {								
				$('#user-remind form').html('<p style="line-height:150%;">'+data['message']+'</p>');				
			} else {
				$('#user-remind form .field').addClass('error');				
			}			
		},'json');		
		return false;
	});
	
	ShopClass.cartUpdated();
	ShopClass.resize();	
}

ShopClass.initOrderForm = function() {
	$('#OrderForm').submit( function(){
		if( ShopClass.cart.length == 0 ) {
			$('#OrderEmpty').show();
			return false;
		}		
	});
}

ShopClass.showTab = function ( tabName ) {
	var tab = ['user-login','user-form','user-remind'];
	for( var i=0; i<tab.length; i++ ) {
		if( tab[i] == 'user-'+tabName ) {
			$('#'+tab[i]).show();
		} else {
			$('#'+tab[i]).hide();
		}
	}
	//$('#user-'+tabName+' form .field').removeClass('error');	
}

ShopClass.addCart = function ( productId, amount ) {
	
	$('#Product'+productId ).val( amount );
	var product = ShopClass.getProduct( productId );
		
	var cartIndex = ShopClass.getCartIndex( product.id );
	if( cartIndex == -1 ) {
		cartIndex = ShopClass.cart.length;
		ShopClass.cart.push( {id:product.id, amount:amount } );		
		var html = '<tr id="CartProduct'+product.id+'"><td class="index">'+(cartIndex+1)+'.</td><td style="width:100%;">'+product.label+'</td><td class="amount">'+amount+'</td><td class="remove"><a href="#" onclick="ShopClass.removeCart('+(product.id)+');return false;"><img src="/site/assets/image/remove.gif" alt="" /></a></td></tr>';
		$('#CartTable').append( html );
	} else {
		ShopClass.cart[ cartIndex ].amount = amount;
		$('#CartProduct'+product.id+' td.amount').html( amount );
	}		
	//$('#CartProduct'+product.id ).css( {backgroundColor:'#CECECE',opacity:0} ).animate( {backgroundColor:'#FFFFFF',opacity:1}, 'slow' );
	//$('#CartProduct'+product.id ).animate( {opacity:'#FF0000'}, 'slow' );	 
	ShopClass.cartUpdated();
	
}

ShopClass.getCartIndex = function( productId ) {
	var retVal = -1;
	for( var i=0; i<ShopClass.cart.length; i++ ) {
		var cartItem = ShopClass.cart[ i ];		
		if( cartItem.id == productId ) {
			retVal = i;
			break;			
		}
	}
	return( retVal );
}

ShopClass.removeCart = function ( productId ) {
	var cart = [];
	for( var i=0; i<ShopClass.cart.length; i++ ) {
		var cartItem = ShopClass.cart[ i ];		
		if( cartItem.id != productId ) {
			cart.push( cartItem );			
		}
	}
	$('#Product'+productId ).val( 0 );
	ShopClass.cart = cart;
	$('#CartProduct'+productId ).remove();
	for( var i=0; i<ShopClass.cart.length; i++ ) {
		$('#CartProduct'+ShopClass.cart[i].id+' td.index').html( (i+1)+'.' );
	}	
	ShopClass.cartUpdated();
		
}

ShopClass.cartUpdated = function () {

	if( ShopClass.cart.length == 0 ) {
		$('#CartEmpty').show();
		$('#CartBlock').hide();
	} else {
		$('#CartEmpty').hide();
		$('#CartBlock').show();
		$('#OrderEmpty').hide();
	}
	
	var cookie = '';	
	for( var i=0; i<ShopClass.cart.length; i++ ) {
		cookie += ShopClass.cart[i].id+'='+ShopClass.cart[i].amount+'&';
	}
	cookie = cookie.substring(0, (cookie.length-1) );
	$.cookie('neptunas_cart', cookie, { path: '/' } );
	$('#form-cart').val( cookie );	
	 
}

ShopClass.addProduct = function ( id, data ) {
	ShopClass.product[ id ] = jQuery.parseJSON( data );
}

ShopClass.getProduct = function( id ) {
	return( ShopClass.product[ id ] );
}

ShopClass.resize = function(){
	$('#shop .shop-row').each(function(){	
		var maxHeight = 0;
		$(this).find('.shop-mid').each(function(){
			maxHeight = Math.max( maxHeight, $(this).height() );
		});
		$(this).find('.shop-mid').height( maxHeight );
	});
}

$(document).ready(function(){
	ShopClass.init();
});
$(window).resize(function(){
	ShopClass.resize();
});
