// JavaScript Document

(function($){
// THE MODAL DIALOG PLUGIN
		/*********************************************************
			STRUCTURE when predefined using javscript:
			
			$(element).detDialog({
						html:data, 
						url:href, 
						callback:function(){}, 
						width:300
					});
			
			for standalone:
			$.detDialog({
						html:data, 
						url:href, 
						callback:function(){}, 
						width:300
					});
		*********************************************************/
	
	var effectSpeed = 250;
	var DATA = null;
	var default_option = { url: null, data: null, html: null, callback: null , width: null };
	var modal_options = {};
	var modalCss = { width: '' };
	
	$.fn.detDialog = function(option){
				var option = $.extend({},default_option,option);	
				
				if($(this).length <= 0) return this;
				
				// initialize modal by creating the DIVs
				overlay.create();
				dialog.create();
				
				// for each links that launches the lightbox, attach the click event
				return $(this).each(function(){
						
						var tag = $.trim(this.tagName.toString().toLowerCase());
						switch(tag){
							case 'a': case 'img':
										var bindEvent = 'click';
										var url = this.href || $(this).attr('href') || this.src || $(this).attr('src');
										var req_type = 'GET';
										break;
							case 'form':
										var bindEvent = 'submit';
										var url = this.action || $(this).attr('action');
										var req_type = 'POST';
										break;
						} // end switch
						
						$(this).unbind(bindEvent).bind(bindEvent,function(event){
								var tag = $.trim(this.tagName.toString().toLowerCase());
								switch(tag){
									case 'a': case 'img':
												var bindEvent = 'click';
												var url = this.href || $(this).attr('href') || this.src || $(this).attr('src');
												var req_type = 'GET';
												break;
									case 'form':
												var bindEvent = 'submit';
												var url = this.action || $(this).attr('action');
												var req_type = 'POST';
												break;
								} // end switch
						
								// load the url of the element's href or action URL
								option.url = url;
								
								var meta = $(this).getDialogmeta();
								option.width = meta ? meta.width : null;
								
								// display the modal dialog box using the standalone function
								$.detDialog(option);
								
								// prevent the default event for this element
								event.preventDefault(); event.stopPropagation();
								return false;
							});
					});
			}; // end function
	
	$.detDialog = function(option){
		// standalone function used to be called to launch the lightbox from within another function
				var option = $.extend({},default_option,option);
				
				// initialize modal by creating the DIVs
				overlay.create();
				dialog.create();
				
				if(option.width) dialog.dialogCSS.width = option.width;
				else dialog.dialogCSS.width = '';
				
				// if the user defined a HTML to be displayed, display this instead
				if(option.html){
					// $.loadDialogByHTML(option.html);
					dialog.loadHTML(option.html).show().bindevents().loadcallback(option.callback);
					overlay.show().bindclose();
					return false;
				} // end if
				
				// if the user defined a URL, use AJAX and display the result
				if(option.url){
					$.ajax({
							url: option.url,
							type:"GET",
							dataType: 'html',
							success:function(responseText,statusText){
								$.loadDialogByHTML(responseText,option.callback);
							}
						  });
				} // end if
				return false;
			}; // end function
	
	/* create the overlay DIV */
	var overlay = {
					overlayID: "overlay",
					overlay_selector: null,
					create: function(){
						this.overlay_selector = $("#"+this.overlayID);
						
						// if the overlay already exist, use the existing, do not create a new one
						if( this.overlay_selector.length > 0) return this; 
						
						// if not existent, create a new overlay
						$('<div id="'+this.overlayID+'"></div>').css({"z-index":99999,"display":"none"}).appendTo("body");
						return this;
					},
					hide: function(){
						this.overlay_selector.fadeOut(effectSpeed); return this; 
					},
					show: function(){
						var $this = this.overlay_selector;
						var filter = $this.css('filter');
						
						
						
						
						
						/*added 2008-09-09*/
						var win_width = $(document).width();
						var win_height = $(document).height();
						this.overlay_selector.css({height:win_height,width:win_width});
						/******************/
						
						
						
						
						this.overlay_selector.fadeIn(effectSpeed, function(){ $this.css({'filter':filter}); }); return this; 
					},
					bindclose: function(){
						this.overlay_selector.bind('click',function(event){
								//overlay.hide();
								// dialog.hide();
								event.preventDefault(); event.stopPropagation(); return false;
							});
						return this;
					},
					test: function(){ alert('overlay is available'); }
				}; // end overlay class
	
	$.createOverlay = function(){ overlay.create(); }; // end function
			
	$.hideOverlay = function(){ overlay.hide(); }; // end function
			
	$.showOverlay = function(){ overlay.show(); }; // end function
	
	/* create the modal dialog DIV */
	var dialog = {
					dialogPageContainerID: 'lightboxPageContainer',
					dialogPage_selector: null,
					dialogID: 'lightboxContainer_O',
					dialog_selector: null,
					dialog_content_ID: 'lightboxContainer_I',
					create: function(){
						this.dialog_selector = $("#"+this.dialogID);
						this.dialogPage_selector = $("#"+this.dialogPageContainerID);
						
						if(this.dialog_selector.length > 0){ return this; }			
						$('<div id="'+this.dialogPageContainerID+'"><div id="'+this.dialogID+'"><div id="'+this.dialog_content_ID+'"></div></div></div>')
							.css({"z-index":999999,"display":"none"}).appendTo("body");
						return this; 
					},
					hide: function(){ 
						this.dialogPage_selector.add(this.dialog_selector).fadeOut(effectSpeed); 
						
						
						
						/*added 2008-09-09*/
						$("#"+this.dialog_content_ID).empty();
						this.dialogPage_selector.css({zIndex:99999});
						/******************/
						
						
						return this;
					},
					dialogCSS: {width:''},
					show: function(){
						this.dialog_selector.css(this.dialogCSS).positionTop();
						this.dialogPage_selector.add(this.dialog_selector).fadeIn(effectSpeed);
						return this; 
					},
					loadHTML: function(html){
						$("#"+this.dialog_content_ID).html(html);
						return this;
					},
					loadURL: function(url){
						var dialog = this;
						$.ajax({
									url:url,
									type:'GET',
									success:function(responseText){
										dialog.loadHTML(responseText).show().bindevents();
										if(dialog.callback) dialog.callback.call(this);
										overlay.show();
									}
							  });
						return this;
					},
					callback: null,
					loadcallback: function(callback){
						if(callback && $.isFunction(callback)){
							callback.call(this);
							this.callback = callback;
						} // end if
						return this;
					},
					closebutton: ".lbClose, .lbAction[@rel='deactivate']",
					bindclose: function(){
						var dialog = this;
						$(this.closebutton).bind("submit click",function(event){
								dialog.hide();
								overlay.hide();
								event.preventDefault(); event.stopPropagation(); return false;
							});
						return this;
					},
					bindevents: function(callback){
						this.bindclose();
						return this;
					},
					test: function(){ alert('dialog is available'); }
				}; // end dialog class
	
	
	
	var modalID = 'lightboxContainer_O';
	var modalContentID = 'lightboxContainer_I';
	var modalDIV = '<div id="lightboxPageContainer"><div id="'+modalID+'"><div id="'+modalContentID+'"></div></div></div>';
	
	$.createDialog = function(options){ dialog.create(); }; // end function
		
	$.showDialog = function(){ dialog.show(); }; // end function
		
	$.hideDialog = function(options){ dialog.hide(); }; // end function

	$.loadDialogByHTML = function(){
		// loads the defined HTML data onto the modal dialog content container
				var callback = null;
				for(var x = 0; x < arguments.length; x++){
					if(typeof arguments[x] == 'function'|| $.isFunction(arguments[x])) callback = arguments[x];
					if(typeof arguments[x] == 'string') html = arguments[x];
				} // end for
				dialog.loadHTML(html).loadcallback(callback).show().bindevents();
				overlay.show().bindclose();
				return this;
			}; // end function
	
	$.fn.bindClose = function(){ dialog.bindclose(); }; // end function
			
	$.closeDialog = function(){ dialog.hide(); overlay.hide(); }; // end function
	
	$.fn.positionTop = function(top){
		// positions the DIALOG box from the top of the document
			
				if(top == 'undefined' || top == null){ var ps = $.getScrollPosition(); var top = ps[1] + 100; } // end if
				this.css({top:top});
				return this;
			}; // end function
			
	$.fn.positionLeft = function(left){
		// positions the DIALOG from the left of the document
			
				if(left == 'undefined' || left == null){ 
					var ps = $.getPageSize();
					var ps_center = (ps[0]/2) - ($(this).width()/2);
					
					var pos = $.getScrollPosition();
					var left =  pos[0];// + ps_center;
				} // end if
				this.css({left:left});
				return this;
			}; // end function
			
	$.fn.centerVertical = function(){
		// positions the DIALOG on the center of the document
			
				var ps = $.getScrollPosition();
				var top = ps[1] + 100;
				$(this).css({top:top});
				return this;
			}; // end function
	
	$.getPageSize = function(){
		// get the size of the DOCUMENT / PAGE
		
				var de = document.documentElement;
				var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
				var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
				arrayPageSize = [w,h];
				return arrayPageSize;
			}; // end function
			
	$.getScrollPosition = function(){
		// get the SCROLL position
		
				var y = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
				var x = window.pageXOffset || document.body.scrollLeft || document.documentElement.scrollLeft;
				arrayPos = [x,y];
				return arrayPos;
			}; // end function
			
	$.fn.getDialogmeta = function(){
		// get the meta from the link to display the DIALOG lightbox
		
				var modalclass = $(this).attr('class');
				var regex = new RegExp("modal:({.+})");
				if(regex.test(modalclass) != false){ return modalsetting = eval("modalsetting = " + regex.exec(modalclass)[1]); }
				else{ return null; } // end if
			}; // end function
})(jQuery);

hideLightBox = function(){
     $.hideOverlay();
     $.hideDialog();     
}
