﻿/*
 *  i-TRAK javascript functions
 *  (c) 2008 ilutra systems ltd
 *
 *	Requires:
 *		prototype.js
 *
 */
 
itrak = {};

itrak.dom = {};

/**
 * ATTN: Needs to be made more efficient using object prototype to define methods
 */
itrak.dom.scroll = function(o, w, h, cont) {
	var self  = this; // reference to self for timer callback
	var _vwidth   = w; // viewport width
	var _vheight  = h; // viewport height
	var _twidth   = o.offsetWidth;   // total width
	var _theight  = o.offsetHeight;  // total height
	var _timer; // movement timer
	var _x, _y; // current position
	var _cont = cont;
	this.onScrollStart = function (){};
	this.onScrollStop  = function (){};
	this.onScroll      = function (){};
	this.scrollSpeed   = 30;
	
	//private functions
	function setPosition (x, y) {
	
		if (_cont) {
			// continuous scrolling 'ticker' mode
			// x scrolling
			if (x < 0 - _twidth) {
				x = _vwidth;
			}
			if (x > _vwidth) {
				x = _vwidth;
			}
			
			// y scrolling
			if (y < 0 - _theight) {
				y = _vheight;
			}
			if (y > _vheight) {
				y = _vheight;
			}
			
			_x = x;
			_y = y;
			
		}
		else {
			// discrete scroll mode
			// x scrolling
			if (x < _vwidth - _twidth) {
				x = _vwidth - _twidth;
			}
			if (x > 0) {
				x = 0;
			}
			
			// y scrolling
			if (y < _vheight - _theight) {
				y = _vheight - _theight;
			}
			
			if (y > 0) {
				y = 0;
			}
			
			_x = x;
			_y = y;
			
		}
		
		o.style.left = _x +"px";
		o.style.top  = _y +"px";
	};
	
	//public functions
	
	// Get/Set continuous mode
	this.continuous = function(cont) {
		if(!isUndefined(cont)) {
			_cont = cont ? true : false;
		}
		return _cont;
	}

	this.scrollBy = function (x, y) { 
		setPosition(_x - x, _y - y);
		this.onScroll();
	};
	
	this.scrollTo = function (x, y) { 
		setPosition(-x, -y);
		this.onScroll();
	};
	
	this.startScroll = function (x, y) {
		this.stopScroll();
		this.onScrollStart();
		_timer = window.setInterval(
			function () { self.scrollBy(x, y); }, this.scrollSpeed
		);
	};
		
	this.stopScroll  = function () { 
		if (_timer) window.clearInterval(_timer);
		this.onScrollStop();
	};
	
	this.reset = function () {
		_twidth  = o.offsetWidth
		_theight = o.offsetHeight;
		if (_cont) {
			_x = 0;
			_y = _vheight;
			
			o.style.left = "0px";
			o.style.top  = _y + "px";
		}
		else {
			_x = 0;
			_y = 0;
			
			o.style.left = "0px";
			o.style.top  = "0px";
		}		
		
	};
	
	this.swapContent = function (c, w, h) {
		o = c;
		var list = o.getElementsByTagName("div");
		for (var i = 0; i < list.length; i++) {
			if (list[i].className.indexOf("Scroller-Container") > -1) {
				o = list[i];
			}
		}
		
		if (w) _vwidth  = w;
		if (h) _vheight = h;
		reset();
	};
	
	this.getDimensions = function () {
		return {
			vwidth  : _vwidth,
			vheight : _vheight,
			twidth  : _twidth,
			theight : _theight,
			x : -_x, y : -_y
		};
	};
	
	this.getContent = function () {
		return o;
	};
	
	this.reset();
};





itrak.form = {};

itrak.form.textbox = {
	
	/**
	 * Creates a shadow message effect on the supplied text box.
	 * If the text box is empty or equals shadowMsg then text box's value
	 * is set to shadowMsg and the font colour made grey.
	 * When the user clicks in the box the message is cleared and font colour 
	 * set back full black.
	 * When a user exits the text box if the box contents is blank or equals
	 * shadowMsg then the value is returned to shaodwMsg in grey.
	 *
	 * @param (string) elid ID of the textbox element
	 * @origVal (string) shadow value to set blank text box to
	 */
	applyShadowMessage: function(elid, shadowMsg) {
		var el = $(elid);
		if(el.value == shadowMsg || el.value == "") {
			el.value = shadowMsg;
			el.style.color = "#bbbbbb"
		}
		else {
			el.style.color = "#000000"
		}
		// add event handlers
		Event.observe(el, "focus", function() {
			itrak.form.textbox.shadowMessageBox_onFocus(elid, shadowMsg); 
		});
		Event.observe(el, "blur", function() {
			itrak.form.textbox.shadowMessageBox_onBlur(elid, shadowMsg); 
		});
	},
	
	/**
	 * Clears the textbox of it's original value, if set, onFocus
	 * @param (string) elid ID of the textbox element
	 * @origVal (string) shadowMsg value to replace with blank string if textbox contains that value
	 */
	shadowMessageBox_onFocus: function(elid, shadowMsg) {
		var el = $(elid);
		if(el.value == shadowMsg) {
			el.value = "";
			el.style.color = "#000000";
		}
	},
	
	/**
	 * Adds back the shadow message if text field empty onBlur
	 * @param (string) elid ID of the textbox element
	 * @origVal (string) shadowMsg value to set replace if textbox does not contain a value
	 */
	shadowMessageBox_onBlur: function(elid, shadowMsg) {
		var el = $(elid);
		if(el.value == "") {
			el.value = shadowMsg;
			el.style.color = "#bbbbbb";
		}
	}

};

