/*

	HR and Training Solutions
	http://www.hrandtrainingsolutions.co.uk/
	
	Systemagic
	http://www.systemagic.co.uk/

*/

$(document).bind("ready", function(){
								   
	$(window).overlay();
	
	$('#sysframe', top.document).css({ "height" : $('.iframe').height() });
	
	if($('.dialog-fill', top.document).length > 0){
		$('.dialog #sysframe', top.document).css({ "width" :  parseFloat($('.browser-header').width())+20 });
	}else{
		w = parseFloat($('.iframe').width());
		$('.dialog #sysframe', top.document).css({ "width" : w+15 });
	}

	window.parent.document.move();
});

document.move = function(){
	$(window).move();
}

document.overlay = function(){
	$(window).overlay();
	return this;
}

document.dialog = function(){
	$(window).dialog();
	return this;
}
	  
jQuery.fn.extend({
	modal: function(){
		if($(this).start()){
			$('.dialog-modal').fadeOut(0);
		}else{
			if($('.dialog-modal').css("display") == "block"){
				$('.dialog-modal').fadeOut(300);
			}else{
				$('.dialog-modal').css({ "display":"block", "opacity":0 }).fadeTo(300,0.7);
			}
		}
	},
				 
	overlay: function(){
					
		if($(this).start()){
			
			$(window).bind("resize scroll", function(){
				$(window).move()
			}).resize();

			
			$(window).move(function(){
				if($('.dialog').css("display") != "none"){
					left = (parseFloat($(window).width())*0.5)-($('.dialog').width()/2)+'px';
					t	 = $(window).scrollTop();
					t	+= 50;
					topp = t+'px';
					
					$('.dialog').css({ 'left':left, 'top':topp });
				}
			});
		}else{
			if($('.overlay').css("display") == "block"){
				$('.overlay').fadeOut(100);
			}else{
				t	 = $(window).scrollTop();
				topp = (parseFloat($(document).height()))+'px';
				$('.overlay').css({ "display":"block", "opacity":0, "top":"0px", "height":topp }).fadeTo(100,0.7, function(){ $(window).move(); });
			}
		}
		
		return this;
	},
	
	dialog: function(){
		if($('.dialog').css("display") == "block")
				$('.dialog').fadeOut(100);
			else
				$('.dialog').fadeIn(100);
		
		return this;
	},
	
	move: function(callback){
		if(callback)
			$(this).bind("move", callback);
		else
			$(this).trigger("move");
		return this;
	},
	
	start: function(){
		if($(this).attr("magicinit") != "true"){
			$(this).attr("magicinit", "true");
			return true;
		}else{
			return false;
		}
	},
	
	terminate: function(){
		// May need work, or not needed?
		if($(this).attr("init") != "false"){
			$(this).attr("init", "false");
			return false;
		}else{
			return true;
		}
	},

	magic: function(){
		return $(this).attr("magic");
	},
	
	related: function(selector){
		if(selector)
			return $("*[magic="+$(this).magic()+"]").filter(selector);
		else
			return $("*[magic="+$(this).magic()+"]");
	}
	
});



$.callback = 0;

jQuery.fn.extend({

	ask: function(options, buttons){
		$(this.selector).die("click");
		
		selector = this.selector;

		$(this.selector).live("click", function(){

			if($('.overlay').css("display") == "none") $(window).overlay().dialog();
			
			options.title = (options.title == undefined) ? $(this).html() : (options.title ? options.title : '');
			
			options.message = (options.message == undefined) ? "" : options.message;
			
			if(options.editable != undefined){
				options.message = "<textarea id=\"content\">" + $(this).html() + "</textarea>";
			}
			
			$('.dialog h1').html(options.title);
			
			$('.dialog p').html(options.message);
			
			if(options.editable != undefined){
				$("#content").makeEditable();
			}
			
			b = '';
			ba = [];
			
			for(button in buttons){
				attributes = "";
				role = button.toLowerCase().replace(/ /g, "-");
				ba[role] = buttons[button];
				attributes += (options[role] == 'hidden') ? " style=\"display:none;\"" : "";

				b += "<button role=\"dialog-"+role+"\""+attributes+">"+button+"</button>\n";
			}
			
			$('.dialog .buttons').html(b);
			
			// Message events are bound to #handler, prevents clashing with other live/bound events
			if($.message){
				elem = $.message;
			}else{
				elem = this;
			}
			
			for(button in ba){
				$('button[role=dialog-'+button+']').bind("click", {'me':elem, role:button, callback:ba[button]}, function(event){
					e = "ask"+event.data.role;
					if(typeof event.data.callback == 'function'){
						d = '';
						if($(this).attr("options")){
							d = $(this).attr("options");
							d = JSON.parse(d);
							$(this).removeAttr("options");
						}
						
						if($(event.data.me).bind(e, d, event.data.callback).triggerHandler(e)){
							$(window).overlay().dialog();
						}
						
						$(event.data.me).unbind(e);
						
					}else if(event.data.callback.indexOf("/") > 0){
						
						$(window).link(event.data.callback);
						
					}else if(event.data.callback == false || event.data.callback == 'cancel'){
						
						$(window).overlay().dialog();
						
					}
					
					event.preventDefault();
					event.stopPropagation();
					return false;
				});
			}
			
			$('.dialog button:first').focus();

			$('.dialog button').bind("keydown", function(event){
				if(event.keyCode == 37){
					$(this).prev().focus();
				}else if(event.keyCode == 39){
					$(this).next().focus();
				}
			});
			
			$('.dialog button:last').bind("keydown", function(event){
				if(event.keyCode == 9){
					$('.dialog button:first').focus();
					return false;
				}else if(!event.keyCode){
					$('.dialog button:first').focus();
				}
			});
			
			//$("body").animate({ 'scrollTop':0 }, 1800);
			$(window).resize();
			
			$(window).one("move", function(){
				$('.dialog .buttons').fadeIn(0);
			});
				
			return false;
		});
		return this;
	},
	
	message: function(options, buttons){
		$.message = this;
	
		$("#handler").ask(options, buttons).click();
		
		delete $.message;
	},
	
	browser: function(options, buttons){
		options.title = false;
		
		options.browser = true;
		options.iframe = '/admin/editor/browser/';
		prefix = "?";
		
		if(options.folder != "" && options.folder != null){
			if(options.folder.charAt(0) == "/") options.folder = options.folder.substr(1, options.folder.length);
			options.iframe += "?folder="+options.folder;
			prefix = "&";
		}
		
		if(options.type != "" && options.type != null){
			options.iframe += prefix+"type="+options.type;
		}
		
		options['refresh'] = 'hidden';
		options['confirm'] = 'hidden'
		
		buttons['Refresh'] = 'refresh';
		if(options.upload != false){
			buttons['Upload'] = 'upload';
		}
		if(options['new-folder'] != false){
			buttons['New Folder'] = 'new-folder';
		}
		buttons['Cancel'] = 'cancel';
		
		$(this).ask(options, buttons);
	},
	
	editable: function(buttons){
		options = {};
		options.title = false;
		options.editable = true;
		
		$(this).ask(options, buttons);
	}
	
});

