// Search format.
$(document).ready(function() {
	
		
		// Select search type
		$(".search-drop div").click(select_search_type);
		
		$(".search-options .option-items .item").click(select_search_typev2);
		$("#show_options_search").click(function(){
			
			$(".search-options").show();
				return false;								 
		});
		
		


});



// global variables

var acDelay		  = 300;
var acURL		  = null;
var acSearchBox	  = null;
var acResultBox	  = null;
var acSearchField = null;
var acResultsDiv  = null;

var acSearchInput	= null;
var acSearchType	= null;		
var acSearchResults = null;
var acLoaderClass = null;

function set_search(search_box , field_results, get_url, LoaderClass ){
	

	
	//set vars
	acSearchBox	=$("#" + search_box);
	acResultBox =$("#" + field_results);
	acURL		=get_url;
	acLoaderClass=$(LoaderClass);
	
	// Mostly used vars.
	
	acSearchInput	= $(" #search", acSearchBox);
	acSearchType	= $(" #search_type", acSearchBox);
	acSearchResults = $(" .link-results", acResultBox);

	// click function input
	acSearchInput.click(function(bla){
		if( $(this).val()=='Seek And Find...'){
			$(this).val('');	
		}
								 
	});
	
	// Blur function input
	acSearchInput.blur(function(bla){
		if( $(this).val()==''){
			$(this).val('Seek And Find...');	
		}
								 
	});
	
	// Close results
	$(" .close a",acResultBox).click(function(a){
				removeLoad();
				clearResultBox();	
				return false;
	 });
	


	// Keyup function 
	acSearchInput.keyup(function (e) {
		// grab keycode			
		var keyCode = e.keyCode || window.event.keyCode;	
		
		// up/down do nothing		
		if(keyCode == 40 || keyCode == 38)
			return false;						  
		
		acSearchBox	=$("#" + search_box);
		acResultBox =$("#" + field_results);
		acURL		=get_url;
		acLoaderClass=$(LoaderClass);
		
		// Mostly used vars.
		
		acSearchInput	= $(" #search", acSearchBox);
		acSearchType	= $(" #search_type", acSearchBox);
		acSearchResults = $(" .link-results", acResultBox);

		
		
		//remove load & clear old results
		removeLoad();
		clearResultBox();
			
			
			
		// esc of enter  ; remove results
		if(keyCode == 13 || keyCode == 27)
			return false;		
		
		
		var lastVal = acSearchInput.val();// search query
		var lastType= acSearchType.val();// search type
		
		
		
		// Grab results
		setTimeout(function () {autoComplete(lastVal,lastType)}, acDelay);
	 });
	
	return false;
	
};

// Start searching and give results
function autoComplete(lastVal,lastType){
	
	var acSearch =acSearchInput.val();
	var acType 	 =acSearchType.val();
	
	
	
	// empty then stop
	if(acSearch == '' || acSearch == 'Seek And Find...' )
		return false;
	
	// is it the correct value on the time of the call.
	if( (acSearch != lastVal) || ( acType!=lastType) )
		return false;
		
	//at least 3 characters long
   if(lastVal.length <3)
   		return false;
		
	//loading	
	doLoad();	
	if(acSearchBox.attr("id")!="search-box-bottom"){
		
		$(".search-options").show();
		
	}
	

    // Search the database
	$.getJSON(acURL,{ s: acSearch, type:acType }, function(j){
														   
		// loaded, remove loading
		 removeLoad();
		 
		// clear results
		acSearchResults.html('');
		
		if(!j){
			return false;	
		}
		// total results
		var ansLength = j.length;
		
		// more then zero results
		if(ansLength > 0 ){
			
			// loop results
			for(i=0; i < ansLength; i++) {
				
				// create result item.
				var result='<p class="normal"><img  class="icon" src="http://www.nzbmovieseeker.com/images/arrow_right.png" />';
				result+='<a href="'+j[i]['link']+'">'+ j[i]['name'] +"</a>";
				
				// if Aka in movie , show it.
				if(j[i]['aka'].length!=0){
					result+='<span class="aka">aka : '+j[i]['aka']+' </span>';	
				}
				
				result+='</p>';
				
				// Append item.
				acSearchResults.append(result);
	
			}
			// More results link..
				if(ansLength==5){
					var result='<p class="normal"><img  class="icon" src="http://www.nzbmovieseeker.com/images/arrow_right.png" />';
					result+='<a href="http://www.nzbmovieseeker.com/Search/?q='+acSearch+'&type='+acType+'">More results ...</a> </p>';
					acSearchResults.append(result);
				}
			
			// Show results
			acResultBox.show();
			
			if(acSearchBox.attr("id")=="search-box-bottom"){
				// check height
				var height =acResultBox.height() + 20;
				
				// absolute position above the search box.
				acResultBox.css("margin-top","-"+height+"px");
			}
			
		}
		

		


	});
	

	
	
};

