/* #############################################################################

© Tesco.com 2008. All rights reserved.

NS:
	TESCODIRECT.lib.DOM.rect.Position.Animation.Carousel

dependancies:
	TESCODIRECT

History:
	July 2008	-  S. Harness	Created for Cross Sell Up Sell
	
############################################################################# */

TESCODIRECT.lib.DOM.rect = function(x, y, height, width, clientHeight, clientWidth) {
	this.VERSION = "1.0.2";
	this.NAME = "TESCODIRECT.lib.DOM.rect";

	this.x = x;
	this.y = y;
	this.height = height;
	this.width = width;
	this.clientHeight = clientHeight || height;
	this.clientWidth = clientWidth || width;
    //alert('x=' + this.x + ' y=' + this.y + ' h=' + this.height + ' w=' + this.width);
	this.intersects = function(rect) {
		return ((this.y - rect.y) < rect.height && (rect.y - this.y) < this.height) && ((this.x - rect.x) < rect.width && (rect.x - this.x) < this.width);
	}

	this.isSameSize = function(rect) {
		return (this.height == rect.height && this.width == rect.width);
	}

	this.isSamePosition = function(rect) {
		return (this.y == rect.y && this.x == rect.x);
	}

	this.isSameSizeAndPosition = function(rect) {
		return (this.isSameSize(rect) && this.isSamePosition(rect));
	}
}

//	Position object, derived from rectangle
TESCODIRECT.lib.DOM.rect.Position = function(rect, iFrame) {
	if (!rect) return;
	this.rect = rect;
	//this.rect.x = 100;
	//this.rect.y = 200;
	this.css = rect.style;
	
	TESCODIRECT.lib.DOM.rect.call(this,
		parseInt(this.css.left) || rect.offsetLeft,
		parseInt(this.css.top) || rect.offsetTop,
		rect.offsetHeight,
		rect.offsetWidth,
		rect.clientHeight,
		rect.clientWidth
	);
	this.VERSION = "1.0.0";
	this.NAME = "TESCO.system.DOM.rect.Position";
	/*@cc_on if (iFrame == true) {
		this.iFrame = new TESCO.system.DOM.rect.Position(document.createElement("IFRAME"), false);
		this.iFrame.rect.setAttribute("src", "about:blank");
		this.iFrame.css.position = "absolute";
		this.iFrame.hide();
		rect.parentNode.appendChild(this.iFrame.rect);
	} @*/
	this.v = TESCODIRECT.lib.DOM.node.getStyle(this.rect, "visibility");
	this.z = parseInt(TESCODIRECT.lib.DOM.node.getStyle(this.rect, "zIndex"), 10) || 0;
	TESCODIRECT.lib.DOM.rect.Position.zCeiling = Math.max(this.z, TESCODIRECT.lib.DOM.rect.Position.zCeiling);
	return this;
}

//	static property to record highest z index
TESCODIRECT.lib.DOM.rect.Position.zCeiling = 100;

TESCODIRECT.lib.DOM.rect.Position.prototype.raise = function() {
	this.z = this.css.zIndex = (TESCODIRECT.lib.DOM.rect.Position.zCeiling += 1);
}

TESCODIRECT.lib.DOM.rect.Position.prototype.resize = function(w, h) {
	/*@cc_on if (this.iFrame) this.iFrame.resize(w, h); @*/
	this.css.width =  (this.width = Math.max(0, w)) + TESCODIRECT.lib.browser.px;
	this.css.height = (this.height = Math.max(0, h)) + TESCODIRECT.lib.browser.px;
}

TESCODIRECT.lib.DOM.rect.Position.prototype.moveTo = function(x, y) {
	this.moveToX(x);
	this.moveToY(y);
}

TESCODIRECT.lib.DOM.rect.Position.prototype.moveToX = function(x) {
	/*@cc_on if (this.iFrame) this.iFrame.moveToX(x); @*/
	this.x = parseInt(this.css.left = x + TESCODIRECT.lib.browser.px);
}

TESCODIRECT.lib.DOM.rect.Position.prototype.moveToY = function(y) {
	/*@cc_on if (this.iFrame) this.iFrame.moveToY(y); @*/
	this.y = parseInt(this.css.top = y + TESCODIRECT.lib.browser.px);
}

