var SlideShow;
window.addEvent('domready', function(){

	SlideShow = new SimpleSlideShowDemo({
            slides: $$('#show_image_fader img')
            ,controlTarget: $('image_control')
    });
        
	if (window.ie6) { // fix navigation li:hover for ie
		//alert('?');		 
		$('navigation').getElements('li').each(function(item, index) {
			item.addEvent('mouseenter', function() {				
				item.addClass('hover');
			});

			item.addEvent('mouseleave', function() {
				item.removeClass('hover');
			});	
		
		});
	}
	
	$$('a.new-window').each(function(item, index) {
	if ($defined(item.href) && item.href.length > 0) {
		item.addEvent('click', function(e) {
			e = new Event(e).stop();
			window.open(item.href, '_blank');
			return false;
		});
	}
	});
	
});

/*
function changeSelected(element)
{
 var el = $(element);
	var layers = $$('#show_image_fader img'); 
	layers.each(function(item, index) {
		var fadeFxOut = new Fx.Style(item, 'opacity').set(1);			
		fadeFxOut.start(0);
		//item.fade('out');	
	});
	
	var fadeFxIn = new Fx.Style(el, 'opacity').set(0);			
	fadeFxIn.start(1);
	//el.fade('in');
	//startGalleryRotation('#show_image_fader img');
	//stopGalleryRotation();

}
var s;
function stopGalleryRotation()
{
	window.clearInterval(s);
	setInterval(function() {startGalleryRotation('#show_image_fader img',5000)},2000);
	//TODO: work out how to start it again after a delay!!!!
}

function startGalleryRotation(selector,delay){
	var i = 0; 
	var layers = $$(selector); 
	if (layers.length > 1) //if there is a fader with images on the page
	{
		delay = (delay == undefined)? 7000:delay;
		s = setInterval(function(){
			var fadeFx = new Fx.Style(layers[i], 'opacity').set(1);			
			fadeFx.start(0);
			//layers[i].fade('out'); 
			i = (i == layers.length-1)? 0 : i+1;
			var fadeFxNext = new Fx.Style(layers[i], 'opacity').set(0);		
			fadeFxNext.start(1); 
			//layers[i].fade('in');
		
		},delay);
	}
}
*/

function ComingSoon(Show)
{
	var coming_soon = $("coming_soon");
  	if (parseInt(Show) == 1)
	{
		coming_soon.style.display="block";
		/*
		var pos = parseInt(Pos);		
		switch (pos) {
		  case 1:
		    coming_soon.style.left="750px";
		    break;
		  case 2:
		    coming_soon.style.left="750px";
		    break
		  case 3:
		    coming_soon.style.left="750px";
		    break;
		}	
		*/
	}
	else
	{
		coming_soon.style.display="none";
	}
}



var CycleThrough = function() {
		SlideShow.cycleForward();
}

var SimpleSlideShowDemo = new Class({
		periodical: null,
		options: {
			slides: [],
            startIndex: 0,
            onShow: Class.empty,
          	wrap: true,
          	controlTarget: null
        },
        initialize: function(options){
                this.setOptions(options)
                this.controlTarget = this.options.controlTarget;
                this.slides = [];
				this.effects = [];
				this.addSlides(this.options.slides);
				if(this.slides.length)
				{
					this.showSlide(this.options.startIndex);
				}
				periodical = CycleThrough.periodical(5000);
        },
        addSlides: function(slides){
                $$(slides).each(function(slide, index){
                        this.slides.include($(slide));
                        this.effects[this.slides.indexOf(slide)] = new Fx.Style(slide, 'opacity');
                        slide.addEvent('click', this.cycleForward.bind(this));
                        //slide.set('style', 'cursor:pointer');
                        var anchor	= new Element('p').setText(index + 1 + ' | ').injectInside(this.controlTarget).addEvent('click', function() {
							this.showSlide(index);
							//$clear(perodical);
						}.bind(this));
						
                }, this);
        },
        addSlide: function(slide){
                this.addSlides([slide]);
        },
        cycleForward: function(){
                if($chk(this.now) && this.now < this.slides.length-1) this.showSlide(this.now+1);
                else if (this.now && this.options.wrap) this.showSlide(0);
                else if(!$defined(this.now)) this.showSlide(this.options.startIndex);
        },

        cycleBack: function(){
                if(this.now > 0) this.showSlide(this.now-1);
                else if(this.options.wrap) this.showSlide(this.slides.length-1);
        },
        showSlide: function(iToShow){

                var now = this.now;
                var currentSlide = this.slides[now];
                var slide = this.slides[iToShow];

                function fadeIn(s){
                        s.setStyles({
                                display:'block',
                                visibility: 'visible',
                                opacity: 0
                        });
                        this.effects[this.slides.indexOf(s)].start(1);
                        this.fireEvent('onShow', [slide, iToShow]);
                };
                if(slide) {
                        if($chk(now) && now != iToShow){
                                this.effects[now].start(0).chain(function(){
                                        this.slides[now].setStyle('display', 'none');
                                        fadeIn.apply(this, [slide]);
                                }.bind(this));
                        } else fadeIn.apply(this, [slide]);
                        this.now = iToShow;
                }
                //periodical = CycleThrough.periodical(5000);

        }

});
SimpleSlideShowDemo.implement(new Options, new Events);





