(function($) {
	/**
	 * Konstruktor ajaxoveho compare, bude se jednat o singletonni aplikaci
	 * @param node
	 */	 	
	function CompareWindow(options) {
		var defaults = {
			posLeft: 500,
			posTop: 200,
			lang: 'cs',
			timeout: 1000,
			className: 'form_porovnat'
		};

		this.timeOut = null;
		this.options = $.extend({}, defaults, options);
	}

	$.extend(CompareWindow.prototype, {
		init: function() {
			var thisRef = this;
			var inner_html = $("<div>",{
				id: 'vrstva_compare',
				'class': 'compare_'+thisRef.options.lang
			});
			var inner_head = $("<div>",{
				id: 'vrstva_compare_nadpis'
			});
			inner_head.append($("<a>",{
				id: 'zavrit_compare'
			}));
			inner_html.append(inner_head);
			inner_html.append($("<div>",{
				id: 'vrstva_compare_cekej'
			}));
			inner_html.append($("<div>",{
				id: 'vrstva_compare_obsah'
			}));
			$("#pozadi").append(inner_html);

			//posouvani okna
			$("#vrstva_compare").draggable( {
				zIndex  : 10000,
				ghosting: false,
				opacity : 0.7,
				handle  :  "#vrstva_compare_nadpis",
				start   : function() {
					clearTimeout(thisRef.timeOut);
				}
			});

			$("#vrstva_compare").hide();
		
			//automaticky posun pri scrollu
			$(window).scroll(function() {
				$("#vrstva_compare").animate({
					top:$(window).scrollTop() + "px"
				}, {
					queue: false,
					duration: 350
				});
			});
		
			//zavreni okna
			$("#zavrit_compare").click(function() {
				$("#vrstva_compare").hide();
				$("#vrstva_compare_obsah").hide();
				$("#vrstva_compare_cekej").show();
				return false;
			});

			// definice eventu pro odeslani
			$("." + thisRef.options.className).each(function (){
				$(this).append($("<input>",{
					type: 'hidden',
					name:'ajax_view',
					value:'1'
				}));
			});
			
			$("." + thisRef.options.className).ajaxForm({
				dataType: 'xml',
				success: function(xml) {
					if ($(xml).find('status').text() == 'redirect') {
						document.location.href = $(xml).find('params param[name=url]').attr('value');
					} else {
						// aktualizuji cenu v boxu s compareem
						$("#compare .price_no_format").html($(xml).find('param[name=price_no_format]').attr('value'));
						$("#compare .count_items").html($(xml).find('param[name=count_items]').attr('value'));
						
						// zobrazim data do compareu (nazev a cenu)
						thisRef.showCompare(this,$(xml).find('params'));
					}
				},
				error: function(a,b) {
					alert(a);
				}
			});
		},
		showCompare: function(node,params) {
			var thisRef = this;
			// TODO nastaveni pozice okna
			var scrollTop = $(window).scrollTop();
			if (thisRef.options.posTop < scrollTop) 
				thisRef.options.posTop = scrollTop;
			$("#vrstva_compare").css({
				top: thisRef.options.posTop + "px",
				left: thisRef.options.posLeft + "px"
			});

			// zpracuji parametry a zobrazim okno
			var paramArr = new Object();
			$(params).find('param').each(function(){
				paramArr[$(this).attr('name')] = $(this).attr('value');
			});

			$("#vrstva_compare_cekej").show();
			$("#vrstva_compare_obsah").load(web_root_lang + "scripts/ajax_compare.php", paramArr, function() {
				$("#vrstva_compare_obsah").show();
				$("#vrstva_compare_cekej").hide();
				$("#vrstva_compare").show();
				thisRef.timeOut = setTimeout(function(){
					$("#vrstva_compare_obsah").hide();
					$("#vrstva_compare_cekej").show();
					$("#vrstva_compare").hide('slow');
				}, thisRef.options.timeout);
				
				//pokud hover, pak se okno NEzavira
				$("#vrstva_compare").hover(
					function(){
						clearTimeout(thisRef.timeOut);
					},
					function(){
						thisRef.timeOut = setTimeout(function(){
							$("#vrstva_compare_obsah").hide();
							$("#vrstva_compare_cekej").show();
							$("#vrstva_compare").hide('slow');
						}, thisRef.options.timeout)
					});
			});
		}
	});
	$.comparewindow = function(options) {
		var comparewindow = new CompareWindow(options);
		comparewindow.init();
	}
})(jQuery);
