var test_div = document.createElement('div');
test_div.setAttribute('className', 'ignore_test');
var classText = (test_div.className) ? 'className' : 'class';

function ClearSearch(field) {
	if(field.value == "search site...") {
		field.value = "";
	}
}

function PopulateSearch(field) {
	if(field.value == "") {
		field.value = "search site...";
	}
}

function ClearEalert(field) {
	if(field.value == "Your email address") {
		field.value = "";
	}
}

function PopulateEalert(field) {
	if(field.value == "") {
		field.value = "Your email address";
	}
}

function ShowHideReviewForm() {
	GetCurrentState = document.getElementById('ReviewForm').style.display;
	if(GetCurrentState == "none") {
		document.getElementById('ReviewForm').style.display = "";
	} else {
		document.getElementById('ReviewForm').style.display = "none";
	}
}

function checkGenericForm() {
	AlertMessage = "";
	if(document.getElementById('your_name').value == "") {
		AlertMessage += "\nPlease enter your NAME.";
	}
	if(document.getElementById('your_email').value == "") {
		AlertMessage += "\nPlease enter your EMAIL ADDRESS.";
	}
	if(document.getElementById('your_review').value == "") {
		AlertMessage += "\nPlease enter your REVIEW.";
	}
	if(AlertMessage == "") {
		return true;
	} else {
		alert("Sorry, your form is not complete:" + AlertMessage);
		return false;
	}
}

function newsPrint(id) {
	urlToOpen = '/news_print.php?show_story=' + id;
	open(urlToOpen);
}

function display_audio(aud_uid, aud_width, aud_height) {
	var so = new SWFObject('/data/mediaplayer.swf','download_audio_object',aud_width,aud_height,'8','#000');
	so.addParam('allowscriptaccess','always');
	so.addParam('wmode','transparent');
	so.addParam('quality','best');
	so.addParam('menu','false');
	so.addVariable('file','http://s206619174.websitehome.co.uk/ata/' + aud_uid + '.mp3');
	so.addVariable('autostart','true');
	so.addVariable('callback','analytics');
	so.addVariable('backcolor','0x267eb2');
	so.addVariable('frontcolor','0xdeeff8');
	so.addVariable('lightcolor','0xffffff');
	so.write('audio_container');
}

function safariResizeFix(obj) {
	var test_div_resize = document.createElement('div');
	test_div_resize.style.resize = "none";
	if(test_div_resize.style.resize) {
		document.getElementById(obj).style.resize = "none";
	}
}

function calcage(LeadingZero, secs, num1, num2) {
	s = ((Math.floor(secs/num1))%num2).toString();
	if (LeadingZero && s.length < 2)
		s = "0" + s;
	return s;
}

function CountBack(secs, holderId, LeadingZero, DisplayFormat, FinishMessage, SetTimeOutPeriod) {
	if (secs < 0) {
		document.getElementById(holderId).innerHTML = FinishMessage;
		return;
	}
	DisplayStr = DisplayFormat.replace(/%%D%%/g, calcage(LeadingZero, secs,86400,100000));
	DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(LeadingZero, secs,3600,24));
	DisplayStr = DisplayStr.replace(/%%M%%/g, calcage(LeadingZero, secs,60,60));
	DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(LeadingZero, secs,1,60));

	document.getElementById(holderId).innerHTML = DisplayStr;
	if (CountActive)
		setTimeout("CountBack(" + (secs+CountStepper) + ", '" + holderId + "', " + LeadingZero + ", '" + DisplayFormat + "', '" + FinishMessage + "', " + SetTimeOutPeriod + ")", SetTimeOutPeriod);
}

function showCountdown(holderId, TargetDate, LeadingZero, DisplayFormat, FinishMessage) {
	CountStepper = -1;
	CountActive = true;
	var SetTimeOutPeriod = (Math.abs(CountStepper)-1)*1000 + 990;
	var dthen = new Date(TargetDate);
	var dnow = new Date();
	if(CountStepper>0)
		ddiff = new Date(dnow-dthen);
	else
		ddiff = new Date(dthen-dnow);
	gsecs = Math.floor(ddiff.valueOf()/1000);
	CountBack(gsecs, holderId, LeadingZero, DisplayFormat, FinishMessage, SetTimeOutPeriod);
}

// Library
ATA = function(){};

(function() {
	
    this.ns = function(fn){
        var ns = {};
        fn.apply(ns);
        return ns;
    };
}).apply(ATA);





