function on_click(event){
	
	if (event.keyCode == 13)
	{
		if(help_search.val() =='')
		{
	 	  help_search.val('enter question here'); 
	 	  help_search.focus();	 	  
			  return false;
		}
		else if(help_search.val() =='enter question here')
		{
			return false;
		 }
		 else
		 { 			 	 
			openSearchPage();
		 }
	}
}

function on_blur(obj){
	if($(obj).val()==''){
		$(obj).val('enter keywords...');
		$(obj).addClass('greytext');		
		return false;
	}
}

function on_focus(obj){
	if($(obj).val() == 'enter keywords...'){		
		$(obj).val('');
		$(obj).removeClass('greytext');		
	}
}
 
function openSearchPage()
{	
	var query = str_replace (' & ', ' and ', $('#help_search').val());
	menuitem = ( menuitem == 'more' ) ? 'web' : menuitem; 		
	parent.location = 'search/' + menuitem + '.php?query=' +  encodeURIComponent(query) + '&amp;page=1&amp;per_page=' + $('#per_page').val() + '&amp;category=-1';	
	
}

function str_replace ( search, replace, subject ) 
{			 // Replace all occurrences of the search string with the replacement string
		    //
		    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
		    // +   improved by: Gabriel Paderni
		 
		    if(!(replace instanceof Array)){
		        replace=new Array(replace);
		        if(search instanceof Array){//If search is an array and replace is a string, then this replacement string is used for every value of search
		            while(search.length>replace.length){
		                replace[replace.length]=replace[0];
		            }
		        }
		    }
		 
		    if(!(search instanceof Array))search=new Array(search);
		    while(search.length>replace.length){//If replace has fewer values than search , then an empty string is used for the rest of replacement values
		        replace[replace.length]='';
		    }
		 
		    if(subject instanceof Array){//If subject is an array, then the search and replace is performed with every entry of subject , and the return value is an array as well.
		        for(k in subject){
		            subject[k]=str_replace(search,replace,subject[k]);
		        }
		        return subject;
		    }
		 
		    for(var k=0; k<search.length; k++){
		        var i = subject.indexOf(search[k]);
		        while(i>-1){
		            subject = subject.replace(search[k], replace[k]);
		            i = subject.indexOf(search[k],i);
		        }
		    }
		 
		    return subject;
		 
}

/*********************************************************************************/
var line = 0;
var search;
var suggarray= [];
var help_search;
var suggdivs = [];
var flag = false;
var menuitem = 'web';
var button_shifts = [];
var shift = 0;
var mouse_is_inside = false;



function suggestion(field)
{	
	var dataString = 'key='+  encodeURIComponent(field);	
	line = 0;
	$.ajax({  
		type: "POST",  
		url: "search/include/getsuggestion.php",  
		data: dataString,  
		success: function(data) { 
			$('.sugg','#suggestionwords').remove();	
			suggarray.splice(0,suggarray.length);
			suggdivs.splice(0,suggdivs.length);
			if (data[0] != 'error')
			{					
				for(var i =1;i<data.length;i++)
				{	
					suggarray[i] = data[i];
					data[i] = data[i].substr(field.length);
					suggdivs[i] = $('<div class="search-text sugg" id="s'+i+'">'+field+'<span class="sugg2">'+data[i]+'</span></div>');						
					$('#suggestionwords').append(suggdivs[i]);						
				}
				suggdivs.splice(data.length,suggarray.length-data.length);					
				$('#suggestionwords').css('display','block');		
				
				$('.sugg').click(function(){
											
					help_search.val(suggarray[line]);	
					suggarray.splice(0,suggarray.length);
					suggdivs.splice(0,suggdivs.length);
					$('.sugg','#suggestionwords').remove();					
					$('#suggestionwords').css('display','none');					
					openSearchPage();
				});				
					
				$('.sugg').hover(over, out);
				
				if(!flag)
				{				
					$(document).bind('keyup', keyupf);
					flag = true;					
				}	
			}
			else
			{
				suggarray.splice(0,suggarray.length);
				suggdivs.splice(0,suggdivs.length);
				$('.sugg','#suggestionwords').remove();					
				$('#suggestionwords').css('display','none');				
			}
		} 
	});
}

