﻿//****************************************************************
//* COMMON LIBRARY
//****************************************************************


//********************************************
jQuery.HandleEnter = function(element,callback)
{
	jQuery(element).bind($.browser.mozilla ? 'keypress' : 'keydown', function(event){
		var code=event.charCode || event.keyCode;
		if(code && code == 13) {
  			callback(event.target);
  			event.preventDefault();
		};
	});
};

jQuery.HandleClick = function(element,callback)
{
	jQuery(element).bind('click', function(event){
  		callback(event.target);
  		event.preventDefault();
	});

};


//********************************************
var GoogleSearch = 
(
	{
		SearchResultPage 	: "/vysledky-vyhledavani/sc-236/",

		Settings 		: {
						cx	: "partner-pub-2813022319849157:5pnzqe-15gt",
						cof	: "FORID:9",
						ie	: "UTF-8",
						sa	: "Hledat"
					  },

		ShowFullUrl		: false,

		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;
					  },

		Search			: function()
					  {
						var oLocation =  GoogleSearch.SearchResultPage;

						if(GoogleSearch.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 += "&q=" + encodeURIComponent($("input[name='q']").val());
						}
						else
						{
							oLocation += "?q=" + encodeURIComponent($("input[name='q']").val());
						}
						document.location = oLocation;
					  },

		Init			: function()
					  {
						$(document).ready(function(){
							$("input[name='q']").css(($("input[name='q']").val()=="") ? {background : "#FFFFFF url(http://www.google.cz/cse/intl/cs/images/google_custom_search_watermark.gif) no-repeat scroll 10px center"} : {background : "#FFFFFF"});
							$("input[name='q']").bind("focus", function(){
								$(this).css({background : "#FFFFFF"});
							});
							$.HandleClick($("#btnTxtSearch"), function(){
								GoogleSearch.Search();
							});
							$.HandleEnter($("input[name='q']"), function(){
								GoogleSearch.Search();
							});
						});
					  }
	}
);

GoogleSearch.Init();

