var dialog_created = false;
var ProductAjaxCompare = jQuery.extend(Products || {},
{
	init: function()
	{
		this.compare_dialog		= $('<div style="display:hidden" id="compare_form"></div>').appendTo('body');
		this.compare_link		= $($('a.compare').get(0));
		this.product_id			= this.compare_link.attr('id').replace(/[^\d]+/,'');
		this.current_page		= 1;

		this.save_selected_filters();

		this.dialog_created = false;

		this.compared_items_block_content = '';//used to save the products who are currently in the compare list
		this.first_load = true;

		var _this = this;
		if($('#compare_form').length) {
			this.compare_link.click(function() {
				return _this.load_popup.call(_this, arguments);
			});
		}
	},

	create_popup: function()
	{
		var _this = this;

		var additional_params = $('#ajax_compare_filters_form').length ? '&' + $('#ajax_compare_filters_form').serialize() : ''
			additional_params += '&page=' + this.current_page;

		this.compare_dialog.load
		(
			'/xml_http_request',
			'method=show_compare_form&product_id='+_this.product_id + additional_params,
			function (responseText)
			{
				_this.compare_dialog.dialog({
					modal			: true,
					closeOnEscape	: true,
					resizable		: false,
					width			: 670,
					minWidth		: 670,
					minHeight		: 350,
					position		: 'top',
					title			: 'Изберете 1 или 2 продукта за сравнение с текущия'
				});

				dialog_created = true;

				if(_this.first_load) {
					_this.first_load = false;
					_this.compared_items_block_content = $('#compared_items_block').html();
				}
				else {
					$('#compared_items_block').html(_this.compared_items_block_content);
				}

				_this.set_saved_filter_values();
				_this.observe_filter_change();
				_this.observe_compare_links();
				_this.rebuild_paging();
				Products.save_back_link_url();

				//$('#ajax_compare_filters_form').jqTransform();
			}
		);

		return false;
	},

	load_popup: function()
	{
		if(!dialog_created) {
			this.create_popup();
		}
		else {
			this.compare_dialog.dialog('open');
		}
	},

	make_search: function(reset_paging)
	{
		if(reset_paging) this.current_page = 1;

		this.save_selected_filters();
		this.compared_items_block_content = $('#compared_items_block').html();
		this.create_popup();
	},

	observe_compare_links: function()
	{
		var _this = this;

		//make disabled all products who are allready added to compare
		$('#compared_items_block a.go_compare').each(function(ind, el){
			$('#' + el.id.replace('remove_','')).addClass('inactive');
		});

		//attach go to compare list functionality
		$('#items_block a.go_compare').click(function()
		{
			//make nothing when the product is already added
			if($(this).hasClass('inactive')) return;

			if($('#list_of_items_to_compare a.go_compare').length == 2) {
				alert('Може да изберете максимум 2 продукта, които да сравните с текущия')
				return;
			}

			var _link = this;
			var product_tr = $($(this).parents('tr').get(0));

			if($('#no_items_to_compare:visible').length) {
				$('#no_items_to_compare').hide();
			}

			$(this).addClass('inactive');

			$(product_tr.clone().appendTo('#list_of_items_to_compare'))
				.find('.go_compare')//find the link
					.removeClass('add_to_cart')//remove unneeded class
					.removeClass('inactive')//remove unneeded class
					.addClass('delete_ico')//change icon via css class
					.html(_LABELS.remove_label)//replace the label
					.attr('id', 'remove_'+this.id)//attach a new id
					.click(function()
					{
						$(_link).removeClass('inactive');
						$($(this).parents('tr').get(0)).remove();//remove the row in compared item table

						if($('#list_of_items_to_compare tr').length == 2) {
							$('#no_items_to_compare').show();
							$('#make_the_compare').hide();
						}
						_this.recreate_compare_link();
					});

			$('#make_the_compare').show();

			_this.recreate_compare_link();

		});

		//attach remove from compare list funcitonality
		$('#compared_items_block a.go_compare').click(function()
		{
			_link = $('#' + this.id.replace('remove_',''));

			$(_link).removeClass('inactive');
			$($(this).parents('tr').get(0)).remove();//remove the row in compared item table

			if($('#list_of_items_to_compare tr').length == 2) {
				$('#no_items_to_compare').show();
				$('#make_the_compare').hide();
			}

			_this.recreate_compare_link();
		});
	},

	observe_filter_change: function() {
		var _this = this;
		document.getElementById('sort_sel') ? document.getElementById('sort_sel').onchange = function(){_this.make_search(true);} : null
		document.getElementById('brand_id') ? document.getElementById('brand_id').onchange = function(){_this.make_search(true);} : null
		document.getElementById('subcategory_id') ? document.getElementById('subcategory_id').onchange = function(){_this.make_search(true);} : null
		document.getElementById('filter_id') ? document.getElementById('filter_id').onchange = function(){_this.make_search(true);} : null
	},

	save_selected_filters: function() {
		this.selected_sort_sel 			= $('#sort_sel').val();
		this.selected_brand_id			= $('#brand_id').val();
		this.selected_subcategory_id	= $('#subcategory_id').val();
		this.selected_filter_id			= $('#filter_id').val();
	},

	set_saved_filter_values: function()
	{
		var _this = this;

		$('#sort_sel option').each(function(ind, el) {
			if(el.value == _this.selected_sort_sel) {el.selected = true;}
		});

		$('#brand_id option').each(function(ind, el) {
			if(el.value == _this.selected_brand_id) {el.selected = true;}
		});

		$('#subcategory_id option').each(function(ind, el) {
			if(el.value == _this.selected_subcategory_id) {el.selected = true;}
		});

		$('#filter_id option').each(function(ind, el) {
			if(el.value == _this.selected_filter_id) {el.selected = true;}
		});
	},

	recreate_compare_link: function()
	{
		var ids = new Array();
		$('#list_of_items_to_compare a.go_compare').each(function(ind, elem){
			ids.push($(elem).attr('id').toString().replace(/[^\d]+/,''));
		});
		//add current product id
		ids.push(this.product_id);

		link_href = $('#make_the_compare').attr('href');
		link = link_href.substr(0, link_href.lastIndexOf('/')+1);

		$('#make_the_compare').attr('href', link+'?products='+ids.join(','))
	},

	rebuild_paging: function()
	{
		var _this = this;

		$('.paging a[class!=disabled]').each(function(){
			if(!$(this).hasClass('disabled'))
			{
				var link_href = this.href;
				this.href = 'javascript:;';
				$(this).click(function(){
					_this.current_page = parseInt(_this.get_paging_vars(link_href)['page']);
					_this.make_search();
				});
			}
		});
	},

	get_paging_vars:function (link_href)
	{
		var vars = {};
		var parts = link_href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, key, value) {
			vars[key] = value;
		});
		return vars;
	}
});

$(function ()
{
	ProductAjaxCompare.init();
});

