/// / / // ///


var area1, area2, area3, area4;
	
function addArea(area,box) {
	var a = eval(""+area+"") = new nicEditor({buttonList : ['bold','italic','underline','strikeThrough','link','ol','ul'], iconsPath : '/scripts/js/nicEditorIcons.gif', xhtml: true }).panelInstance(box);
 	alert(a);
}
function removeArea(area,box) {
	var x = eval(area).removeInstance(box);
	x=null;
}

function stripTags(area, tgt){

//	removeArea(area,tgt);
	
	var str =$("#"+tgt).val();
	//alert(str);
	$("#"+tgt).attr("value",str.replace(/<\/?[^>]+>/gi,''));
     
	
};


function formatSup() {	
	$("sup").each(function(i) {
		var $h = $(this);  // get the current H3 has a jQuery object 
        var html = $h.html();  // get the HTML content of the element 
        html = html.replace('-', ' ');  // remove '&hellip;' 
        $h.html(html);  // store the HTML back in the element 
	});
	
	$("sup").each(function(i) {
		var $h = $(this);  // get the current H3 has a jQuery object 
        var html = $h.html();  // get the HTML content of the element 
        html = html.replace('-', ' ');  // remove '&hellip;' 
        $h.html(html);  // store the HTML back in the element 
	});
}

function doSearch(keywords){
 	document.location = "/search/"+escape(keywords)+"/";
}

function joinNewsletter(str){
 	document.location = "/newsletter/"+escape(str)+"/";
}

 
function showTabs(tabType){
	$("#eventsList li").hide();
	tabType = jQuery.trim(tabType);
	//	alert("'"+tabType+"'");
	if(tabType=="recent" || tabType=="recent "){
		$("#eventsList li").show();
	}else if(tabType=="events"){
		$("#eventsList li[title=current_event]").show();
	}else if(tabType=="private views"){
		$("#eventsList li[title=current_private-view]").show();
	}else if(tabType=="exhibitions"){
		$("#eventsList li[title=current_exhibition]").show();
	}else if(tabType=="tours"){
		$("#eventsList li[title=current_tour]").show();
		$("#eventsList li[title=current_studio-visit]").show();
	}else if(tabType=="news"){
		$("#eventsList li[title=current_news]").show();
	}else if(tabType=="trustee"){
		$("#eventsList li[title=current_trustee]").show();
	}else{
		$('#eventsList li').show();
	}
	
}

function addAssoc(assocType, tgtMenu, tgtList, refTable){

  	
	var selected = $("#"+tgtMenu+" option:selected");
	var refID = selected.val();
	var locLabel = selected.text();
	
	contentID = $("#contentID").val();
	
	if(refTable == "location") {
	var editLink = $(" <a href='/youradmin/addLocation.php?locationID="+refID+"'>Edit</a> ");
	} else if(refTable == "artist") {
	var editLink = $(" <a href='/youradmin/addArtist.php?artistID="+refID+"'>Edit</a> ");
	} else if(refTable == "content") {
	var editLink = $(" <a href='/youradmin/addContent.php?contentID="+refID+"'>Edit</a> ");
	}

	if(refID!=""){
	
		if(checkIsntThere(tgtList, locLabel)<1){
		
		/*	if(setAssocType=="artist"){
				refTable="artist";
				assocType="artist";
			}*/
			
		//alert(assocType+" "+tgtMenu+" "+tgtList+" "+refTable+" "+locLabel);
		
			$.post("/scripts/php/process.php",{addAssoc : "add", contentID:contentID, refID:refID, refTable:refTable, assocType:assocType }, function(data){
				var handle = $("<span/>");
				handle.attr("class","handle");
				
				var newContent = $("<li id='item_"+data+"'><span class='text'>"+locLabel+"</span></li>");
				$('#'+tgtList).append(newContent);
				newContent.append(editLink);
				newContent.append(handle);	
				bind(handle);
			});
					 
		}else{
			alert("this is already there! ");
		}
		
	} 
	
}


function addMediaAssoc(assocType, tgtMenu, tgtList, refTable){

  	
	var selected = $("#"+tgtMenu+" option:selected");
	var refID = selected.val();
	var locLabel = selected.text();
	
	mediaID = $("#mediaID").val();

	if(refID!=""){
	
		if(checkIsntThere(tgtList, locLabel)<1){
	 
		//alert(assocType+" "+tgtMenu+" "+tgtList+" "+refTable+" "+locLabel);
		
			$.post("/scripts/php/process.php",{addMediaAssoc : "add", mediaID:mediaID, refID:refID, refTable:refTable, assocType:assocType }, function(data){
				var handle = $("<span/>");
				handle.attr("class","handle");
				
				var newContent = $("<li id='item_"+data+"'><span class='text'>"+locLabel+"</span></li>");
				$('#'+tgtList).append(newContent);
				newContent.append(handle);	
				bind(handle);	
			});
					 
		}else{
			alert("this is already there! ");
		}
		
	} 
	
}

function checkIsntThere(list, str){
	var mtch = 0;

	$("#"+list+" .text").each(function(i) {
	
		var compare = $(this).text();
		if(compare == str){
			mtch ++;
		}
	
	});
	
	return mtch;
	
}

  	function createAssocMenu(src, addTo){
		var selected = $("#"+src+" option:selected");
		var refID = selected.val();
		var refType = selected.text();
		
		setAssocType = refID;
 		
		$(addTo).removeAttr("disabled");
		
		$.post("/scripts/php/process.php",{buildAssocMenu : "build", table:'content', q1:" WHERE contentType=\'"+refID+"\' "}, function(data){
			
			var vals = "<option >choose...</option>"+data;
			$(addTo).html(vals);

 		});
		
 	}
 	
function bind(handle) {			
	$(handle).bind('click', function(){
	var ele = $(this);
	var type = ele.parent().parent().attr("id");
	var parentId = ele.parent().attr("id");
	//alert(listType+" "+parentId);
			
	$.ajax({
		url: "/scripts/php/process.php",
		type: "POST",
		data: "type="+type+"&parentId="+parentId+"&removeAssoc=1",
		
		// complete: function(){ $('#data').html(); },
		success: function(){ ele.parent().remove(); }
		// error: function(){ $('#data').html(); }
		
	});
						
	});
}