// Empty result box
function clearResultBox()
{
	acSearchResults.html('');
	acResultBox.hide();
};

// do loading search
function doLoad(){
	
	$(acLoaderClass).show();
		
};

// remove loading
function removeLoad(){
	$(acLoaderClass).hide();	
};

// CHange search type
function select_search_type(evt){
	var type =$(this).text();
	var img ='<img class="icon" src="http://www.nzbmovieseeker.com/images/plus.gif">';
	
	acSearchBox		= $("#search-box-bottom");
	acSearchInput	= $(" #search", acSearchBox);
	acSearchType	= $(" #search_type", acSearchBox);
	acLoaderClass	= $(".load_search2");
	acResultBox		= $("#bottom-results");
	acSearchResults = $(" .link-results", acResultBox);
	//change type.
	
	acSearchType.val(type);
	acSearchType	= $(" #search_type", acSearchBox);
	
	//create loading tekst
	var loadingText='<img  class="icon" src="http://www.nzbmovieseeker.com/images/loader.gif" />';
	loadingText+='Searching '+type+'...';
	$(acLoaderClass).html(loadingText);
	
	// start search if input already typed in.
	if(acSearchInput !='' && acSearchInput !='Seek And Find...'){
		
			var lastVal = acSearchInput.val();// search query
			var lastType= acSearchType.val();// search type
			//remove results now.
			clearResultBox();
			autoComplete(lastVal,lastType);
		
	}
	
	// change tekst menu.
	$(this).parent().prev().html(img+type);
	$(this).parent().hide();
	
	
};




// CHange search type
function select_search_typev2(evt){
	
	var type =$(" .option-tekst",this).text();
	var acItems=$(this).parent();
	
	acSearchBox		= $(acItems).parent().prev();
	acSearchInput	= $(" #search", acSearchBox);
	acSearchType	= $(" #search_type", acSearchBox);
	acLoaderClass	= $(".load_search3");
	acResultBox		= $("#top-results");
	acSearchResults = $(" .link-results", acResultBox);
	//change type.
	
	acSearchType.val(type);
	acSearchType	= $(" #search_type", acSearchBox);
	
	// remove all filled boxes.
	$(" .item .option-box-selected").removeClass("option-box-selected").addClass("option-box");
	
	// Fill selected box.
	$(" .option-box",this).removeClass("option-box").addClass("option-box-selected");
	
	//create loading tekst
	var loadingText='<img  class="icon" src="http://www.nzbmovieseeker.com/images/loader.gif" />';
	loadingText+='Searching '+type+'...';
	$(acLoaderClass).html(loadingText);
	
	
	// start search if input already typed in.
	if(acSearchInput !='' && acSearchInput !='Seek And Find...'){
		
			var lastVal = acSearchInput.val();// search query
			var lastType= acSearchType.val();// search type
			//remove results now.
			clearResultBox();
			autoComplete(lastVal,lastType);
		
	}

	
};
