/**
 * Unlike Prototype, jQuery doesn't define OOP primitives, so we need to use plain old javascript
 * for creating classes. CreateClass is a handy function for doing this:
 */
 var CreateClass = function(){
     return function(){this.initialize.apply(this, arguments);};
 };

 /** ROTATING IMAGE GALLERY **/
 var TOGallery = new Class.create({
     initialize: function(galleryEl,delay) {

	 this.galleryEl = galleryEl;	    
	 this.slideShowDelay = delay;	    
	 this.slideShowFadeDuration = 0.5;	    
	 this.currentIter = 0;                           
         this.slideShowTimer = null;
	 this.galleryFrames = this.galleryEl.getElementsBySelector('div#gallery')[0].getElementsBySelector('div.frame');
	 this.galleryFrames.each(function(el){el.setStyle('display', 'block');});
	 this.galleryInfoWindows = this.galleryEl.getElementsBySelector('div#gallery')[0].getElementsBySelector('div.info-window');
	 this.carouselElements = this.galleryEl.getElementsBySelector('ul#carousel')[0].childElements();
	 
	 if (this.carouselElements.length>1){
	     for(i=0;i<this.galleryFrames.length;i++)
	     {
	         this.galleryFrames[i].observe( 'mouseover', this.pauseSlideShow.bind(this) );
	         this.galleryFrames[i].observe( 'mouseout', this.playSlideShow.bind(this) );
	     }
	     for(i=0;i<this.carouselElements.length;i++)
	     {
	         this.carouselElements[i].observe( 'mouseover', function(num){ this.skipToItem(num) }.bind(this, i) );
		 this.carouselElements[i].observe( 'mouseout', this.playSlideShow.bind(this) );
	     }
	     this.playSlideShow();
         }
     },
     skipToItem: function(num) {
	 this.pauseSlideShow();
	 for(i=0;i<this.galleryFrames.length;i++)
	 {
	     if (i != num) this.galleryFrames[i].setStyle({opacity: '0', display: 'none'});
	 }
         this.galleryFrames[num].setStyle({opacity: '1',display: 'block'});
         this.galleryInfoWindows[this.currentIter].setStyle({display: 'none'});
         this.galleryInfoWindows[num].setStyle({display: 'block'});
	 this.setCurrentIter(num);
     },
     setCurrentIter: function(iter) {
	 this.currentIter = iter;
	 this.carouselElements.each(function(el){el.removeClassName('selected')});
	 this.carouselElements[iter].addClassName('selected');
     },	
     nextIteration: function(currentIter) {
	 var nextIter = currentIter+1;
	 if (nextIter >= this.galleryFrames.length) nextIter = 0;
	 return nextIter;
     },
     prevIteration: function(currentIter) {
	 var prevIter = currentIter-1;
	 if (prevIter < 0) prevIter = this.galleryFrames.length-1;
	 return prevIter;
     },
     pauseSlideShow: function() {
	 this.slideShowTimer.stop();
     },
     playSlideShow: function() {
	 this.slideShowTimer = new PeriodicalExecuter( this.fadeToNextItem.bind(this), this.slideShowDelay );
     },	
     fadeToNextItem: function() {
	 for(i=0;i<this.galleryFrames.length;i++)
	 {
	     if (i != this.currentIter) this.galleryFrames[i].setStyle({opacity: '0', display: 'none'});
	 }
         var oldFrame = this.galleryFrames[this.currentIter];
         var oldInfoWindow = this.galleryInfoWindows[this.currentIter];
         var newFrame = this.galleryFrames[this.nextIteration(this.currentIter)];
         var newInfoWindow = this.galleryInfoWindows[this.nextIteration(this.currentIter)];
         this.setCurrentIter(this.nextIteration(this.currentIter));
	 
	 new Effect.Morph(oldFrame, { style: 'opacity: 0', duration: this.slideShowFadeDuration, transition: Effect.Transitions.linear });
         oldInfoWindow.setStyle({display: 'none'});

         newFrame.setStyle({display: 'block'});
	 new Effect.Morph(newFrame, { style: 'opacity: 1', duration: this.slideShowFadeDuration, transition: Effect.Transitions.linear });
	 newInfoWindow.setStyle({display: 'block'});
     }
 });


/**
 *  Takes an element and clears or sets the default text. Used in forms  
 *  with auto filled fields.
 *
 *  @author Dave Folan
 *  @usage onclick="clearDefaultText(this, 'Your email address');"
 *  @param (Object) targetElement    Id or HtmlElement that needs toggling.
 *  @param (String) defaultText      The default text of the field to check
 **/
function clearDefaultText(targetElement, defaultText, defaultClassName)
{
    if (defaultClassName == '' || defaultClassName == undefined) defaultClassName = 'default';

    if (targetElement.value == defaultText) 
    {
        targetElement.value = '';
        targetElement.className = '';
    } 
    else if (targetElement.value == '') 
    {
        targetElement.value = defaultText;
        targetElement.className = defaultClassName;
    }
}

function inputValueSearch(self, initialValue, isFocus){
	if(isFocus == true)
	{
	    if(self.value == initialValue){
	        self.value = '';
	        self.style.color = '#333';
	    }
	} else {
		if(self.value.replace(/^\s+|\s+$/g,"") == ''){
		    self.style.color = '#aaa';
	        self.value = initialValue;
	    }
	}
}