/**
 * @author piraja
 */

var Lapsi = {
	_init : function() {
		LapsiContextualHelper.init();
		
		jQuery('body').ready(function() {
			Lapsi._postLoadInit();
		});
		
		jQuery('body').append("<div id=\"dynamicDiv\"></div>");
		jQuery('#dynamicDiv').addClass("reveal-modal");
		
	},

	_postLoadInit : function() {
		Lapsi.initGridCommandColumn();
	},
	
	initGridCommandColumn: function()
	{
		jQuery("table tbody tr td").has("input[type='image']").css("text-align", "center");
		jQuery("table tbody tr td").has("input[type='image']").addClass("GridCommandColumn");
	},

	OpenPopup : function(p_url) {
		window.open(p_url, "JANELA", "");
	},

	ShowLoading : function(p_text) {
		if (!p_text)
			p_text = "&nbsp;";

		jQuery.blockUI( {
			applyPlatformOpacityRules : false,
			centerY : 0,
			css : {
				backgroundColor : 'transparent',
				border : '1px'
			},
			overlayCSS : {
				backgroundColor : '#000',
				opacity : 0.6
			},
			showOverlay : true,
			centerX : true,
			centerY : true,
			baseZ : 1000,
			message : '<div class="loading">' + p_text + '</div>',
			fadeIn : 20,
			fadeOut : 20
		});
	},

	HideLoading : function() {
		jQuery.unblockUI();
	},

	OpenDialog : function(elem, p_width, p_height) {
		Lapsi.HideLoading();

		jQuery(elem).dialog( {
			close : function(event, ui) {
				Lapsi.CloseDialog(elem);
			},
			modal : true,
			width : p_width,
			height : p_height,
			minHeight : 300,
			zIndex : 900,
			title : jQuery(elem + " > fieldset legend").text()
		}).show();
	},

	CloseDialog : function(elem) {
		jQuery(elem).dialog('destroy');
	},

	Alert : function(msg) {
		alert(msg);
	},

	Hide : function(elem) {
		jQuery(elem).hide();
	},

	Show : function(elem) {
		jQuery(elem).show();
	},
	
	Wait: function(message, elem)
	{
		jQuery('#' + elem.options.TextBoxID).addClass("hidden");
		jQuery('#' + elem.options.TextBoxID).attr("disabled","disabled");
		jQuery('#' + elem.options.ID).parent().addClass("AjaxWait");
	},
	
	Complete: function(elem)
	{
		jQuery('#' + elem.options.TextBoxID).removeAttr("disabled");
		jQuery('#' + elem.options.TextBoxID).removeClass("hidden");
		jQuery('#' + elem.options.ID).parent().removeClass("AjaxWait");
	},
	
	AutoScroll: function(elem)
	{
		jQuery('#' + elem).animate({ scrollTop: jQuery('#' + elem).attr("scrollHeight")	}, 500);

	},
	
	RevealAjax: function(url, options)
	{
		jQuery.get(url , options.params, function(data) {
			
			jQuery("#dynamicDiv").html(data);
			jQuery("#dynamicDiv").reveal({
			     animation: 'fadeAndPop',                   //fade, fadeAndPop, none
			     animationspeed: 200,                       //how fast animtions are
			     closeonbackgroundclick: true,              //if you click background will modal close?
			     dismissmodalclass: 'close-reveal-modal'    //the class of a button or element that will close an open modal
			});

		   });		
	},
	
	Reveal: function(controlId)
	{
		Lapsi.HideLoading();
		jQuery("#" + controlId).reveal({
		     animation: 'fadeAndPop',                   //fade, fadeAndPop, none
		     animationspeed: 100,                       //how fast animtions are
		     closeonbackgroundclick: true,              //if you click background will modal close?
		     dismissmodalclass: 'close-reveal-modal'    //the class of a button or element that will close an open modal
		});
	},
	
	ShowOverlay: function()
	{
		jQuery.blockUI({ message: null });
	},
	
	HideOverlay:  function()
	{
		jQuery.unblockUI();	
	},
	
	Center: function(obj)
	{
		obj.css({
			"top": (jQuery(window).height() / 2) - (obj.height() / 2),
			"left": (jQuery(window).width() / 2) - (obj.width() / 2)
		});
	},
	
	ExternalPopup: function(url)
	{
		var offset = 45;
		
		jQuery('body').parent().css({
			overflow: 'hidden'
		});
				
				
		var iframe = jQuery('<iframe/>')
			.attr('id','popup')
			.attr('src',url)
			.attr('border','0')
			.css({
				backgroundColor: '#fff',
				width: jQuery(window).width() - (offset * 2),
				height: jQuery(window).height() - (offset * 2),
				position: "absolute",
				padding: 0,
				margin: 0,
				left: offset,
				top: offset
			});
			
		var divPopup = jQuery('<div/>')
			.attr('id', 'divPopup')
			.css({
				backgroundColor: '#444',
				width: jQuery(window).width(),
				height: jQuery(window).height(),
				position: "absolute",
				padding: 0,
				margin: 0,
				left: 0,
				top: 0,
				zIndex: 100
			})
			.append(iframe)
			.bind('click', function() {
				jQuery('body').parent().css({
					overflow: 'auto'
				});
				
				jQuery(this).remove(); 
			})
			.appendTo( jQuery('body') );		
		
	},
	
	ToggleFullscreen: function(obj)
	{
		var size = 98;
		
		if (obj.data('fullscreen') == true)
		{
			obj.data('fullscreen', false);
			obj.width( obj.data('width') );
			obj.height( obj.data('height') );
			obj.css("poistion", obj.data('postion'));
			obj.css("top", obj.data('top') );
			obj.css("left", obj.data('left') );
			
			return false;
		}
		else
		{
			obj.data('fullscreen', true);
			obj.data('postion', obj.css("postion"));
			obj.data('width', obj.width()); obj.data('height', obj.height());
			obj.data('top', obj.css("top")); obj.data('left', obj.css("left"));
			
			var fullWidth = jQuery(window).width() * (size/100);
			var fullLeft =  (jQuery(window).width() - fullWidth)/2;

			var fullHeight = jQuery(window).height() * (size/100);
			var fullTop =  (jQuery(window).height() - fullHeight)/2;
			
			obj.width( fullWidth );
			obj.height( fullHeight );
			obj.css("poistion","fixed");
			obj.css("top", fullTop + "px" );
			obj.css("left", fullLeft + "px" );
			obj.css("left", fullLeft + "px" );
			
			return true;
		}
		
	}	
}

