// JavaScript Document
if (typeof Object.create !== 'function') {   
    Object.create = function (o) {   
        function F() {}   
        F.prototype = o;   
        return new F();   
    };   
}  
if (!Spry.Utils) Spry.Utils = {};
Spry.Utils.getElementByEvent = function (e){
		var targ;
		if (e.currentTarget) targ = e.currentTarget;
		else if (e.target) targ = e.target;
		else if (e.srcElement) targ = e.srcElement;
		if (targ.nodeType == 3) // defeat Safari bug
			targ = targ.parentNode;
		return targ;
}
Spry.Utils.stopDefaultEvents = function(e){
	if (e.preventDefault) e.preventDefault();
	else e.returnValue = false;
	if (e.stopPropagation) e.stopPropagation();
	else e.cancelBubble = true;
}
Spry.Utils.getElementByClassName = function(elements,clssName){
	for(var i =0; i < elements.length; i++){
		if(Spry.Utils.hasClassName(elements[i],clssName))
			return elements[i];
	}
}
Spry.Widget.SlidingPanelTicker = function(element,options){
	this.slidingPanelTickerEle = this.getElement(element);
	this.element = Spry.Utils.getElementByClassName(this.getElementChildren(this.getElement(element)),'SlidingPanels');
	this.interval = 2000;
	this.index = 0;
	Spry.Widget.SlidingPanels.call(this,this.element.id,options);
	this.attachNavigationHandlers();
	this.attachTickerBehaviors();
	this.startTickerInterval();
};
Spry.Widget.SlidingPanelTicker.prototype = Object.create(Spry.Widget.SlidingPanels.prototype);
Spry.Widget.SlidingPanelTicker.prototype.constructor = Spry.Widget.SlidingPanelTicker;
Spry.Widget.SlidingPanelTicker.prototype.startTickerInterval = function(){
	var self = this;
	if(self.tickerTimerID)
		self.stopTickerInterval();
	
	self.tickerTimerID = setTimeout(function(){ self.showTickerNextPanel(); },self.interval);
}
Spry.Widget.SlidingPanelTicker.prototype.stopTickerInterval = function(){
	var self = this;
	if(self.tickerTimerID)
		clearTimeout(self.tickerTimerID);
	
	self.tickerTimerID = 0;
}
Spry.Widget.SlidingPanelTicker.prototype.attachTickerBehaviors = function(){
	var self = this;
	var slidingPanelGroup = Spry.Utils.getElementByClassName(this.getElementChildren(self.element),'SlidingPanelsContentGroup');
	var children = self.getElementChildren(slidingPanelGroup);
	for(var i =0; i < children.length; i++){
		if(Spry.Utils.hasClassName(children[i],"SlidingPanelsContent")){
			Spry.Utils.addEventListener(children[i], "mouseover", function(e){ self.stopTickerInterval(); }, false);
			Spry.Utils.addEventListener(children[i], "mouseout", function(e){ self.startTickerInterval(); }, false);
		}
	}
}
Spry.Widget.SlidingPanelTicker.prototype.attachNavigationHandlers = function(){
	var self = this;
	var children = Spry.$$('*#'+self.slidingPanelTickerEle.id+' .SlidingPanelTickerNavigation .SlidingPanelTickerPagination .SlidingPanelsContentGroup .SlidingPanelTickerLink');
	for(var i =0; i < children.length; i++){
		var ele = children[i];
		 Spry.Utils.addEventListener(ele, "click", function(e){
																self.showTickerPanel(e || window.event); 
																}, false);
		Spry.Utils.addEventListener(ele, "mouseover", function(e){ el = Spry.Utils.getElementByEvent(e); 
																										 if(!Spry.Utils.hasClassName(el,'SlidingPanelTickerLinkFocused')) self.addClassName(el,'SlidingPanelTickerLinkFocused'); }, false);
		Spry.Utils.addEventListener(ele, "mouseout", function(e){ el = Spry.Utils.getElementByEvent(e);  
																										if(self.index != el.tabIndex && Spry.Utils.hasClassName(el,'SlidingPanelTickerLinkFocused')) self.removeClassName(el,'SlidingPanelTickerLinkFocused'); }, false);
	}
	
	ele =Spry.$$('*#'+self.slidingPanelTickerEle.id+' .SlidingPanelTickerNavigation .SlidingPanelTickerPrev')[0];
	Spry.Utils.addEventListener(ele, "mouseover", function(e){ self.scrollNavPanel(e || window.event); }, false);
	Spry.Utils.addEventListener(ele, "click", function(e){ self.showTickerPreviousPanel(e || window.event);  }, false);
	ele =Spry.$$('*#'+self.slidingPanelTickerEle.id+' .SlidingPanelTickerNavigation .SlidingPanelTickerNext')[0];
	Spry.Utils.addEventListener(ele, "mouseover", function(e){ self.scrollNavPanel(e || window.event); }, false);
	Spry.Utils.addEventListener(ele, "click", function(e){ self.showTickerNextPanel(e || window.event);  }, false);
}
Spry.Widget.SlidingPanelTicker.prototype.scrollNavPanel = function(e){
	var navPanel, from, to, fromPos, toPos;
	var n = Spry.Utils.getElementByEvent(e);
	var dir = Spry.Utils.hasClassName(n,'SlidingPanelTickerNext')? 'next': 'previous';
	var nav = n.parentNode;
	var navPanel = Spry.Utils.getElementByClassName(this.getElementChildren(nav),'SlidingPanelTickerPagination');
	groupPanel = Spry.Utils.getElementByClassName(this.getElementChildren(navPanel),'SlidingPanelsContentGroup');
	groupPanel.id = (new Date().getTime());
	fromPos = new Spry.Effect.Utils.Position();
	fromPos.x = groupPanel.offsetLeft;
	toPos = new Spry.Effect.Utils.Position();
	toPos.x = dir == 'next'? (navPanel.offsetWidth - groupPanel.offsetWidth) : 0;
	Spry.Effect.makePositioned(groupPanel);
	var move = new Spry.Effect.Move(groupPanel, fromPos, toPos, {duration: 1000});
	move.start();
	f = function(e){ move.stop(); Spry.Utils.removeEventListener(n,'mouseout',f,false);}
	Spry.Utils.addEventListener(n,'mouseout',f,false);
}
Spry.Widget.SlidingPanelTicker.prototype.showTickerPreviousPanel = function(e){
	var self = this;
	self.stopTickerInterval();
	Spry.$$('a[tabindex="'+self.index+'"]').removeClassName('SlidingPanelTickerLinkFocused');
	if((self.index - 1) >= 0){
		self.showPreviousPanel();
		self.index--;
	}else{  index = self.getContentPanelsCount() - 1;self.showLastPanel();}
	Spry.$$('a[tabindex="'+self.index+'"]').addClassName('SlidingPanelTickerLinkFocused');
	self.animator.finish = function(){ self.startTickerInterval(); };
	if(e) Spry.Utils.stopDefaultEvents(e);
}
Spry.Widget.SlidingPanelTicker.prototype.showTickerNextPanel = function(e){
	var self = this;
	self.stopTickerInterval();
	Spry.$$('a[tabindex="'+self.index+'"]').removeClassName('SlidingPanelTickerLinkFocused');
	if((self.index + 1) < self.getContentPanelsCount()){
		self.showNextPanel();
		self.index++;
	}else{ self.index = 0;self.showFirstPanel(); }
	Spry.$$('a[tabindex="'+self.index+'"]').addClassName('SlidingPanelTickerLinkFocused'); 
	self.animator.finish = self.animator.finish = function(){ self.startTickerInterval(); };
	if(e) Spry.Utils.stopDefaultEvents(e);
}
Spry.Widget.SlidingPanelTicker.prototype.showTickerPanel = function(e){
	
	var n = Spry.Utils.getElementByEvent(e);
	var a = Spry.$$('a'+n.hash)[0];
	var self = this;
	Spry.$$('a[tabindex="'+self.index+'"]').removeClassName('SlidingPanelTickerLinkFocused');
	self.index = n.tabIndex;
	self.stopTickerInterval();
	self.showPanel(a.name);
	Spry.$$('a[tabindex="'+self.index+'"]').addClassName('SlidingPanelTickerLinkFocused'); 
	self.animator.finish = function(){ self.startTickerInterval(); };
	if(e) Spry.Utils.stopDefaultEvents(e);
}

Spry.Utils.addLoadListener(function(e){sp1 = new Spry.Widget.SlidingPanelTicker('slidingPanelTicker',{interval:2500}); });
