function mail(start, end){
    return start + '@' + end;
}

function isBadBrowser(){
    return window.ie6;
}

window.onload = function(){

    var baseWidth;
    var baseHeight;
    
    var rollLink = $ES('img.roll_image');
    //var bgimage = $ES('.background-image');
    
    var c = 0;
    
    for (var i = 0; i < rollLink.length; i++) {
    
        rollLink[i].addEvent('mouseenter', function(){
            // actual mouseover action				
            this.src = this.src.replace('-off-', '-on-');
            // effect is optional				
            var opacityChange = new Fx.Style(this, 'opacity', {
                duration: 500
            });
            opacityChange.start(0, 1);
        });
        rollLink[i].addEvent('mouseleave', function(){
            // actual mouseover action
            this.src = this.src.replace('-on-', '-off-');
            // effect is optional
            var opacityChange = new Fx.Style(this, 'opacity', {
                duration: 500
            });
            opacityChange.start(0, 1);
        });
        
    }
	
    /* slide show image */
    var slideshowImage = $ES('.slideshow-image');
    if (!isBadBrowser()) {
        for (var i = 1; i < slideshowImage.length; i++) {
            slideshowImage[i].setStyles("");
            slideshowImage[i].setStyle("position", "absolute");
            slideshowImage[i].setStyle("width", slideshowImage[0].getCoordinates().width);
            slideshowImage[i].setStyle("left", slideshowImage[0].getCoordinates().left);
            slideshowImage[i].setStyle("top", slideshowImage[0].getCoordinates().top);
            slideshowImage[i].setOpacity(0);
        }
    }
    
    opacitySlide();
};

var SLIDE_SHOW_TIME = 400;

var pictureInView;
function switchVisibility(itemToHide, itemToView){	
    if (!isBadBrowser()) {
        var opacityChange = new Fx.Style(itemToHide, 'opacity', {
            duration: SLIDE_SHOW_TIME
        });
        opacityChange.start(1, 0);
        var opacityChange = new Fx.Style(itemToView, 'opacity', {
            duration: SLIDE_SHOW_TIME
        });
        opacityChange.start(0, 1);
    }
    else {
        itemToHide.setStyle("display", "none");
        itemToView.setStyles("");
    }
}

function hideItem(itemToHide){
    var opacityChange = new Fx.Style(itemToHide, 'opacity', {
        duration: SLIDE_SHOW_TIME
    });
    opacityChange.start(1, 0);
}

function visibleItem(itemToVisible){
    var opacityChange = new Fx.Style(itemToVisible, 'opacity', {
        duration: SLIDE_SHOW_TIME
    });
    opacityChange.start(0, 1);
}

/* slide */

var slidePos = 0;
var increment = 1;

function animSlide() {
	slidePos=slidePos+increment;
	var slideContainer = $ES('.thumbnails-slide .images');
	slideContainer[0].setStyle("margin-left", slidePos+"px");
	opacitySlide();
}

var change = "*";
function animSlideToCurrentImage() {
		if (pictureInView) {
			var slideContainer = $ES('.thumbnails-slide');
			var center = slideContainer[0].getCoordinates().left + slideContainer[0].getCoordinates().width / 2;
			var imageCenter = pictureInView.getCoordinates().left + pictureInView.getCoordinates().width / 2
			
			increment = -Math.ceil((imageCenter - center) / 8);
			
			if (change == "*") {
				change = "+";
			}
			else {
				change = "*";
			}
		}
		
		slidePos = slidePos + increment;
		var slideContainer = $ES('.thumbnails-slide .images');
		slideContainer[0].setStyle("margin-left", slidePos + "px");
		opacitySlide();	
}

function opacitySlide(){
    /* slide show image */
    var slideContainer = $ES('.thumbnails-slide');
	var center = slideContainer[0].getCoordinates().width/2;
	var left = slideContainer[0].getCoordinates().left;
    var slideImage = $ES('.thumbnails-slide .slide-image img');
	
	if (slideImage[0].getCoordinates().left > slideContainer[0].getCoordinates().left+center) {
		increment = -increment;
		if (slideImage[slideImage.length - 1].getCoordinates().right < slideContainer[0].getCoordinates().right-center) {
			increment = 0;
		}
	} else if (slideImage[slideImage.length-1].getCoordinates().right < slideContainer[0].getCoordinates().right-center) {
		increment = -increment;
		
	}
    for (var i = 0; i < slideImage.length; i++) {
		var relativePos = (slideImage[i].getCoordinates().left+slideImage[i].getCoordinates().width/2) - left;		
		if (relativePos > 0) {
			
			var dist = relativePos - center;
			if (dist<0) {
				dist=-dist;
			}			
			var opacity = (center-dist) / center;
			if (opacity < 0) {
				opacity = 0;
			}
			
			slideImage[i].setOpacity(opacity);
		}		
    }    
}

/** 3D THUMB **/

