/* CartWorks.com Ajax Add To Cart */
var cached_img_loader = [];
//cached_img_loader['quick_cart'] = "<div align='center' style='padding: 0px; width:120px; height:120px; border: 1px solid red;'><br /><br /><br /><img width='100px' src='"+images_dir+"/img_loader.gif' alt='' /><br /><br /><br /></div>";
var alt_post_params = null;
var txt = '';

function update_minicart_area(items, cost, quick_cart) {

	if (items > 0 ) {
		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';
	
		if (document.getElementById('quick_cart') != null) {	
			document.getElementById('quick_cart').style.display = '';
			document.getElementById('quick_cart').innerHTML = quick_cart; 
		}
	} else {
                if (document.getElementById('minicart_total_info_1') != null)
                    document.getElementById('minicart_total_info_1').style.display = 'none';

                if (document.getElementById('minicart_total_info_2') != null)
                    document.getElementById('minicart_total_info_2').style.display = 'none';

                if (document.getElementById('minicart_empty_info') != null)
                    document.getElementById('minicart_empty_info').style.display = '';

                if (document.getElementById('quick_cart') != null) 
         	       document.getElementById('quick_cart').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 aatc_popup (txt, upselling_lines_num) {
	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*upselling_lines_num;

                Shadowbox.open({
                	player: 'html',
                        content:        txt,
                        options:        {displayNav: false, onFinish : myFinish},
                        height:         sh_box_height,
                        width:      390
                });
        } else {
		aatc_position_popup (); 
        }
}

function aatc_position_popup () {
	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";
}

function aatc_hide_popup () {
	 document.getElementById('ajax_minicart').innerHTML = '';
         document.getElementById('ajax_minicart').style.display = 'none';
}

function aatc_collect_product_post () {
	var post_params = [];
	var qty_input_control = "product_avail";

        productidparam = document.orderform.productid.value;
        qtyparam = 1;
        if (document.getElementById("product_avail_input") != null && document.getElementById("product_avail") != null) {
       		if (document.getElementById("product_avail_input").style.display == "" && document.getElementById("product_avail").style.display != "")
                	qty_input_control = "product_avail_input";
        }

        if (document.getElementById(qty_input_control) != null) {
	        qtyparam = document.getElementById(qty_input_control).value;
        } else if (document.orderform.amount != null) {
        	qtyparam = document.orderform.amount.value;
        }

        post_params = {amount:qtyparam, productid:productidparam, mode:'add', ajaxmode:'Y', mainproductid:productidparam, access_on:x_access_on, accessories:x_accessories, product_options_access:x_product_options_access};
	/* Accessories */
        var x_access_on = new Array();
        var x_accessories = new Array();
        var x_product_options_access = new Array();
        if (typeof(access_arr) != 'undefined') {
	        if (access_arr != null) {
        	        for(var i=0; i<access_arr.length; i++) {
                	        if (document.getElementById("access_on_" + access_arr[i][0]).checked == true) {
                                	x_access_on[access_arr[i][2]] = "Y";
                                        x_accessories[access_arr[i][2]] = document.getElementById("accessories_" + access_arr[i][0]).value;

                                        if (names_access[access_arr[i][0]] != null) {
                                                for (naac in names_access[access_arr[i][0]]) {
                                                        x_product_options_access[access_arr[i][2]] = [];
                                                        x_product_options_access[access_arr[i][2]][naac] = document.getElementById("xpo"+naac).value;
                                                }
                                        }
                                }
                        }
                }
        }
        post_params['access_on'] = x_access_on;
        post_params['accessories'] = x_accessories;
        post_params['product_options_access'] = x_product_options_access;

/* Accessories */
        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;
        }
	return post_params;
	
}

function aatc_img_loader (area_id) {
	if (document.getElementById(area_id) != null) {
	        if (cached_img_loader[area_id] != null) {
	                document.getElementById(area_id).innerHTML = cached_img_loader[area_id];
        	} else {
                	cached_img_loader[area_id] = document.getElementById(area_id).innerHTML;
	        }
	}
}

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]) {  
					aatc_popup(req.responseJS[0]['div_content'], req.responseJS[0]['upselling_lines_num']); 
					update_minicart_area(req.responseJS[0]['minicart_total_items'], req.responseJS[0]['minicart_total_cost'], req.responseJS[0]['quick_cart']);
				} else {
					aatc_hide_popup();
				}
			} else {
				aatc_hide_popup();
			}		
		}
	}

	var post_params = [];

	if (alt_post_params != null) {
		post_params = alt_post_params;
		alt_post_params = null;
	} else {
        	post_params = aatc_collect_product_post();
    	}
	
	aatc_img_loader('ajax_minicart');
	aatc_img_loader('quick_cart');

	if (!aatc_popup_in_shadowbox) { 
		document.getElementById('ajax_minicart').style.display = 'block';
		aatc_position_popup ();
	}

	req.caching = false;
	req.open('POST', 'ajax_cart.php', true);
	req.send(post_params);
	aatc_ga_track('/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);
}

function delete_ajax_item(prodid) {
        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]) {
                                        update_minicart_area(req.responseJS[0]['minicart_total_items'], req.responseJS[0]['minicart_total_cost'], req.responseJS[0]['quick_cart']);
                                } else {
                                        aatc_hide_popup();
                                }
                        } else {
                                aatc_hide_popup();
                        }
                }
        }

        var post_params = [];
	post_params = {mode:'delete', ajaxmode:'Y', productindex:prodid};

        aatc_img_loader('quick_cart');

        req.caching = false;
        req.open('POST', 'ajax_cart.php', true);
        req.send(post_params);
}

function aatc_popup_open() {
        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]) {
					aatc_popup(req.responseJS[0]['div_content'], req.responseJS[0]['upselling_lines_num']);
//                                        update_minicart_area(req.responseJS[0]['minicart_total_items'], req.responseJS[0]['minicart_total_cost'], req.responseJS[0]['quick_cart']);
                                } else {
                                        aatc_hide_popup();
                                }
                        } else {
                                aatc_hide_popup();
                        }
                }
        }

        var post_params = [];
        post_params = {ajaxmode:'Y'};

        if (aatc_productid != 0) {
                post_params['productid'] = aatc_productid;
        }

        req.caching = false;
        req.open('POST', 'ajax_cart.php', true);
        req.send(post_params);
}



function aatc_ga_track(ga_string) {

        if (ga_minicart_on) {
        if (typeof (pageTracker) != "undefined") {
                        if (pageTracker._trackPageview) {
                                pageTracker._trackPageview(ga_string);
                        }
                }
        }
}


//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;
}

/* CartWorks.com Ajax Add To Cart */

