/**
 * Prototypes for Ads part
 */

	leftAdsCount		= 9;
	topAdsCount			= 2;
	bottomAdsCount		= 1;

//Prototype for ads request
	var adsRequest = function(query){

	//Constructor
		this.query			= query;
		this.region			= 'uk';
		this.adsEngine		= 'Yahoo';
		this.adsUrl;
		this.linkArgs;

	//Abstract methods
		this.onRequestReceived = new Function();

	//Public methods

		//Prepare query to searchWebService as xml string
		this.prepareQuery = function(){
			var sendQuery = this.query;

			if (sendQuery.length > 0) {
				sendQuery = sendQuery.replace(/&/g, '&amp;');
				sendQuery = sendQuery.replace(/</g, '&lt;');
				sendQuery = sendQuery.replace(/>/g, '&gt;');
				sendQuery = sendQuery.replace(/\\/g, '\\\\');
			};

			var xmlQuery	= '<searchQuery>'
							+ '<query>'+sendQuery+'</query>'
							+ '<language>'+this.region+'</language>'
							+ '<adsEngine>'+this.adsEngine+'</adsEngine>'
							+ '<count>'+this.count+'</count>'
							+ '<linkArgs><![CDATA['+this.linkArgs+']]></linkArgs>'
							+ '</searchQuery>';
			return xmlQuery;
		};

		//Send query to searchWebService via HTTP/POST
		this.sendQuery = function() {
			var adsString = this.prepareQuery();
			$.post(this.adsUrl, {query: adsString}, this.onRequestReceived);
		};

		//Import from xml string
		this.importFromXml = function(xml) {
			this.query			= $('query', xml).text();
			this.region			= $('language', xml).text();
			this.adsEngine		= $('adsEngine', xml).text();
			this.count			= $('count', xml).text();
		};
	};

//Prototype for ads response
	var adsResponse = function(xml){

	//Constructor
		this.totalHits 		= 0;
		this.time			= $('time', xml).text();

		this.adsQuery		= new adsRequest();
		this.adsQuery.importFromXml( $('searchQuery', xml) );

		this.resultsList	= new Array();
		var resultsList 	= this.resultsList;

		var results			= $('responseResults', xml);
		var i=0;

		$('result', xml).each(function() {
			resultsList[i] = new Object();
			resultsList[i].abstract		= $('abstract', this).text();
			resultsList[i].clickUrl		= $('clickUrl', this).text();
			resultsList[i].title		= $('title', this).text();
			resultsList[i].url			= $('url', this).text();
			resultsList[i].idDoc		= $('idDoc', this).text();
			i++;
		});

		this.totalHits = i;

		this.prevArgs = $('prevArgs', xml).text();
		this.nextArgs = $('nextArgs', xml).text();
	};

//Prototype for search container
	var adsContainer = function(adsResponseObjectsArray) {

	//Constructor
		this.adsResponseArray	= adsResponseObjectsArray;
		this.displayedAds		= 0;

		this.leftAdsContainer	= '';
		this.topAdsContainer	= '';
		this.bottomAdsContainer	= '';

		this.leftAdsCount		= leftAdsCount;
		this.topAdsCount		= topAdsCount;
		this.bottomAdsCount		= bottomAdsCount;

	//Public methods

		//Prepare search header
		this.prepareAdsResultsHtml = function (){
			var t = 0;

			for (var k = 0; k < this.adsResponseArray.length; k++) {

				for (var i = 0; i < this.adsResponseArray[k].resultsList.length; i++) {

					var resultLine = this.adsResponseArray[k].resultsList[i];

					//var siteIcon = '<div style="width: 16px; height: 16px; float: left;"><img src="favicons/?faviconUrl=http://'+resultLine.url+'" alt="" /></div>';
					var siteIcon = '<div style="width: 16px; height: 16px; float: left;"><img src="favicons/html/img/default_favicon.png" alt="" /></div>';
					var adsResultHtml = new String();
					adsResultHtml += '<div class="adsResultLine">';
					adsResultHtml += '<div id="adsResultTitle">';
					adsResultHtml += '%%SITE_ICON%%';
					adsResultHtml += '<a href="' + resultLine.clickUrl + '" title="' + resultLine.title + '" class="adsResultTitle" target="_blank">';
					adsResultHtml += resultLine.title;
					adsResultHtml += '</a>';
					adsResultHtml += '</div>';
					adsResultHtml += '<div class="adsResultAbstract">';
					adsResultHtml += resultLine.abstract;
					adsResultHtml += '</div>';
					adsResultHtml += '<div class="adsResultInfo">';
					adsResultHtml += '<span class="adsResultLink">' + resultLine.url + '</span> ';
					adsResultHtml += '</div>';
					adsResultHtml += '</div>';

					if (this.displayedAds<this.leftAdsCount+this.topAdsCount+this.bottomAdsCount) {

						if ((this.displayedAds<this.topAdsCount)&&(k==0)) {
							adsResultHtml = adsResultHtml.replace('%%SITE_ICON%%',siteIcon);
							this.topAdsContainer += adsResultHtml;
						}
						else if ((this.displayedAds<this.topAdsCount+this.bottomAdsCount)&&(k==0)) {
							adsResultHtml = adsResultHtml.replace('%%SITE_ICON%%',siteIcon);
							this.bottomAdsContainer += adsResultHtml;
						}
						else if (t<this.leftAdsCount) {
							adsResultHtml = adsResultHtml.replace('%%SITE_ICON%%','');
							this.leftAdsContainer += adsResultHtml;
							t++;
						};

					};
					this.displayedAds++;
				};
			};
			return true;
		};
	};