var over = function(){
	if( line !=0 )
	{	
		suggdivs[line].removeClass('over');
	}
	var id = $(this).attr('id');
	line = parseInt(id.replace("s", ''));		
	$(this).addClass('over');						
	
}

var out = function(){
	$(this).removeClass('over');
}		

var keyupf = function(e){		
	switch (e.keyCode) {
	case 38:
	
	    	if(line !==0)
	    	{	
	    		suggdivs[line].removeClass('over');	
	    	}	
	    	if (line > 1 )
	    	{
	    		line--;	    		
	    		suggdivs[line].addClass('over');											
	    		help_search.val(suggarray[line]);		
		    } 				    	 
	    	else if(line == 1)
	    	{		    		
	    		help_search.val(search);	
	    		help_search.focus();	
	    		line = 0;
	    	}				    	
			  
	    break;
	    case 40:	
	    	
	    	if(line == 0)
	    	{		    		
	    		help_search.blur();		    		
	    		line = 1;    							   				
				help_search.val(suggarray[line]);	
				suggdivs[line].addClass('over');				
	    	}
	    	else if ( line <  suggdivs.length-1 )
		    {	
	    	   suggdivs[line].removeClass('over');	 	 
		       line++;		      			   				
			   help_search.val(suggarray[line]);
			   suggdivs[line].addClass('over');			   
		    }					    	 
	    	else if( line == suggdivs.length-1 )
	    	{	    		
	    		help_search.val(search);	
	    		help_search.focus();		   
	    		suggdivs[line].removeClass('over');		    		
		    	line = 0;		    	 
	    	}	    	
	    				
	    break;
	    case 13:	    	
	
	 		  help_search.val(suggarray[line]);
	 		  help_search.focus();
	 		  openSearchPage(); 		   	
	    	
	    break;				  
	}			
}  

