google.load('search', '1', {"language" : lang } );
google.setOnLoadCallback(OnLoad);

var cs;
$(document).ready(function() {
	
	bindSearchlinks();
	bindFilteraction();
	setLanguages();
	
	cs = new cse();
	$('.gsc-cursor-prev').live('click', function() {
		cs.searcher.gotoPage(prev);   
		});
	$('.gsc-cursor-next').live('click', function() {
		cs.searcher.gotoPage(next);
		});
	
	
	 if($('#town').val() != '') {
		  $('#street').show();
		  $('#street').removeClass().addClass("street_"+$('#country').val());
		  $('#street-town').html($('#town').val().slice(0,1).toUpperCase() + $('#town').val().slice(1));
		  
		  if($('#town').val().length > 8) 
			  $('#street-town').removeClass().addClass("medium");
		  if($('#town').val().length > 10) 
			  $('#street-town').removeClass().addClass("small");
		  if($('#town').val().length > 14) 
			  $('#street-town').removeClass().addClass("xsmall");
	  }
	 
	 $('#searchForm form').submit(function() {
		 $('#query').val(getCompleteQuerystring(true));
		 $('#lquery').val(getCompleteQuerystring());
		 hideGetVars();
		});
	 $('#searchForm2 form').submit( function() {
		 $('#searchForm form').submit();
		 return false;
		});
	 
	 $('#searchForm input.gsc-input').bind("change", function() {
		 $('#vquery').val($(this).val());
		});
	 $('#searchForm input.gsc-input').bind("keyup", function() {
		 $('#vquery').val($(this).val());
		});
	  $('#searchForm input.gsc-input').bind("keyup", function() {
		 $('#searchForm2 input.gsc-input').val($('#searchForm input.gsc-input').val());
		});
	 $('#searchForm2 input.gsc-input').bind("keyup", function() {
		 $('#searchForm input.gsc-input').val($('#searchForm2 input.gsc-input').val());	
		 $('#searchForm input.gsc-input').change();
		});
	 
	 updateLeftsideVisibility();
});

function OnLoad() {
}
		  
function cse() {
	var query = $('#vquery').val();
	
	if(document.getElementById("searchForm") != null) {
	    this.searchForm = new google.search.SearchForm(true, document.getElementById("searchForm"));
	    $(this.searchForm.userDefinedCell).html($('#hiddenfields input').clone());
	}
	
    if(document.getElementById("searchForm2") != null) {
	    this.searchForm2 = new google.search.SearchForm(true, document.getElementById("searchForm2"));
    }
    
    if(document.getElementById("ext_search_form") != null) {
    	var tmp = $('#ext_search_form').clone();
    	$('#ext_search_form').remove();
    	tmp.insertAfter('#searchForm  table.gsc-branding')
    	initExtSearch();
    }
    
    if(document.getElementById("searchForm_cloneButton") != null) {
    	$('#searchForm_cloneButton').html($("#searchForm input.gsc-search-button").clone());
    }
    
    
    $('#hiddenfields .exsearch-link').clone().insertAfter('table.gsc-branding');
    $('#hiddenfields').remove()
    $('input.gsc-input').removeAttr('name'); 
    $('form.gsc-search-box').attr('action',"search.php");
    
    $('#searchForm input.gsc-input').focus();
    $("#searchForm input.gsc-input").setCursorPosition( $("#searchForm input.gsc-input").val().length );
    
    if($('body#exsearch #as_q').length == 1) {
	    $('body#exsearch #as_q').focus();
	    $('body#exsearch #as_q').setCursorPosition($('body#exsearch #as_q').val().length );
    }
    
    if(getCompleteQuerystring() == "" || document.getElementById("ext_search_form"))
    	return;
    
    var leftScDiv2 = document.getElementById("searchArea2");
    this.searchControl2 	= new google.search.SearchControl();
    this.searchControl2.setSearchCompleteCallback(this,cse.prototype.onSearchComplete);
    this.searchControl2.setResultSetSize(google.search.Search.LARGE_RESULTSET);
    this.searchControl2.setLinkTarget(google.search.Search.LINK_TARGET_SELF);
    
    var options = new google.search.SearcherOptions();
    options.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);
    this.searcher = new google.search.WebSearch();
    this.searcher.setUserDefinedLabel('Browser-Fix');
	
    if($("#site").val() != "") 
		this.searcher.setSiteRestriction($("#site").val());
	
    this.searcher.setRestriction(google.search.Search.RESTRICT_EXTENDED_ARGS, { "lr" : "lang_"+$("#lang").val() });
	this.searcher.setRestriction(google.search.Search.RESTRICT_EXTENDED_ARGS, { "filter" : "0" });
    
	this.searchControl2.addSearcher(this.searcher, options);
   
    var drawOptions = new google.search.DrawOptions();
    drawOptions.setDrawMode(GSearchControl.DRAW_MODE_TABBED);
    
    this.searchControl2.draw(leftScDiv2, drawOptions);
    this.searchControl2.execute(getCompleteQuerystring());
}

