$(function (){
	
	
	jQuery.fn.outerHTML = function() {
	    return $('<div>').append( this.eq(0).clone() ).html();
	};
	
	
	
	var leave = 0;
	
	$("#startPanel").live('click', function() {
		
		if ($("#fullMenu").is(":visible") ) {
			
			leave = 0;		
			$("#fullMenu").hide();	
		
		}
		
		else {
			
			leave = 1;
			$("#fullMenu").show();			
			
		}
		

		
	});
	
	$("#startPanel").live('mouseenter', function() {
		
		$(this).css({"background-color" : "#98BECD"});
		
	});
	
	$("#startPanel").live('mouseleave', function(event) {
		
		if(leave == 0) $(this).css({"background-color" : "#4DACCF"});

		
	});		
	
	$("#chooseDataDomain").live('change', function() {
		
		var value = $(this).val();
		

		$.xmodify({url:	'index.php?controller=AjaxSearch&action=chooseDataDomain&dataDomainID='+value,   
			
			complete: function(){
				 
				$('.searchMetadata input').each(function() {
						
					var parentWidth = $(this).parents('.searchMetadata').find('div:eq(0)').width();
					
					$(this).width(parentWidth+4);
					
				});
			}
		
		});
			
	
	});
	
	$("a.destroyMetadata").live('click', function() {
		
		$(this).parents('.searchMetadata').remove();
		
		return false;
		
	});
	
	$("#searchForm").live('submit', function() {
		
		var submitData = [];
		
		$(".searchMetadata").each(function() {
			
			var obj = {};
			
			obj.metaID = $(this).find('input[name=metaID\[\]]').val();
			
			
			if($(this).find('select[name=metaTextContent\[\]]').val() != undefined) {
				
				obj.contentTextValue = $(this).find('select[name=metaTextContent\[\]]').val();
				
			}
			
			else {
				
				obj.contentTextValue = $(this).find('input[name=metaTextContent\[\]]').val();				
				
			}



			obj.contentType = $(this).find('input[name=metaTextContent\[\]]').attr('wztype');
			obj.searchType = $(this).find('select[name=searchType\[\]]').val();
				
			submitData.push(obj);
		
		});
		
		var jsonData = JSON.stringify(submitData);
		 
		var dataDomainID = $("#chooseDataDomain").val();


		$.xmodify({url:	'index.php?controller=AjaxSearch&action=search&dataDomainID='+dataDomainID+'&searchParam='+jsonData});
		
		
		return false;
		
	});
 	
	$(".searchMetadata input:text").live('focus', function() {
	
		$("#expandInput>input").val($(this).val());
		var labelText = $(this).parents('.searchMetadata').find('div>span').text();
		$("#expandInput>span").text(labelText);
				
	});
	
	$("#expandInput>input").live('focusout', function() {
	
		var metaText = $(this).parents('#expandInput').find('span').text();
		$("span:contains("+metaText+")").parents(".searchMetadata").find('input[name="metaTextContent\[\]"]').val($(this).val());
		
		
	})
	
	$("#q").one('focus', function() {
		
		$(this).val("");
		return false;
		
	});
	
	
	$("input.expInput").live("focus", function() {
		
		expandInput(this);
		
	});
	
	$("textarea.expInput").live("keyup", function() {
		FitToContent(this, document.documentElement.clientHeight);
	});	
	
	
	$("#moreCriteriaUp").live("click", function() {

/*		var lastMeta = $("#quickMenu").find("div").last().attr("id");
		code = $($("#"+lastMeta).outerHTML()).hide();
		$("#"+lastMeta).remove();
		code.prependTo("#quickMenu").fadeIn('slow'); */
		
		$("#quickMenu").find("div:hidden").first().show(); 

	});	
	
	
	$("#moreCriteriaBottom").live("click", function() {

/*		var firstMeta = $("#quickMenu").find("div").first().attr("id");
		code = $($("#"+firstMeta).outerHTML()).hide();
		$("#"+firstMeta).remove();
		code.appendTo("#quickMenu").fadeIn('slow'); */
		
/*		var firstMeta = $("#quickMenu").find("div:visible").first().attr("id");
		code = $($("#"+firstMeta).outerHTML()).hide();
		$("#"+firstMeta).hide();
		code.appendTo("#quickMenu").fadeIn('slow'); */
		
		$("#quickMenu").find("div:visible").first().hide();
		
		

	});	
	
	
	$('.helperTrigger').live('click', function() {
		
		var empty = "no";
		init = $(this);
		var end = "";
		
		while(empty == "no") {
			init = init.parent();
			if(init.length == 0) empty = "yes";
			
			if(init.find(".helperDiv").length != 0) {
	
				if(init.find(".helperDiv").is(":visible") == false) init.find(".helperDiv").show();
				else init.find(".helperDiv").hide();
				
				break;
					
			}
			
			
		}
		
		return false;
		
	});	
	
	
});


$.fn.setCursorPosition = function(pos) {
  this.each(function(index, elem) {
    if (elem.setSelectionRange) {
      elem.setSelectionRange(pos, pos);
    } else if (elem.createTextRange) {
      var range = elem.createTextRange();
      range.collapse(true);
      range.moveEnd('character', pos);
      range.moveStart('character', pos);
      range.select();
    }
  });
  return this;
};


function FitToContent(id, maxHeight) {
	
   var text = id && id.style ? id : document.getElementById(id);
   if ( !text )
      return;

   var adjustedHeight = text.clientHeight;
   if ( !maxHeight || maxHeight > adjustedHeight )
   {
      adjustedHeight = Math.max(text.scrollHeight, adjustedHeight);
      if ( maxHeight )
         adjustedHeight = Math.min(maxHeight, adjustedHeight);
      if ( adjustedHeight > text.clientHeight )
         text.style.height = adjustedHeight + "px";
   }
}


function expandInput(e) {
	
	var thisElem = e;
	
	
	if($(e).get(0).tagName.toLowerCase() == 'input') {

		var newElement = $("<textarea></textarea>");
		
		var oldValue = $(e).val();
		
		$(newElement).val(oldValue);		
	}
	
	if($(e).get(0).tagName.toLowerCase() == 'textarea') {
		
		var newElement = $("<input></input>");					
	
		// $(this).replaceWith("<input class= \"expInput\"></input>");
		
	}
	
	$.each(e.attributes, function(index) {
		$(newElement).attr(thisElem.attributes[index].name, thisElem.attributes[index].value);
	});	
	
	$(newElement).css({"overflow" : "hidden"});
	
	$(e).replaceWith(newElement).click();
	
	$(newElement).focus().setCursorPosition(oldValue.length);
	
	
}

