function $c(msg) {
	if(typeof console != 'undefined') console.log(msg);
}

Event.delegate = function(rules) {
  return function(e) {
      var element = $(e.element());
      for (var selector in rules)
        if (element.match(selector)) return rules[selector].apply(this, $A(arguments));
    }
}

function changeOpacity(el,opacity) {
  var image = document.getElementById(el);
  // For Mozilla
  image.style.MozOpacity = (opacity / 100);
  // For IE
  image.style.filter = "alpha(opacity=" + opacity + ")";
  // For others
  image.style.opacity = (opacity / 100);

  if(opacity > 0)
	image.style.display = 'block';
  else
	image.style.display = 'none';
}

function fade(el,milli,start,end) {
  var fadeTime = Math.round(milli/100);
		
  var i = 0;  // Fade Timer
  // Fade in
  if(start < end) {
    for(j = start; j <= end; j++) {
      // define the expression to be called in setTimeout()
      var expr = "changeOpacity('" + el + "'," + j + ")";
      var timeout = i * fadeTime;
      // setTimeout will call 'expr' after 'timeout' milliseconds
      setTimeout(expr,timeout);
      i++;
    }
  }
  // Fade out
  else if(start > end) {
    for(j = start; j >= end; j--) {
      var expr = "changeOpacity('" + el + "'," + j + ")";
      var timeout = i * fadeTime;
      setTimeout(expr,timeout);
      i++;
    }
  }
}

/* toggle() checks to see if the images has already been faded
   or not and sends the appropriate variables to opacity(); */
function toggle(el,milli) {
  // Get the opacity style parameter from the image
  var currOpacity = document.getElementById(el).style.opacity;
  if(currOpacity != 0) { // if not faded
    fade(el, milli, 100, 0);
  } else { // else the images is already faded
    fade(el, milli, 0, 100);
  }
} 

var currentSlide = 0;
function swapSlides() {
	//alert('swapTestimonials');
	var elements = $$('.slide');
	var previousSlide = currentSlide;
	currentSlide++;
	currentSlide = currentSlide >= elements.length ? 0 : currentSlide;
	//console.log('testimonial-item-' + previousTestimonial);
	//console.log('testimonial-item-' + currentTestimonial);
	fade('slide-' + previousSlide, 1000, 100, 0);
	fade('slide-' + currentSlide, 1000, 0, 100);
}

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}



//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


var HomeSlideshow = Behavior.create({
	initialize: function(options) {
		if($('slideshow')) {
			if($$('#slideshow .slide').length > 1) {
				var timeout = setInterval("swapSlides()", 5000);
			}
		}
	}
});

var ControlledSlideshow = Behavior.create({
	initialize: function() {
		this.currentSlide = 0;
		this.slides = this.element.select('.slide');
		this.totalSlides = this.slides.length
	},
	onclick: Event.delegate({
		'.prev-slide': function() {
			var nextSlide = this.currentSlide;
			this.currentSlide--;
			this.currentSlide = this.currentSlide < 0 ? 0 : this.currentSlide;
			this._fadeBetween(this.slides[nextSlide].id, this.slides[this.currentSlide].id)
			this._updateControls();
		},
		'.next-slide': function(e) {
			var previousSlide = this.currentSlide;
			this.currentSlide++;
			this.currentSlide = this.currentSlide >= this.slides.length ? 0 : this.currentSlide;
			this._fadeBetween(this.slides[previousSlide].id, this.slides[this.currentSlide].id)
			this._updateControls();
		}	
	}),
	_updateControls: function() {
		var prevLink = '<a href="javascript:;" class="prev-slide">&lt;</a>';
		var nextLink = '<a href="javascript:;" class="next-slide">&gt;</a>';
		var controls = "Image " + (this.currentSlide + 1) + " of " + this.totalSlides;
		if(this.currentSlide > 0) controls += ' ' + prevLink;
		if(this.currentSlide < this.totalSlides - 1) controls += ' ' + nextLink;
		this.element.down('.slideshow-controls').innerHTML = controls;
	},
	_fadeBetween: function(id1, id2) {
		fade(id1, 1000, 100, 0);
		fade(id2, 1000, 0, 100);
	}
	
});

var Thumbnail = Behavior.create(Remote.Link, {
	initialize: function(options) {
		this.options = Object.extend({
	      evaluateScripts : true
	    }, options || {});
	
		this.window = $('modal-window');
		this.background = $('modal-window-background');
		this.background.style.height = getPageSize()[2] + 240 + 'px';
		this.image = new Image();
	},
	onclick: function(e) {
		if(this.image.src) this._show();
		else {
			this.image.onLoad = this._show;
			this.image.src = this.element.up().href;		
		}
		return false;
	},
	_show:function() {
		this.background.show();
		$('modal-window-body').style.height = this.image.height + 'px';
		this.window.style.height = this.image.height + 40 + 'px';
		this.window.show();
		this.window.down('#modal-window-body').update('<img src="' + this.element.up().href + '" />');
		return false;
	}
});

var ModalWindowBackground = Behavior.create({
	onclick: function(e) {
		$('modal-window-background').hide();
		$('modal-window').hide();
	}
});

var ModalWindowClose = Behavior.create({
	onclick: function() {
		$('modal-window-background').hide();
		$('modal-window').hide();
	}
});
	
Event.addBehavior({
	'body': HomeSlideshow,
	'.controlled-slideshow': ControlledSlideshow,
	'img.thumbnail': Thumbnail({ update : 'modal-window' }),
	'div#modal-window-background': ModalWindowBackground,
	'a#modal-window-close': ModalWindowClose
});