/*
 * ajaxChanger with loaderImage
 * Cypyright (c) 2010 Stanislav Lacko, Space Systems, s.r.o., www.spacesystems.sk
 */
(function($) {
	$.ajaxChanger= {
		author:"Stano Lacko, Space Systems, s.r.o., www.spacesystems.sk",
		version:"0.1",
		defaults:{
			loaderPosition:'none',
			loaderImage: '../libs/space/loader.gif',
			url: null,
			key: 'value',
			data: {},
			method: 'post'
		},
		build: function(options){
			return this.each (function (){
				this.options = {};
				$.extend (this.options, $.ajaxChanger.defaults);
				$.extend (this.options, options);
				var right = this.className.match(/right/i);
				if (this.options.loaderPosition!='right') {
					if (right) {
						this.options.loaderPosition = 'left';
					} else {
						this.options.loaderPosition = 'right';
					}
				}
				if (this.options.method!='post') {
					this.options.method='get';
				}
				var input;
				input=$(this);
				input.css('background-repeat','no-repeat');
				input.css('background-position',this.options.loaderPosition+' center');
				if (this.options.url) {
					$(this).bind('change',$.ajaxChanger.saveChanges);
				}
			});
		},
		saveChanges: function(){
			var input= $(this);
			input.css('background-image','url("'+this.options.loaderImage+'")');
			if (this.options.method=='get') {
				alert('Get method not supported yet.');
				return false;
			}
			var data = this.options.data;
			data['key'] = input.attr('spc:value');
			data['value']=input.val();
			$.ajax({
				type: this.options.method,
				url: this.options.url,
				data: data,
				success: function(return_value,status) {
					if (return_value=='true') {
						if (input.attr('spc:reload')=='true') {
							document.location = document.location;
							return true;
						} else {
							input.css('background-image','none');
						}
					} else {
						alert('error');
					}
					return true;
				},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
					alert('error');
				}

			});
		}
	}
	$.fn.ajaxChanger = $.ajaxChanger.build;
})(jQuery);
/*
 * numberValue
 * Cypyright (c) 2010 Stanislav Lacko, Space Systems, s.r.o., www.spacesystems.sk
 */
(function($) {
	$.numberValue= {
		author:"Stano Lacko, Space Systems, s.r.o., www.spacesystems.sk",
		version:"0.1",
		build: function(options) {
			return this.each (function (){
				var right = this.className.match(/right/i);
				if (!right) {
					$(this).addClass('right');
				}
				if (this.className.match(/integer/i)) {
					$.numberValue.changeInteger($(this));
					$(this).bind('change',function(){
						$.numberValue.changeInteger($(this));
					});
				}
				if (this.className.match(/monetary/i)) {
					$.numberValue.changeMonetary($(this));
					$(this).bind('change',function(){
						$.numberValue.changeMonetary($(this));
					});
				}
			});
		},
		changeInteger: function(object) {
			$.numberValue.checkMinMax(object);
			if (object.val().length>0) {
				object.val(object.val().toInteger());
			}
		},
		changeMonetary: function(object) {
			$.numberValue.checkMinMax(object);
			object.val(object.val().toMoney());
			var value = object.val()*1;
			if (value<0) {
				$(this).addClass('monetary_negative');
			} else {
				$(this).removeClass('monetary_negative');
			}
		},
		checkMinMax: function(object) {
			if (object.val().length>0) {
				var value = object.val().toFloat()*1;
				var min = object.attr('spc:min');
				var max = object.attr('spc:max');
				if (min && value<min) {
					value = min;
				}
				if (max && value>max) {
					value = max;
				}
				object.val(value.toString().toFloat());
			}
		}
	}
	$.fn.numberValue = $.numberValue.build;
})(jQuery);
/*
 * modalFrame
 * Cypyright (c) 2010 Stanislav Lacko, Space Systems, s.r.o., www.spacesystems.sk
 */
