/**
 * By GAMIDOV-Studio, 2009
 */

var shopItemClassName = 'shop-item' ;
var itemDescriptionClassName = 'item-description' ;
var itemDescriptionClassName2 = '' ;
var itemDescriptionClassName3 = '' ;
var itemPriceClassName = 'item-price' ;
var itemButtonClassName = 'item-button' ;

var cartIsEmptyText = 'Your cart is empty' ;
var addToCartText = 'Buy' ;
var itemNameText = 'Name' ;
var itemQuantityText = 'Quantity' ;
var itemPriceText = 'Price' ;
var totalPriceText = 'Total' ;
var itemDeleteText = 'Delete' ;
var checkOutText = 'Check Out' ;
var sendText = 'Send' ;
var yourNameText = 'Name' ;
var yourPhoneText = 'Phone' ;
var yourAddressText = 'Address' ;
var yourEmailText = 'E-mail' ;
var currencyName = 'AZN' ;

var itemQuantityClassName = 'item-quantity' ;
var itemAddButtonClassName = 'item-add-button' ;
var itemButtonHtml = '<input type="text" class="'+itemQuantityClassName+'" style="width:40px;" value="1" /> <input type="image" src="img/button_buy_now.gif" class="'+itemAddButtonClassName+'" value="'+addToCartText+'" />' ;

var gsCartCookieName = 'test_gs_cart_cookie' ;

var gsCartHeader = false ;

function updateGsCart() {
    var totalPriceAllItems = 0 ;
	$('.cart-item div.div-4').each( function() {
		var currItemTotal = $(this).html();
		currItemTotal = parseFloat(currItemTotal);
		totalPriceAllItems += currItemTotal ;
	});
	if( totalPriceAllItems == 0 ) {
		$.cookie(gsCartCookieName,cartIsEmptyText);
		$('#gs-cart-div').html(cartIsEmptyText);
        $("#show-cart-button").hide();
        var t=setTimeout("hideGsCart()",1000);
	}
	else {
		totalPriceAllItems = totalPriceAllItems.toFixed(2);
		$('div.shopping-cart-footer').html(totalPriceText+': '+ totalPriceAllItems +' '+currencyName);
		var gsCartContent = $('#gs-cart-div').html();
		$.cookie(gsCartCookieName,cartIsEmptyText);
        $("#show-cart-button").slideDown();
	}
}

function showGsCart() {
	$('#gs-cart-div').slideDown();
}
function hideGsCart() {
    $('#gs-cart-div').slideUp();
}
function toggleGsCart() {
    $('#gs-cart-div').slideToggle();
}

function activateGsCart() {
    var leftPosition1 = ( $(document).width() - 600 ) / 2 ;
    leftPosition1 = leftPosition1.toFixed(0);
	$('body').append('<div style="position:fixed; left:'+leftPosition1+'px; top:20px; width:600px; padding:10px; border:2px solid #340700; max-height:400px; overflow:auto; background-color:#fff; display:none; z-index:900;" id="gs-cart-div"></div>');
    var leftPosition = ( $(document).width() - 100 ) / 2 ;
    leftPosition = leftPosition.toFixed(0);
    $('body').append('<div style="position:fixed; left:'+leftPosition+'px; top:0px; float:left; padding:5px; cursor:pointer; color:#340700; background-color:#ff3; font-weight:bold; display:none; z-index:990;" id="show-cart-button" onClick="toggleGsCart()">Show/Hide Cart</div>');
	
    var gsCartContent = $.cookie(gsCartCookieName);
	if( !gsCartContent ) {
		gsCartContent = cartIsEmptyText ;
		$.cookie(gsCartCookieName,gsCartContent);
	}
	$('#gs-cart-div').html(gsCartContent);

    updateGsCart();

	$('.'+itemButtonClassName).html(itemButtonHtml);
    
    bindAddButtons();
    bindDeleteButtons();
    
    $("div").not("#show-cart-button, #gs-cart-div, ."+itemButtonClassName).click( function(){ hideGsCart(); } );

}

function bindDeleteButtons() {
    
    $('.cart-item div.div-5').live('click', function() {
    		$(this).parents('div.cart-item').remove();
            var t=setTimeout("updateGsCart()",200);
	});
}