var Thumbnails3D = new Class ({
	
	options: {
		container: $ES('body')[0],
		images: $ES('img')
		},
		
	init: function() {
		
		/*this.options.container.setStyle("margin", "0px"); // debug
		this.options.container.setStyle("padding", "0px"); // debug
		//this.options.container.setStyle("position", "absolute"); // debug
		//this.options.container.setStyle("left", this.options.container.getLeft()); // debug
		this.options.container.setStyle("border", "2px red solid"); // debug*/
				
		var step = Math.PI*2 / this.options.images.length;
		
		this.startx=this.options.container.getCoordinates().left;
		this.starty=this.options.container.getCoordinates().top;
		
		this.centerx=this.options.container.getCoordinates().width/2+this.options.container.getCoordinates().left;
		
		/*for (var i = 0; i < this.options.images.length; i++) {
			this.options.images[i].setStyle("position", "absolute");
			this.options.images[i].setStyle("left", this.options.container.getPosition().x + "px");
		}

		return false; // debug*/ 
		
		this.r=this.options.container.getCoordinates().width/2;
		this.options.container.setStyle("height", this.r*1.5+"px");
		this.options.container.setStyle("overflow", "hidden");		
		this.centery=this.options.container.getCoordinates().height/2+this.options.container.getCoordinates().top;
		
		this.step = 0;
		this.direction = 1;	
		this.imageTarget = 0;
		
		this.maxImageWidth = 0;
		this.maxImageHeight = 0;
		this.maxReduction = (this.r / 200) + 1;
				
		for (i=0; i<this.options.images.length; i++) {			
			
			this.options.images[i].baseWidth = this.options.images[i].getCoordinates().width;
			this.options.images[i].baseHeight = this.options.images[i].getCoordinates().height;
			
			this.options.images[i].posy = 10;
			this.options.images[i].num = i;
			this.options.images[i].animation = this;
			
			this.options.images[i].angle = (i*step-Math.PI/2);
			this.options.images[i].posx = this.r*Math.cos(this.options.images[i].angle);
			this.options.images[i].posz = this.r*Math.sin(this.options.images[i].angle)+this.r;

			this.options.images[i].setStyle("position", "absolute");
			this.options.images[i].setStyle("display", "block");
			this.options.images[i].setStyle("margin", "0");
			this.options.images[i].setStyle("padding", "0");
						
			this.options.images[i].addEvent('click', function(){
				this.animation.imageTarget = this.num;
				this.animation.imageClicked = true;
			});
			
			if (this.maxImageWidth < this.options.images[i].getCoordinates().width) {
					this.maxImageWidth = this.options.images[i].getCoordinates().width;
					this.r=this.options.container.getCoordinates().width/2-this.maxImageWidth/4;
			}
			
			if (this.maxImageHeight < this.options.images[i].getCoordinates().height) {
					this.maxImageHeight = this.options.images[i].getCoordinates().height;
					this.centery=this.options.container.getCoordinates().height/2+this.options.container.getCoordinates().top-this.maxImageHeight/8;					
			}
			
			this.options.images[i].addEvent('load', function(){
				this.setStyle("width", "auto");
				this.setStyle("height", "auto");
				this.setStyle("z-index", "-1"); /* no flick when image loader */
				this.baseWidth = this.getCoordinates().width;
				this.baseHeight = this.getCoordinates().height;
				if (this.animation.maxImageWidth < this.getCoordinates().width) {
					this.animation.maxImageWidth = this.getCoordinates().width;
					this.animation.r=this.animation.options.container.getCoordinates().width/2 - this.animation.maxImageWidth/4;
				}
				if (this.animation.maxImageHeight < this.getCoordinates().height) {
					this.animation.maxImageHeight = this.getCoordinates().height;
					this.animation.centery=this.animation.options.container.getCoordinates().height/2+this.animation.options.container.getCoordinates().top-this.animation.maxImageHeight/8;				
				}
			});

		}
		
	},
	projection: function(img) {
		
		var mx=1;
		var my=1;
		
		var Distance = 10;
		
		var reduction = (img.posz / 200) + 1;
		
		var previousLeft = img.getCoordinates().left;
			
		img.setStyle("width", img.baseWidth/reduction);
		img.setStyle("height", img.baseHeight/reduction);
		img.setStyle("z-index", this.r*2-Math.round(img.posz) );
		
		var x = img.posx - ((mx / (img.posz + Distance)) * img.posx);
		var y = img.posy - ((mx / (img.posz + Distance)) * img.posy);

		img.setStyle("left", x+this.centerx-img.getCoordinates().width/2);
		img.setStyle("top", this.centery-y-img.getCoordinates().height/2);

	},
	animate: function() {

		var nextStep = 0;

		for (var i=0; i<this.options.images.length; i++) {
			this.options.images[i].angle=this.options.images[i].angle+this.step;
			this.options.images[i].posx = this.r*Math.cos(this.options.images[i].angle);
			this.options.images[i].posz = this.r*Math.sin(this.options.images[i].angle)+this.r;
			this.options.images[i].posy = (this.r/2)*Math.sin(this.options.images[i].angle);
			this.projection(this.options.images[i]);
			
			if (i==this.imageTarget) {				
				if (this.imageClicked) {
					if ((this.options.images[i].getCoordinates().left+this.options.images[i].getCoordinates().width/2>this.centerx)) {
						this.direction = -1;
					} else {
						this.direction = 1;
					}
				}
				if (this.options.images[i].posz > 0) {
					nextStep = (Math.log(this.options.images[i].posz*4)/(this.r/4)) * this.direction;
				}
				
				this.imageClicked = false;
			}
		}
		this.step = nextStep;
	},

	initialize: function(options) {
		this.options = options;
		this.init();
		this.animate.periodical(75, this);
	}
});

/** preview edit **/

var PreviewEdit = new Class ({
	
	initialize: function(options) {
		var components = $ES(".editable-component");
		for (var i=0; i<components.length; i++) {
			var component = components[i];
			component.dcID = component.getProperty("id").substring(3)
			component.addEvent("click", function() {
				var id = this.dcID;
				if (this.getProperty("href") != null) {
					this.setProperty("href", "#"); /* desactive link */
				}
				var url = ajaxURL+"?webaction=editone&number="+id;
				editComponent(url);
				return false;
			});
		}
	}
	
});


