SlideShow = Ext.extend(Object, {
    constructor: function(config){
    	SlideShow.superclass.constructor.apply(this);
    	
    	this.list = config.list;
    	this.limit = config.limit;
    	this.body = Ext.get(config.body);
    	
    	this.text = Ext.get('rotate-asp');
    	
    	this.init();
    },
    init:function()
    {	
		this.counter = 0;
		
		this.task = {
			run:this.rotateImage,
			scope:this,
			interval:10000
		};
		
		this.runner = new Ext.util.TaskRunner();
				
		var l = Ext.get(this.list);
		if(l != null){
			l.on('click',this.selectImage,this,{delegate:'a'});
			
			this.initLastLink();
			
			this.delayRotate();
			//this.runner.start(this.task);
		}
    },
    initLastLink:function(){
    	var ul = Ext.get(this.list);
    	
    	var li = new Ext.Element(ul.dom.children[this.counter]);
    	
    	this.lastLink = li.child('a');
    	this.lastLink.addClass('selected');
    },
    delayRotate:function(){
    	if(this.dtask)this.dtask.cancel();
    	
    	this.dtask = new Ext.util.DelayedTask(function(){
			this.runner.start(this.task);	
		},this);
		
		this.dtask.delay(10000);
    },
    rotateImage:function(){
    	var ul = Ext.get(this.list);
		
		this.lastLink.removeClass('selected');
    	
    	this.counter = (++this.counter)%this.limit;
    	
    	var li = new Ext.Element(ul.dom.children[this.counter]);
    	
    	this.lastLink = li.child('a');
    	this.lastLink.addClass('selected');
    	
    	this.setBackground(this.counter);
    },
    selectImage:function(e,target,o){
    	var el = new Ext.Element(target);
    	el.addClass('selected');
    	
    	if(this.lastLink){
    		this.lastLink.removeClass('selected');
    		this.lastLink = el;
    	}
    	
    	var img = el.dom.firstChild.data -1;
    	
    	this.runner.stopAll();
  		
  		this.counter = img;
  		this.setBackground(img);
  		
  		
  		this.delayRotate();
    },
    setBackground:function(val){
		
		var first= '<h2>10th Annual Shutdowns Superconference 2011</h2><div class="left top10 text">The most well-established and largest shutdowns event in Canada! Providing the latest knowledge in turnaround project management from global companies and leading experts.<br/><br/><a href="/why-attend"><img src="/images/learn.png" height="30" border="0"></img></</div>';
    	
		var second = '<h2>Establishing a Roadmap to a Successful Shutdown</h2><div class="left top10 text">Get an in-depth look at the start of the turnaround life cycle.<span class="bold">   Canadian Natural Resources Limited</span> will walk you through the detailed planning and critical phases you need to go through at the initial pre-planning stage of your turnaround. Learn how improved mapping at the planning stage will enhance your project success.<br/><br/><a href="/agenda"><img src="/images/learn.png" height="30" border="0"></img></a></div>';
		
		var third = '<h2>Taking Control of Your Shutdown Before it Takes Control of You</h2><div class="left top10 text">Learn how <span class="bold">SaskPower</span> has implemented effective schedule development by moving away from a reactive way of doing shutdowns to a more process oriented and structured management method. Drive your performance to an increased level of excellence and minimize your business risks with the use of new technologies.<br/><br/><a href="/agenda/developments"><img src="/images/learn.png" height="30" border="0"></img></a></div>';
		
		var four = '<h2>Overcoming Execution Challenges of a Major Turnaround at a Heavy Oil Upgrader</h2><div class="left top10 text">Gain unique insights into the complexities of a turnaround involving over 2000 employees and contractors. Uncover the latest techniques and see the step-by-step strategies that were used to manage the complexities of <span class="bold">Husky Energy&rsquo;s</span> Lloydminster Upgrader and walk away with essential techniques that you can apply to your shutdown.<br/><br/><a href="/agenda/directives"><img src="/images/learn.png" height="30" border="0"></img></a></div>';
	  var five = '<h2>INCENTIVES FOR EARLY REGISTRATIONS</h2><div class="left top10 text"> Early bird savings when you register and pay by NOVEMBER 8TH! <br/><br/><a href="/pricing"><img src="/images/learn.png" height="30" border="0"></img></a></div>';
		
    
    	var items = [first,second,third,four,five];
    	
    	/*	var extra="";
    	if(val == 2) extra = " 15% #dddddd";*/
    	
    	var bgVal = val;
    
    	/*if(val >= 3 && val <=8){
    		bgVal = 3; 
    	}
    	
    	if(val >=9 && val <=12){
    		bgVal = 4;
    	}*/
    	
    	this.body.setStyle('background',"url('../images/slideshow/"+bgVal+".png') no-repeat");
    	this.text.update(items[val]);
    }
});