itrak.style = {
	
	/**
	 * Toggles the display style of the element between none and defaultStyle
	 * @param (string) elid ID of the element to toggle display of
	 * @param (string) defaultStyle default style e.g. "inline", "block"
	 */
	toggleDisplay: function(elid, defaultStyle) {
		var el = $(elid);
		if(el.style.display == "none") {
			el.style.display = defaultStyle;
		}
		else {
			el.style.display = "none";
		}
	},
	
	/**
	 * Displays the element using specified style
	 * @param (string) elid ID of the element to show
	 * @param (string) defaultStyle default style e.g. "inline", "block"
	 */
	show: function(elid, defaultStyle) {
		var el = $(elid);
		el.style.display = defaultStyle;
	},
	
	/**
	 * Hides the element (sets display style to none
	 * @param (string) elid ID of the element to hide
	 */
	hide: function(elid) {
		var el = $(elid);
		el.style.display = "none";
	}
	
	
};


itrak.shoppingBasket = {
	numItems: null,
	auiNumItems: null,
	subTotal: null,
	auiSubTotal: null,
	checkout: null,
	isInit: false,
	
	/**
	 * Initialises the shoppingbasket to be updatable
	 * @param (string) numItemsID ID of the element displaying number of items in basket
	 * @param (string) auiNumItemsID ID of the updating num items image
	 * @param (string) subTotalID ID of the element displaying basket currency
	 * @param (string) auiSubTotalID ID of the updating sub total image
	 * @param (string) checkoutID ID of the checkuot button element
	 */
	init: function(numItemsID, auiNumItemsID, subTotalID, auiSubTotalID, checkoutID) {
		itrak.shoppingBasket.numItems = $(numItemsID);
		itrak.shoppingBasket.auiNumItems = $(auiNumItemsID);
		itrak.shoppingBasket.subTotal = $(subTotalID);
		itrak.shoppingBasket.auiSubTotal = $(auiSubTotalID);
		itrak.shoppingBasket.checkout = $(checkoutID);
		
		itrak.shoppingBasket.numItems.style.display = "inline";
		itrak.shoppingBasket.auiNumItems.style.display = "none";
		itrak.shoppingBasket.subTotal.style.display = "inline";
		itrak.shoppingBasket.auiSubTotal.style.display = "none";
		isInit = true;
	},
	/**
	 * When called, performs an update via AJAX of the shopping basket
	 */
	update: function() {
		if(isInit) {
			itrak.shoppingBasket.numItems.style.display = "none";
			itrak.shoppingBasket.auiNumItems.style.display = "inline";
			itrak.shoppingBasket.subTotal.style.display = "none";
			itrak.shoppingBasket.auiSubTotal.style.display = "inline";
			ITRAK.Web.ShopWS.ShoppingCartSummary(itrak.shoppingBasket.updateComplete, itrak.shoppingBasket.updateError);
		}
		else {
			alert("Shopping Basket not initiliased");
		}
	},
	/**
	 * Handles shopping cart update AJAX callback
	 * @param (string) results string[2] array containing {numberOfItems, totalCostIncCurrencySymbol}
	 */
	updateComplete: function(results) {
		if (results != null) {
				itrak.shoppingBasket.numItems.update(results[0]);
				itrak.shoppingBasket.subTotal.update(results[1]);
				itrak.shoppingBasket.numItems.style.display = "inline";
				itrak.shoppingBasket.auiNumItems.style.display = "none";
				itrak.shoppingBasket.subTotal.style.display = "inline";
				itrak.shoppingBasket.auiSubTotal.style.display = "none";
				if(results[0] > 0 ){
					itrak.shoppingBasket.checkout.style.display = "inline";
				}
				else {
					itrak.shoppingBasket.checkout.style.display = "none";
				}
		}
	},
	/**
	 * Handles shopping cart update AJAX callback error.
	 */
	updateError: function() {
		itrak.shoppingBasket.numItems.update("???");
		itrak.shoppingBasket.subTotal.update("???");
	}
};
