/* xCartMod.com Ajax Add To Cart */
var cached_img_loader = null;
var alt_post_params = null;
var txt = '';

function update_minicart_area(items, cost) {

	if (document.getElementById('minicart_total_items') != null)
		document.getElementById('minicart_total_items').innerHTML = items;

	if (document.getElementById('minicart_total_cost') != null)
	    document.getElementById('minicart_total_cost').innerHTML = cost;

	if (document.getElementById('minicart_total_info_1') != null)
	    document.getElementById('minicart_total_info_1').style.display = '';

	if (document.getElementById('minicart_total_info_2') != null)
	    document.getElementById('minicart_total_info_2').style.display = '';

	if (document.getElementById('minicart_empty_info') != null)
	    document.getElementById('minicart_empty_info').style.display = 'none';
}

function myFinish( cacheElem ){
	var imgs = document.getElementsByTagName("img");
	for(var i=0; i<imgs.length; i++){
		if (imgs[i].id.search('upsell_img_'!= -1)) {
			imgs[i].style.display='';
		}
	}
} 

function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}


function add2cart_ajax(event_obj) {
var req = new Subsys_JsHttpRequest_Js();
req.onreadystatechange = function() {
	if (req.readyState == 4) {
		if (req.responseJS != null && req.responseJS[0] != null) { 
			if (req.responseJS[0]) {  
				txt = req.responseJS[0]['div_content'];  
				document.getElementById('ajax_minicart').innerHTML = txt;	
				document.getElementById('ajax_minicart').style.display = 'block';

				if (aatc_popup_in_shadowbox) {
					var sh_box_height = document.getElementById('ajax_minicart').offsetHeight;
	
					document.getElementById('ajax_minicart').style.display = 'none';
					document.getElementById('ajax_minicart').innerHTML = '';
					sh_box_height += 4+upselling_line_height*req.responseJS[0]['upselling_lines_num'];

    				Shadowbox.open({
        				player:     'html',
						content: 	txt,
						options:	{displayNav: false, onFinish : myFinish}, 
						height:		sh_box_height,	
        				width:      390 
    				});
				} else {
            		m_cart_left = (document.body.clientWidth-400)/2;
	            	document.getElementById('ajax_minicart').style.left = m_cart_left+"px";
				
		          	m_cart_top = 30 + f_scrollTop();
        	    	document.getElementById('ajax_minicart').style.top = m_cart_top+"px";
	//				scroll_page(10);	
				}
				update_minicart_area(req.responseJS[0]['minicart_total_items'], req.responseJS[0]['minicart_total_cost']);
			} else {
				document.getElementById('ajax_minicart').innerHTML = '';
				document.getElementById('ajax_minicart').style.display = 'none';
			}
        } else {
            document.getElementById('ajax_minicart').innerHTML = '';
            document.getElementById('ajax_minicart').style.display = 'none';
        }		
	}
}

    var post_params = [];

    if (alt_post_params != null) {
        post_params = alt_post_params;
		alt_post_params = null;
    } else {

		productidparam = document.orderform.productid.value;

		qtyparam = 1;
        
		if (document.getElementById("product_avail") != null) {
	        qtyparam = document.getElementById("product_avail").value;
        } else if (document.orderform.amount != null) {
            qtyparam = document.orderform.amount.value;
        }

		post_params = {amount:qtyparam, productid:productidparam, mode:'add', ajaxmode:'Y', mainproductid:productidparam};

		if (document.getElementById("customers_price") != null)
			post_params['price'] = document.getElementById("customers_price").value;

        var product_options_vals = [];

        if (typeof(names) != "undefined") {
	        for (clsid in names) {
    	        product_options_vals[clsid] = document.getElementById("po"+clsid).value;
            }
            post_params['product_options'] = product_options_vals;
        }
    }

//	if (!aatc_popup_in_shadowbox)
//	    scroll_page(10); 

    if (cached_img_loader != null) {
        document.getElementById('ajax_minicart').innerHTML = cached_img_loader;
    } else {
        cached_img_loader = document.getElementById('ajax_minicart').innerHTML;
    }

	if (!aatc_popup_in_shadowbox) { 
	    document.getElementById('ajax_minicart').style.display = 'block';
	    m_cart_left = (document.body.clientWidth-400)/2;
    	document.getElementById('ajax_minicart').style.left = m_cart_left+"px";
    	m_cart_top = 30 + f_scrollTop(); 
    	document.getElementById('ajax_minicart').style.top = m_cart_top+"px";
	}

	req.caching = false;
	req.open('POST', 'ajax_cart.php', true);
	req.send(post_params);

	if (ga_minicart_on) {
    	if (typeof (pageTracker) != "undefined") {
			if (pageTracker._trackPageview) {
 				pageTracker._trackPageview('/minicart');
			}
		}
	}
}

function add2cart_ajax_2(prodid, qty, mainprodid) {
	alt_post_params = {productid:prodid, mode:'add', ajaxmode:'Y', mainproductid:mainprodid, amount:qty};
	add2cart_ajax(null);
}

function add2cart_ajax_item(prodid, qty) {
    alt_post_params = {productid:prodid, mode:'add', ajaxmode:'Y', mainproductid:prodid, amount:qty};
	add2cart_ajax(null);
}


//mouse down on dragged DIV element
function aatc_startdrag(t, e) {
	if (e.preventDefault) e.preventDefault(); //line for IE compatibility
	e.cancelBubble = true;
	window.document.onmousemoveOld = window.document.onmousemove;
	window.document.onmouseupOld = window.document.onmouseup;
	window.document.onmousemove=aatc_dodrag;
	window.document.onmouseup=aatc_stopdrag;
	window.document.draged = t;
	t.dragX = e.clientX;
	t.dragY = e.clientY;
	return false;
}
//move the DIV
function aatc_dodrag(e) {

	if (!e) e = event; //line for IE compatibility
	t = window.document.draged;
	t.style.left = (t.offsetLeft + e.clientX - t.dragX)+"px";
	t.style.top = (t.offsetTop + e.clientY - t.dragY)+"px";

	t.dragX = e.clientX;
	t.dragY = e.clientY;
	return false;
}
//restore event-handlers
function aatc_stopdrag() {
   window.document.onmousemove=window.document.onmousemoveOld;
   window.document.onmouseup=window.document.onmouseupOld;
}

/* xCartMod.com Ajax Add To Cart */
