/*
 @to-do: document and all images have to be set to visibility: hidden/opacity: 0 by using invisible class in content.css
 */

function dbugScripts(baseurl,libs){
	var value = document.cookie.match('(?:^|;)\\s*jsdebug=([^;]*)');
	var debugCookie = value ? unescape(value[1]) : false;
	if (window.location.href.indexOf("basePath=this")>0){
		var path=baseurl.substring(baseurl.substring(7,baseurl.length).indexOf("/")+8,baseurl.length);
		var href=window.location.href;
		baseurl=href.substring(href.substring(7,href.length).indexOf("/")+8,href.length);
	}
	if (window.location.href.indexOf("jsdebug=true")>0 || window.location.href.indexOf("jsdebugCookie=true")>0 || debugCookie == 'true'){ 
		if (libs) {
			for(var i=0;i<libs.length;i++){
				document.write("<scri"+"pt src=\""+baseurl+libs[i]+"\" type=\"text/javascript\"></scr"+"ipt>");
			}
		} else {
			document.write("<scri"+"pt src=\""+baseurl+"\" type=\"text/javascript\"></scr"+"ipt>");
		}
		return true;
	}
	return false;
};

if (!dbugScripts("http://www.clientcide.com/cnet.gf/svn/", ["/Source/Layout/SimpleCarousel.js","/Compatibility/Layout/SimpleCarousel.js"])) {

/*
Script: SimpleCarousel.js

Builds a carousel object that manages the basic functions of a generic carousel (a carousel	here being a collection of "slides" that play from one to the next, with a collection of "buttons" that reference each slide).

License:
	http://www.clientcide.com/wiki/cnet-libraries#license
*/
var SimpleCarousel = new Class({
	Implements: [Options, Events],
	options: {
//		onRotate: $empty,
//		onStop: $empty,
//		onAutoPlay: $empty,
//		onShowSlide: $empty,
		slideInterval: 4000,
		transitionDuration: 700,
		startIndex: 0,
		buttonOnClass: "selected",
		buttonOffClass: "off",
		rotateAction: "none",
		rotateActionDuration: 100,
		autoplay: true,
                extraButtons : '',
                extraButtonsAction : ''
	},
	initialize: function(container, slides, buttons, options){
		this.container = $(container);
		var instance = this.container.retrieve('SimpleCarouselInstance');                
		if (instance) return instance;
		this.container.store('SimpleCarouselInstance', this);
		this.setOptions(options);
		this.container.addClass('hasCarousel');
		this.slides = $$(slides);
		slides.each(function(slide){
		   slide.setStyle('opacity',0);
		   slide.setStyle('visibility','hidden');
		});
		this.buttons = $$(buttons);
		this.createFx();
                if (this.options.extraButtonsAction == '') {
                    this.options.extraButtonsAction = this.options.rotateAction;
                }
		this.showSlide(this.options.startIndex);
		if (this.options.autoplay) this.autoplay();
		if (this.options.rotateAction != 'none') this.setupAction(this.options.rotateAction);
		return this;
	},
	toElement: function(){
		return this.container;
	},
	setupAction: function(action) {
		
		this.buttons.each(function(el, idx){
			$(el).addEvent(action, function(ev) {
				/**
				 * custom by Luke Hoggett <luke@fatpublisher.com.au>
				 * ensures that links aren't triggered
				 */
                                if ($chk(ev)) ev.stop();
				this.slideFx.setOptions(this.slideFx.options, {duration: this.options.rotateActionDuration});
				if (this.currentSlide != idx) this.showSlide(idx);
				this.stop();
			}.bind(this));
		}, this);

                if (this.options.extraButtons != '') {
                   $$(this.options.extraButtons).each(function(el, idx){
                       $(el).addEvent(this.options.extraButtonsAction, function(ev) {
                            ev.stop();
                            this.stop();
                            this.slideFx.setOptions(this.slideFx.options, {duration: this.options.rotateActionDuration});                            
                            if (this.currentSlide != idx) this.showSlide(idx);
                       }.bind(this));
                    }, this);
                    var wrapper = $$(this.options.extraButtons).getParent('ul');
                    if ($chk(wrapper)) {
                        wrapper.addEvent('mouseout', function() {
                            this.stop();
                            this.autoplay();
                        }.bind(this));
                    }
                }
                
	},
	createFx: function(){
		if (!this.slideFx) this.slideFx = new Fx.Elements(this.slides, {duration: this.options.transitionDuration});
		this.slides.each(function(slide){
			slide.setStyle('opacity',0);
		});
	},
	showSlide: function(slideIndex){
		var action = {};
		this.slides.each(function(slide, index) {
			if (index == slideIndex && index != this.currentSlide){ //show
				$(this.buttons[index]).swapClass(this.options.buttonOffClass, this.options.buttonOnClass);
                                if (this.options.extraButtons != '') {
                                    $$(this.options.extraButtons)[index].swapClass(this.options.buttonOffClass, this.options.buttonOnClass);
                                }
				action[index.toString()] = {
					opacity: 1
				};
			} else {
				$(this.buttons[index]).swapClass(this.options.buttonOnClass, this.options.buttonOffClass);
                                if (this.options.extraButtons != '') {
                                    $$(this.options.extraButtons)[index].swapClass(this.options.buttonOnClass, this.options.buttonOffClass);
                                }
				action[index.toString()] = {
					opacity:0
				};
			}                        
		}, this);
		this.fireEvent('onShowSlide', slideIndex);
		this.currentSlide = slideIndex;
		this.slideFx.set(action);
		return this;
	},
	autoplay: function(){
		this.slideshowInt = this.rotate.periodical(this.options.slideInterval, this);
		this.fireEvent('onAutoPlay');
		return this;
	},
	stop: function(){
		$clear(this.slideshowInt);
		this.fireEvent('onStop');
		return this;
	},
	rotate: function(){
		var current = this.currentSlide;
		var next = (current+1 >= this.slides.length) ? 0 : current+1;
		this.showSlide(next);
		this.fireEvent('onRotate', next);
		return this;
	}
});

var CNETcarousel = new Class({
	Extends: SimpleCarousel,
	options:{
		slidesSelector: ".slide",
		buttonsSelector: ".button"
	},
	initialize: function(container, options){
		this.setOptions(options);
		container = $(container);
		slides = container.getElements(this.options.slidesSelector);
		buttons = container.getElements(this.options.buttonsSelector);
		this.parent(container, slides, buttons, options);
	}
});
var CNETcarouselWithButtons = new Class({
	Extends: SimpleCarousel,
	options: {
		bubbleButtonBGImgSelector: '.bbg',
		buttonOnGifSrc: 'http://i.i.com.com/cnwk.1d/i/fd/c/green_button.gif',
		buttonOffGifSrc: 'http://i.i.com.com/cnwk.1d/i/fd/c/gray_button.gif'
	},
	showSlide: function(slideIndex){
		this.buttons.each(function(button, index){
			$(button).getElement(this.options.bubbleButtonBGImgSelector).src = (index == slideIndex)?this.options.buttonOnGifSrc:this.options.buttonOffGifSrc;
		}, this);
		this.parent(slideIndex);
	}
});
var carousel = null;
window.addEvent('domready', function(){
	if ($('Carousel')) {
		carousel = new CNETcarouselWithButtons($('Carousel'),{buttonsSelector:'.bubble', rotateAction:'mouseover'});
	}
});


}