var url_ajax_product = "/modules/tshirtecommerce/ajax.php?method=products";
var url_ajax_product_changed = "/modules/tshirtecommerce/ajax.php?method=changeproduct";
var url_ajax_options_ps = "/modules/tshirtecommerce/ajax.php?method=attributes";
//var url_ajax_categories = "/modules/tshirtecommerce/ajax?method=categories";

design.ajax.getPrice = function() {
    if(jQuery("#ps_color_pick_hidden").val() === 'undefined' || jQuery("#ps_color_pick_hidden").val() == "") {
        jQuery("#ps_color_pick_hidden").val(jQuery("#product-list-colors .bg-colors.active").attr("id-attribute"));
    }

    var datas = this.form();
    var options_ps = jQuery("#tool_cart_ps").serializeObject();
    var datas_ps = jQuery.extend(datas, options_ps);
    jQuery.ajax({
        url: mainURL + "modules/tshirtecommerce/ajax.php",
        type: "POST",
        data: {
            method: "prices",
            id: window.parent.ps_product_id,
            lang: window.parent.ps_language_id,
            shop: window.parent.ps_shop_id,
            attributes: datas_ps,
            id_customer: window.parent.ps_id_customer,
            id_currency: window.parent.ps_id_currency,
            id_country: window.parent.ps_id_country,
            id_group: window.parent.ps_id_group,
            quantity: jQuery("#quantity").val(),
        },
        dataType: "json",
        success: function(json) {
            datas.ps_price_sale = json.price;
            datas.ps_price_old = json.price;
            datas.ps_taxes = json.taxes;
            min_order = json.min_order;
            max_order = json.max_order;

            jQuery("#idCombination").val(json.ipa);
            //datas.ps_taxes = json.taxes;
            var lable = jQuery("#product-price .product-price-title");
            var div = jQuery("#product-price .product-price-list");
            var title = "";
            lable.html("Updating...");
            jQuery.ajax({
                type: "POST",
                processData: false,
                dataType: "json",
                url: siteURL + "ajax.php?type=prices",
                data: JSON.stringify(datas),
                contentType: "application/json; charset=utf-8"
            }).done(function(data) {
                if (data != "") {
                    if (typeof data.sale != "undefined") {
                        jQuery(document).triggerHandler("price.addtocart.design", data);
                        jQuery(".price-sale-number").html(data.sale);
                        jQuery(".price-old-number").html(data.old);
                        if (data.sale == data.old) {
                        	jQuery("#product-price-old").css("display", "none");
                        } else {
                       		jQuery("#product-price-old").css("display", "inline-block");
                       	}
                    }
                }
            }).always(function() {
                lable.html(title);
                design.print.colors();
            });
        }
    });
};

jQuery(document).on('before.product.change.design', function(event, product) {
    jQuery('#app-wrap .product-design').html('');
    var ps_id = product.parent_id;

    jQuery.ajax({
        url: mainURL + url_ajax_product_changed,
        type: "POST",
        dataType: "json",
        data: {
            id: ps_id,
            shop: window.parent.ps_shop_id,
            lang: window.parent.ps_language_id,
        }
    }).done(function(res) {
        c_parent_id = res.parent_id;
        product.parent_id = res.parent_id;
        window.parent.ps_product_id = res.parent_id;
        product.min_order = res.min_order;
        product.max_order = res.max_order;
        product.id = res.design_id;
        product.price = res.price;
        jQuery("#quantity").val(product.min_order);
    });
});
jQuery(document).on('product.change.design', function(event, product) {
    jQuery("#tool_cart_ps").remove();
});

design.saveadmin = function(check) {
    if (design.ajax.isBlank() == false) return false;
    if (admin_id == 0) {
        is_save = 1;
        jQuery("#f-login").modal();
    } else {
        var view = "front";
        jQuery(".labView").each(function() {
            if (jQuery(this).find(".product-design").html() != "") {
                view = jQuery(this).attr("id").replace("view-", "");
                return false;
            }
        });
        if (jQuery(".labView.active .design-area").hasClass("zoom")) {
            design.tools.zoom();
        }
        if (admin_id == design.designer_id) {
            if (typeof check != "undefined" && check == 1) {
                jQuery("#save-design-info-admin").modal("hide");
            } else {
                jQuery("#save-design-info-admin").modal("show");
                return false;
            }
            design.svg.items(view, design.saveDesignAdmin);
        } else {
            if (typeof check != "undefined" && check == 1) {
                jQuery("#save-design-info-admin").modal("hide");
                jQuery("#dg-mask").css("display", "block");
                jQuery("#dg-designer").css("opacity", "0.3");
                design.svg.items(view, design.saveDesignAdmin);
            } else {
                jQuery("#save-design-info-admin").modal("show");
                return false;
            }
        }
    }

    return true;
};

