/*
* JQuery based.
*/
function initArtistListAnimate(id) {
	var duration = 250;
	scope = $('#'+id);
	$(".holder01", scope).css({'top':'165px','display':'block'});
	$(".holder02", scope).css({'right':'-185px','display':'block'});
	$(scope).hover(
	function(){
		$(".holder01", this).stop().animate({top:'95px'},{queue:false,duration:duration});
		$(".holder02", this).stop().animate({right:'0px'},{queue:false,duration:duration});
	}, 
	function() {
		$(".holder01", this).stop().animate({top:'165px'},{queue:false,duration:duration});
		$(".holder02", this).stop().animate({right:'-185px'},{queue:false,duration:duration});
	});
}

function initStuffListAnimate(id) {
	var duration = 250;
	scope = $('#'+id);
	$(".holder01", scope).css({'top':'205px','display':'block'});
	$(scope).hover(
	function(){
		$(".holder01", this).stop().animate({top:'155px'},{queue:false,duration:duration});
	}, 
	function() {
		$(".holder01", this).stop().animate({top:'205px'},{queue:false,duration:duration});
	});
}

function initHomepageAnimate(id) {
	var duration = 250;
	scope = $('#'+id);
	$(".holder01", scope).css({'right':'-112px','display':'block'});
	$(scope).hover(
	function(){
		$(".holder01", this).stop().animate({right:'0px'},{queue:false,duration:duration});
	}, 
	function() {
		$(".holder01", this).stop().animate({right:'-112px'},{queue:false,duration:duration});
	});
}