ATA.ns(function() {with(ATA) {
	Carousel = Class.create({
		initialize: function(containerId, options) {
			this.setOptions(options);
			
			this.currentIndex = 0;
			this.liSize = 0;
			this.animDone = true;
			
			this.containerId = containerId;
	
			this.updateCount();
			
			//this.buttonLeft = $(this.options.leftId);
			//this.buttonRight = $(this.options.rightId);
		
			//this.buttonLeft.observe('click', this.moveLeft.bindAsEventListener(this));
			//this.buttonRight.observe('click', this.moveRight.bindAsEventListener(this));

			if(this.options.show_selected)
				this._show_selected();
			else			
				this.moveLeft(); // force the left button to start disabled

            this.automove(8500);
		},

        automove: function(timeout){
            var obj = this;

            clearTimeout(obj.current_timer);

            if(!timeout)
                var timeout = 6000;

            if(obj.currentIndex + 1 == obj.listCount){
                obj.current_timer = setTimeout(function() {obj.allLeft();}, timeout);
            }
            else {
                obj.current_timer = setTimeout(function() {obj.moveRight();}, timeout);
            }
        },
		
		updateCount: function() {
			this.ul = $$('#' + this.containerId + ' ul')[0];
			this.listCount = this.ul.getElementsByTagName("li").length;
			
			if(this.listCount < this.options.numVisible) {
				this.options.numVisible = this.listCount;
			}
		},
		
		moveRight: function() {
	
			if (this.animDone != true) 
				return false;
			
			if (this.currentIndex + this.options.numVisible + this.options.increment <= this.listCount)
				this._scroll(-this.options.increment);
			else 
				this._scroll(-(this.listCount - (this.currentIndex + this.options.numVisible)));
			
			if(this.listCount == this.currentIndex + this.options.numVisible)
				Element.addClassName(this.buttonRight, this.options.disabledClass);
			
			Element.removeClassName(this.buttonLeft, this.options.disabledClass);

            this.automove();

		},
		
		moveLeft: function() {
			if (this.animDone != true)
				return false;
			
			var inc = this.options.increment;
	
			if (this.currentIndex - inc < 0)
				inc = this.currentIndex;
			
			this._scroll(inc);
			
			if (this.currentIndex == 0)
				Element.addClassName(this.buttonLeft, this.options.disabledClass);
			
			Element.removeClassName(this.buttonRight, this.options.disabledClass);

            this.automove();
			
		},
		
		allRight: function() {
			if (this.animDone != true)
				return false;
		
			var inc = this.listCount - this.currentIndex - this.options.numVisible;
			this._scroll(-inc);
			
			Element.addClassName(this.buttonRight, this.options.disabledClass);
			Element.removeClassName(this.buttonLeft, this.options.disabledClass);

            this.automove();
		},
		
		allLeft: function() {
			if (this.animDone != true)
				return false;
		
			this._scroll(this.currentIndex, true);

            this.automove(200);
		},
		
		_show_selected: function() {

			var selected = this.ul.select('.' + this.options.selected_class)[0];
			
			var index = this.ul.childElements().indexOf(selected);
			
			var offset = this.options.show_offset;
			
			if(index > offset){

				if((this.listCount - index) < offset)
					offset = this.options.numVisible - (this.listCount - index);

				if(offset > 0)
					index = -index + offset;

				if (index + this.options.numVisible > this.listCount)
					index = this.listCount - this.options.numVisible;
				
				if (index < 0)
					index = 0;
				
				this._scroll(-1 * index);

			}
		},
		
		_scroll: function(delta, effect_duration) { 
			this.animDone = false;

            if(!effect_duration)
                var effect_duration = 0.5;
            else
                var effect_duration = 0.0
			
			if(this.liSize <= 0) 
				this.liSize = this._getLiElementSize();

            new Effect.MoveBy(this.ul, 0, delta * this.liSize, {duration: effect_duration, afterFinish: function() {this.animDone = true}.bind(this)});
    
            this.currentIndex -= delta;

            this._select_current_nav_item(this.currentIndex);
	
		},
		
		_getLiElementSize: function() {
			var li = $(this.ul.getElementsByTagName("li")[0]);
			return li.getDimensions().width + parseFloat(li.getStyle("margin-left")) + parseFloat(li.getStyle("margin-right"));
		},

        moveTo: function(list_position){
			if (this.animDone != true)
				return false;

            var delta = this.currentIndex - list_position;

            //this will happen if we're at the last element (which is fake)
            if(delta == this.listCount){
                list_position = 0;
                delta = this.currentIndex - this.listCount;
            }

            this._scroll(delta);

            this.automove();

        },

        _select_current_nav_item: function(current_list_position) {

            if(current_list_position + 1 == this.listCount)
                current_list_position = "0";

            $$('.carousel_nav li').each(function(nav_item) {
                nav_item.removeClassName('carousel_nav_s');
                nav_item.addClassName('carousel_nav_ns');
            });

            $('carousel_nav_pos_' + current_list_position).removeClassName('carousel_nav_ns');
            $('carousel_nav_pos_' + current_list_position).addClassName('carousel_nav_s');
        },
	
		setOptions: function (options) {
			this.options = {
				numVisible: 1,
				slider_element: 'ul',
				increment: 1,
				leftId: 'slide_left',
				rightId: 'slide_right',
				disabledClass: 'slide_disabled',
				show_selected: false,
				selected_class: 'selected',
				show_offset: 0,
                instanceName: 'slideGallery' 
			};
			Object.extend(this.options, options || {});
		}
	});
}});