cse.prototype.onSearchComplete = function (a, response) {
	var query = $('#vquery').val();
	
    this.searchControl 	= new google.search.CustomSearchControl('partner-pub-6219185998865271:dit9tm-ieqv');
    this.searchControl.setResultSetSize(google.search.Search.LARGE_RESULTSET);
    this.searchControl.draw(document.getElementById("searchArea1"));
    this.searchControl.execute(getCompleteQuerystring());
	
	if(response.cursor)
	{
		var formated_value = $().number_format(response.cursor.estimatedResultCount, {
			     numberOfDecimals:0,
			     decimalSeparator: ',',
			     thousandSeparator: '.'}); 
		  
		if(response.cursor.estimatedResultCount > 1)
			$('#resultcount-counter').html(formated_value+"&nbsp;");
		  
		$('#resultcount-counter').show();
		  
		  $('.gs-visibleUrl-long').each(function(index) {
			  try { this.innerHTML = decodeURIComponent($(this).html()); }
			  catch(error) {}
		  });
		  
		  if(response.cursor.pages.length > 1)
		  {
			  $('.gsc-cursor-box').show();
			  if(response.cursor.currentPageIndex > 0)
				  $('<div class="gsc-cursor-prev">Prev</div>').insertBefore('.gsc-cursor');
			  if(response.cursor.currentPageIndex < (response.cursor.pages.length - 1) )
				  $('<div class="gsc-cursor-next">Next</div>').insertAfter('.gsc-cursor');
			  prev = response.cursor.currentPageIndex-1;
			  next = response.cursor.currentPageIndex+1;
			  
			//if(this.searchControl.ob)
			  //this.searchControl.ob.gotoPage(response.cursor.currentPageIndex);
			  
			  $('.gsc-cursor-box').show();
			  $('#searchForm2').show();
		  }
		  
		  $('#searchArea2').show();
		  
		  url = "cloud.php?query="+$("#leftsidequery").val()+"&rand="+new Date().getTime();
		  //url = "cloud.php?query="+query+"&lang="+escape($('#lang').val())"&zip="+escape($('#zip').val())+"&town="+escape($('#town').val())+"&country="+escape($('#country').val())+"&basezip="+escape($('#basezip').val())+"&basetown="+$('#basetown').val()+"&site="+$('#site').val()+"&as_filetype="+$('#as_filetype').val()+"&rand="+new Date().getTime();
		  $.get(url, function(data) {
			  $('#tagcloud-content').html(data);
			  if($('#tagcloud-content').html() != '')
				  $('#tagcloud-wrapper').show();
			});
		  
		if($('#as_filetype').val() != "") {
			$("<span class='filetype'>["+$('#as_filetype').val()+"]</span>").insertBefore('div.gs-title');
	    }
		
		if($('#town').val() != '' ) {
		  var sc = new RawSearchControl();
		  sc.searcher.execute(getCompleteQuerystring());
		  $('.localsearch').html("in "+ $('#zip').val() + " " + $('#town').val().slice(0,1).toUpperCase() + $('#town').val().slice(1));
		}
	}
	else
	{
		$('#noResultMsg').show();
	}

	$('.gsc-input').val(getCompleteQuerystring(true));
	$('.searchword').html($('#leftsidequery').val().slice(0,1).toUpperCase() + $('#leftsidequery').val().slice(1));
	$('#resultcount').show(); 
	
	return false;
}  