//-------- based on jQuery ---------
function Carousel(selector, pager, options) {
	this.items = $(selector);
	this.total = this.items.size();
	this.pager = $(pager);
	this.options = options;
	this.stopOnClick = (this.options && this.options.stopOnClick) ? this.options.stopOnClick  :  false; 
	this.switchDelay = (this.options && this.options.delay) ? this.options.delay * 1000 :  6*1000; 
	this.delayBeforeResume = 25*1000; 
	this.switcherLocked = false; 
	this.resumePeriodical = false;
	this.effectDuration = 500; // ms
	this.lockTimeout = 750; // effectDuration * 1.5
	this.current = 0;   
	if (typeof document.carousels=='undefined') document.carousels = new Array();
		document.carousels.push(this); 
	this.start();
}
Carousel.prototype = {
	timedSwitch: function () {
		var instance = this;
		if (this.switcherLocked) return false;
		this.switcherLocked = true;
		curr = this.current;
		if (curr < this.total-1) { next = curr+1; } else { next = 0 };
		$(this.items[curr]).fadeOut(this.effectDuration);
		$(this.items[next]).fadeIn(this.effectDuration);
		$(this.pager[curr]).removeClass('act');
		$(this.pager[next]).addClass('act');
		//this.updateTitle( this.items[next].down('img').alt );
		this.current = next;
		//setTimeout(this.unlock.bind(this),this.lockTimeout);
		setTimeout(function(){instance.unlock();},this.lockTimeout);
		this.periodical = setTimeout(function(){instance.timedSwitch();},this.switchDelay);
		return false;
	},
	needStopOnClick: function(){
		return this.stopOnClick;
	},
	stop: function() {
		if (this.periodical) clearInterval(this.periodical);
	},
	updateTitle: function(title){
		imageTitle = $$('.slideshow-bar h3').first();
		if (imageTitle) {
			$(imageTitle).update( title );
		}
	},
	unlock: function(){
		this.switcherLocked = false;
	},
	prev: function() {
		if (this.switcherLocked) return false;
		var instance = this;
		this.switcherLocked = true;
		curr = this.current;
		clearInterval(this.periodical);
		if (curr > 0) prev = curr-1; else prev = this.total-1;
		$(this.items[curr]).fadeOut(this.effectDuration);
		$(this.items[prev]).fadeIn(this.effectDuration);
		this.current = prev;
		setTimeout(function(){instance.unlock();},this.lockTimeout);
		this.periodical = setTimeout(function(){instance.timedSwitch();},this.switchDelay);
		return false;
	},
	next: function() {
		if (this.switcherLocked) return false;
		var instance = this;
		this.switcherLocked = true;
		curr = this.current;
		clearInterval(this.periodical);
		if (curr < this.total-1) next = curr+1; else next = 0;
		$(this.items[curr]).fadeOut(this.effectDuration);
		$(this.items[next]).fadeIn(this.effectDuration);
		this.current = next;
		setTimeout(function(){instance.unlock();},this.lockTimeout);
		this.periodical = setTimeout(function(){instance.timedSwitch()},this.switchDelay);
		return false;
	},
	switchItem: function(next1) {
		next = next1 - 1;
		if (this.current == next) return false;
		if (this.switcherLocked) return false;
		
		if (typeof document.activePlayerId != 'undefined' && document.activePlayerId) {
			var player = document.getElementById(document.activePlayerId);
//try { console.log(document.activePlayerId);	console.log(typeof player.sendEvent); } catch (e) {}; //alert(player.sendEvent);
			if (player && typeof player.sendEvent == 'function')
			setTimeout(function(){player.sendEvent('STOP');},this.effectDuration);
		}
		var instance = this;
		this.switcherLocked = true;
		clearInterval(this.periodical);
		if (this.resumePeriodical) clearInterval(this.resumePeriodical);
		curr = this.current;
		$(this.pager[curr]).removeClass('act');
		$(this.pager[next]).addClass('act');
		$(this.items[curr]).fadeOut(this.effectDuration);
		$(this.items[next]).fadeIn(this.effectDuration);
		this.current = next;
		setTimeout(function(){ instance.unlock();},this.lockTimeout);
//		if (this.delayBeforeResume) 
//			this.resumePeriodical = setTimeout(this.periodical = setTimeout(this.timedSwitch.bind(this),this.switchDelay),this.delayBeforeResume);
//		this.setControlState('play');
		/*if (typeof document.activePlayer == 'object') {
			document.activePlayer.sendEvent('STOP');
		}*/
//		if (this.stopOnClick) return false;
		this.periodical = setTimeout(function(){ instance.timedSwitch();},this.switchDelay);
		return false;
	},
	setControlState: function(state){
		control = $('control_button');
		if (control){
			$(control).removeClassName('play');
			$(control).removeClassName('pause');
			$(control).addClassName(state);
		}			
	},
	togglePlay: function() {
		if (this.total < 2) return;
		control = $('control_button');
		clearInterval(this.periodical);
		if (this.resumePeriodical) clearInterval(this.resumePeriodical);
		if (control.hasClassName('pause')) {
			this.setControlState('play');
		} else {
			this.setControlState('pause');
			this.periodical = setTimeout(this.timedSwitch.bind(this),250);
		}
		return false;
	},
	start: function() {
		if (this.total < 2) return;
		if (this.options && this.options.delay) this.switchDelay = this.options.delay * 1000;
		var instance = this;
		this.items.each(function(){ $(this).hide() });
		$(this.items[0]).show();

		if (this.stopOnClick) 
			$(this.items).each(function(){ 
				$(this).click( function(){ instance.stop(); });
			});
		
		$(this.pager[0]).addClass('act');
		this.periodical = setTimeout(function(){ instance.timedSwitch();},this.switchDelay);
/* 
		imageTitle = $$('.slideshow-bar h3').first();
		if (imageTitle) {
			$(imageTitle).update( this.items.first().down('img').alt );
		}
		// drawing 
		ul = $$('.slideshow-bar ul').first();
		if (ul){
			count = 0;	label = 1;
			this.items.each(function(e){
				li = document.createElement('li');
//				a.click = this.switchItem(count);
				$(li).update('<a href="javascript:void(0);" onclick="Carousel.switchItem('+count+');return false;">'+label+'</a>'); 
				ul.insert(li);
				count++; label++;
			});
		}
 */		
	}
}; 

function stopVideoCarousels(playerObj){
//try { console.log('-- OLD:'+playerObj.oldstate); console.log('-- NEW:'+playerObj.newstate);} catch (e) {}
	if (typeof document.carousels == 'undefined') return;
	if(playerObj.newstate == 'BUFFERING' || playerObj.newstate == 'PLAYING' ) {	// stopping all video carousels;
		for (var i=0; i< document.carousels.length; i++) {
			if (document.carousels[i].needStopOnClick()) {
				document.carousels[i].stop();
				document.activePlayerId = playerObj['id'];
			}	
		}		
	} 
}

var player;
function playerReady(obj) {
	player = document.getElementById(obj['id']);
//try{ console.log(player.id); } catch (e) { alert(id); }	
	player.addModelListener('STATE','stopVideoCarousels'); 	
};