$(function(){			
		
	help_search = $('#help_search');																					
	
	$(document).click(function(){
		suggarray.splice(0,suggarray.length);
		suggdivs.splice(0,suggdivs.length);
		$('.sugg','#suggestionwords').remove();								
		$('#suggestionwords').css('display','none');		
	});	
	
	$('#button-container').click(function(){
		
		openSearchPage();		
	});
	
	$('#button-container').hover(function(){		
		$('#button-left-container').css('background-position','0 -44px');		
		$('#button-right-container').css('background-position','0 -44px');
	},
	function(){	
		$('#button-left-container').css('background-position','0 0');		
		$('#button-right-container').css('background-position','0 0');	
	});
	
	$('.menuitem').click(function(){
		
		$('#'+menuitem+'menu').removeClass('activ');
		$('#'+menuitem+'right').removeClass('activ');
		$('#'+menuitem+'left').removeClass('activ');	
		menuitem = str_replace('menu', '', $(this).attr('id'));
		if ( menuitem != 'more' && menuitem != 'search-settings')
		{
			if ( menuitem != 'articles' && menuitem != 'blogs' && menuitem != 'books' )
			{	
				$(this).addClass('activ');
				$('#'+menuitem+'right').addClass('activ');
				$('#'+menuitem+'left').addClass('activ');			
				
				$('#'+menuitem).removeClass('activ');
			}
						
			
			$('#button-left-container').css('width', button_shifts[menuitem][0] + 'px');
			$('#button-text').html(button_shifts[menuitem][1]);
		}
		else if( menuitem == 'search-settings' )
		{
			parent.location = 'search/search-settings.php';
		}
		else
		{
			show_additional_menu('morefl');
		}
		
		return false;
	});
	
	$('#settings').click(function(){
		
		show_additional_menu('settingsfl');
	});
	
	help_search.keyup(function(e){		
		
		search = $(this).val();			
		
		if (e.keyCode != 40 )
		{					
			suggestion(search);
		}		
	});			

	$("#hidden-drop-down").hide();
	$("#jq").hover( function() { $("#hidden-drop-down").fadeIn("fast").stop;}
							, function() {$("#hidden-drop-down").show("slow").stop; }); 			

	$('#hidden-drop-down').hover(function(){},function() {$("#hidden-drop-down").fadeOut("fast").stop; });

 
 	$('.popup_link','#hidden-drop-down').hover(function(){
		$(this).css("background","url('../images/newsearch/popdown_rollover_layer_only.png') no-repeat");				
		$(this).css("cursor","pointer");},

	function(){
		$(this).css("background","none"); 								
	}); 
 
 
 	$('.small-container').hover(function(){
		
	//	if ($(this).attr('id') != 'aa4' )
	//	{	
			$('.hideShow',this).fadeIn("fast").stop;
			$(".a",this).hide();		
	//	}
		},
										  function(){
	
	$('.hideShow',this).fadeOut("fast").stop;
	$(".a",this).show();    
	});

	$('.popup_link','.hideShow').hover(function(){
							$(this).css("background","url('../images/newsearch/popup_rollover_layer_only.png') no-repeat");											
							$(this).css("cursor","pointer");},
		
						function(){
							$(this).css("background","none"); 								
	}); 
								

 /**************************************************************************************/ 

	$(".click_here1").hover(function(){$(".click_here1").css("cursor","pointer");
											   $(".click_here1").css("textDecoration","underline");


 	},function(){$(".click_here1").css("cursor","none"); $(".click_here1").css("textDecoration","none");});	

	$(".click_here2").hover(function(){$(".click_here2").css("cursor","pointer");
											   $(".click_here2").css("textDecoration","underline");


	},function(){$(".click_here2").css("cursor","none"); $(".click_here2").css("textDecoration","none");});	
	

	
	$(".click_here1").click(function () { $("#top_icon_text1").hide();
													 $("#top_icon_text2").show();
													$("#big-grey-gradient").css("background","url('../images/background.png') no-repeat");
													$("#big-grey-gradient").css("background-position","top left");
													$("#freshiest-source").hide();
													$(".freshiest-source_olympic").show();
													//document.getElementById('hiddvalue').value = '1';
										});	
	$(".click_here2").click(function () { $("#top_icon_text2").hide();
													 $("#top_icon_text1").show();
													 $("#big-grey-gradient").css("background","url('../images/grey_gradient_big.png') no-repeat");
													 $("#big-grey-gradient").css("background-position","top left");
													 $("#freshiest-source").show();
													 $(".freshiest-source_olympic").hide();
 													//document.getElementById('hiddvalue').value = '2';
								   });	
	
	

				if(queryString('theme')==2){ $(".click_here1").trigger('click');}
				else{$(".click_here2").trigger('click');}
		
	$('*').keydown(function(e){
		
		if( $('#suggestionwords').css('display') == 'block' )
		{
			var code;		
			
			if (window.event) {
				e = window.event;			
			}	
			
			if (e.keyCode){
				code = e.keyCode;
			}			
			
			if(code == 40 || code == 38){	
				
				return false;
			}
		}	
		
		return true;		
	});
	
	$('#zoom_query').attr('autocomplete','off');
	
});  

function show_additional_menu(id)
{
	$('#' + id).css('display','block');
	
	$('#' + id).hover(function(){ 
        mouse_is_inside=true; 
    }, function(){ 
        mouse_is_inside=false; 
    });
 
    $('body').mouseup(function(){ 
        if(!mouse_is_inside) $('#' + id).css('display','none');
    });
}
		
function queryString(parameter) { 
  var loc = location.search.substring(1, location.search.length);
  var param_value = false;

  var params = loc.split("&");
  for (i=0; i<params.length;i++) {
      param_name = params[i].substring(0,params[i].indexOf('='));
      if (param_name == parameter) {
          param_value = params[i].substring(params[i].indexOf('=')+1)
      }
  }
  if (param_value) {
      return param_value;
  }
  else {
      return false; //Here determine return if no parameter is found
  }
}
