/**
* @author Asvin
*/

/**
 * Custom inital load handler. Called when the carousel loads the initial
 * set of data items. Specified to the carousel as the configuration
 * parameter: loadInitHandler
 **/
var loadInitialItems = function(type, args) {
    var start = args[0];
    var last = args[1]; 
    load(this, start, last);    
};


/**
 * Custom load next handler. Called when the carousel loads the next
 * set of data items. Specified to the carousel as the configuration
 * parameter: loadNextHandler
 **/
var loadNextItems = function(type, args) {
    var start = args[0];
    var last = args[1]; 
    var alreadyCached = args[2];
    if(!alreadyCached) {
        load(this, start, last);
    }
};

/**
 * Custom load previous handler. Called when the carousel loads the previous
 * set of data items. Specified to the carousel as the configuration
 * parameter: loadPrevHandler
 **/
var loadPrevItems = function(type, args) {
    var start = args[0];
    var last = args[1]; 
    var alreadyCached = args[2];
    if(!alreadyCached) {
        load(this, start, last);
    }
};

var load = function(carousel, start, last) {
    var tabScreenPlayer = YAHOO.util.Dom.getElementsByClassName('screenplayer', 'div');
    var parent = YAHOO.util.Dom.get('manchette');
	carousel.setProperty("size", tabScreenPlayer.length,true);
	carousel.setProperty('autoplay', 10000);
	carousel.setProperty('wrap', true);
	for(var i=0;i<tabScreenPlayer.length;i++){
		carousel.addItem(i+1, tabScreenPlayer[i].innerHTML);
		// remove element from dom
		parent.removeChild(tabScreenPlayer[i]);
	}
	
};

var changePage = function(e, args) {
    var carousel = args[0];
    var pageNum = args[1];
    carousel.scrollTo(pageNum);
};

/**
 * You must create the carousel after the page is loaded since it is
 * dependent on an HTML element (in this case 'dhtml-carousel'.) See the
 * HTML code below.
 **/
 // for ease of debugging; globals generally not a good idea
var pageLoad = function() {
	YAHOO.util.Dom.get('dhtml-carousel').innerHTML = '<div class="carousel-clip-region"><ul class="carousel-list"></ul></div>';
	carousel = new YAHOO.extension.Carousel("dhtml-carousel", {
																numVisible: 1,
																animationSpeed: 0.5,
																//autoplay: 1000,
																//size: 2,
																scrollInc: 1,
																navMargin: 0,
																//animationMethod: YAHOO.util.Easing.backBoth,
																loadInitHandler:   loadInitialItems,
																//loadNextHandler:   loadNextItems,
																//loadPrevHandler:   loadPrevItems,
																nextElement: 'manchette-next'
																//prevElement: 'manchette-prev'
																//wrap: true
																}
	);
    
    YAHOO.util.Event.addListener("manchette-prev", "click", changePage, [carousel, 100]);
    YAHOO.util.Event.addListener("manchette-pause", "click", function(){carousel.stopAutoPlay();});
};

var changeBackground = function(o, params){
	try{
		YAHOO.util.Dom.setStyle(params.id, 'background', 'url(' + params.sprite + ') no-repeat ' + params.positionX + 'px '+ params.positionY + 'px'); 
	}
	catch(e){}
};

var changeTabForeground = function(o, params){
	try{
		YAHOO.util.Dom.setStyle(params.id, 'color', '#ff6600');
		if(params.id == 'search-web'){
			YAHOO.util.Dom.setStyle('search-images', 'color', '#fff');
			YAHOO.util.Dom.setStyle('search-video', 'color', '#fff');
			YAHOO.util.Dom.get('module').value = 'web';
		}
		else if(params.id == 'search-images'){
			YAHOO.util.Dom.setStyle('search-web', 'color', '#fff');
			YAHOO.util.Dom.setStyle('search-video', 'color', '#fff');
			YAHOO.util.Dom.get('module').value = 'image';
		}
		else{
			YAHOO.util.Dom.setStyle('search-web', 'color', '#fff');
			YAHOO.util.Dom.setStyle('search-images', 'color', '#fff');
			YAHOO.util.Dom.get('module').value = 'video';
		}
	}
	catch(e){}
};

var showElement = function(o, params){
	YAHOO.util.Dom.setStyle(params.id, 'display', 'block');
};

var popup = function(o, params){
	aWindow = window.open(params.url,"Orange","toolbar=no,width="+params.width+",height="+params.height+", status=no,scrollbars="+params.scrollbars+",resize=no,menubars=no");
};

YAHOO.util.Event.addListener(window, 'load', pageLoad);
YAHOO.util.Event.addListener('search-web', 'click', changeBackground, {id : 'search', sprite : 'http://www.orangemali.com/images/searchtab1.jpg', positionX : 0, positionY : 0});
YAHOO.util.Event.addListener('search-web', 'click', changeTabForeground, {id : 'search-web'});
YAHOO.util.Event.addListener('search-images', 'click', changeBackground, {id : 'search', sprite : 'http://www.orangemali.com/images/searchtab2.jpg', positionX : 0, positionY : 0});
YAHOO.util.Event.addListener('search-images', 'click', changeTabForeground, {id : 'search-images'});
YAHOO.util.Event.addListener('search-video', 'click', changeBackground, {id : 'search', sprite : 'http://www.orangemali.com/images/searchtab3.jpg', positionX : 0, positionY : 0});
YAHOO.util.Event.addListener('search-video', 'click', changeTabForeground, {id : 'search-video'});