﻿/*
This script helps managing history with ajax.
*/
Site.extend({
	initHistory:function () {
		dhtmlHistory.create({
			toJSON:JSON.encode,
			fromJSON:JSON.decode
		});
		$DL(function () {
			dhtmlHistory.initialize();

			if (typeof(Ads)!="undefined") {
				Ads.searchesHistory=[];
				Ads.addEvent("onSearching",function (searchTerms,searchTags,topRated,editorsChoice) {
					//dhtmlHistory.add("AdSearch",args);

					// argsKey contains a json string of all the argumetns sent from the search
					var argsKey=JSON.encode($A(arguments));
					
					// if this search already happened those args are stored in the Ads.searchesHistory array
					var searchIdx=Ads.searchesHistory.indexOf(argsKey);
					// if not found in the array store it and get the new index
					if (searchIdx==-1) {
						Ads.searchesHistory.push(argsKey);
						searchIdx=Ads.searchesHistory.length-1;
						//dhtmlHistory.add("AdSearch="+searchIdx,{key:"AdSearch",args:arguments});
						dhtmlHistory.add("Tags="+searchTerms+"&Terms="+searchTags+"&TR="+topRated+"&EC="+editorsChoice);
					}
				});
				Ads.addEvent("onShowing",function () {
					dhtmlHistory.add("AdSearch");
				});
			}
			if (typeof(FullAd)!="undefined") FullAd.addEvent("onOpening",function (adId) {
				//dhtmlHistory.add("ItemId="+adId,adId);
				dhtmlHistory.add("ItemName="+adId,adId);
			});

			dhtmlHistory.addListener(function (newLocation,historyData) {
				var method=null;

				var found=false;
				Site.historyManager.each(function (value,key) {
					if (found) return;
					var values=null;
					if (value.rx && (values=newLocation.match(value.rx))) {
						// String#match method returns at [0] the whole string itself. we don't need it so pull it out with Array#shift
						values.shift();
						// activate the method in historyManager related to the current action that needs be performed
						value.method(values,historyData);
					}
				});
			});
			
			var currHash=location.hash.substr(1);
			if (currHash) dhtmlHistory.fireHistoryEvent(currHash);
		}.create({delay:500}));
	},
	historyManager:new Hash({
		None:{
			rx:/^$/,
			method:function (args,historyData) {
				if (typeof(FullAd)!="undefined") FullAd.close();
				if (typeof(Ads)!="undefined") Ads.searchWithProperties();
			}
		},
		AdSearch:{
			rx:/^AdSearch(?:\=(.*))?$/,
			method:function (args,historyData) {
				if (typeof(FullAd)!="undefined") FullAd.close();
				if (historyData) Ads.search.apply(Ads,historyData.args);
				//Ads.searchWithProperties();
			}
		},
		AdSearchByUrl:{
			rx:/^Tags=(.*)&Terms=(.*)&TR=(.*)&EC=(.*)$/,
			method:function (args,historyData) {
				if (typeof(FullAd)!="undefined") FullAd.close();
				//if (historyData) Ads.search.apply(Ads,historyData.args);
				var terms = args[0].split(",");
				var tags = args[1].split(",");
				for(var c=0; c<tags.length; c++) if(isFinite(tags[c])) tags[c] = parseInt(tags[c]);
				Ads.search(terms,tags,args[2],args[3]);
				//Ads.searchWithProperties();
			}
		},
		FullAdById:{
			rx:/^ItemId=(.*)$/,
			method:function (args,historyData) {
				if(isNaN(args[0]))
					FullAd.openByName(args[0]);
				else
					FullAd.open(args[0]);
			}
		},
		FullAdByName:{
			rx:/^ItemName=(.*)$/,
			method:function (args,historyData) {
				FullAd.openByName(args[0]);
			}
		}
	})
});
Site.initHistory();
