/**
* @version $Id: popup.js 38126 2010-07-12 17:10:22Z daniel $
*/

jQuery.extend({
	"popup_plugin": {

		"url": "",

		"form_flag": false,

		"id": "",

		"init": function () {
			jQuery("a[href*='page=item']").each(function () {
				if (jQuery(this).attr("href").match(/action=cart/)) {
					jQuery(this).addClass("popup_upgrade");
				}
			});
			jQuery("a.popup_upgrade").click(function (){
				jQuery.popup_plugin.url = jQuery(this).attr("href");
				jQuery.popup_plugin.id = jQuery(this).attr("href").substring(jQuery(this).attr("href").indexOf("id="));
				jQuery.popup_plugin.get_upgrade("link");
				return false;
			});

			jQuery("form #masform").submit(function () {
				if (smarty_vars['divinity']['page'] != "item") {
					return true;
				}
				if (jQuery.popup_plugin.form_flag) {
					return true;
				}
				jQuery.popup_plugin.id = "id="+(jQuery("#masform :input[name='id']").val().length ?
					jQuery("#masform :input[name='id']").val() :
					jQuery("#masform :input[name='id[0]']").val());
				jQuery.popup_plugin.get_upgrade("form");
				return false;
			});
		},

		"get_upgrade": function (act) {
			jQuery.ajax({
				"async": true,
				"type": "POST",
				"url": smarty_vars['rel_html_url']+"index.php?type=ajax&page=upgrade_item&action=get_upgrade&"+jQuery.popup_plugin.id,
				"dataType": "html",
				"success": function (o) {
					if (o == "") {
						if (act == "form") {
							jQuery.popup_plugin.form_flag = true;
							jQuery("form[name='display']").submit();
						} else if (act == "link") {
							jQuery(location).attr("href", jQuery.popup_plugin.url);
						}
					} else {
						jQuery.item.popup(o);
					}
				}
			});
		}
	}
});

jQuery(document).ready(function () {
	jQuery.popup_plugin.init();
});