/**
 * @product     Shutterbox
 * @file        /public/js/shutterbox.js
 * @version     v1.0
 * @copyright   (c) 2010 mukutu GmbH (http://www.mukutu.de)       
 */

wait = false;

function Shutterbox(id, shuttertime) {
    this.id = id;
    this.shuttertime = shuttertime;
    
    this.holder = document.getElementById(this.id);
    this.images = this.holder.getElementsByTagName("img");
    this.counter = 0;

    // Events
    this.mouseoverAction = function() {
    	wait = true;
    }
    
    this.mouseoutAction = function() {
    	wait = false;
    }
    
    this.mousedownAction = function() {
    	wait = 'next';
    }
    
    if (window.addEventListener) { // Mozilla, Netscape, Firefox
        this.holder.addEventListener("mouseover", this.mouseoverAction, false);
        this.holder.addEventListener("mouseout",  this.mouseoutAction, false);
        this.holder.addEventListener("click",  this.mousedownAction, false);
    } else { // IE
    	this.holder.attachEvent('onmouseover', this.mouseoverAction);
    	this.holder.attachEvent('onmouseout', this.mouseoutAction);
    	this.holder.attachEvent('onclick', this.mousedownAction);
    }

    this.next = function() {
    	var shutter = this;
    	
	    if (this.images.length > 1) {
	    	// wait until mouseout
	    	if (wait==true) { 
	    		window.setTimeout(function() { shutter.next(); }, this.shuttertime);
	    	} else {
		    	for (i=0; i<this.images.length; i++) {
		    		this.images[i].style.display='none';
		    	}
		    	
		        if (this.counter < this.images.length-1) {
		        	this.counter++;
		        } else {
		        	this.counter=0;
		        }
		        
		        this.images[this.counter].style.display='';
		        window.setTimeout(function() { shutter.next(); }, this.shuttertime);
		        
		        if (wait=='next') {
		        	wait = true;
		        }
	    	}
	    }
    }
    
    this.start = function() {
        var shutter = this;
        window.setTimeout(function() { shutter.next(); }, this.shuttertime);
        
        this.preloader = document.getElementById('shutterbox_preloader');
        this.preloader.style.display='none';
    }
}
