﻿//****************************************************************
//* GOOGLE ADSENSE SEARCH
//****************************************************************

var GoogleSearch = 
(
	{
		SearchResultPage 	: {
						Protocol	: "http://",
						Site		: "digiarena.e15.cz",
						Page		: "/vysledky-vyhledavani"
					  },

		Settings 		: {
						cx		: "000816743487181314629:ghi5zaop9hq", //"partner-pub-2813022319849157:9c0yde48myl",
						cof		: "FORID:9",
						ie		: "UTF-8",
						sa		: "Hledat",
						Controls	: {
									input 	: "#search-web-text",
									button	: "#search-web > .input-button > a",
									qparam  : "q"
								  },
						ShowFullUrl	: false,
						Type		: 0, //0 - adsense iframe, 1 - back service
						ResultCount	: 20
					  },

		UrlEncode		: function(oUrl)
					  {
						oUrl = oUrl.toString();
						var output='';
						var i=0;
						var regex=/(^[a-zA-Z0-9_.]*)/;
						while(i<oUrl.length)
						{
							var match=regex.exec(oUrl.substr(i));
							if(match!=null && match.length>1 && match[1]!='')
							{
								output += match[1];
								i += match[1].length;
							}
							else
							{
								if(oUrl[i]==' ')
								{
									output+='+';
								}
								else
								{
									var charCode=oUrl.charCodeAt(i);
									var hexVal=charCode.toString(16);
									output+='%'+(hexVal.length<2 ? '0' : '' ) + hexVal.toUpperCase();
								}
								i++;
							}
						}
						return output;
					  },

		GetSearchUrl		: function(oSite)
					  {
						var oUrl = $(GoogleSearch.Settings.Controls.input).val();
						if(oUrl.indexOf(" site:")!=-1)
						{
							oUrl = oUrl.substring(0, oUrl.indexOf(" site:"));
						}
						return encodeURIComponent( oUrl) + " site:" + oSite;
					  },

		Search			: function()
					  {
						var oLocation =  GoogleSearch.SearchResultPage.Protocol + GoogleSearch.SearchResultPage.Site + GoogleSearch.SearchResultPage.Page;

						if(GoogleSearch.Settings.Type == 0)
						{
							if(GoogleSearch.Settings.ShowFullUrl)
							{
								oLocation += "?cx=" + GoogleSearch.UrlEncode(GoogleSearch.Settings.cx);
								oLocation += "&cof=" + GoogleSearch.UrlEncode(GoogleSearch.Settings.cof);
								oLocation += "&ie=" + GoogleSearch.Settings.ie;
								oLocation += "&sa=" + GoogleSearch.Settings.sa;
								oLocation += "&" + GoogleSearch.Settings.Controls.qparam + "=" + encodeURIComponent($(GoogleSearch.Settings.Controls.input).val()) + " site:" + GoogleSearch.SearchResultPage.Site;
							}
							else
							{
								oLocation += "?" + GoogleSearch.Settings.Controls.qparam + "=" + encodeURIComponent($(GoogleSearch.Settings.Controls.input).val()) + " site:" + GoogleSearch.SearchResultPage.Site;
							}
						}
						else
						{
								oLocation += "?" + GoogleSearch.Settings.Controls.qparam + "=" + encodeURIComponent($(GoogleSearch.Settings.Controls.input).val())  + "&start=0&c=" + GoogleSearch.Settings.ResultCount;
						}
						document.location = oLocation;
					  },

		Init			: function()
					  {
						$(document).ready(function(){
							$(GoogleSearch.Settings.Controls.input).focus(function(){
								var oThis = $(this);
								if(oThis.val()=="Vyhledat")
								{
									oThis.val("");	
								}
							}).css({"backgroundColor":"#FFFFFF", "backgroundImage":"none"});
							$.HandleEnter($(GoogleSearch.Settings.Controls.input), function(){
								$(GoogleSearch.Settings.Controls.button).click();
							});
							$(GoogleSearch.Settings.Controls.button).bind("click", function(){
								GoogleSearch.Search();
							});
						});
					  }
	}
);

GoogleSearch.Init();
