var Carrousel = Class.create({
 initialize: function(element){
 this.mainElement = element;
 this.images = new Array();
 this.animationTimer = 10000;
 this.animationDuration = 2000;
 this.animation = true;
 this.paused = false;
 this.previousSelectedImage = 0;
 this.selectedImage = 0;
 var elements = jQuery(this.mainElement).children('div');
 var inst = this;
 var imageCounter = 0; 
 for(var i = 0; i < elements.length; i++)
 {
 if(elements[i].getAttribute("id") != null && elements[i].getAttribute("id") != "")
 {
 this.images[imageCounter] = jQuery(elements[i]); 
 imageCounter++;
 }
 else
 {
 if(jQuery(elements[i]).hasClass('carrousel-control-panel'))
 {
 this.carrousel = jQuery(elements[i]);
 }
 }
 }

 if(this.images.length > 1)
 {
 jQuery('div.teaser-skipper > div.teaser-skip-backward').first().click(function(){inst.previous();});
 jQuery('div.teaser-skipper > div.teaser-skip-foreward').first().click(function(){inst.next();});
 jQuery('div.teaser-tracker > div.teaser-track-pause').first().click(function(){inst.toggleAutoAnimate(true);});
 var tracks = jQuery('div.teaser-tracker > div.teaser-track');
 this.tracks = tracks;
 this.setActivTrackButton();
 jQuery('.badge', this.mainElement).hover(function(){inst.toggleAutoAnimate(false)});
 for(j = 0; j < tracks.length; j++)
 {
 jQuery(tracks[j]).bind("click",{param1: j},function(event){inst.tracker(event.data.param1);});
 }

 this.timer = setTimeout(function(){inst.animate()}, this.animationTimer);
 }
 },
 next: function(){
 var next = null;
 if((this.images.length - 1) == this.selectedImage)
 {
 next = 0;
 }
 else
 {
 next = this.selectedImage+1;
 }

 this.tracker(next);
 },

 previous: function(){
 var previous = null;
 if(this.selectedImage == 0)
 {
 previous = this.images.length - 1;
 }
 else{
 previous = this.selectedImage - 1;
 }
 
 this.tracker(previous);
 },

 tracker: function(num)
 {
 if(this.paused != true){
 var inst = this;
 clearTimeout(this.timer);
 this.timer = setTimeout(function(){inst.animate()}, this.animationTimer);
 }
 if(num != this.selectedImage)
 { 
 jQuery('.badge', this.images[this.selectedImage]).fadeOut(this.animationDuration);
 this.images[this.selectedImage].fadeOut(this.animationDuration);

 jQuery('.badge', this.images[num]).fadeIn(this.animationDuration);
 this.images[num].fadeIn(this.animationDuration);
 this.previousSelectedImage = this.selectedImage;
 this.selectedImage = num;
 this.setActivTrackButton();

 }
 },

 setActivTrackButton: function()
 {
 jQuery(this.tracks[this.previousSelectedImage]).removeClass('activated');
 jQuery(this.tracks[this.selectedImage]).addClass('activated');
 },

 animate: function()
 {
 this.next(); 
 },

 toggleAutoAnimate: function(pauseButton)
 {
 if(this.animation == true)
 {
 clearTimeout(this.timer);
 this.animation = false;
 if(pauseButton == true)
 {
 this.paused = true;
 }
 }
 else
 {
 if(((pauseButton == true) && (this.paused == true)) || (this.paused == false))
 {
 var inst = this;
 clearTimeout(this.timer);
 this.timer = setTimeout(function(){inst.animate()}, this.animationTimer);
 this.animation = true;
 }
 }
 }
})  


/*
 * Copyright (c) 2006 Jonathan Weiss <jw@innerewut.de>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 * 
 * tooltip-0.2.js - Small tooltip library on top of Prototype 
 * by Jonathan Weiss <jw@innerewut.de> distributed under the BSD license.
 */