var LapsiContextualHelper = 
{
	init : function() 
	{
		var contentSelector = ".contextual-helper-content";
		var titleSelector = ".contextual-helper-title";

		jQuery(contentSelector).hide();

		jQuery(titleSelector).css("cursor", "help");

		//jQuery(contentSelector).css("z-index", "-1000");
		//jQuery(contentSelector).css("position", "absolute");

		jQuery(titleSelector).click(
				function() 
				{
					var checkElement = jQuery(this).prev();

					// If content is not already visible
					if ( (checkElement.is(contentSelector)) &&
						 (checkElement.is(':visible'))) 
					{
						checkElement.slideUp('normal');
						checkElement.parent().removeClass('selected');
					}

					if ( (checkElement.is(contentSelector)) &&
						 (!checkElement.is(':visible'))) 
					{
						jQuery(contentSelector + ':visible').slideUp('normal');
						jQuery(contentSelector + ':visible').parent().removeClass('selected');

						checkElement.slideDown('normal');
						checkElement.parent().addClass('selected');

						return false;
					}

				});
	}
}

jQuery(function() {
	Lapsi._init();
});

if ((Prado == undefined ) == false && (Prado.WebUI.CallbackControl == undefined ) == false)
{
	Prado.WebUI.CustomDropContainer = Class.extend(Prado.WebUI.CallbackControl);

	Object.extend(Prado.WebUI.CustomDropContainer.prototype, 
	{
		initialize : function(options) 
		{
			this.container = jQuery("#" + options.ID);
			this.options = options;
			
			var customOptions = Object.extend({
				onDrop : this.onDrop.bind(this)
			}, options);
			
			Droppables.add(options.ID, customOptions);
	
			Prado.Registry.set(options.ID, this);
	
			this.container.height(this.container.parent().parent().innerHeight() - 15);
			//jQuery("td ." + options.accept).height(this.container.parent().parent().innerHeight() - 13);
		},
		
		onDrop : function(dragElement, dropElement) 
		{
			var customOptions = jQuery.extend({}, this.options, {
				'CallbackParameter' : dragElement.id || '',
				'onSuccess' : this.onSuccess.bind(this),
				'onLoading' : this.onLoading.bind(this)
			});
			
			jQuery(dropElement).height('');
			
			request = new Prado.CallbackRequest(customOptions.EventTarget, customOptions);
			request.dispatch();
	
			if (this.options.onDrop)
				this.options.onDrop(dragElement, dropElement);
		},
		
		onLoading: function(sender, parameter)
		{
			if (this.options.dropclass)
				this.container.addClass(this.options.dropclass); 
			
			if (this.options.onLoading)
				this.options.onLoading(this.container, this.options);		
		},
		
		onComplete: function(sender, parameter)
		{
			if (this.options.onComplete)
				this.options.onComplete(this.container, this.options);		
		},
		
		onSuccess: function(sender, parameter)
		{
			if (this.options.dropclass)
				this.container.removeClass(this.options.dropclass); 
	
			if (this.options.onSuccess)
				this.options.onSuccess(this.container, this.options);			
		}	
	
	});
}
