jQuery(document).ready(function() {
	
	
	jQuery('#menu_editor').click(function() {
		
		jQuery('#edit_menu_popup').remove();

		jQuery('body').append('<div id="edit_menu_popup" style="overflow:hidden;"><br>'+'<iframe width="440" height="590" frameborder="0" src="/static/test_tree/index.php"> </iframe>'+'</div>');
		jQuery('#edit_menu_popup').dialog({ autoOpen: false,height: 550,minWidth: 440, maxWidth: 600,resizable: false });
		jQuery('#edit_menu_popup').dialog('open');
		jQuery( "#edit_menu_popup" ).bind( "dialogclose", function(event, ui) {
			//alert('closing');
			jQuery('#edit_menu_popup').remove();
			});
		return false;
		
	});
	
	
	
	

	var wideList = jQuery('.body-wide');	
	var i=0;
	for (i=0;i<=wideList.length;i++)
	{
		var wideItem = wideList[i];
		if(jQuery(wideItem).parent().offset() != null){
			var top = jQuery(wideItem).parent().offset().top; 
			jQuery(wideItem).appendTo('body'); 
			jQuery(wideItem).css('top',top);
		}

	}



	
	
	function global_edit(){
		var dragList = jQuery('[id^=div_]');
		var i=0;
		for (i=0;i<=dragList.length;i++)
		{
			var dragItem = dragList[i];
			
			if(typeof(dragItem)!="undefined"){ 
			
				var id = dragItem.id.replace(/div_/i, "");
				
				jQuery('[id=div_'+id+']').draggable({ disabled: true });
				jQuery('[id=div_'+id+']').removeClass('ui-draggable-disabled ui-state-disabled');
				
				jQuery('img#showcontext_'+id).hide();
				jQuery('table#contexttable_'+id).hide();
				jQuery(dragItem).find('.ui-resizable-handle').hide();
			}

		}
	}
	

	jQuery('.add_editor').click(function() {
		var div_id = jQuery(this).closest('table').attr('id').replace('contexttable_','');
		var div = 'div_' + div_id;

		jQuery('#contexttable_'+div_id).remove();
		jQuery('#showcontext_'+div_id).remove();
		jQuery('#div_'+div_id).find('.ui-resizable-handle').remove();
		var data = jQuery('#div_'+div_id).html().replace(/<!-- center absolute div -->/g,'');
		jQuery('#div_'+div_id).empty();
		jQuery('#'+div).append('<div style="width: 100%; height: 100%; border: 0px solid #000000; display: block;" id="dv_text_Area_'+div_id+'">'+data+'</div>'+
				'<button class="live_edit" div=\''+div_id+'\');">Editor</button>'+
		        '<button class="continue">Continue</button>'+
		        '<!--<div class="continue">Continue</div><br />-->'+ 
		        '<div style="clear: both;">'+
		'</div>');
		global_edit();
		return false;
	});
	
	var tmp;
	function toggleTextArea (id){

		var div_id = id.replace('dv_text_Area_','');		
		var dot_id = jQuery('#div_'+div_id).attr('dot');

		if(!tmp) {
			tmp = new nicEditor({fullPanel : true, iconsPath : '/assets/js/nicEditorIcons.gif' ,onSave : function(content, id, instance) {
		    
				jQuery.post("http://"+window.location.host+"/editor/edit/page/empty/dot/"+dot_id, 						
						{ 
							cmd: 'submit',
							controller: 'savecontent',
							readme: 'empty', 
							content: content,
							position: 'center-absolute',
							title: 'empty' 
							//class: 'df', 
							//pfbc-token0: '5677f00bc8af5f2c6d8bfabbc4632fc7',
							//pfbc-token1: '',
							//pfbc-token2: '7cf2364cbbafb8d6c2f5fa8cb00f7765'
						});
				//alert('save to dot '+dot_id+' element '+id+' = '+content);
			} }).panelInstance(id,{hasPanel : true});
		} else {
			tmp.removeInstance(id);
			tmp = null;
		}
	}

	jQuery('.continue').live('click', function() {
		window.location.reload();
	});
	
	jQuery('.live_edit').live('click', function() {
		var div_id = jQuery(this).attr('div');
		//console.log(div_id);
		toggleTextArea ('dv_text_Area_'+div_id);
		global_edit();
	});
	
	function isAlphaNumeric(val)
	{
		if (val.match(/^[a-zA-Z0-9]+$/))
		{
			return true;
		} else {
			return false;
		} 
	}
	
	function post(name){
		var url = '/static/test_tree/manageStructure.php';
		jQuery.post(url, 
				{ 
					action: "insertElement",
					name: name,
					ownerEl: "0",
					slave: "1",
					site_id: "0",
					type: "6",
					url: ""
				},
				   function(data) {
					
					var data = data.replace('(','').replace(')','');
					var obj = jQuery.parseJSON(data);
					var path = window.location.pathname.substr(1);
					
					
					if(path.length == 0){
						path = 'index';
					}
					//alert('path: '+path + ' length: ' +path.length);
				    //return;
				     // action copy
				     
						jQuery.post(url, 
								{ 
									action: "copyElement",
									name: obj.elementName, // elementName
									ownerEl: "0", // get from db
									elemId: obj.tree_id, // tree_id
									elem_id: obj.elem_id, // elem_id
									slave: obj.slave,
									type: obj.type
									//url: "" potential bug

								},
								   function(data) {		
				     // action past				     
						jQuery.post(url, 
						{ 
							action: "pasteElement",
							name: path, // window.location.pathname
							ownerEl: "0"
						},
							function(data) {	
								window.location.reload();
							});
						});
				   });
		
	}
	
	jQuery('#add_item').click(function(){
		
		var name = 'newele sdaf ment';
//		
		jQuery('body').append('<div id="add_item_form" style=""><form><input id="item_name" value=""><input id="item_name_save" type="submit" value="save"></form><br></div>');
		jQuery('#add_item_form').dialog({ autoOpen: false });
		jQuery('#add_item_form').dialog('open');
//		
//		
		jQuery('#item_name_save').click(function(){
			var name = jQuery('#item_name').val();
			
			
			if(!isAlphaNumeric(name)){
				alert('please choose a name with letter or numbers and no spaces');
				return false;
			}else{
				post(name);
			}
			return false;
		});
		return false;

	});


	


	

});