var Tooltip = Class.create();
Tooltip.prototype = {
 initialize: function(elements, tool_tip) {
 var options = Object.extend({
 default_css: false,
 margin: "0px",
 padding: "2px 5px",
 backgroundColor: "#d6d6fc",
 min_distance_x: 5,
 min_distance_y: 5,
 delta_x: 0,
 delta_y: 0,
 zindex: 1337
 }, arguments[2] || {});

 this.elements = $(elements);

 this.options = options;
 
 // use the supplied tooltip element or create our own div
 if($(tool_tip)) {
 this.tool_tip = $(tool_tip);
 } else {
 this.tool_tip = $(document.createElement("div"));
 document.body.appendChild(this.tool_tip);
 this.tool_tip.addClassName("tooltip-bubble");
 }

 // hide the tool-tip by default
 this.tool_tip.hide();

 //this.eventMouseOver = this.showTooltip.bindAsEventListener(this);
 //this.eventMouseOut = this.hideTooltip.bindAsEventListener(this);
 this.eventMouseMove = this.moveTooltip.bindAsEventListener(this);

 this.registerEvents();
 },

 destroy: function() {
 //Event.stopObserving(this.elements, "mouseenter", this.eventMouseOver);
 //Event.stopObserving(this.elements, "mouseleave", this.eventMouseOut);
 Event.stopObserving(this.elements, "mousemove", this.eventMouseMove);
 },

 registerEvents: function() {
 inst = this;
 this.elements.each(function(el) {
 Event.observe(el, "mouseenter", function() {
 inst.showTooltipByElement(el);
 });
 Event.observe(el, "mouseleave", function() {
 inst.hideTooltipByElement(el);
 });

 /*Event.observe(el, "mousemove", function() {
 inst.moveTooltipByElement(el);
 });*/
 
 //Event.observe(el, "mouseenter", inst.eventMouseOver);
 //Event.observe(el, "mouseleave", inst.eventMouseOut);
 Event.observe(el, "mousemove", inst.eventMouseMove);
 });
 },

 moveTooltip: function(event){
 Event.stop(event);
 // get Mouse position
 var mouse_x = Event.pointerX(event);
 var mouse_y = Event.pointerY(event);
 
 // decide if wee need to switch sides for the tooltip
 var dimensions = Element.getDimensions( this.tool_tip );
 var element_width = dimensions.width;
 var element_height = dimensions.height;
 
 if ( (element_width + mouse_x) >= ( this.getWindowWidth() - this.options.min_distance_x) ){ // too big for X
 mouse_x = mouse_x - element_width;
 // apply min_distance to make sure that the mouse is not on the tool-tip
 mouse_x = mouse_x - this.options.min_distance_x;
 } else {
 mouse_x = mouse_x + this.options.min_distance_x;
 }

 if ( (element_height + mouse_y) >= ( this.getWindowHeight() - this.options.min_distance_y) ){ // too big for Y
 mouse_y = mouse_y - element_height;
 // apply min_distance to make sure that the mouse is not on the tool-tip
 mouse_y = mouse_y - this.options.min_distance_y;
 } else {
 mouse_y = mouse_y + this.options.min_distance_y;
 }

 // now set the right styles
 this.setStyles(mouse_x, mouse_y);
 },
 
 
 showTooltip: function(event) {
 var element = event.findElement('.tooltip');
 var title = element.getAttribute('title');
 this.tool_tip.innerHTML = title;
 this.tool_tip.show();
 element.setAttribute('title', '');
 },

 showTooltipByElement: function(el) {
 var title = el.getAttribute('title');
 this.tool_tip.innerHTML = title;
 this.tool_tip.show();
 el.setAttribute('title', '');
 },
 
 setStyles: function(x, y){
 // set the right styles to position the tool tip
 Element.setStyle(this.tool_tip, {
 position:'absolute',
 top:y + this.options.delta_y + "px",
 left:x + this.options.delta_x + "px",
 zindex:this.options.zindex
 });
 
 // apply default theme if wanted
 if (this.options.default_css){
 Element.setStyle(this.tool_tip, {
 margin:this.options.margin,
 padding:this.options.padding,
 backgroundColor:this.options.backgroundColor,
 zindex:this.options.zindex
 });
 }
 },

 hideTooltip: function(event){
 var title = this.tool_tip.innerHTML;
 event.findElement('.tooltip').setAttribute('title', title);
 this.tool_tip.innerHTML = '';
 this.tool_tip.hide();
 },

 hideTooltipByElement: function(el){
 var title = this.tool_tip.innerHTML;
 el.setAttribute('title', title);
 this.tool_tip.innerHTML = '';
 this.tool_tip.hide();
 },

 getWindowHeight: function(){
 var innerHeight;
 if (navigator.appVersion.indexOf('MSIE')>0) {
 innerHeight = document.body.clientHeight;
 } else {
 innerHeight = window.innerHeight;
 }
 return innerHeight - 5000;
 },
 
 getWindowWidth: function(){
 var innerWidth;
 if (navigator.appVersion.indexOf('MSIE')>0) {
 innerWidth = document.body.clientWidth;
 } else {
 innerWidth = window.innerWidth;
 }
 return innerWidth - 30;
 }

}


document.observe("dom:loaded", function() {
 $$('.teaser-box-inner').each(function(element) {
 element.setStyle({cursor:'pointer'});
 element.observe('click', function(event) { if(event.target.down('a')) {
 window.location = event.target.down('a').readAttribute('href') }
 }
 );
 });
});  