design.saveDesignAdmin = function() {
	if (design.view != "done") {
	    //if (jQuery('#view-'+design.view+' .product-design').html() != '')
	    //{
	    if (design.view == "back") {
	        design.view = "left";
	        design.svg.items("back", design.saveDesignAdmin);
	        return false;
	    } else if (design.view == "left") {
	        design.view = "right";
	        design.svg.items("left", design.saveDesignAdmin);
	        return false;
	    } else if (design.view == "right") {
	        design.view = "done";
	        design.svg.items("right", design.saveDesignAdmin);
	        return false;
	    }
	} else {
	    var leftViewFlg = true, rightViewFlg = true;
	    if (jQuery("#view-left .product-design").html() == "") {
	        leftViewFlg = false;
	    }
	    if (jQuery("#view-right .product-design").html() == "") {
	        rightViewFlg = false;
	    }
	    if (design.view == "back") {
	        if (leftViewFlg) {
	            design.view = "right";
	            design.svg.items("left", design.saveDesignAdmin);
	            return false;
	        } else {
	            if (rightViewFlg) {
	                design.view = "done";
	                design.svg.items("right", design.saveDesignAdmin);
	                return false;
	            }
	        }
	    } else if (design.view == "left") {
	        if (rightViewFlg) {
	            design.view = "done";
	            design.svg.items("right", design.saveDesignAdmin);
	            return false;
	        }
	    }
	}
	var data = design.ajax.form();
	data.images = {};
	if (jQuery("#view-front .product-design").html() != "") {
	    if (design.isIE()) {
	        data.images.front = design.front.svgThum;
	    } else {
	        data.images.front = design.output.front.toDataURL();
	    }
	} else {
	    data.images.front = "";
	}
	if (jQuery("#view-back .product-design").html() != "") {
	    if (design.isIE()) {
	        data.images.back = design.back.svgThum;
	    } else {
	        data.images.back = design.output.back.toDataURL();
	    }
	} else {
	    data.images.back = "";
	}
	if (jQuery("#view-left .product-design").html() != "") {
	    if (design.isIE()) {
	        data.images.left = design.left.svgThum;
	    } else {
	        data.images.left = design.output.left.toDataURL();
	    }
	} else {
	    data.images.left = "";
	}
	if (jQuery("#view-right .product-design").html() != "") {
	    if (design.isIE()) {
	        data.images.right = design.right.svgThum;
	    } else {
	        data.images.right = design.output.right.toDataURL();
	    }
	} else {
	    data.images.right = "";
	}
	var vectors = JSON.stringify(design.exports.vector());
	var teams = JSON.stringify(design.teams);
	var productColor = design.exports.productColor();
	var thumb = "";
	if (data.images.front != "") {
	    thumb = data.images.front;
	} else if (data.images.back != "") {
	    thumb = data.images.back;
	} else if (data.images.left != "") {
	    thumb = data.images.left;
	} else if (data.images.right != "") {
	    thumb = data.images.right;
	}
	data.image = thumb;
	data.vectors = vectors;
	data.teams = teams;
	data.fonts = design.fonts;
	data.product_id = product_id;
    if (typeof window.parent.ps_id_product !== 'undefined') {
        parent_id = window.parent.ps_id_product;
    }
    data.parent_id = parent_id;
	data.design_id = design.design_id;
	data.design_file = design.design_file;
	data.designer_id = design.designer_id;
	data.design_key = design.design_key;
	data.product_color = productColor;
	data.isIE = design.isIE();
	data.title = jQuery("#design-save-title-admin").val();
	data.description = jQuery("#design-save-description-admin").val();
	jQuery(document).triggerHandler("before.save.design", data);
	jQuery.ajax({
	    url: siteURL + "ajaxps.php?type=saveDesignAdmin",
	    type: "POST",
	    contentType: "application/json",
	    async: true,
	    data: JSON.stringify(data)
	}).done(function(msg) {
	    var results = eval("(" + msg + ")");
	    if (results.error == 1) {
	        alert(results.msg);
	        jQuery("#dg-mask").css("display", "none");
	        jQuery('.modal-backdrop').removeClass('in');
	        jQuery('.modal-backdrop').css('display', 'none');
	        jQuery('#product-thumbs > .box-thumb.active').click();
		    jQuery("#dg-designer").css("opacity", "1");
		    jQuery('#save-design-info-admin').modal('hide');
	        return false;
	    } else {
	        design.design_id = results.content.design_id;
	        design.design_file = results.content.design_file;
	        design.designer_id = results.content.designer_id;
	        design.design_key = results.content.design_key;
	        design.productColor = productColor;
	        design.product_id = product_id;
	        results.content.productColor = productColor;
	        results.content.product_id = product_id;
	        //jQuery(document).triggerHandler("done.save.design", results.content);
	        var linkEdit = siteURL + "sharing.php?/" + results.content.user_id + ":" + results.content.design_key + ":" + product_id + ":" + productColor + ":" + parent_id;
	        window.parent.document.getElementById('_product_id').value =results.content.user_id + ":" + results.content.design_key + ":" + product_id + ":" + productColor + ":" + parent_id;
	        jQuery("#link-design-saved").val(linkEdit);
	        jQuery("#dg-share").modal('hide');

	        jQuery("#dg-mask").css("display", "none");
	        jQuery('.modal-backdrop').removeClass('in');
	        jQuery('.modal-backdrop').css('display', 'none');
	        jQuery('#product-thumbs > .box-thumb.active').click();
		    jQuery("#dg-designer").css("opacity", "1");
		    jQuery('#save-design-info-admin').modal('hide');

		    // fixed save on 1.6
		    if (window.parent.document.getElementById('_submitAddproductAndStay')) {
		    	window.parent.document.getElementById('_submitAddproductAndStay').click();
		    }

	        return true;
	    }
	});

	return false;
};

