
$(function() {

		//run the currently selected effect
		function runEffect(){
			//get effect type from 
			//var selectedEffect = $('#effectTypes').val();
			var selectedEffect = "blind";//$('#effectTypes').val();
			
			//most effect types need no options passed by default
			var options = {};
			//check if it's scale, transfer, or size - they need options explicitly set
			if(selectedEffect == 'scale'){  options = {percent: 100}; }
			else if(selectedEffect == 'transfer'){ options = { to: "#post-button", className: 'ui-effects-transfer' }; }
			//else if(selectedEffect == 'size'){ options = { to: {width: 280,height: 185} }; }
			
			//run the effect
			//$("#effect").show(selectedEffect,options,500,callback);
			$("#post-effect").show(selectedEffect,options,500,"");
			$("#post-effect").css({padding:'2px',height: '100%'});
		};
		
		//callback function to bring a hidden box back
		function callback(){
			//setTimeout(function(){
			var selectedEffect = "blind";//$('#effectTypes').val();
			var options = {};
			if(selectedEffect == 'scale'){  options = {percent: 100}; }
			else if(selectedEffect == 'transfer'){ options = { to: "#post-button", className: 'ui-effects-transfer' }; }
				$("#post-effect:visible").removeAttr('style').hide(selectedEffect,options,500,"").fadeOut();
			//}, 1000);
			$("#post-effect").css({padding:'2px',height: '100%'});
		};
		
		//set effect from select menu value
		$("#post-button").click(function() {
			//alert($("#effect").is(':hidden'));
			if($("#post-effect").is(':hidden')){
				runEffect();
				//$("#button").text("- [Hide Tags]");
				$("#post-button").html("<img src='/wp-content/themes/maroon/images/tags-hide.png' border='0'>");
			}
			else{
				callback();
				//$("#button").text("+ [View Tags]");
				$("#post-button").html("<img src='/wp-content/themes/maroon/images/tags.png' border='0'>");
			}
			return false;
		});
		
		$("#post-effect").hide();
		//$("#button").text("+ [View Tags]");
		$("#post-button").html("<img src='/wp-content/themes/maroon/images/tags-hide.png' border='0'>");
		$("#post-button").html("<img src='/wp-content/themes/maroon/images/tags.png' border='0'>");
		$("#post-button").css({cursor: 'pointer'});
	
	});



var new_tag_element_counter = 0;
function addmoreEcoEntity(tableName,pType,pName,pValue,aName,aValue,aMeta_Id,postition,posValue,meta_value,cat_type){
	var container = document.getElementById(tableName);
	var tr = container.insertRow(0);
	var tdchar = document.createElement("td");
	var tdtext = document.createElement("td");
	tdtext.setAttribute("width","10px");

	var redImage  = document.createElement("img");

	
	if(aMeta_Id==''){
		new_tag_element_counter++;
		aMeta_Id = new_tag_element_counter;
	}

	redImage.setAttribute("name","img-td-id-"+aMeta_Id);
	redImage.setAttribute("id","img-td-id-"+aMeta_Id);
	redImage.setAttribute("onclick","validateTagForm('"+pType+"')");

	var inputHidName  = document.createElement("input");
	inputHidName.setAttribute("type","hidden");
	//inputName.setAttribute("name","web_presences_name[]");
	inputHidName.setAttribute("name","hid_"+aName+"[]");
	inputHidName.setAttribute("size","10");
	inputHidName.setAttribute("style","background-color: gray");
	inputHidName.setAttribute("value",pName);
	if (pType=='events'){
		inputHidName.setAttribute("value",pName+"~~"+pValue+"~~"+cat_type);
	}
	else{
		inputHidName.setAttribute("id","hid-entity-id-"+aMeta_Id+meta_value);
	}
	

	var inputName  = document.createElement("input");
	inputName.setAttribute("type","hidden");
	//inputName.setAttribute("name","web_presences_name[]");
	inputName.setAttribute("name",aName+"[]");
	inputName.setAttribute("size","10");
	inputName.setAttribute("style","background-color: gray");
	inputName.setAttribute("value",pName);
	inputName.setAttribute("id","entity-id-"+aMeta_Id+meta_value);

	var inputUrl  = document.createElement("input");
	inputUrl.setAttribute("type","text");
	//inputUrl.setAttribute("name","web_presences_url[]");
	inputUrl.setAttribute("name",aValue+"[]");
	inputUrl.setAttribute("size","30");
	inputUrl.setAttribute("autocomplete","off");
	inputUrl.setAttribute("value",pValue);
	inputUrl.setAttribute("id","entity-value-"+aMeta_Id+meta_value);
	inputUrl.setAttribute("onkeypress","suggestEcoTag(this,'"+aMeta_Id+"','"+pType+"','"+"ecosystem-"+aName+"-"+aMeta_Id+meta_value+"','"+meta_value+"')");
	inputUrl.setAttribute("onchange","checkModification(this,'"+aMeta_Id+"')");

	var inputPos  = document.createElement("input");
	inputPos.setAttribute("type","text");
	//inputUrl.setAttribute("name","web_presences_url[]");
	inputPos.setAttribute("name",aValue+"_pos[]");
	inputPos.setAttribute("size","30");
	inputPos.setAttribute("value",posValue);
	inputPos.setAttribute("id","name");

	var inputMeta_key_id  = document.createElement("input");
	inputMeta_key_id.setAttribute("type","hidden");
	//inputMeta_key_id.setAttribute("name","web_presences_url[]");
	inputMeta_key_id.setAttribute("name","meta_key_id[]");
	inputMeta_key_id.setAttribute("size","50");

	if(aMeta_Id !='' && aMeta_Id !='undefined'){
		inputMeta_key_id.setAttribute("value",aMeta_Id);
	}
	else{
		inputMeta_key_id.setAttribute("value","0");
	}
	inputMeta_key_id.setAttribute("id","name");

	tdchar.appendChild(inputHidName);
	tdchar.appendChild(inputName);
	tdchar.appendChild(inputUrl);
	
	if(postition=='POSITION'){
		tdchar.appendChild(inputPos);
	}
	else if (pType=='events'){
		var selectCategory  = document.createElement("select");
		selectCategory.setAttribute("name","entity_category[]");
		selectCategory.setAttribute("id","name_category");
		
			for ( var i in event_options ){
				//alert( web_presences_options[i].value );
				var optionCat  = document.createElement("option");
				optionCat.setAttribute("value",event_options[i].value);
				optionCat.text = event_options[i].text;
				if(cat_type == event_options[i].value){
					optionCat.setAttribute("SELECTED",'true');					
				}
	
				selectCategory.appendChild( optionCat);
			} 		
		tdchar.appendChild(selectCategory);
		
	}
	
	
	tdchar.appendChild(inputMeta_key_id);
	
	/*
	if(pName==''){
		//tdtext.setAttribute("style","background-color: red");
		redImage.setAttribute("src","/wp-content/themes/maroon/images/large-red-star.gif");
	}
	else{
		redImage.setAttribute("src","/wp-content/themes/maroon/images/blank.gif");
		//tdtext.setAttribute("style","background-color: green");		
	}
	tdtext.appendChild(redImage);
	*/
	tr.appendChild(tdtext);
	tr.appendChild(tdchar);
	
	tr = container.insertRow(1);	
	var spanLayer = document.createElement("span");
		spanLayer.setAttribute("id","ecosystem-"+aName+"-"+aMeta_Id+meta_value+"-list");	
		spanLayer.setAttribute("style","border: 1px solid;left:30px;width: 300px; display: none; position: absolute; z-index: 10000; background-color: rgb(255, 255, 255);");		
	tr.appendChild(spanLayer);
/*
	tr = container.insertRow(2);	
	spanLayer = document.createElement("span");
		spanLayer.setAttribute("id","msg-span-"+aMeta_Id);	
		spanLayer.setAttribute("style","border: 1px solid;left:30px;width: 300px; display: none; position: absolute; background-color: rgb(255, 255, 255);");		
	tr.appendChild(spanLayer);
*/
	return;
}

