/*
 * AZ Belgium site-specific JavaScript functions
 * */

var AZBelgium = function(){
	return {
		rotateHomePageFeature : function(){
			//this essentially simluates a click on each of the feature panel links in turn
			//goes back to the first one when it reaches the end
			if(undefined === $$('#hero .features .pagination ul .selected')[0]){
				//cannot find the currently selected feature: abort
				return;
			}	
			//this is the currently selected feature
			var el = $$('#hero .features .pagination ul .selected')[0];
			
			var linkToClick;	
			//go up to the li, then along to the next li, then down to the anchor
			if (undefined !== el.next('li')){
				//get the next one
				linkToClick = el.next('li').down(0);
			}else{
				//get the first one
				//go up to the parent ul 
				//then select the first child (li) 
				//then the first child of that (a)
				linkToClick = el.up(0).firstDescendant().firstDescendant();
			}	
			
			//this is where the response will be written
			var nodeToReplace;
			//find the containing features div
			$$('.features').each(function(features){
				if(el.descendantOf(features)){
					nodeToReplace = features;
				}	
			});
			
			if(undefined === nodeToReplace){
				//cannot find where to direct response to: abort
				return;
			}
			var params = {};
			//link querystring format is 
			//?9299766page=1
			//and for some reason the requestItemKey is present as well
			//on querystrings in the response to an AJAX request
			//like this
			//?9299766page=1&amp;requestItemKey=1234567
			//horrible format for QS
			var qs = linkToClick.search;
			//strip out the ?
			qs = qs.replace(/\?/,'');
			//and the requestItemKey (or anything else after the &)
			qs = qs.replace(/&.+/,'');
			var qsList = qs.split('=');
			//this will reproduce 9299766page=1
			params[qsList[0]] = qsList[1]; 
			params.panelItemId = qs.replace(/page.+/,'');
			params.ajaxSource = true; //to fool app into thinking this is a Spring AJAX request
			params.tileRefresh = true; 
			params.tileDefinition = 'tiles.panel.decorator';

			//find the requestItemKey from the HTML
			var requestItemKey = $('home_page_3').firstDescendant().id.replace('itemid','');
			params.requestItemKey = requestItemKey;

			//would be better to have converted this panel to use prototype AJAX 
			var updater = new Ajax.Request('/',{
				method: 'get',
				parameters: params,
				onSuccess: function(response){
					nodeToReplace.replace(response.responseText);
				}
			});
			
		},
		setUpRotator : function(){
			//attaches the feature rotator to any page with a hero div containing features 
			$$('#hero .features .pagination ul .selected').each(function(el){
				//assumption: there will only be one of these
				//call this function every x seconds
				var frequency = 8;
				var executer = new PeriodicalExecuter(AZBelgium.rotateHomePageFeature, frequency);
			});
			
		}
	};
}();

var toCall = new Array();
toCall[0] = AZBelgium.setUpRotator;