TESCODIRECT.lib.DOM.rect.Position.prototype.moveBy = function(x, y) {
	this.moveTo(this.x + x, this.y + y);
}

TESCODIRECT.lib.DOM.rect.Position.prototype.getScroll = function() {
		var scrOfX = 0, scrOfY = 0;
		var element = this.rect;
		if( typeof( element.pageYOffset ) == 'number' ) {
			//Netscape compliant
			scrOfY = element.pageYOffset;
			scrOfX = element.pageXOffset;
		} else if( element.scrollLeft || element.scrollTop ) {
			//DOM compliant
			scrOfY = element.scrollTop;
			scrOfX = element.scrollLeft;
		}
		return [ scrOfX, scrOfY ];
	}

TESCODIRECT.lib.DOM.rect.Position.prototype.scrollBy = function(x, y) {
	var element = this.rect;
	
	//alert('SCROLLBY el scrleft scrtop=' + element.scrollLeft + ',' + element.scrollTop + ' scrollby ' + x + ',' + y);
	if( typeof( element.pageYOffset ) == 'number' ) {
			//Netscape compliant
			element.pageYOffset -= y;
			element.pageXOffset -= x;
		} else if( (element.scrollLeft != null) && (element.scrollTop != null) ) {
			//DOM compliant
			element.scrollTop -= y;
			element.scrollLeft -= x;
		}
}

TESCODIRECT.lib.DOM.rect.Position.prototype.scrollTo = function(x, y) {
	var element = this.rect;
	
	if( typeof( element.pageYOffset ) == 'number' ) {
			//Netscape compliant
			element.pageYOffset = y;
			element.pageXOffset = x;
		} else if( (element.scrollLeft != null) && (element.scrollTop != null) ) {
			//DOM compliant
			element.scrollTop = y;
			element.scrollLeft = x;
		}
}

TESCODIRECT.lib.DOM.rect.Position.prototype.show = function() {
	/*@cc_on if (this.iFrame) {
		this.iFrame.resize(this.rect.offsetWidth, this.rect.offsetHeight);
		this.iFrame.show();
	} @*/
	this.v = this.css.visibility = "visible";
}

TESCODIRECT.lib.DOM.rect.Position.prototype.hide = function() {
	/*@cc_on if (this.iFrame) this.iFrame.hide(); @*/
	this.v = this.css.visibility = "hidden";
}

TESCODIRECT.lib.DOM.rect.Position.prototype.getPageCoordinates = function() {
	if (this.rect.offsetTop || this.rect.offsetTop === 0) {
	// todo: make this xbrowser compatible
		var o = this.rect;
		var y = 0, x = 0;
		while (o && (o.offsetLeft != null) && (o.offsetTop != null)) {
			x += o.offsetLeft;
			y += o.offsetTop;
			o = o.offsetParent;
		}
		o = this.rect;
		return {
			"x" : x,
			"y" : y
		}
	}
}

TESCODIRECT.lib.DOM.rect.Position.prototype.getScreenCoordinates = function() {

	var _o = this.rect
	var pc = this.getPageCoordinates();
	// add the scroll position to get screen coordinates
	while ((new String(_o.tagName)).toUpperCase() != "BODY" && (_o.scrollLeft != null) && (_o.scrollTop != null)) {
		pc.x -= _o.scrollLeft;
		pc.y -= _o.scrollTop;
		_o = _o.parentNode;
	}
	return pc;
}

TESCODIRECT.lib.DOM.rect.Position.prototype.moveIntoView = function(x, y) {
	//  Move the element to given location but force onto the viewable screen if necessary
	var _winSize = TESCODIRECT.lib.DOM.element.getWindowSize();
	//this.moveTo(	Math.max( Math.min( x || this.x , 0 + _winSize.width - this.width), 0),
	//				Math.max( Math.min( y || this.y , 0 + _winSize.height - this.height), 0));
	this.moveTo(	Math.max( Math.min( x || this.x , 0 + _winSize.width - this.width), 0),
					( y || this.y ));
}

TESCODIRECT.lib.DOM.rect.Position.Animation = function(rect, iFrame) {
    
	TESCODIRECT.lib.DOM.rect.Position.call(this, rect, iFrame);
	this.VERSION = "1.0.0";
	this.NAME = "TESCODIRECT.lib.DOM.rect.Position.Animation";
}

