//define the weblist namespace
Weblist = {
	Pages : 
	{
		Index	: null,
		List	: null,
		Edit	: null
	},
	ListViews: {},
	
	Forms: 
	{
		Login:
		{
			init: function()
			{
				this.clear();
				this.messageBoxCSS = {
						"position"  : "relative",
						"top"		: "-95px",
						"left"      : "0px",
						"width"		: "288px"
				};
				
				//bind the events
				$("input#username").bind("invalid_field",null,function(){
					//create a new message box
					Weblist.Forms.Utils.createMessageBox("Invalid username",Weblist.Forms.Login.messageBoxCSS, "form#form_login");
				});
				
				$("input#password").bind("invalid_field",null,function(){
					Weblist.Forms.Utils.createMessageBox("Invalid username",Weblist.Forms.Login.messageBoxCSS, "form#form_login");
				});
			},
			
			validate: function()
			{
				if($("input#username").val().length == 0)
				{
					$("input#username").trigger("invalid_field");
					return false;
				}
				if($("input#password").val().length == 0)
				{
					$("input#password").trigger("invalid_field");
					return false;
				}
				
				return true;
			},
			
			clear: function()
			{
				//clear the form
				$("input#username").val("");
				$("input#password").val("");
			},
			
			submit: function()
			{
				//perform a validation first
				if(this.validate())
				{
					$("form#form_login").submit();
				}
			}
		},
		
		/* UTILS CLASS */
		Utils: {
			
			createMessageBox : function(msg, css, container)
			{
				var messageBox;
				
				if($("#dialog_messagebox").length > 0)
				{
					messageBox = $("#dialog_messagebox");
					messageBox.hide();
				}
				else
					messageBox = $("<div class='dialog_messagebox' id='dialog_messagebox'></div>");
				
				messageBox.css(css);
				$(container).append(messageBox);
				
				messageBox.html(msg);
				messageBox.fadeIn(200);
				
				clearTimeout(this.messageTimeOut);
				this.messageTimeOut = setTimeout(function(){
					messageBox.fadeOut(200);
				},4000);
			}
		}
	},
	
	Dialog: {
		
		Options: 
		{
			Login:
			{
				autoOpen: true,
				height: 300,
				width: 400,
				modal: true,
				
				buttons: {
					'Login': function() 
					{ 
						Weblist.Forms.Login.submit();
					},
					'Forgot' : function() { 
						window.location = weblistRootPath + 'forgot';
					}
				},
				
				open: function(){
					Weblist.Forms.Login.init();
				}
			},
			
			Message:
			{
				autoOpen: true,
				height: 270,
				width :  370,
				modal : true, 
				
				buttons: 
				{
					'Ok': function() 
					{ 
						Weblist.Dialog.close();
					}
				},
				
				title : "Weblist error"
			}
			
		},
		
		open: function(options,content,footer)
		{
			if(!options) return;
			if(content) content = $("#"+content);
			if(footer) footer = $("#" + footer);
			
			if(options.beforeOpen)
				options.beforeOpen();
			options.position = 'center';
			
			//set the properties
			this.setProperties(options, content);
	
			//set the content
			this.setContent(content, footer);
			
			//hide the message box; 
			$("#dialog_messagebox").hide();
			
			$("#weblist_dialog").dialog(options);
		},
		
		close: function()
		{
			$(".ui-dialog-titlebar-close").trigger("click");
		},
		
		//this handler is very important to every dialog because it defines the width and height
		setProperties: function(options, content)
		{
			$("#weblist_dialog").attr('style', 'width: ' + options.width + 'px !important');
			
			if($("span#ui-dialog-title-weblist_dialog").length > 0)
				$("span#ui-dialog-title-weblist_dialog").html(content.attr("title"));
			else
				$("#weblist_dialog").attr("title", content.attr("title"));
			
			$("#weblist_dialog").children("#top_middle").attr("style" ,"width: " + (options.width - 64) + "px !important");
			$("#weblist_dialog").children("#content_middle").attr("style","width: " + (options.width - 64) + "px !important");
			$("#weblist_dialog").children("#bottom_middle").attr("style","width: " + (options.width - 64) + "px !important");
		
			$("#weblist_dialog").children("#content_left").css("height",(options.height - 215) + "px");
			$("#weblist_dialog").children("#content_middle").css("height",(options.height - 215) + "px");
			$("#weblist_dialog").children("#content_right").css("height",(options.height - 215) + "px");
			
		},
			
		setContent: function(content, footer)
		{
			var old_content = $("#weblist_dialog").children("#content_middle").children();
			old_content.css("display" , "none");
			$("body").append(old_content);
			
			var container = $("#weblist_dialog").children("#content_middle");
			container.html("");
			container.append(content);
			content.css("display","block");
			
			//old footer content
			var old_footer = $("#weblist_dialog").children("#bottom_middle").children();
			old_footer.css("display" , "none");
			$("body").append(old_footer);
			
			$("#weblist_dialog").children("#bottom_middle").html("");
			
			
			if(footer)
			{
				$("#weblist_dialog").children("#bottom_middle").append(footer);
				footer.css("display","block");
			}
		},
		
		showPreloader: function(msg,css)
		{
			//hide the content
			$("#weblist_dialog").children("#content_middle").children().hide();
			
			//place the preloader
			var preloader = ($("div#dialog_preloader").length > 0) ? $("div#dialog_preloader") : $("<div class='dialog_preloader' id='dialog_preloader'><span>"+msg+"</span><br/><img src='"+weblistRootPath+"public/images/index/ajax.gif'/></div>"); 
			preloader.children("span").html(msg);
			
			if(css)
				preloader.css(css);
			
			$("#weblist_dialog").children("#content_middle").append(preloader);
			
			preloader.show();
			
			//block page buttons
			$("button").attr("disabled", "disabled");
			
			//hide the message boxes
			$("div#dialog_messagebox").hide();
		},
		
		hidePreloader: function()
		{
			$("#dialog_preloader").remove();
			$("#weblist_dialog").children("#content_middle").children().show();
		
			//unblock the buttons
			
			//hide the messageboxes
			$("div#dialog_messagebox").hide();
			
			//unblock page buttons
			$("button").removeAttr("disabled");
		},
		
		showMessage: function(msg)
		{
			this.Options.Message.beforeOpen = function()
			{
				$('span#weblist_message_text').html(msg);
			};
			
			this.open(Weblist.Dialog.Options.Message, "dialog_message");
		}
	},
	
	Tooltip: function(options)
	{
		//this function creats a tooltip and is registering the handlers for it
		//options.parent - the element for which the tooltip is created
		//options.follow - this defines where the element would be displayed (centered, top-right, top-left ...)
		//options.title  - the title of the tooltip
		//options.content - the text that is displayed as the content
		
		if(!options || !options.parent)
		{
			//hide the tooltip
			$('div#weblist_tooltip').hide();
			return;
		}
		
		//get the tooltip element
		var tooltip = $('div#weblist_tooltip');
		//hide it
		tooltip.hide();
		//reset the tooltip content
		tooltip.children().html();
		
		//set the new position of the tooltip
		var parent = $(options.parent);
		var position = {left:0,top:0};
		
		switch(options.follow)
		{
			case "centered":
				position.left = Math.ceil(parent.position().left + parent.width() /2);
				position.top = Math.ceil(parent.position().top + parent.height()/2 );
			break;
			case "top-centered":
				position.left = Math.ceil(parent.position().left + parent.width() /2);
				position.top = Math.ceil(parent.position().top - tooltip.height() + 15);
			break;
			default:
				//left top corner
				position.left = Math.ceil(parent.position().left);
				position.top = Math.ceil(parent.position().top);
		}
		
		//update the position and insert the content
		tooltip.css(position);
		tooltip.children().html("<b>" + options.title + "</b>" + "<br/><span>" + options.content + "</span>");
		tooltip.css("opacity",1);
		//show the tooltip
		tooltip.fadeIn(100);
		
	},
	//this is the logic used for the frames from the homepage
	Frames: { },
	
	UploadService: 
	{
		//this object handles the upload through the upload iframe 
		//on a page there is only one iframe for uploading files
		//in this way we avoid collisions between dialogs ...
		//there is only one handler registered for onload event at a time
		clean:function()
		{
			$("iframe#dialog_upload_iframe").attr("src","");
			$("iframe#dialog_upload_iframe").unbind("load");
		},
		
		bindLoadEvent: function(handler)
		{
			this.clean();
			
			
			//bind the event for loading 
			$("iframe#dialog_upload_iframe").bind("load", null, function()
			{
				var response = Weblist.UploadService.getResponse();
				if(response != null)
				{
					handler(response);
				}
			});
		},
		
		getResponse: function()
		{
			iframe = $("iframe#dialog_upload_iframe");
			
			var iframewindow= (iframe[0].contentWindow != undefined) ? iframe[0].contentWindow : iframe[0].contentDocument.defaultView;
			var response = iframewindow.document.body.innerHTML; 
			return (response.length > 0 ) ? eval("(" + response + ")") : null;
		}
	},
	
	Captcha: 
	{
		getCaptcha : function(fnCallBack)
		{
			if(fnCallBack == undefined)
				return;
			
			jQuery.ajax({
				  url: weblistRootPath + 'ajax/captcha',
				  dataType: "json",
				  method: "post",
				  success: fnCallBack
			});
			
		}
	},
	
	Utils:
	{
		ItemTypeEnum:
		{
			Webpage : 1,
			Image	: 2,
			Document: 3,
			File	: 4,
			Video	: 5,
			Text	: 6
		},
		
		resolveUrl:function(url)
		{
			if(url.match(/^http|ftp/) == null)
				url = "http://" + url;
			
			//if the url is does not have an http in front just add one
			return url;
		}
	} 
};

