//.........Ajax

//requires URL_SERVER and URL_CHECKOUT;

var products = [];
var products_count;

var id_class = "id_class";
var price_class = "price_class";
var nmb_class = "nmb_class";

var cartTable;
var currencyControl;

var cartTableID = 'shoppingCard';
var removeBt;
var addBt;


function removeFromCart(ItmId) {
	checkCartOperation(ItmId, 'rem');
}

function checkCartOperation(ItmId, command) {
	var str = '', bt_id='#add_remove_button' + ItmId , buttont_text, operation;

	cartTable = $(cartTableID);	
	
	if(!isInteger(ItmId)) {
		alert(INVALIDID + " " + ItmId);
		return;
	}
	
	var operation;
	var str = '';
//	alert(print_r(products));
	
	if (command == 'add') {
		if(products[ItmId]) alert(ALREADYIN);
		else operation = command;
	}
	
	else if(command == 'rem') {
		if(!products[ItmId]) alert(NOTIN);
		else operation = command;
	}
	
	else {
		if(!products[ItmId]) operation = "add";
		else operation = "rem";
	}
	
	if(operation) {
		
		if (typeof (jQuery(bt_id).attr('id')) != 'undefined'){
//			if(operation == "add"){
//				jQuery("#add_bt_tab").hide();
//			}
//			else {
//				jQuery("#add_bt_tab").show();
//			}
			
			
			
//			if(operation == "add"){
//				buttont_text = REMOVE;
//				jQuery(bt_id).removeClass('button_std');
//				jQuery(bt_id).addClass('button_xsmall');
//			}
//			else {
//				buttont_text = ADD_TO_CART;
//				jQuery(bt_id).removeClass('button_xsmall');
//				jQuery(bt_id).addClass('button_std');
//			}
//			jQuery(bt_id).html(buttont_text);
		}
		
		ajaxLoadPost(URL_SERVER, preparePostAJAX({ItmId:ItmId, operation:operation}), getCartOperation);
	}
	
}




function getCartOperation(ajaxObject) {
	
	var res = processAJAX(ajaxObject);
	//alert(ajaxObject.responseText);
	if(!res || !res.operation || res.success == undefined || !res.ItmId == undefined) {
		alert("Invalid server responce");
		return;
	}
	//alert(print_r(res));
	
	var ItmId = res.ItmId;
	var button_prefix = "addBt";
	var bt = $(button_prefix + ItmId);
	var abstr_id = typeof (abstract_id) != 'undefined' ? abstract_id : false;

	if (res.success == '1') {	
		if(res.operation == "add" && res.product) {
			products[ItmId] = res.product;
			if(bt) {
				checkAddBtText(bt);
				var remove = removeBt ? removeBt : REMOVE;
				 bt.innerHTML = '<a class="remove_link" href="javascript:checkCartOperation(\'' + ItmId + '\')">' + remove + '</a>';
			}
			if (abstr_id == ItmId){
				jQuery("#add_bt_tab").hide();	 
			}
		}
		
		
		else if(res.operation == "rem") {
			products[ItmId] = null;
			if(bt) {
				addBt ? bt.innerHTML = '<a href="javascript:checkCartOperation(\'' + ItmId + '\')">' + addBt + '</a>' : 
				bt.innerHTML = '<a href="javascript:checkCartOperation(\'' + ItmId + '\')">' + ADD + '</a>';
				/* + ' (' + ADD2 + ')'; */
			}
			if (abstr_id == ItmId){
				jQuery("#add_bt_tab").show();	 
			}
		}

		track_arr_remove(res.operation, res.product);

	}
	
	if (res.error) {
		refreshCart(res.cart, res.error);
	}
	else if (res.cart) {
		refreshCart(res.cart);
	}
	
	if (res.ajax_prom && $('proms')) {
		$('proms').innerHTML = res.ajax_prom;
		acdCD.initcountdown(res.ajax_prom_end);
	}
	
}

function checkAddBtText(bt) {
	if (addBt && addBt !== bt.childNodes[0].innerHTML) {
		addBt = bt.childNodes[0].innerHTML;
	}
}