/*
function validateTagForm(frmName){
	var frmElement = eval("document.saveData_"+frmName+".entity_name[]");
	//alert(frm);
	//var entity_name_arr = document.forms[0]. getElementsByName("entity_name[]");//frm.entity_name[];
	//alert(entity_name_arr);
	document.forms[0]
	alert(frmElement.length);
}
*/
function suggestEcoTag(fld,aMeta_Id,type,divID,meta_value){
	//currentVisibleDivLayer['ecosystem']='ecosystemchecklist-company';
	currentVisibleDivLayer['ecosystem']='ecosystem-'+type;
	suggest_box_from_regular_page=divID;
	field_id = "entity-id-"+aMeta_Id+meta_value;
	field_text = "entity-value-"+aMeta_Id+meta_value;
	
	var list_div = document.getElementById(suggest_box_from_regular_page+"-list"); //company-list
	//var keyword = document.getElementById("new-search-eco_tag");
	var keyword = fld;//document.getElementById("new-search-eco_tag");
	if(fld.value.length > 1 ){
		//alert('ajax call to populate companies');
		list_div.style.display = "block";
		//var company_name = "Displaying company "+Math.random();
		var company_name = "Displaying company 1";
		makeEcoAjax(keyword.value,aMeta_Id,type,'');
		//list_div.innerHTML += "<br><a onclick=\"selectCompany('id','"+company_name+"')\">"+company_name+"</a>";
	}
	
	//suggestSearch(fld,type,'')
	//suggestSearch(fld);
}

function makeEcoAjax(keyword,aMeta_Id,source_type,source_id,meta_value){

			if ( window.XMLHttpRequest ) {
				x = new XMLHttpRequest();
			} else {
				try{x=new ActiveXObject('Msxml2.XMLHTTP');}catch(e){try{x=new ActiveXObject('Microsoft.XMLHTTP');}catch(e){};}
			}

			if (x) {
				x.onreadystatechange = function() {
					var r, h;
					if ( x.readyState == 4 ) {
						r = x.responseText;//.substr(0, 18);
						h = x.getResponseHeader('Content-Encoding');
						showResults(r, h, keyword,true);
					}
				}
				//currentVisibleDivLayer['ecosystem'] will havevalues like (ecosystem-companies,ecosystem-people )
				x.open('GET', '/wp-admin/admin-ajax.php?mode=tag-search&key_name=entity-id-'+aMeta_Id+'&value_name=entity-value-'+aMeta_Id+'&action=ajax-ecosystem-search&search_type='+currentVisibleDivLayer['ecosystem']+'&tax=post_tag&q='+keyword+'&source_type='+source_type+'&source_id='+source_id+'&meta_value='+meta_value+'&'+(new Date()).getTime(), true);
				x.send('');
			}
}

function checkModification(fld,aMeta_Id){
	/*
	var img1 = document.getElementById("img-td-id-"+aMeta_Id);
	var hid_val2 = document.getElementById("entity-id-"+aMeta_Id);
	alert('hid:'+hid_val2.value);
	alert('img:'+img1);
	if(hid_val2.value == '' ){
		img1.src= "/wp-content/themes/maroon/images/large-red-star.gif";
	}
	else{
		img1.src = "/wp-content/themes/maroon/images/blank.gif";
	}
	*/
}