/*
 * Author: VRB
 * Date: 16.XI.2009
 */
;(function($){
	$.fn.print = function(op){
		var _this,
		$this,
		o = [],
		_defaults = {
			description			: 'Please select one of the following print options:',
			printWindowId		: 'print_window',
			cokiePosTopName		: 'print_window_pos_top',
			cokiePosLeftName	: 'print_window_pos_left',
			docID				: 0
		},
		setPosition = function($pf){
			$pf.css("left", getXPosition($pf) + "px");
			$pf.css("top", getYPosition($pf) + "px");

			// TODO: Dodac odczytywanie pozycji z cokiesow
			// MMO: i mozliwosc ustawiania description z zewnatrz tego pliku :/
		},
		getXPosition = function($pf){
			return Math.floor(($("#wrapper").width() - $pf.width()) / 2);
		},
		getYPosition = function($pf){
			return Math.floor(($(window).height() - $pf.height()) / 2) + $(window).scrollTop( );
		},
		addCheckbox = function($pf, c_name, c_value){
			return $pf.append("<div><input type='radio' name='print_option' value='" + c_value + "' id='poid" + c_value + "'/><label for='poid" + c_value + "'>" + c_name + "</label></div>").find("input");
			//$pf.append("<div><input type='radio' name='print_option' value='" + c_value + "' id='poid" + c_value + "'/></div>");
		};

		return this.each(function(i) {
			_this = this;
			$this = $(this);
			o = $.extend({},_defaults,op);
			//debug2($this);
			$this.click(function(e){
				$("#" + o.printWindowId).remove();
				$pw = $("<div id='" + o.printWindowId + "'><h3>Print Options <a href='#close'>Close this window</a></h3></div>").appendTo($("#wrapper"));
				$pw.append("<div class='print_description'>" + o.description + "</div>");
				var $pf = $("<form></form>").appendTo($pw);
				$pw.find("a").click(function(){
					//alert($pw.position().left);
					setCookieValue(o.cokiePosLeftName, $pw.position().left, 100);
					setCookieValue(o.cokiePosTopName, $pw.position().top, 100);
					$("#" + o.printWindowId).remove();
				});
				addCheckbox($pf, "Print this <span>page</span>", 1).attr("checked","checked");
				addCheckbox($pf, "Print this <span>section</span>", 2);
				addCheckbox($pf, "Print all <span>solutions</span>", 3);
				addCheckbox($pf, "Print Futron <span>overview</span>", 4);
				$("<button>Print</button>").appendTo($pf).click(function(){
					var po = $pf.find("input:radio:checked").val();
					var location = new String(document.location);
					var pos = location.indexOf("#");
					if (pos > 0)
						location = location.substr(0, pos);
					pos = location.indexOf("?");
					if (pos >= 0)
						location = location + "&";
					else
						location = location + "?";


					if (po==1){
						//document.location = location + "?media=print";
						window.open(location + "media=print");
						return false;
					}
					location = "/print.xml?doc_id=" + o.docID;

					if (po==3)
						location = location + "&option=solutions";

					if (po==4)
						location = location + "&option=overview";


					window.open(location);
					return false;
				});

				setPosition($pw);
				$pw.draggable({ containment: 'parent' });
				return false;
			});

		});
	};

})(jQuery);
