/* 
 * Copyright (c)  Sequence Collective Ltd (www.sequence.co.uk)
 * By Sequence for Cardiff Uni
 * v1.0 - No Pause
 */
if(typeof seq=="undefined"||!seq){var seq={};}
(function(){var timer={running:false,timer:null,funcs:[],sleepd:[],last:0,add:function(func){if(!this.running)
this.run();return this.funcs.push(func)-1;},remove:function(index){},run:function(){var that=this;this.timer=window.setInterval(function(){that.update();},14);this.last=new Date().getTime();this.running=true;},update:function(){var now=new Date().getTime();var time=(now-this.last)/100;this.last=now;for(var i=0;i<this.funcs.length;i++){if(!this.sleepd[i]){var func=this.funcs[i];func(time)}}},sleep:function(index){this.sleepd[index]=true;if(this.sleepd.length==this.funcs.length){window.clearTimeout(this.timer);this.timer=null;this.running=false;}},awaken:function(index){if(this.sleepd[index]){this.run();}
this.sleepd[index]=false;}}
var pagination=function(holder,slides,callback){this.div=document.createElement("div");this.div.className="pagination";with(this.div.style){position="absolute";bottom=0;right=0;}
this.links=[];for(var i=0,l=slides.length;i<l;i++){var a=document.createElement("a");a.href="#slide"+(i+1);a.slideIndex=i;var title=slides[i].title;if(title){a.innerHTML="<span>"+title+"</span>";}
else{a.innerHTML="<span>"+(i+1)+"</span>";}
var that=this;a.onclick=function(){that.active(this.slideIndex);if(typeof callback=="function")
callback(this.slideIndex)
return false;}
this.links.push(a);this.div.appendChild(a);this.current=0;}
holder.appendChild(this.div);}
pagination.prototype={active:function(index){this.links[this.current].className="";this.links[index].className="active";this.current=index;}}
seq.cujs=function(holder,subject,options){if(typeof holder=="string")
var holder=document.getElementById(holder);this.holder=holder;this.holder.style.position="relative";this.holder.style.overflow="hidden";this.options={loop:true,autostart:false,enablelinks:false,paginate:true,delay:6000,target:0}
if(typeof options=="object"){for(var i in options){this.options[i]=options[i];}}
else{}
this.target=this.options.target;this.slides=[];for(i=0,l=this.holder.childNodes.length;i<l;i++){var el=this.holder.childNodes[i];if(el.className&&el.className.match(/slide/i)){with(el.style){position="absolute";top=0;left=0;width="100%";height="100%";}
this.slides.push(el);}}
if(typeof subject=="string")
subject=seq.cujs.fx[subject];if(typeof subject=="function")
subject=new subject();this.subject=subject;this.subject.init(this.holder,this.slides,this.target);if(this.options.enablelinks){var links=this.holder.getElementsByTagName("a");for(var i=0,l=links.length;i<l;i++){var rel=links[i].rel;if(rel){var that=this;var link=links[i];if(rel.match(/^next$/i)){link.onclick=function(){that.next();return false;}}
else if(rel.match(/^prev$/i)){link.onclick=function(){that.prev();return false;}}
else{var to=rel.match(/^to\[([0-9]+)\]$/i)
if(to[1]){link.onclick=function(){that.to(to[1]);return false;}}}}}}
if(this.options.paginate){var that=this;this.pagination=new pagination(this.holder,this.slides,function(index){that.to(index);});this.pagination.active(this.target);}
if(this.options.autostart)
this.start();}
seq.cujs.prototype={start:function(){if(this.running==null){var that=this;this.running=timer.add(function(time){that.update(time);});this.to(this.options.target);}},stop:function(){if(this.running){timer.remove(this.running);if(this.autotimer){window.clearTimeout(this.autotimer);this.autotimer=null;}}},update:function(time){if(!this.subject.update(this.holder,this.slides,this.target,time)){timer.sleep(this.running);this.asleep=true;}},to:function(index){if(!this.running)
this.start();if(this.asleep){timer.awaken(this.running);this.asleep=false;}
var length=this.slides.length;if(this.options.loop){if(index<0){index=length-1;}
else if(index>length-1){index=0;}}else{if(index<0){index=0;}
else if(index>length-1){index=length-1}}
if(this.pagination)
this.pagination.active(index);if(this.options.autostart){if(this.autotimer){window.clearTimeout(this.autotimer);this.autotimer=null;}
var that=this;this.autotimer=window.setTimeout(function(){that.next();},this.options.delay);}
this.target=index;},next:function(count){if(!count)count=1;this.to(this.target+count);},prev:function(count){if(!count)count=1;this.to(this.target-count);}}
seq.cujs.fx={}
seq.cujs.fx.fader=function(){this.o=[];}
seq.cujs.fx.fader.prototype={_opacity:function(el,o){el.style.opacity=o;el.style.MozOpacity=o;el.style.filter='alpha(opacity='+o*100+')';},init:function(holder,slides,target){for(var i=0;i<slides.length;i++){var el=slides[i];if(i==target){this._opacity(el,1);this.o[i]=1;el.style.display="block";}else{this._opacity(el,0);this.o[i]=0;el.style.display="none";}}},update:function(holder,slides,target,time){var done=true;for(var i=0;i<slides.length;i++){var el=slides[i];var o=this.o[i];if(i==target){if(o<1){o+=0.25*time;this._opacity(el,o);this.o[i]=o;el.style.display="block";done=false;}
else{this._opacity(el,1);this.o[i]=1;el.style.display="block";}}
else{if(o>0){o-=0.25*time;this._opacity(el,o);this.o[i]=o;el.style.display="block";done=false;}
else{this._opacity(el,0);this.o[i]=0;el.style.display="none";}}}
if(done)
return false;return true;}}
seq.cujs.fx.slider=function(accel,damp){if(!accel)accel=6;if(!damp)damp=1.1;this.accel=accel;this.damp=damp;this.canvas=null;this.speed=0;this.width=0;this.pos=0;}
seq.cujs.fx.slider.prototype={_setpos:function(p){this.canvas.style.left=-Math.round(p)+"px";this.pos=p;},init:function(holder,slides,target){this.canvas=document.createElement("div")
this.width=holder.clientWidth;with(this.canvas.style){position="absolute";top=0;left=this.width*slides.length+"px";width="100%";height="100%";}
for(var i=0;i<slides.length;i++){var el=slides[i];el.style.left=this.width*i+"px";holder.removeChild(el);this.canvas.appendChild(el);}
holder.appendChild(this.canvas);this._setpos(this.width*target);},update:function(holder,slides,target,time){var tpos=target*this.width;var dist=tpos-this.pos;this.speed=(dist/this.accel+this.speed)/this.damp;this._setpos(this.pos+(this.speed*time));if(Math.abs(this.speed)<=0.5&&this.pos==tpos){return false;}
return true;}}})();
