/**
 * ...
 * @author Daynier Brown <daynierbrown@gmail.com>
 */
var stages = ['/templates/bob2012/images/stagebg2.jpg','/templates/bob2012/images/stagebg3.jpg'];
var bgindex = 0;
var thumbSlider = { elements:'.artist', fx:null, index:0, max:0, parent:null, target:null, timer:null, width:0 }

window.addEvent('domready', function(){
	if($$('.module.artists .frames').length)
		thumbSlider.setup($$('.module.artists .frames')[0]);
		
	if($('litePhotos')){
		$('litePhotos').getElements('.sets .set.lead .images .image').each(function(el){
			var link = new Element('a', {'href':el.get('src'), rel:'milkbox'});
			$('litePhotos').getElements('.sets .set.lead .images')[0].adopt(link);
			el.inject(link, "bottom"); //Force the el inside the link
		});
		milkbox.reloadGalleries();
	}
	if($$('.buyticketsnow').length){
		/*$$('.buyticketsnow')[0].addEvent('mouseenter', clearTimeout.bind(window, window.blinker));
		$$('.buyticketsnow')[0].addEvent('mouseleave', function(){ 
			clearTimeout.bind(window, window.blinker); 
			window.blinker = $$('.buyticketsnow')[0].fade.periodical(400, $$('.buyticketsnow')[0], 'toggle'); 
		});*/
		window.blinker = $$('.buyticketsnow')[0].fade.periodical(400, $$('.buyticketsnow')[0], 'toggle');
	}
});

function GetCount(){

        dateNow = new Date();                                                            
        amount = dateFuture.getTime() - dateNow.getTime()+5;               
        delete dateNow;

        /* time is already past */
        if(amount < 0){
                out=
				"<div id='days'><span></span>0<div id='days_text'></div></div>" + 
				"<div id='hours'><span></span>0<div id='hours_text'></div></div>" + 
				"<div id='mins'><span></span>0<div id='mins_text'></div></div>" + 
				"<div id='secs'><span></span>0<div id='secs_text'></div></div>" ;
                document.getElementById('countbox').innerHTML=out;       
        }
        /* date is still good */
        else{
                days=0;hours=0;mins=0;secs=0;out="";

                amount = Math.floor(amount/1000); /* kill the milliseconds */

                days=Math.floor(amount/86400); /* days */
                amount=amount%86400;

                hours=Math.floor(amount/3600); /* hours */
                amount=amount%3600;

                mins=Math.floor(amount/60); /* minutes */
                amount=amount%60;

                
                secs=Math.floor(amount); /* seconds */


                out=
				"<div id='days'><span></span>" + days +"</div>" + 
				"<div id='hours'><span></span>" + hours +"</div>" + 
				"<div id='mins'><span></span>" + mins +"</div>" + 
				"<div id='secs'><span></span>" + secs +"</div>" ;
                document.getElementById('countbox').innerHTML=out;  
			

                setTimeout("GetCount()", 1000);
        }
}

function blinkText() {
        if (!document.all) return;
        
        if($$('*.blink').length) {
                Array.each($$('*.blink'), function(el){
                        if(!el.getStyle('opacity'))
                        	el.fade('show');
                        else
                        	el.fade('hide');
                });
        }
        
        //Start the blinker
        if(!window.blinker){
        	window.blinker = setInterval('blinkText()',400);
        }
}

function switchStage(){
	//performs the cross fade of the background image
	bgindex = (bgindex > stages.length -1)? 0 : bgindex+1;
	$('stage').set('tween', {duration:5000});
	$('stage').crossfade(stages[bgindex]);
	console.log("switchStage: ", bgindex);
};

thumbSlider.setup = function (holder){
	//Parse the list and get the children
	this.target = holder;
	this.parent = this.target.getParent();
	this.max = this.target.getElements(this.elements);
	this.width = this.parent.getStyle('width');
	this.index = 0;
	
	//Make the left and right flaps
	this.leftFlap = new Element('div', {'class':'leftFlap', styles:{width:20, height:'100%', display:'block', left:0, position:'absolute', 'z-index':10000}, 
		events:{mouseover:this.panLeft.bind(this), mouseout:this.panStop.bind(this)}
	});
	this.rightFlap = new Element('div', {'class':'rightFlap', styles:{width:20, height:'100%', display:'block', right:0, position:'absolute', 'z-index':10000}, 
		events:{mouseover:this.panRight.bind(this), mouseout:this.panStop.bind(this)}
	});
	
	//Setup Fx
	this.fx = new Fx.Tween('myElement', {
		duration: 'long',
		transition: 'bounce:out',
		link: 'cancel',
		property: 'left'
	});
	
	//Check for existing flaps and dispose of them.
	var disposables = this.parent.getElements('.leftFlap, .rightFlap');
	if(disposables.length)
		disposables.dispose();
		
	this.parent.adopt(this.leftFlap, this.rightFlap);
}

thumbSlider.panLeft = function(){
	var o = parseInt( this.target.getStyle('left') );
	//if(o > "-"+(this.width * this.max-1)
	this.timer = this.move.periodical(1000, this, ['left', 0]);
	if(window.console != undefined)
		console.log('panLeft: ', o);
}

thumbSlider.panRight = function(){
	var o = parseInt( this.target.getStyle('left') );
	if(o > this.width * this.max)
		this.panStop();
	
	this.timer = this.move.periodical(1000, this, ['left', 1]);
	if(window.console != undefined)
		console.log('panRight: ', o);
}

thumbSlider.panStop = function(event){
	clearInterval(this.timer);
	this.timer = null;
	this.fx.cancel(); //Stop the fx event
	if(window.console != undefined)
		console.log('panStop: ', event);
}

thumbSlider.move = function(property, polarity){
	//Continues the tweening
	v = parseInt( this.target.getStyle('left') );
	v = (polarity)? v-- : v++;
	this.target.tween(property, value);
	if(window.console != undefined)
		console.log('move: ', arguments);
}