TESCODIRECT.lib.DOM.rect.Position.Animation.prototype = new TESCODIRECT.lib.DOM.rect.Position();

TESCODIRECT.lib.DOM.rect.Position.Animation.prototype.slide = function(distance, xAxis, yAxis, action, speed) {
	var _self = this;
	if(!this.slidePos) this.slidePos = 0;
	this.slidePos += xAxis;
	this.moveBy(xAxis*action, yAxis*action);

	if (this.slidePos <= distance) {
		this.timer = setTimeout(
					function(){
						_self.slide(distance, xAxis, yAxis, action, speed)
					}, speed);
	} else {
		this.slidePos = 0;
	}
}

TESCODIRECT.lib.DOM.rect.Position.Animation.prototype.scroll = function( xAxis, yAxis, speed) {
	var _self = this;
	if(this.timer)
		clearTimeout(this.timer);
	var scrollPos = this.getScroll();
	var scrX = scrollPos[0];
	var scrY = scrollPos[1];
	var distX =  scrX - xAxis;
	if(distX != 0)
	{
		var move = Math.round(distX / 3);
		move = (move == 0) ? distX/Math.abs(distX) : move;
		this.scrollBy(move, 0);
		this.timer = setTimeout(
			function(){
				_self.scroll( xAxis, yAxis, speed)
			}, speed);
	}
}

TESCODIRECT.lib.DOM.element = new function() {
	this.VERSION = "1.0.1";
	this.NAME = "TESCODIRECT.lib.DOM.element";

	this.getPosition = function(rect) {
		if (rect) {
			return new TESCODIRECT.lib.DOM.rect.Position(rect);
		}
	}

	this.getDocumentHeight = function() {
		return document.documentElement.scrollHeight;
		// TODO: this needs to use the relevant property depending on browser/support/etc...
		//	return document.body.scrollHeight;
	}

	this.getDocumentWidth = function() {
		return document.documentElement.scrollWidth;
		// TODO: this needs to use the relevant property depending on browser/support/etc...
		//	return document.body.scrollWidth;
	}

	this.getScrollXY = function() {
		var scrOfX = 0, scrOfY = 0;
		if( typeof( window.pageYOffset ) == 'number' ) {
			//Netscape compliant
			scrOfY = window.pageYOffset;
			scrOfX = window.pageXOffset;
		} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			//DOM compliant
			scrOfY = document.body.scrollTop;
			scrOfX = document.body.scrollLeft;
		} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			//IE6 standards compliant mode
			scrOfY = document.documentElement.scrollTop;
			scrOfX = document.documentElement.scrollLeft;
		}
		return [ scrOfX, scrOfY ];
	}

	this.getWindowSize = function() {
		var innerWidth,innerHeight;
		var width,height;

		if( window.clientHeight ) {
			width = window.clientWidth, height = window.clientHeight;
		}
		if(window.innerHeight) {
			innerWidth = window.innerWidth; innerHeight = window.innerHeight;
		}
		if( document.body.clientWidth) {
			width = width || document.body.clientWidth;
			height = height || document.body.clientHeight;
		}
		var de=document.documentElement;
		if(de && de.clientWidth) {
			width = width || de.clientWidth;
			height = height || de.clientHeight
		}
		return { width:width, height:height, innerWidth:(innerWidth||width), innerHeight:(innerHeight||height)
		}
	}

	this.makeCentreOfWindow = function(o) {
		var pos = this.getPosition(o);
		var scrollArray = this.getScrollXY();
		var winSize = this.getWindowSize();
		//console.log(pos.width+','+pos.height);
		//console.log(winSize.width+','+winSize.height);
		pos.moveTo((winSize.innerWidth - pos.width) / 2, ((winSize.innerHeight - pos.height) / 2) + ((TESCODIRECT.lib.DOM.node.hasClassName(o, "fixed")) ? 0 : scrollArray[1] ));
	}

	this.makeCentreOfElement = function(o, e) {
		var pos = this.getPosition(o);
		if (window.innerWidth) {
			pos.moveTo((e.offsetWidth - pos.width) / 2, (e.offsetHeight - pos.height) / 2);
		} else {
			// TODO: fix this for non-complient browsers....
			//o.style.left = "1em";
			//o.style.top = "1em";
		}
	}
}