$().ready(function() {
	$('a.dialog').live('click', function () {
    	if ($(this).attr('href')) {
	        _url = $(this).attr('href');
	        _nobackground = false;
	        if ($(this).hasClass('noPopBackground')) {
	            _nobackground = true;
	        }
	       
	        if ($(this).attr('target')) {
	        	_breadcrumb = '/breadcrumb/'+ $(this).attr('target');
	        }
	        
	        $('body').append('<div class="openerHelper"><div id="dialog-object"><//div></div>');
	        $el = $(this);
	        
	        _width = 600;
	        _height = 400;
	        _breadcrumb = '';
	        
	        if ($el.attr('rel')) {
	        	dims = $el.attr('rel').split('-');
	        	if ($.browser.mozilla) {
	        		_width = parseInt(dims[0]) + 50;
		        	_height = parseInt(dims[1]) + 60;
	        	}
			else if ($.browser.msie) {
				if ($.browser.version != '8.0') {
					_width = parseInt(dims[0]) + 50;
		        		_height = parseInt(dims[1]) + 110;
				}
				else {
	        			_width = parseInt(dims[0]) + 50;
		        		_height = parseInt(dims[1]) + 60;
				}
	        	}
	        	else {
	        		_width = parseInt(dims[0]) + 50;
		        	_height = parseInt(dims[1]) + 60;
	        	}
		    }

	        $.ajax({
	               type: "POST",
	               url: _url + _breadcrumb,
	               success: function (msg) {
	            	   $('.openerHelper #dialog-object').hide();
	            	   if (msg != '') {
	                       $('.openerHelper #dialog-object').html(msg);
	                       $('.openerHelper #dialog-object').fadeIn();
	                       $object = $('#dialog-object');
			       //call object
	                       $object.dialog(
	                       {
	                           resizable: false,
	                           modal: true,
	                           width: _width,
	                           height: _height,
	                           title : $el.attr('title'),
	                           beforeclose: function () {
	                        	   if ($('textarea.tinymce').length > 0) {
		                        	   $('textarea.tinymce').each( function () {
                                           if (tinyMCE.get($(this).attr('id'))) {
                                                tinyMCE.get($(this).attr('id')).remove();
                                           }
		                        	   });
	                        	   }
	                           },
	                           close: function() {
	                               $('.openerHelper').remove();
	                               $('#dialog-object').remove();
	                           }
	                       });
	
	                       if(_nobackground) {
	                           $object.prev().hide();
	                           $object.parent().css({background : 'none', border: 'none' });
	                       }
	                   }
	               },
	               complete: function () {
	            	   $().ready(function() {
	            		    if ($('textarea.tinymce').length > 0) {
		            			var _buttons1 = 'bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,|,outdent,indent,blockquote,|,search,replace';
		            			var _buttons2 = 'undo,redo,|,link,unlink,anchor,cleanup,|,insertdate,inserttime,|,forecolor';
		            			var _buttons3 = 'cut,copy,paste,pastetext,emotions,image,|,fontselect,fontsizeselect';
		            			
		            			
		            			$('textarea.tinymce').tinymce({
		            				// Location of TinyMCE script
		            				script_url : baseurl + '/javascript/vendors/tiny_mce/tiny_mce.js',

		            				// General options
		            				theme : "advanced",
		            				plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,advlist",
		            				width: '525px',
		            				// Theme options
		            				theme_advanced_buttons1 : _buttons1,
		            				theme_advanced_buttons2 : _buttons2,
		            				theme_advanced_buttons3 : _buttons3,
		            				theme_advanced_toolbar_location : "bottom",
		            				theme_advanced_toolbar_align : "center",
		            				theme_advanced_statusbar : false,
		            				theme_advanced_resizing : false,
		            				relative_urls : false,
							valid_elements : '*[*]'
						});
	            		    }
	            		});
	               }
	        });
	        return false;
    	  }
	   });
});