jQuery(document).on('ini.design', function(event) {
	if (typeof window.parent.tshirtecommerce_admin === "undefined" && window.parent.tshirtecommerce_admin != 1) {
        $jd.ajax({
            url: mainURL + "modules/tshirtecommerce/ajax.php",
            type: "POST",
            data: {
                method: "attributes",
                id: window.parent.ps_product_id,
                lang: window.parent.ps_language_id,
                shop: window.parent.ps_shop_id
            },
            dataType: "html",
            success: function(json) {
                $jd("#product-details").append(json);
                jQuery(".price-restart").trigger("click");
            }
        });
    }
});

jQuery(document).on('after.load.design', function(event, data) {
    jQuery('#design-save-title-admin').val(data.design.title);
    jQuery('#design-save-description-admin').val(data.design.description);
});

jQuery(document).on('form.addtocart.design', function(event, datas) {
	var options_ps = jQuery("#tool_cart_ps").serializeObject();
    datas = jQuery.extend(datas, options_ps);
});

jQuery(document).on('after.addtocart.design', function(event, data) {
	if (data != "") {
	    var content = data;
	    if (content.error == 0) {
	        content.product.product_id = parent_id;
	        content.product.token = window.parent.token;
	        content.product.id_combination = datas.id_product_attribute;
	        content.product.design_imgs = "";
	        if (typeof data.product.images !== "undefined") {
	            content.product.design_imgs = data.product.images;
	        }
	        content.product.tshirtecommerce_design_cart_id = "";
	        if (typeof data.product.rowid !== "undefined") {
	            content.product.tshirtecommerce_design_cart_id = data.product.rowid;
	        }
	        window.parent.app.cart(content.product);
	    } else {
	        alert(content.msg);
	    }
	}
	return;
});

jQuery(document).on('price.addtocart.design', function(event, data) {
	var obj_old = '', obj_sale = '';
	jQuery('#product-price-old').html('');
	jQuery('#product-price-sale').html('');

	if (typeof data.old_symbol !== 'undefined' && typeof data.currency_symbol !== 'undefined') {
		if (typeof data.currency_postion !== 'undefined' && data.currency_postion == 'right') {
			obj_old = '<span class="price-sale-number">' + data.old + '</span>' + data.currency_symbol;
			obj_sale = '<span class="price-sale-number">' + data.sale + '</span>' + data.currency_symbol;
		} else {
			obj_old = data.currency_symbol + '<span class="price-sale-number">' + data.old + '</span>';
			obj_sale = data.currency_symbol + '<span class="price-sale-number">' + data.sale + '</span>';
		}

		jQuery('#product-price-old').html(obj_old);
		jQuery('#product-price-sale').html(obj_sale);
	}
});
