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) {
	if(document.getElementById(el)){
		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() {
	if($('slideshow-controls')){
		//alert('swapTestimonials');
		var elements = $$('.slide');
		var previousSlide = currentSlide;
		currentSlide++;
		currentSlide = currentSlide >= elements.length ? 0 : currentSlide;

		fadeBetween('slideshow-slide-'+previousSlide, 'slideshow-slide-'+currentSlide);
		updateControls(currentSlide);
		$c(currentSlide);
		$c(elements);
		$c('swapSlides();');		
	}
}

function fadeBetween(id1, id2) {
	fade(id1, 1000, 100, 0);
	fade(id2, 1000, 0, 100);
}


function updateControls(currentSlide){
	var prevLink = '<a href="javascript:;" class="prev-slide">&lt;</a>';
	var nextLink = '<a href="javascript:;" class="next-slide">&gt;</a>';
	var elements = $$('.slide');
	totalSlides = elements.length;
	var controls = "Image " + (currentSlide + 1) + " of " + totalSlides;

	if(currentSlide > 0) controls += ' ' + prevLink;
	if(currentSlide < totalSlides - 1) controls += ' ' + nextLink;
	
	$c(controls);
	
	$('slideshow-controls').innerHTML = controls;
}


//
// 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 ControlledSlideshow = Behavior.create({
	initialize: function() {
		this.currentSlide = 0;
		$c(this.element.id);
		
		var slideshowId = this.element.id;
		
		this.slides = this.element.select('.slide');
		$c(this.element);
		$c(this.slides);
		this.totalSlides = this.slides.length
	},
	onclick: Event.delegate({
		'.prev-slide': function(e) {
			$c('prev' + this.element.id);
			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) {
			$c('next');
			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 += ' <span class="off"><</span>';		
		if(this.currentSlide > 0) controls += ' ' + prevLink;
		if(this.currentSlide < this.totalSlides - 1) controls += ' ' + nextLink;
		if(this.currentSlide == (this.totalSlides - 1)) controls += ' <span class="off">></span>';
		this.element.down('.slideshow-controls').innerHTML = controls;
				
		$('slide_caption_0').hide();
		$('slide_caption_1').hide();
		$('slide_caption_2').hide();
		
		$('slide_caption_' + this.currentSlide).show();
		
	},
	_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();
	}
});

var StartSlideshow = Behavior.create({
	initialize: function(options) {
		slideshowTimer = setInterval(this._showNextSlide.bind(this), 5 * 1000);
	},
	_showNextSlide: function() {
		//alert('sdf');
		swapSlides();
	}
});

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

var ShowMoreInfo = Behavior.create({
	onclick: function() {
		$$('.' + this.element.id)[0].toggle();
		//$(this.element.id).update('Less Info');
		
		var less_text = 'Hide Info';
		
		if($(this.element.id).innerHTML == less_text){
			$(this.element.id).update('More Info &raquo;');
		}else{
			$(this.element.id).update(less_text);
		}
		
		return false;
	}
});
	
Event.addBehavior({
	'body': HomeSlideshow,
	'.controlled-slideshow': ControlledSlideshow,
	'img.thumbnail': Thumbnail({ update : 'modal-window' }),
	'div#modal-window-background': ModalWindowBackground,
	'a#modal-window-close': ModalWindowClose,
	'#slideshow': StartSlideshow,
	'.show_more_info': ShowMoreInfo
});
