var FormElements				= {
	sActive : 'activeField',
	sInactive : 'inactiveField',
	sClearText : 'fieldClearText',
	
	addFunctions : function () {
		if(!document.getElementById||!document.createTextNode){return;}
		
		var aInputs = document.getElementsByTagName("input");
		for(var i=0; i < aInputs.length; i++){
			if(aInputs[i].type=='text'||aInputs[i].type=='password'){
				
				aInputs[i].onfocus = function() {
					DOMUtils.cssjs('remove', this, FormElements.sInactive);
					DOMUtils.cssjs('add', this, FormElements.sActive);
					
					if(DOMUtils.cssjs('check', this, FormElements.sClearText)){
						this.value = '';
					}
				}
				aInputs[i].onblur = function() {
					DOMUtils.cssjs('remove', this, FormElements.sActive);
					DOMUtils.cssjs('add', this, FormElements.sInactive);
				}
			}		
		}
	}
}

// search boxes

$(document).ready(function(){
// sets the initial value for search phrase field, then removes text on focus
		var sDefaultPhrase				= 'Search for...';
		
		$("#frmNewsSearch--sSearchPhrase").val(sDefaultPhrase).focus(function () {
		 if (this.value === sDefaultPhrase) {
		  this.value    = '';
		 }
		});
});

var SiteSearch							= {
	sDefaultPhrase : 'Search for...',
	
	init:function() {
		if(!document.getElementById||!document.createTextNode){return;}
		var eQuickSearchForm			= document.getElementById('frmNewsSearch');
		var eSearchForm					= document.getElementById('frmSearch');
		
		if (eQuickSearchForm) {
			eQuickSearchForm.onsubmit	= function() {return SiteSearch.submitSiteSearch(eQuickSearchForm);};
		}
		
		if (eSearchForm) {
			eSearchForm.onsubmit		= function() {return SiteSearch.submitSiteSearch(eSearchForm);};
		}
	},
	
	submitSiteSearch:function(eForm) {
		var reSearchPhrase				= new RegExp("/");
		var sSearchLocation				= eForm.action;
		var sPhrase						= eForm.sSearchPhrase.value;
		if (eForm.sSearchType) {
			sSearchLocation				= eForm.sSearchType[eForm.sSearchType.selectedIndex].value;
		}
		
		// make sure not searching on the default phrase
		if (sPhrase === SiteSearch.sDefaultPhrase) {
			sPhrase						= '';
		} 
		// make sure phrase is escaped and URL compatible
		else {
			sPhrase						= sPhrase.replace(reSearchPhrase," ");
			sPhrase						= escape(sPhrase);
		}
		
		if (sPhrase != '') {
			sSearchLocation				+= 'phrase/' + sPhrase + '/';
		}
		
		window.location					= sSearchLocation;
		return false;
	}
};

var NewsImageCaptionResizer				= {
	init : function() {
		if(!document.getElementById||!document.createTextNode){return;}
		var eImageHolder				= document.getElementById('articleImageHolder');
		var eImageCaption				= document.getElementById('imageCaption');
		if (!eImageHolder || !eImageCaption) {return;}
		var aImages						= eImageHolder.getElementsByTagName('img');
		if (aImages.length == 0) {return;}
		
		eImageCaption.style.width		= DOMUtils.getStyle(aImages[0], 'width');
		var nWidth						= eImageCaption.style.width.replace(/\D/, '');
		if (nWidth > 330){
			var eArticleBody				= document.getElementById('articleBody');
			if (eArticleBody) {
			DOMUtils.cssjs('add',eArticleBody, 'clear');
		}
		}
			
	}
}

WindowListener.add("load","FormElements.addFunctions()");
WindowListener.add("load","SiteSearch.init()");
WindowListener.add("load","NewsImageCaptionResizer.init()");

// jquery functions for the following effects:
// * rollover effect for promo items
// * tab switching for most read and most commented modules
// * calendar widget used across the site

$(document).ready(function(){
	
	// button rollover for all browsers   
		$('.button, .inputButton input').hover(
		function() {
		    $(this).addClass('hover');
		}, 
		function() {
		    $(this).removeClass('hover');
		});
	
		
	// Title, Text & Image promo in the xCol, rollover effect
		
		// sets the element that is the hover trigger
		var sPromoItem					= '#xcol .smallPromos .promoImageTitleText';
		
		// sets the child element of sPromoItem that is the popup
		var sPopupElement				= 'p'
		
		// sets the speed of the popup in milliseconds
		var nSpeed						= 800;
		
		var sPopup						= $(sPromoItem).find(sPopupElement);
		var bHover						= false;
		var sPopupAnchor				= $(sPopup).find('a');
		
		// hides the popup on page load and sets styling
		$(sPopup).hide().css({'position':'absolute','opacity':'0.8'});
		
		$(sPromoItem).hover(
		function() {
			// on mouseover
			if (!bHover) {
				$(sPopupElement, this).stop().css({'top': 'auto','position': 'absolute'}).show("slide",{direction: "up"}, nSpeed);
				bHover = true;
			}
		}, 
		function() {
			// on mouseout
			$(sPopupElement, this).stop().hide("slide", {direction: "up"}, nSpeed);
			bHover = false;
		});			
		
		
		// function for calendar widget as seen in http://jqueryui.com/demos/datepicker/
		$(function() {
		$("input.calendarField").datepicker({
			showOn: 'button', /* displays calendar icon */
			buttonImage: '/_images/calendar.gif', /* location of calendar icon */
			buttonImageOnly: true,
			dateFormat: 'dd/mm/yy', /* date format to use */
			changeMonth: true, /* provides drop down for month selection */
			changeYear: true /* provides drop down for year selection */
			});
		
		$('#disruptorInfo').hide();
		$('#closeDisruptor').hide();
		
		//Disable default href which is in place for non javascript users
		$(".inThisIssue").click(function() { return false; });
	
		
		$('.inThisIssue').click(function() {
			$('#disruptorInfo').fadeIn();
			$('#closeDisruptor').show();
			$('h2 a.inThisIssue').hide();
		});
		
		//Disable default href which is in place for non javascript users
		$("#closeDisruptor").click(function() { return false; });
		
		$('#closeDisruptor').click(function() {
			$('#disruptorInfo').fadeOut();
			$('#closeDisruptor').hide();
			$('h2 a.inThisIssue').show();
		});
	});

});