(function($) {
	$.modalFrame= {
		author:"Stano Lacko, Space Systems, s.r.o., www.spacesystems.sk",
		version:"0.1",
		defaults:{
			alert: 'Are you sure to close?'
		},
		counter: 1,
		modalFrames: new Array(),
		showed_modal: false,
		initialized: false,
		destroyprocess: false,
		build: function(options){
			if (!$.modalFrame.initialized) {
				$.modalFrame.initialized=true;
				$(window).bind("resize", $.modalFrame.resizeModal);
			}
			return this.each (function (){
				this.options = {};
				$.extend (this.options, $.modalFrame.defaults);
				$.extend (this.options, options);
				var counter = $.modalFrame.counter;
				$.modalFrame.modalFrames[counter]=new Array(this.options.alert,this);
				$(this).click(function() { $.modalFrame.showModal(counter); });
				$(this).click($.modalFrame.resizeModal);
				$.modalFrame.counter++;
			});
		},
		resizeModal: function() {
			if ($.modalFrame.showed_modal) {
				var maskHeight = $(document).height();  
				var maskWidth = $(window).width();
				var to_show = $($.modalFrame.modalFrames[$.modalFrame.showed_modal][1]);
				var position = to_show.position();
				$('#modalFrameWindow_1').css({ top: 0, left: 0, position: 'absolute', 'z-index': 3000,
					width: maskWidth, height: position.top });
				var bottom_top = position.top+to_show.height();
				$('#modalFrameWindow_2').css({ top: bottom_top.toString()+'px', left: 0, position: 'absolute', 'z-index': 3000,
					width: maskWidth, height: (maskHeight-bottom_top).toString()+'px' });
				$('#modalFrameWindow_3').css({ top: position.top.toString()+'px', left: 0, position: 'absolute', 'z-index': 3000,
					width: position.left.toString()+'px', height: to_show.height().toString()+'px' });
				var right_left = position.left+to_show.width();
				$('#modalFrameWindow_4').css({ top: position.top.toString()+'px', left: right_left.toString()+'px', position: 'absolute', 'z-index': 3000,
					width: (maskWidth-right_left).toString()+'px', height: to_show.height().toString()+'px' });
				var close_width = 10;
				var close_height = 10;
				if (position.close_height<10) {
					var close_top = bottom_top;
				} else {
					var close_top = position.top-close_height;
				}
				$('#modalFrameWindow_5').css({ top: close_top.toString()+'px', left: (right_left-close_width).toString()+'px', position: 'absolute', 'z-index': 3001,
					width: close_width.toString()+'px', height: close_height.toString()+'px' });
			}
		},
		showModal: function(counter) {
			if (!$.modalFrame.showed_modal) {
				$.modalFrame.showed_modal=counter;
				$('body').after('<div id="modalFrameWindow_1" style="background-color: #000000; display: none"></div>');
				$('body').after('<div id="modalFrameWindow_2" style="background-color: #000000; display: none"></div>');
				$('body').after('<div id="modalFrameWindow_3" style="background-color: #000000; display: none"></div>');
				$('body').after('<div id="modalFrameWindow_4" style="background-color: #000000; display: none"></div>');
				$('body').after('<div id="modalFrameWindow_5" style="background-color: red; display: none"></div>');
				//Set height and width to mask to fill up the whole screen
				$('#modalFrameWindow_5').html('<a href="javascript:$.modalFrame.destroy('+counter+')">x</a>');
				$.modalFrame.resizeModal();
				$('#modalFrameWindow_1, #modalFrameWindow_2, #modalFrameWindow_3, #modalFrameWindow_4, #modalFrameWindow_5').fadeIn(1000);      
				$('#modalFrameWindow_1, #modalFrameWindow_2, #modalFrameWindow_3, #modalFrameWindow_4, #modalFrameWindow_5').fadeTo("slow",0.8);
			}
		},
		destroy: function(counter) {
			if ($.modalFrame.showed_modal && !$.modalFrame.destroyprocess) {
				$.modalFrame.destroyprocess=true;
				if (window.confirm($.modalFrame.modalFrames[counter][0])) {
					$('#modalFrameWindow_1, #modalFrameWindow_2, #modalFrameWindow_3, #modalFrameWindow_4, #modalFrameWindow_5').remove();      
					$.modalFrame.showed_modal=false;
		//			$('#modalFrameWindow_1, #modalFrameWindow_2, #modalFrameWindow_3, #modalFrameWindow_4, #modalFrameWindow_5').fadeTo("fast",1);
		//			$('#modalFrameWindow_1, #modalFrameWindow_2, #modalFrameWindow_3, #modalFrameWindow_4, #modalFrameWindow_5').fadeTo("slow",0);
				}
				$.modalFrame.destroyprocess=false;
			}
		}
	}
	$.fn.modalFrame = $.modalFrame.build;
})(jQuery);
function disableEnterKey(e)
{
     var key;
     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox
     if(key == 13)
          return false;
     else
          return true;
}
$(function(){
	$(".integer,.monetary").numberValue(); // Number a monetary values
	$(document).pngFix({ suffix: '-trans.png' }); // Transparentne png v MS IE 6
/*	$(".disable_enter_submit").bind('keydown',function(e) { // Zrusenie submit pri enter pre Chrome/Webkit
		var key = e.which ? e.which : e.keyCode;
//		if (e.currentTarget typeof HTMLForm)
		alert(e.currentTarget);
		if (key == 13) {
			return false;
		} else {
			return e;
		}
	});
	$(".disable_enter_submit").find("textarea").each(function() {
		$(this).bind('keydown',function(e) { // Zrusenie submit pri enter pre Chrome/Webkit
			var key = e.which ? e.which : e.keyCode;
	//		if (e.currentTarget typeof HTMLForm)
			alert(e.currentTarget+'::'+key+'::'+e.preventDefault);
			if (key == 13) {
				e.preventDefault();
			} else {
				return e;
			}
		});
	}); */
});