function bindAddButtons(){
    
    $('.'+itemAddButtonClassName).live('click', function() {
		
		var thisItemHtml = '' ;
		
		var gsCartContent = $('#gs-cart-div').html();
		if( gsCartContent == cartIsEmptyText ) {
			thisItemHtml += '<div class="shopping-cart">' ;
			thisItemHtml += '<div class="cart-header" style="margin:0; padding:0; line-height:27px; font-family:tahoma; font-size:11px; font-weight:bold;">' ;
			thisItemHtml += '<div class="div-1" style="height:29px; margin:1px; padding:2px; background-color:#eee; float:left; width:300px; text-align:center;">'+itemNameText+'</div>' ;
			thisItemHtml += '<div class="div-2" style="height:29px; margin:1px; padding:2px; background-color:#eee; float:left; width:60px; text-align:center;">'+itemPriceText+'</div>' ;
			thisItemHtml += '<div class="div-3" style="height:29px; margin:1px; padding:2px; background-color:#eee; float:left; width:60px; text-align:center;">'+itemQuantityText+'</div>' ;
			thisItemHtml += '<div class="div-4" style="height:29px; margin:1px; padding:2px; background-color:#eee; float:left; width:60px; text-align:center;">'+totalPriceText+'</div>' ;
			thisItemHtml += '<div class="div-5" style="height:29px; margin:1px; padding:2px; background-color:#eee; float:left; width:60px; text-align:center;">'+itemDeleteText+'</div><div style="clear:both;"></div>' ;
			thisItemHtml += '<div class="clear"></div></div><div class="clear"></div></div><div style="clear:both;"></div>' ;
			thisItemHtml += '<div class="shopping-cart-footer" style="padding:15px 0; text-align:center;"></div>' ;
			thisItemHtml += '<div class="form"><div class="name">'+yourNameText+':</div> <input type="text" value="" /></div>' ;
			thisItemHtml += '<div class="form"><div class="name">'+yourPhoneText+':</div> <input type="text" value="" /></div>' ;
			thisItemHtml += '<div class="form"><div class="name">'+yourAddressText+':</div> <input type="text" value="" /></div>' ;
			thisItemHtml += '<div class="form"><div class="name">'+yourEmailText+':</div> <input type="text" value="" /></div>' ;
			thisItemHtml += '<div class="form"><input type="button" value="'+sendText+'" id="submit-button" /></div>' ;
			$('#gs-cart-div').html(thisItemHtml);
			thisItemHtml = '' ;
            $('#submit-button').click( function() {
        		var submitThisForm = 1 ;
        		var alertText = $('#alert-text').val(); 
        		if( !alertText )
        			alertText = 'Fill the form please.';
        		$('#feedback .ne-pusto').each( function() {
        			if( $(this).val()=='' ) {
        				submitThisForm = 0 ;
        				alert(alertText);
        			}
        		} );
        		if( submitThisForm == 0 )
        			{ return false; }
        		$('#gs-cart-div input:checked').remove();
        		$('#gs-cart-div input:radio').parent().remove();
        		$('#gs-cart-div input:checkbox').parent().remove();
        		$('#gs-cart-div input:text').each( function() {
        			var inputText = $(this).val();
        			$(this).replaceWith(inputText);
        		});
        		$('#gs-cart-div textarea').each( function() {
        			var inputText = $(this).text();
        			$(this).replaceWith(inputText);
        		});
        		$('#gs-cart-div select').each( function() {
        			var inputText = $(this).find('option:selected').val();
        			$(this).replaceWith(inputText);
        		});
        		$('#gs-cart-div .do-not-send').remove();
        		$('#gs-cart-div input').remove();
        		var formHtml = $('#gs-cart-div').html();
        		$('#gs-cart-div').html('<img src="images/lightbox-ico-loading.gif" border="0" alt="" />');
        		$.post (
        			"submit.php" , 
        			{ 'feedback' : formHtml } ,
        			function(data) {
        				$('#gs-cart-div').html(data);
        			}
        		); 
        		return false; 
        	});
		}
		
		var thisItemDescription = $(this).parents('.'+shopItemClassName).find('.'+itemDescriptionClassName).html();
		if( itemDescriptionClassName2 != '' )
			thisItemDescription = thisItemDescription + ' / ' + $(this).parents('.'+shopItemClassName).find('.'+itemDescriptionClassName2).html();
		if( itemDescriptionClassName3 != '' )
			thisItemDescription = thisItemDescription + ' / ' + $(this).parents('.'+shopItemClassName).find('.'+itemDescriptionClassName3).html();
		
		var thisItemPrice = $(this).parents('.'+shopItemClassName).find('.'+itemPriceClassName).html().replace(/[^0-9\.]/gi,"");
		thisItemPrice = parseFloat(thisItemPrice).toFixed(2);
		
		var thisItemQuantity = '0' + $(this).parents('.'+shopItemClassName).find('.'+itemQuantityClassName).val().replace(/[^0-9]/gi,"");
		thisItemQuantity = parseFloat(thisItemQuantity).toFixed(0);
		if( thisItemQuantity < 1 ) thisItemQuantity=1 ;
		
		var thisItemTotalPrice = thisItemPrice * thisItemQuantity ;
		thisItemTotalPrice = parseFloat(thisItemTotalPrice).toFixed(2);
		
		thisItemHtml = '' ;
		thisItemHtml += '<div class="cart-item" style="clear:both; border-bottom:1px sold #ccc; padding-bottom:10px; line-height:25px; vertical-align:middle;">' ;
		thisItemHtml += '<div class="div-1" style="height:54px; float:left; width:300px; margin:1px; padding:2px; background-color:#eee; font-family:tahoma; font-size:11px; overflow:hidden;">'+thisItemDescription+'</div>' ;
		thisItemHtml += '<div class="div-2" style="height:54px; float:left; width:60px; margin:1px; padding:2px; background-color:#eee; font-family:tahoma; font-size:11px;">'+thisItemPrice+'</div>' ;
		thisItemHtml += '<div class="div-3" style="height:54px; float:left; width:60px; margin:1px; padding:2px; background-color:#eee; font-family:tahoma; font-size:11px;">'+thisItemQuantity+'</div>' ;
		thisItemHtml += '<div class="div-4" style="height:54px; float:left; width:60px; margin:1px; padding:2px; background-color:#eee; font-family:tahoma; font-size:11px;">'+thisItemTotalPrice+'</div>' ;
		thisItemHtml += '<div class="div-5 do-not-send" style="height:54px; float:left; width:60px; margin:1px; padding:2px; background-color:#eee; font-family:tahoma; font-size:11px;">'+' X '+'</div>' ;
		thisItemHtml += '<div class="clear"></div></div>' ;
		
		$('div.shopping-cart').append( thisItemHtml );
			
        updateGsCart();
        showGsCart();
	});
}

