/***********************
* carousel js and dependencies
**************************/
/**
* global function ala prototype lib
*/
function $(name){
	return document.getElementById(name);
}

Function.prototype.bind = function(obj) {
	var method = this,
		temp = function() {
			return method.apply(obj, arguments);
		};
	return temp;
}

/**
* instanciate global BOPTIX js object
*/
BOPTIX = {};
/**
* set util namespace
*/
BOPTIX.util = {};
/**
* generic utility functions
*/
BOPTIX.util.prototype = {
	setDisplay:function(obj, state){
		obj.style.display = state;
	},
	
	setStyleAtts:function(domnode, styleatt, value){
		domnode.style[styleatt] = value + 'px';
	},
	
	getStyleAtts:function(domnode, styleatt){
		return domnode.style[styleatt];
	},
	
	changeOpacity:function(theobj, opac){
		var obj = theobj.style;
		obj.opacity = (opac / 100);
		obj.MozOpacity = (opac / 100);
		obj.KhtmlOpacity = (opac / 100);
		obj.filter = "alpha(opacity=" + opac + ")"
	},
	findPosition:function(theobject){
		var curleft = curtop = 0;
		if (theobject.offsetParent) {
			curleft = theobject.offsetLeft;
			curtop = theobject.offsetTop;
			while (theobject = theobject.offsetParent) {
				curleft += theobject.offsetLeft
				curtop += theobject.offsetTop
			}
		}
		return [curleft,curtop];
	},
	
	findHeightWidth:function(theobject){
		var theheight = theobject.offsetHeight;
		var thewidth = theobject.offsetWidth;
		return[theheight, thewidth];
	}
}

/**
* event handling object and methods
* usage: BOPTIX.util.Event.addListener(document, "mouseover", this); 
*/
BOPTIX.util.Event = {};
BOPTIX.util.Event.prototype = {
	addListener:function(obj, eventName, listener){
		if (obj.attachEvent){
			obj.attachEvent("on"+eventName, listener);
		}else if(obj.addEventListener){
			obj.addEventListener(eventName, listener, false);
		}else{
			return false;
		}
		return true;
	},
	
	removeListener:function(obj, eventName, listener){
		if(obj.detachEvent){
			obj.detachEvent("on"+eventName, listener);
		}else if(obj.removeEventListener){
			obj.removeEventListener(eventName, listener, false);
		}else{
			return false;
		}
		return true;
	}
}

/**
* set dhtml namespace
*/
BOPTIX.dhtml = {};
/**
* BOPTIX tween lib
*/
BOPTIX.dhtml.Tween = {};

BOPTIX.dhtml.Tween.prototype = {
	
	easing:function(start, end, numsteps, currentstep, pvalue) {
		var delta = end - start; 
		var step = start + (Math.pow(((1 / numsteps) * currentstep), pvalue) * delta); 
		return Math.ceil(step); 
	},
	
	bounce:function(start, end, numsteps, currentstep){
		var delta = end - start;
		if ((currentstep/=numsteps) < (1/2.75)) {
			return delta*(7.5625*currentstep*currentstep) + start;
		} else if (currentstep < (2/2.75)) {
			return delta*(7.5625*(currentstep-=(1.5/2.75))*currentstep + .75) + start;
		} else if (currentstep < (2.5/2.75)) {
			return delta*(7.5625*(currentstep-=(2.25/2.75))*currentstep + .9375) + start;
		} else {
			return delta*(7.5625*(currentstep-=(2.625/2.75))*currentstep + .984375) + start;
		}
	},
	
	back:function(start, end, numsteps, currentstep, over){
		var delta = end - start;
		if (typeof over == 'undefined') over = 1.70158;
		return delta*((currentstep=currentstep/numsteps-1)*currentstep*((over+1)*currentstep + over) + 1) + start;
	}
}


/**
* Generic animation object
* used by various dhtml objects
* dependency: Tween library
*/
BOPTIX.dhtml.Animation = function(args){
	this.init(args);
}

BOPTIX.dhtml.Animation.prototype = {
	init:function(args){
		this.now = new Date().getTime();
		this.framerate = args.framerate;
		this.seconds = args.seconds;
		this.end = this.now + this.seconds;
		this.domelement = args.domelement;
		this.duration = Math.round(this.end - this.now);
		this.totalframes = Math.round(this.duration / this.framerate);
		this.startpoint = args.from;
		this.endpoint = args.to;
		this.method = args.method;
		this.animation;
		this.currentframe = 0;
	},
	
	animate:function(action){
		this.action = action;
		this.animation = setInterval(this[this.action].bind(this), this.framerate);
	},
	
	play:function(){
		/**
		* increment current frame and animate to the endpoint
		*/
		this.currentframe++;
		this.towidth = BOPTIX.dhtml.Tween.prototype[this.method](this.startpoint, this.endpoint, this.totalframes, this.currentframe, 3);
		
		BOPTIX.util.prototype.setStyleAtts(this.domelement, "height", this.towidth);
		
		if(this.currentframe >= this.totalframes){
			this.stop();
		}
	},
	
	rewind:function(){
		/**
		* increment current frame and rewind to starting point
		*/
		this.currentframe++;
		this.fromwidth = BOPTIX.dhtml.Tween.prototype[this.method](this.endpoint, this.startpoint, this.totalframes, this.currentframe, 1);
		BOPTIX.util.prototype.setStyleAtts(this.domelement, "height", this.fromwidth);
		if(this.currentframe >= this.totalframes){
			this.stop();
		}
	},
	stop:function(){
		clearInterval(this.animation);
		this.currentframe = 0;
	}
}