function refreshCart(cart, error) {

	var width = cartTable.style.width;
	var row;
	var pop_address = 'popup.php?id=';
	var content = '';
	var button_class = 'red';
	var count = 0;
	
	var total_style = "style='height:20px; vertical-align:middle;'";
	var total_price = cart['totalprice'];
	var total_pr_price = cart['totalpromprice'];
	var products = cart['products'];
	var products_ids = [];
	
	var price;
	var pr_price;
	var checkbox;
	var link_id;
	
	var pr_pr_class = "class='pr_pr'";
	var td_class;
	
	var i = 1;
	var prop;
		
			for (var ItmId in products) {
				row = products[ItmId];
				products_ids.push(ItmId);
				
				checkbox = "<input type='checkbox' checked='1' onClick='removeFromCart(\"" + ItmId + "\")' />";				
				link_id = '<div class="shopLink" onMouseOver="startSearchTip(\''+ ItmId + '\')" onMouseOut="stopTip()">' + ItmId + '</div>';
				
				
				price = row['cnvrt_price'];
				
				if(cart['prom']) {
				
					pr_price = (row['promo_price']) ? row['promo_price'] : price;
					pr_class = (price != pr_price) ? pr_pr_class : "";
					if (price == pr_price) price = "";				
					content  += "<tr><td>" + checkbox + "</td><td>" + link_id + "</td><td><div  " + pr_class + ">" + price + "</div><div class='pr'>" + pr_price + "</div></td></tr>";
				}
				
				else {
					
					content  += "<tr><td>" + checkbox + "</td><td>" + link_id + "</td><td><div class='pr' >" + price + "<div></td></tr>";
				}
				
				i++;
				count++;
			}
			
			
		
			if(cart['prom']) {
				pr_class = (total_price != total_pr_price) ? pr_pr_class : "";
				total_price = (total_price != total_pr_price) ? total_price : "";
				content  += "<tr class=\"totRow\"><td>&nbsp;</td><td>Total:</td><td class=\"totShop\"><div " + pr_class + ">" + total_price + "</div><div class='pr'>" + total_pr_price + "</div></td></tr>";
			}
			else {
				content  += "<tr class=\"totRow\"><td>&nbsp;</td><td>Total:</td><td class=\"totShop\"><div class='pr'>" + total_price + "</div></td></tr>";
			}
			
			
			
			if (i > 1 && !error) {
				
				content += '<tr><td colspan="3" class="checkoutBtCont"><a id="checkoutBt" class="button_std gradient_green" href="' +
					URL_CHECKOUT + '">' + 
					BT_CHECKOUT + '</a></td></tr>';	
			}
			
			else if (error) {
				content += '<tr><td colspan="3" class="checkoutBtCont" >' + errorMessageFormat(error) + '</td></tr>'; 
			}
			
			content = '<table border="0" cellspacing="0" cellpadding="0" >' + content + '</table>';
			
			
			products_count = count;
			cartTable.innerHTML = content;
			cartTable.style.width = width;
			
			var cart_links = jQuery('#cart_links'), il = cart_links.html(), cl = products_count> 0 ? 'cart_filled' : 'cart_empty';
			cart_links.html(il.replace(/\(\d+\)/, '('+products_count+')'));
			cart_links.attr('class', cl);
			
//			if (products_count > 0) {jQuery('#cart_tab').show()}
//			else {jQuery('#cart_tab').hide()}
			
}



function errorMessageFormat(error) {
	return '<div class="errorMessage" >' + error + '</div>';
}

//...........



function changeCurrency() {
	var currencyControl = $('currencyControl');
	var cur = currencyControl.options[currencyControl.selectedIndex].value;
	ajaxLoadPost(URL_SERVER, preparePostAJAX({currency:cur, operation:'changeCurrency'}), onChangeCurrency);
}
function onChangeCurrency(ajaxObject) {
	window.location.href = window.location.href;
}




function sendForm(form, input, value) {

	var form = document.forms[form];
	var input = form.elements[input];
	
	if(!form || !input) {
		alert("error");
		return;
	}
	else {
		input.value = value;
		form.submit();
	}

}


function editorsPick(id){		
	ajaxLoadPost(URL_SERVER, preparePostAJAX({id:id, operation:'editors_pick'}), getEditorsPick);
}
function getEditorsPick(ajaxObject) {	
	var res = processAJAX(ajaxObject);	
	var text = res['added'] ? 'Remove Editors Pick' : 'Editors Pick'; 	
	if (res['id']) jQuery('#ep'+res['id']).html(text);
}


var CART_SCROLL = true;
jQuery(document).ready(function(){
	
	if (jQuery('#cart_static_container').offset()) {
	var original_cart_top = jQuery('#cart_static_container').offset().top;
	jQuery(document).scroll(function(){

		if ( CART_SCROLL !== true ) {
			return;
		}
    	if (original_cart_top < jQuery(document).scrollTop()) {
	        jQuery('#cart_static_container').offset({left: jQuery('#cart_static_container').offset().left, top: jQuery(document).scrollTop()});
	    } else {
        	jQuery('#cart_static_container').offset({left: jQuery('#cart_static_container').offset().left, top: original_cart_top});
    	}
	});
	}
});



