
if( google )
	google.load("search", "1");

function setCookie(name, value, expires, path, domain, secure) {
        var curCookie = name + "=" + escape(value) +
                ((expires) ? "; expires=" + expires.toGMTString() : "") +
                ((path) ? "; path=" + path : "") +
                ((domain) ? "; domain=" + domain : "") +
                ((secure) ? "; secure" : "")
        if ( (name + "=" + escape(value)).length <= 4000)
                document.cookie = curCookie
        else
                if (confirm("Cookie exeed 4KB size !"))
                        document.cookie = curCookie
}

function getCookie(name) {
        var prefix = name + "="
        var cookieStartIndex = document.cookie.indexOf(prefix)
        if (cookieStartIndex == -1)
                return null
        var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length)
        if (cookieEndIndex == -1)
                cookieEndIndex = document.cookie.length
        return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex))
}

function gup( name )
{
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
     var regex = new RegExp( regexS );
     var results = regex.exec( window.location.href );
     if( results == null )
        return "";
    else
	return results[1];
}
		    
		    
function init_search(){
	var g=google.search, 
	f=document.getElementById('customForm'),
	r=document.getElementById('searchResults'),
	b=document.getElementById('searchBranding'),
	ws=new g.WebSearch;
	ws.setSiteRestriction("004429252197412545519:e0xtjrjimwg");
  	ws.setResultSetSize(g.Search.LARGE_RESULTSET);
	//ws.setLinkTarget( g.Search.LINK_TARGET_SELF );
	//ws.setResultSetSize( 10 );
	
	//check if already got query
	var queryStr = decodeURIComponent( gup( "q" ) );
	/*var queryStr2 = getCookie( "search_query" );

	if( queryStr2 ) {
		f.q.value = queryStr2;
		ws.execute( queryStr2 );
	}*/
		
	if( queryStr ) {
	    //window.alert( "its query : " + queryStr );
		f.q.value = queryStr;
	    ws.execute( queryStr );
	}
	
	
		
	
  	f.onsubmit=function(e){
  		if( f.q.value ) {
	  		document.location.href = "http://www.promholod.com/search_f.php?q=" + encodeURIComponent( f.q.value );
	  	}

		//if(e.preventDefault){e.preventDefault();}
		
  	 	return false;
	}/*
  		
  		/*if( f.q.value ) {
  			setCookie( "search_query", f.q.value );
  		}
  		
		//no r - we are not at result page
		//document.location.href = "search_f.php?search_q=" + encodeURIComponent( f.q.value );
		if(!r) {
			//window.alert( document.location.href );
			//document.location.href = "http://www.promholod.com/search_f.php?q=" + encodeURIComponent( f.q.value );
			//document.location.href = "http://www.promholod.com/search_f.php?q=" + encodeURIComponent( f.q.value );
			//document.location.href = "http://www.promholod.com/search_f.php";
			
			return false;
		} else {
		    if( f.q.value ) {
			    setCookie( "search_query", f.q.value );
		    	ws.execute( f.q.value );
		    }
			
		    return false;
			
		}

		if(e.preventDefault){e.preventDefault();}
		
  	 	return false; 
  	}*/
	
	ws.setSearchCompleteCallback(ws,function(){
	
		res_elem = document.getElementById('res_text');
		
		//clear all previos results
		while( r.firstChild ){ r.removeChild(r.firstChild); }

		var e=this.results, c=this.cursor;
		
		if( c )
			var p=c.pages;
		else {
			res_elem.appendChild( createDiv( 'res_header', 'Не найдено ничего по слову "' + f.q.value + '". Попробуйте поиск с другим ключевым словом.') );
			return;
		}
		
		//r.appendChild( createDiv( 'res_header', 'Search results: ' +  p.length ) );
		res_elem.appendChild( createDiv( 'res_header', 'Найдено совпадений по слову "' + f.q.value + '" -  ' + e.length ) );
				
		for(var z=0;z<e.length;z++){
			var link_el = createDiv('gsc-result',e[z].html.cloneNode( true ) );
  			r.appendChild( link_el );
  		}
  		
  		/*if( p.length > 1 ) {
  			var pages=createDiv('pages','Страницы: ');
			
  			for(var z=0;z<p.length;z++){
  				var pa=p[z],
  				l=createDiv('page'+(c.currentPageIndex==z?' live':''),pa.label);
  				l.onclick=ab(this,z);
  				pages.appendChild(createDiv('page',l));
			}
			r.appendChild(pages);
		}*/
  	});
	
	function createDiv(a,c){
		var b=document.createElement('div');
		if(a){b.className=a;}
		var tOc=typeof(c);
		if(tOc.match(/^(string|number)$/i)){b.innerHTML=c;}
		else if(tOc!='undefined'){try{b.appendChild(c);}catch(e){}}
		return b;
	}
	
   function ab(a,b){
   	return function(){a.gotoPage(b);};
   }
   g.Search.getBranding(b);
  }
  
google.setOnLoadCallback(init_search);
//GSearch.setOnLoadCallback(init_search);
