/* /js/dialog.js */

$(function(){
	$.dialog.bind();
});

(function($){
	$.dialog = function(obj){
		$.dialog.init();
		$.dialog.fetch($(obj).attr('href'));
		return false;
	}

	$.extend($.dialog, {
		settings: {
			error: '<h1>Error Occurred</h1><p class="error">An error occurred while communicating with the server.</p><div class="buttons"><ul><li><a href="javascript:$.dialog.close();" title="Close" class="button primary"><span>Close</span></a></li></ul><div class="clear"></div></div>',
			upload: 'Uploading...'
		},

		init: function(){
			if($('#dialog').length!=1){
				$('body').append('<div id="dialog"><div class="content"></div></div>');
			}
			if($('#screen').length!=1){
				$('body').append('<div id="screen"></div>');
				$('#screen').click($.dialog.close);
			}
		},
		
		bind: function(){
			$('a[rel*=dialog]').bind('click',function(){
			 	$.dialog(this);
			 	return false;
			});
			
			$(window).bind('resize',$.dialog.screen);
		},
		
		fetch: function(url){
			if(url.substr(0,1)=='#'){
				$('#dialog .content').html($(url).html());
			}else{
				$.ajax({
					'type': 'get',
					'url': url,
					'error': function(data,textStatus,errorThrown){
							$.dialog.error(data,textStatus,errorThrown);
						},
					'success': function(data,textStatus,errorThrown){
							var dataT = trim(data);
							if(dataT==""||dataT.substr(0,9)=='<!DOCTYPE'){
								$.dialog.error(data,textStatus,errorThrown);
							}else{
								$('#dialog .content').html(data);
							}
							$.dialog.reveal(url);
						},
				});
			}
		},
		
		error: function(data,textStatus,errorThrown){
			$.dialog.init();
			if(textStatus!='null'){
				$('#dialog .content').html($.dialog.settings.error);
				$.dialog.reveal();
			}
		},
		
		reveal: function(url){
			$.dialog.screen();
			$('#screen').show().fadeTo(50,0.5);
			$('#dialog').css({top:-$('#dialog').height()}).animate({top:0},500);
			$.dialog.complete(url);
		},
		
		close: function(){
			$('#dialog').animate({top:-$('#dialog').height()},500);
			$('#screen').hide();
		},
		
		submit: function(){
			$('#dialog .errors').hide();
			var url = $('#dialog form').attr("action");
			
			if($('#dialog [rel*=wysiwyg]').length>=1){
				var oEditor;
				$('#dialog [rel*=wysiwyg]').each(function(){
					oEditor = FCKeditorAPI.GetInstance($(this).attr('name'));
					$(this).val(oEditor.GetXHTML());
				});
			}

			var formdata = $('#dialog form').serialize();
			$.post(url,formdata,function(data){
				if(data==''){
					ajaxFrame(_web+_refresh);
					$.dialog.close();
				}else{
					$('#dialog .errors').html(data).show();
				}
			});
		},
		
		screen: function(){
			$('#screen').css({
				width: $(window).width(),
				height: $(window).height()
			});
		},
		
		complete: function(url){
			$('#dialog .formapp :input').focus(function(){
				$('#dialog .formapp div').removeClass('focus');
				$(this).parents('div:has(.fieldElements)').addClass('focus');
			});
			
			$('#dialog textarea[rel*=wysiwyg]').each(function(){
				var oFCKeditor = new FCKeditor($(this).attr('name'));
				oFCKeditor.BasePath = _web+'/js/fckeditor/';
				oFCKeditor.Width = $(this).attr('width');
				oFCKeditor.Height = $(this).attr('height');
				oFCKeditor.ReplaceTextarea();
			});
			
			$('#dialog textarea[rel*=tokens]').each(function(){
				var i = get('id',url);
				var n = get('name',url);
				var tokens = (i!=''&&n!='') ? [{id:i,name:n}] : [];
				$(this).tokenInput(_web+'/admin/users/tokens/',{
					classes: {
						tokenList: "token-input-list-facebook",
						token: "token-input-token-facebook",
						tokenDelete: "token-input-delete-token-facebook",
						selectedToken: "token-input-selected-token-facebook",
						highlightedToken: "token-input-highlighted-token-facebook",
						dropdown: "token-input-dropdown-facebook",
						dropdownItem: "token-input-dropdown-item-facebook",
						dropdownItem2: "token-input-dropdown-item2-facebook",
						selectedDropdownItem: "token-input-selected-dropdown-item-facebook",
						inputToken: "token-input-input-token-facebook"
					},hintText: "Type a name or email address",
					prePopulate: tokens
				});
			});

			$('#dialog form input,#dialog form select').bind('keypress',function(e){
				if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
					return false;
				}else{
					return true;
				}
			});
			
			scroll(0,0);
		},
		
		upload: function(){
			if($('#file_upload').length!=1){
				$('body').append('<iframe id="file_upload" name="file_upload" frameborder="0" width="0" height="0"></iframe>');
			}
			$('#dialog .uploading').html($.dialog.settings.upload).show();
			$('#dialog form').submit();
		},
		
		uploadComplete: function(data){
			if(data.status=="success"){
				ajaxFrame(_web+_refresh);
				$.dialog.close();
			}else{
				$('#dialog .uploading').hide();
				$('#dialog .errors').html(data.error).show();
			}
		}
	});
})(jQuery);