﻿//init.js
//2008 Paul Graves paul.graves@nemisys.uk.com
//Initialisation of nemJS docks and widgets
//You can build your own version of this file if required

window.addEvent('domready', function() { initialPageLoad(); });

var _nemWidgetPanel;
var nemWidgets, nemSuggests;
var pageLoadFunctions = new Array();

function initialPageLoad() {
	nemWidgets = new Array();
	nemSuggests = new Array();
	$$("body").initNemWidgetPanel();
	$$("body").initWidgets();

	for (var i = 0; i < pageLoadFunctions.length; i++) {
		pageLoadFunctions[i]();
	}
}

function addPageLoadFunction(func) {
	pageLoadFunctions.push(func);
}

//show and hideBox shows and hides boxes and set the cookie
Element.implement({
	hide: function() {
		this.style.display = 'none';
		// return this;
	},
	show: function() {
		this.style.display = '';
		// return this;
	},
	toggle: function() {
		this.style.display == 'none' ? this.style.display = '' : this.style.display = 'none';
		//  return this;
	},
	//slides an element in and out, also fixes the height issue bug
	toggleSlide: function() {
		this.set('slide', { onComplete: (function() { this.removeClass('nemHidden'); this.removeHeight(); if (_nemWidgetPanel) _nemWidgetPanel.updateCookie(); }).bind(this) });
		this.slide('toggle');
	},
	//slides an element in and out, also fixes the height issue bug using the given duration
	toggleSlide: function(durationIn) {
		this.set('slide', { duration: durationIn * 1000, onComplete: (function() { this.removeClass('nemHidden'); this.removeHeight(); if (_nemWidgetPanel) _nemWidgetPanel.updateCookie(); }).bind(this) });
		this.slide('toggle');
	},
	//slides an element out but with no transition (required if you want to slide it back in later)
	toggleNoSlide: function() {
		this.set('slide', { duration: 10 });
		this.slide('hide');
	},
	//fixes a bug which sets the wrong height on an element on update
	removeHeight: function() {
		if (this.parentNode.style.height != '0px')
			this.parentNode.style.height = '';
	},
	ajax: function(location, transition) {
		var updateRequest = new Request({ method: 'get', async: true, url: location, onSuccess: (function(txt) {
			if (transition == 'slide') {//different transitions to occur when updating the element
				var slide = new Fx.Slide(this)
				slide.toggle().chain((function() { this.set('html', txt); this.initWidgets(); this.initNemWidgetPanel(); slide.toggle(); }).bind(this));
			} else if (transition == 'fade') {
				var fade = new Fx.Tween(this, { property: 'opacity' });
				fade.start(1, 0).chain((function() { this.set('html', txt); this.initWidgets(); this.initNemWidgetPanel(); fade.start(0, 1); }).bind(this));

			} else {
				this.set('html', txt); this.initWidgets(); this.initNemWidgetPanel();
			}

		}).bind(this),
			onFailure: (function(txt) { this.set('html', 'There was an error loading the content'); }).bind(this)
		});
		updateRequest.send("date=" + Date());
	},
	//re-initialises any widgets within the element
	//NEW WIDGETS SHOULD BE ADDED TO THIS FUNCTION OR THEY WILL NOT BE INITIALISED
	initWidgets: function() {
		//do nemExpandables
		this.getElements('.nemExpandable').each(
                                function(el, i) {
                                	var tempExpandable = new nemExpandable(el);
                                }
                                );
		//do nemTabs
		this.getElements('.nemTabs').each(
                                function(el, i) {
                                	var tempTab = new nemTabs(el);
                                }
                                );
		//do nemRotators
		this.getElements('.nemRotator').each(
                                function(el, i) {
                                	var tempRotator = new nemRotator(el);
                                }
                                );
		//do nemSuggests
		this.getElements('.nemSuggest').each(
                                function(el, i) {
                                	var tempSuggest = new nemSuggest(el);
                                }
                                );
		//do nemScrollboxes   
		this.getElements('.nemScrollbox').each(
                                function(el, i) {
                                	;
                                	var tempScrollbox = new nemScrollbox(el);
                                }
                                );
		//do nemTickers 
		this.getElements('.nemTicker').each(
                                function(el, i) {
                                	;
                                	var tempTicker = new nemTicker(el);
                                }
                                );
		//do nemScrollGallery 
		this.getElements('.nemScrollGallery').each(
                                function(el, i) {
                                	;
                                	var tempScrollGallery = new nemScrollGallery(el);
                                }
                                );
		//do nemToolTips
		this.getElements('.nemTooltip').each(
                                function(el, i) {
                                	;
                                	var tempToolTip = new nemTooltip(el);
                                }
                                );
	},
	initNemWidgetPanel: function() {
		_nemWidgetPanel = $('nemWidgetPanel');
		if (_nemWidgetPanel) {
			_nemCookie = false;

			if (Cookie.read("nemWidgetCookie")) {
				_nemCookie = Cookie.read("nemWidgetCookie").split(",");
				Cookie.dispose(_nemCookie);
			}

			var _cols = ['#nemWidgetPanel div.nemColumnLeft', '#nemWidgetPanel div.nemColumnRight'];
			_nemWidgetPanel = new nemWidgetPanel($('nemWidgetPanel'), _cols, '.nemDraggable', '.nemWidgetHandle', _nemCookie);
		}
	}
});
