/**
* @version $Id: popup.js 45772 2011-01-24 17:27:26Z daniel $
*/

jQuery.extend({
	"mas_popup_plugin": {

		/**
		* Initial actions
		*/
		"init": function () {
			if (smarty_vars['divinity']['page'] == "item" && smarty_vars['divinity']['action'] == "index") {
				jQuery.mas_popup_plugin.id = jQuery("#masform input[name='id']").val() 
					|| jQuery("#masform input[name='id[0]']").val();
				jQuery.mas_popup_plugin.id = 'id=' + jQuery.mas_popup_plugin.id;
				
				jQuery.mas_popup_plugin.popup_check_url = smarty_vars['rel_html_url']+
					"index.php?type=ajax&page=upgrade_item&action=check_item_popup&"+jQuery.mas_popup_plugin.id;
				jQuery.mas_popup_plugin.popup_check();
			} else if (smarty_vars['divinity']['page'] == "category") {
				jQuery.mas_popup_plugin.popup_check_url = smarty_vars['rel_html_url']+
					"index.php?type=ajax&page=upgrade_item&action=check_cat_popup&id="+
					smarty_vars['relative_request_uri'].substring(smarty_vars['relative_request_uri'].indexOf("/category/"));
				jQuery.mas_popup_plugin.popup_check();
			}
			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.mas_popup_plugin.url = jQuery(this).attr("href");
				jQuery.mas_popup_plugin.id = jQuery(this).attr("href").substring(jQuery(this).attr("href").indexOf("id="));
				jQuery.mas_popup_plugin.get_upgrade("link");
				return false;
			});
			jQuery("form[name='display']").submit(function () {
				if (!(smarty_vars['divinity']['page'] == "item" && smarty_vars['divinity']['action'] == "index")) {
					return true;
				}
				if (jQuery.mas_popup_plugin.form_flag) {
					return true;
				}
				jQuery.mas_popup_plugin.get_upgrade("form");
				return false;
			});
		},

		/**
		* Shows upgrade item if there is...
		*/
		"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.mas_popup_plugin.id,
				"dataType": "html",
				"success": function (o) {
					if (o == "") {
						if (act == "form") {
							jQuery.mas_popup_plugin.form_flag = true;
							jQuery("form[name='display']").submit();
						} else if (act == "link") {
							jQuery(location).attr("href", jQuery.mas_popup_plugin.url);
						}
					} else {
						jQuery.item.popup(o);
					}
				}
			});
		},

		/**
		* Shows item/category popup if there is...
		*/
		"popup_check": function () {
			jQuery.getJSON(
				jQuery.mas_popup_plugin.popup_check_url,
				function (data) {
					try {
					jQuery.item.popup(
						"<div>"+data.html+"</div>",
						data.width,
						data.height,
						data.delay
					);
					} catch (e) {
					}
				}
			);
		}
	}
});

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

