/********************************
LOGOS
********************************/
var brandTrayTotal = 15;
var brandTrayWidth = (115 * brandTrayTotal) +'px';
var brandTrayWidthRight = '-'+(115 * brandTrayTotal)+'px';
var brandTrayCounter = 0; 

brandTray = function(dir) { 
    if(brandTray != 0) {
        //move left
        if(dir != 0) {
            if(brandTrayCounter == 0) { 
                $('brandLogosGroup2').style.left = brandTrayWidth 
            }
            //reset the photo containers and the counter 
            if(brandTrayCounter == (brandTrayTotal-(brandTrayTotal*2))) { 
                $('brandLogosGroup1').style.left = '0px'; 
                $('brandLogosGroup2').style.left = brandTrayWidth;
                brandTrayCounter = 0
            }    
            brandTrayCounter = brandTrayCounter - 1;
            Effect.MoveBy('brandLogosGroup1', 0, -115, { 
                beforeStart: function() { move(0); }, 
                afterFinish: function() { move(1); }, 
                duration:1
            }); 
            Effect.MoveBy('brandLogosGroup2', 0, -115, {
                duration:1
            });            
        } 
        else { 
            //move right
            if(brandTrayCounter == 0) { 
                $('brandLogosGroup2').style.left = brandTrayWidthRight; 
            }
            //reset the photo containers and the counter 
            if(brandTrayCounter == brandTrayTotal) { 
                $('brandLogosGroup1').style.left = '0px'; 
                $('brandLogosGroup2').style.left = brandTrayWidthRight;
                brandTrayCounter = 0
            }
            brandTrayCounter = brandTrayCounter + 1;
            Effect.MoveBy('brandLogosGroup1', 0, 115, { 
                beforeStart: function() { move(0); }, 
                afterFinish: function() { move(1); }, 
                duration:1
            }); 
            Effect.MoveBy('brandLogosGroup2', 0, 115, {
                duration:1
            }); 
        }
    }
};   

move = function(active) {    
    if(active == 0) { 
        $('advance').onclick =  null;
        $('rewind').onclick =  null;
    }
    if(active == 1) {
        $$('.control').invoke('observe', 'mouseover', kill);
        $$('.control').invoke('observe', 'mouseout', resurrect);
        $('advance').onclick =  brandTray;
        $('rewind').onclick =  new Function("brandTray("+0+")");
    }
};

kill = function(){
    brands.stop();
    //alert('bang! your dead');
}

resurrect = function(){
    brands = new PeriodicalExecuter(brandTray, 2);
    //alert("I'm alive!");
}

Event.observe(window, 'load', function() {
	$('brandLogosGroup1').style.width = (115 * brandTrayTotal)+'px';
    $('brandLogosGroup2').style.width = (115 * brandTrayTotal)+'px';
    brands = new PeriodicalExecuter(brandTray, 2);
});

/********************************
NEWS
********************************/
var Slides = Class.create({
	initialize: function(element, options) {		
		this.options = {
      		Duration: 0.2,
			Delay: 8.0,
			Random: true,
			Slideshow:false,
			Controls:true
    	}
		Object.extend(this.options, options || {});
    	this.element        = $(element);
		this.slides			= this.element.childElements();
		this.num_slides		= this.slides.length;		
		this.current_slide 	= (this.options.Random) ? (Math.floor(Math.random()*this.num_slides)) : 0;
		this.end_slide		= this.num_slides - 1;
		this.slides.invoke('hide');
		this.slides[this.current_slide].show();	
		if (this.options.Slideshow) { 
			this.startSlideshow();
		}
		
		if (this.options.Controls) {
			this.addControls();
		}		
	},
	addControls: function() {
		this.btn_previous	= $('previous');
		this.btn_next 		= $('next');
		//this.btn_start		= $('start');
		//this.btn_stop		= $('stop');
		
		this.btn_previous.observe('click', this.moveToPrevious.bindAsEventListener(this));
		this.btn_next.observe('click', this.moveToNext.bindAsEventListener(this));
		//this.btn_start.observe('click', this.startSlideshow.bindAsEventListener(this));
		//this.btn_stop.observe('click', this.stopSlideshow.bindAsEventListener(this));
		
		
	},
	startSlideshow: function(event) {
		if (event) { Event.stop(event); }
		if (!this.running)	{
			this.fadeStartBtn();
			this.executer = new PeriodicalExecuter(function(){
	  			this.updateSlide(this.current_slide+1);
	 		}.bind(this),this.options.Delay);
			this.running=true;
		}
		
	},
	fadeStartBtn: function() {
		var startBtn = $('start');
		var stopBtn = $('stop');
		Effect.Fade(startBtn, { duration: 0.3 }),
		Effect.Appear(stopBtn, { duration: 0.3 }) 
	},
	stopSlideshow: function(event) {	
		if (event) { Event.stop(event); } 
		if (this.executer) {
			this.fadeStopBtn();
			this.executer.stop();
			this.running=false;
		}	 
	},
	fadeStopBtn: function() {
		var startBtn = $('start');
		var stopBtn = $('stop');
		Effect.Fade(stopBtn, { duration: 0.3 }),
		Effect.Appear(startBtn, { duration: 0.3 }) 
	},
	moveToPrevious: function (event) {
		if (event) { Event.stop(event); }
		//this.stopSlideshow();
  		this.updateSlide(this.current_slide-1);
	},
	moveToNext: function (event) {
		if (event) { Event.stop(event); }
		//this.stopSlideshow();
  		this.updateSlide(this.current_slide+1);
	},
	updateSlide: function(next_slide) {
		if (next_slide > this.end_slide) { 
				next_slide = 0; 
		} 
		else if ( next_slide == -1 ) {
				next_slide = this.end_slide;
		}
		
		this.fadeInOut(next_slide, this.current_slide);		
	},
 	fadeInOut: function (next, current) {		
		new Effect.Parallel([
			new Effect.Fade(this.slides[current], { sync: true }),
			new Effect.Appear(this.slides[next], { sync: true }) 
  		], { duration: this.options.Duration });
		
		this.current_slide = next;		
	}
});

/********************************
FIRE IT UP
********************************/
document.observe('dom:loaded', function(){
	if(!(navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
        var listPath = "/flash/ofbiz-development.xml";
        var location = '/flash/ofbiz-development.swf' + '?' + 'xmlPath' + '=' + listPath;
        new Swiff(location, {container:'flashContent', width:'953', height:'387'});
    }
	var newsBox = $('news').down('ul');
	new Slides(newsBox);
});