/**
* PDPSlider object for PDP redesign
* usage: BOPTIX.dhtml.PDPSlider({triggerelement:"prodshow",animated:"featurebullets",content:"featurebulletsinner",method:"easing"});
*/

BOPTIX.dhtml.PDPSlider = function(args){
	this.trigger = $(""+args.triggerelement+"");
	this.animated = $(""+args.animated+"");
	this.contentheight = parseInt($(""+args.content+"").offsetHeight);
	this.method = args.method;
	this.isOpen = false;
	
	this.openslider = new BOPTIX.dhtml.Animation({domelement:this.animated, framerate:50, seconds:300, to:this.contentheight, from:1, method:this.method});
	this.closeslider = new BOPTIX.dhtml.Animation({domelement:this.animated, framerate:50, seconds:300, to:this.contentheight, from:1, method:this.method});
	
	BOPTIX.util.Event.prototype.addListener(this.trigger, "click", this.animate.bind(this));
}

BOPTIX.dhtml.PDPSlider.prototype = {
	animate:function(){
		if(this.isOpen){
			this.closeslider.animate("rewind");
			this.isOpen = false;
			return false;
		}else{
			this.openslider.animate("play");
			this.isOpen = true;
			return false;
		}
	}
}
/**
* Carousel javascript for ABN
* usage: BOPTIX.dhtml.Carousel({visible:3,increment:3,contentframe:"contentframeid",content:"contentid",backbutton:"backbuttonid",forwardbutton:"forwardbuttonid"}); 
*/
BOPTIX.dhtml.Carousel = function(args){
	this.visibleElements = args.visible;
	this.incrementNum = args.increment;
	this.contentFrame = $(""+args.contentframe+"");
	this.prevButton = $(""+args.backbutton+"");
	this.nextButton = $(""+args.forwardbutton+"");
	this.carouselContent = args.content;
	this.isRunning = false;
	this.init();
}

BOPTIX.dhtml.Carousel.prototype = {
	init:function(){
		//set inital index to 0
		this.currentIndex = 0;
		//set initial scroll increment width to 0
		this.scrollIncrement = 0;
		//set the current increment value to 0
		this.incrementValue = 0;
		// total number of cols
		this.cols = $(""+this.carouselContent+"").getElementsByTagName("li");
		this.checkDisable();
		
		BOPTIX.util.Event.prototype.addListener(this.prevButton, "click", this.getPrev.bind(this));
		BOPTIX.util.Event.prototype.addListener(this.nextButton, "click", this.getNext.bind(this));
	},
	
	getPrev:function(){
		//width of the cols
		this.width = this.cols[0].offsetWidth;
		
		if((this.currentIndex - this.incrementNum) <= 0){
			this.currentIndex = 0;
		}else{
			//decrement the index
			this.currentIndex = parseInt(this.currentIndex - (this.currentIndex - this.incrementNum));
		}
		
		this.scrollIncrement = parseInt(this.currentIndex * this.width);
		
		//set interval for scrollNext function
		if(this.isRunning == true)return;
		
		this.inter = setInterval(this.scrollPrev.bind(this), 10);
		this.isRunning = true;
	},
	
	getNext:function(){
		
		//width of the cols
		this.width = this.cols[0].offsetWidth;
		
		//increment the index by the increment number
		this.currentIndex = parseInt((this.currentIndex + this.incrementNum));
		
		if((this.currentIndex + this.visibleElements) >= this.cols.length){
			this.currentIndex = this.currentIndex - ((this.currentIndex + this.visibleElements) - this.cols.length);
		}		
		
		this.scrollIncrement = parseInt(this.currentIndex * this.width);
		
		//set interval for scrollNext function
		if(this.isRunning == true)return;
		
		this.inter = setInterval(this.scrollNext.bind(this), 10);
		this.isRunning = true;
	},
	
	scrollNext:function(){
		if(this.incrementValue > -(this.scrollIncrement)){
			this.incrementValue = this.incrementValue - 17;
			this.contentFrame.style.left = this.incrementValue + 'px';
		}else{
			clearInterval(this.inter);
			this.checkDisable();
			this.isRunning = false;
			
		}
	},
	
	scrollPrev:function(){
		if(this.incrementValue < -(this.scrollIncrement)){
			this.incrementValue = this.incrementValue + 17;
			this.contentFrame.style.left = this.incrementValue + 'px';
		}else{
			clearInterval(this.inter);
			this.checkDisable();
			this.isRunning = false;
			
		}
	},
	
	checkDisable:function(){
		var back = ((this.currentIndex == 0)) ? BOPTIX.util.prototype.changeOpacity(this.prevButton, 40) : BOPTIX.util.prototype.changeOpacity(this.prevButton, 100);
		var fwd = ((this.currentIndex + this.visibleElements) >= this.cols.length) ? BOPTIX.util.prototype.changeOpacity(this.nextButton, 40) : BOPTIX.util.prototype.changeOpacity(this.nextButton, 100);
	}
}
