// JavaScript Document
$(document).ready(function(){
var config = {
	        siteURL     : GOOGLESEARCHPATH,  //'http://lineapelle.effettispeciali.com/',   //'http://www.lineapelle-fair.it',   // Change this to your site
	        searchSite  : true,
	        type        : 'web',
	        append      : false,
	        perPage     : 8,            // A maximum of 8 is allowed by Google
	        page        : 0             // The start page
}
		




$('#searchSite,#searchWeb').live('change',function(){
        // Listening for a click on one of the radio buttons.
	        // config.searchSite is either true or false.
	 
	        config.searchSite = this.id == 'searchSite';
			
			
			//alert(config.searchSite)
			
			
});


//form cercva dell'overheader
$('form#cercaOverHea').live('submit',function(){
							   
								   
	if(thisP !="S6"){ //non siamo nella pagina di risultato della ricerca
				
			var keyword= $('input#_keyword').attr('value');
			//alert(keyword)	
			var type= true  //setto io di default
			var strUrl=	'results.php?key='+keyword+'&type='+type;
			window.location.href=strUrl;
			//googleSearch(key, type);
			
			
			}else{ //siamo nella pagina di ricerca cancello proprio la form
			
			
			config.term=$('input#_keyword').attr('value');
	        $('input#searchSite').attr('checked',true);
						
			googleSearch();
	       			
			}
			
			 return false;
									   
								   
								   
})

						   



//form di ricerca interna
$('#searchForm').live('submit',function(){
										
			
			//chiudo la tendina se aperta
			$('ul.sb_dropdown').slideUp("slow");
			//riporto la freccetta in posizione iniziale di chiusura
			swapArrows(arrowDown,'down')
			
			//faccio location.href su poagina risultato
			//gli appendo via get il valore del campo di ricerca e il radio
			//quando apro la pagina nuova sull'onload chiamo gogleSearch
			
						
			if(thisP !="S6"){ //non siamo nella pagina di risultato della ricerca
				
			var keyword= $('input#_keywordInterna').attr('value');
			var type= config.searchSite  //true se la ricerca è nel sito false se su web
			var strUrl=	'results.php?key='+keyword+'&type='+type;
			window.location.href=strUrl;
			//googleSearch(key, type);
			
			
			}else{ //siamo nella pagina di ricerca e visualizzo in pagina
			
			config.term=$('input#_keywordInterna').attr('value');
	        $('input#searchSite').attr('checked',true);
						
			googleSearch();
	       			
			}
			
			 return false;
			
			
			
});


function googleSearch(settings){
	
	
	// If no parameters are supplied to the function,
	// it takes its defaults from the config object above:

	settings = $.extend({},config,settings);
	//alert(settings.siteURL)
	settings.term = settings.term || $('#_keywordInterna').val();
	
	
	if(settings.searchSite){
		// Using the Google site:example.com to limit the search to a
		// specific domain:
		settings.term = 'site:'+settings.siteURL+' '+settings.term;
		//alert(settings.term)
	}

	// URL of Google's AJAX search API
	var apiURL = 'http://ajax.googleapis.com/ajax/services/search/'+settings.type+
					'?v=1.0&callback=?';
	var resultsDiv = $('#resultsDiv');

	$.getJSON(apiURL,{
		q	: settings.term,
		rsz	: settings.perPage,
		start	: settings.page*settings.perPage
	},function(r){

		var results = r.responseData.results;
		$('#more').remove();

		if(results.length){

			// If results were returned, add them to a pageContainer div,
			// after which append them to the #resultsDiv:

			var pageContainer = $('<div>',{className:'pageContainer'});

			for(var i=0;i<results.length;i++){
				// Creating a new result object and firing its toString method:
				pageContainer.append(new result(results[i]) + '');
			}

			if(!settings.append){
				// This is executed when running a new search,
				// instead of clicking on the More button:
				resultsDiv.empty();
			}

			pageContainer.append('<div class="clear"></div>')
						 .hide().appendTo(resultsDiv)
						 .fadeIn('slow');

			var cursor = r.responseData.cursor;

			// Checking if there are more pages with results,
			// and deciding whether to show the More button:
			

			if( +cursor.estimatedResultCount > (settings.page+1)*settings.perPage){
				
				
				$('<div>',{id:'more'}).appendTo(resultsDiv).click(function(){
					googleSearch({append:true,page:settings.page+1});
					$(this).fadeOut();
				});
				//ES ADDON
				$('div#more').setImg_lang(b_more)
			}
		}
		else {

			// No results were found for this search.

			resultsDiv.empty();
			$('<p>',{
				className	: 'notFound',
				html		: '<p style="padding:40px;" class="align-center red1 bold bigFont12">Nessun risultato / No Results Were Found!</p>'
			}).hide().appendTo(resultsDiv).fadeIn();
		}
	});
}




		function result(r){

		// This is class definition. Object of this class are created for
		// each result. The markup is generated by the .toString() method.

		var arr = [];

		// GsearchResultClass is passed by the google API
		switch(r.GsearchResultClass){

			case 'GwebSearch':
				arr = [
					'<div class="webResult">',
					'<h2><a href="',r.url,'">',r.title,'</a></h2>',
					'<p>',r.content,'</p>',
					'<a href="',r.url,'">',r.visibleUrl,'</a>',
					'</div>'
				];
			break;
			case 'GimageSearch':
				arr = [
					'<div class="imageResult">',
					'<a href="',r.url,'" title="',r.titleNoFormatting,
					'" class="pic" style="width:',r.tbWidth,'px;height:',r.tbHeight,'px;">',
					'<img src="',r.tbUrl,'" width="',r.tbWidth,'" height="',
					r.tbHeight,'" /></a>','<div class="clear"></div>',
					'<a href="',r.originalContextUrl,'">',r.visibleUrl,'</a>',
					'</div>'
				];
			break;
			case 'GvideoSearch':
				arr = [
					'<div class="imageResult">',
					'<a href="',r.url,'" title="',r.titleNoFormatting,
					'" class="pic" style="width:150px;height:auto;">',
					'<img src="',r.tbUrl,'" width="100%" /></a>',
					'<div class="clear"></div>','<a href="',
					r.originalContextUrl,'">',r.publisher,'</a>',
					'</div>'
				];
			break;
			case 'GnewsSearch':
				arr = [
					'<div class="webResult">',
					'<h2><a href="',r.unescapedUrl,'">',r.title,'</a></h2>',
					'<p>',r.content,'</p>',
					'<a href="',r.unescapedUrl,'">',r.publisher,'</a>',
					'</div>'
				];
			break;
		}

		// The toString method.
		this.toString = function(){
			return arr.join('');
		}
	}
	
	
	
	
	
	
	
	////////////////////SITUAZIONE INIZILE se sono nella pagina di ricerca
	if(thisP=="S6"){
		
		
		
		//faccio override con le variabili che arrivano dalla ricerca da un'altra pagina
		var ty;
		if (type=="false"){ty=false}else{ty=true}
		
		config.searchSite=ty;
		config.term=key;
		//passo al campo il termine cercato
		//alert(key)
		setTimeout(function(){$('input#_keywordInterna').val(key)}, 500);
		
		googleSearch(config.term, config.searchSite);
		
		
	}
	
	
	
	
	
});

