// JavaScript Document
// This file handles the js functions in the custom_index file

var cntPath = 'http://site25.mypowerit.net/images/';
var images = [
				cntPath + 'about.jpg',
				cntPath + 'about-hover.jpg',
				cntPath + 'program.jpg',
				cntPath + 'program-hover.jpg',
				cntPath + 'admission.jpg',
				cntPath + 'admission-hover.jpg',
				cntPath + 'community.jpg',
				cntPath + 'community-hover.jpg',
				cntPath + 'parent.jpg',
				cntPath + 'parent-hover.jpg',
				cntPath + 'support.jpg',
				cntPath + 'support-hover.jpg',
			];
new Asset.images(images);

function dispLogin(imgId){
	var imgValue = imgId.id;
	loginDiv = document.getElementById('logDiv');
	searchDiv = document.getElementById('search');
	logImageRaise = document.getElementById('logImg_raise');	
	searchImageRaise = document.getElementById('srchImg_raise');		
	logImageDown = document.getElementById('logImg_down');	
	searchImageDown = document.getElementById('srchImg_down');		
	
	if(imgValue == 'srchImg_raise'){
		searchDiv.style.display = '';
		loginDiv.style.display = "none";		
		searchImageDown.style.display = '';
		searchImageRaise.style.display = "none";
		logImageDown.style.display = "none";
		logImageRaise.style.display = '';
	}else if(imgValue == 'srchImg_down'){
		searchDiv.style.display = '';
		loginDiv.style.display = "none";		
		searchImageRaise.style.display = "none";
		logImageDown.style.display = "none";
		logImageRaise.style.display = '';
	}else if(imgValue == 'logImg_raise'){
		loginDiv.style.display = 'block';
		searchDiv.style.display ="none";
		logImageDown.style.display = "";
		logImageRaise.style.display = 'none';
		searchImageDown.style.display = 'none';
		searchImageRaise.style.display = "";		
	}else {
		loginDiv.style.display = 'block';
		searchDiv.style.display ="none";		
		logImageRaise.style.display = 'none';
		searchImageDown.style.display = 'none';
		searchImageRaise.style.display = "";
	}	
}

POWERIT = {};

POWERIT = (function(pub) {
	var pub = {};

	return pub;
} (POWERIT || {}));

POWERIT = (function(pub) {
  /**
	 * Provides slideshow functionality to a specific div
	 */
	pub.SlideShow = new Class({

		Implements: [Options, Events],

		options: {
			visible_time: 5000
		},

		slideshow_reference: null,
		active_item: null,

		initialize: function(location, items, options) {
			this.setOptions(options);

			if (items.length == 0) {
				return;
			}

			this.buildShow(location, items);
			this.runShow();
		},

		buildShow: function(location, items) {
			this.slideshow_reference = 'slideshow-' + POWERIT.utilities.randomString(12);
			var container = new Element('div', { // builds the container
				id: this.slideshow_reference,
				'class': 'slideshow-container'
			});
			container.setStyles({
				'position': 'relative',
				'overflow': 'hidden',
				'width': '100%',
				'height': '100%'
			});
			$(location).grab(container); // adds the slideshow container to the specified location
			// Builds each item and adds it to the container
			items.each(function(f, i) {
				var item = new Element('div', {
					'class': 'slideshow-item',
					id: this.slideshow_reference + '-item-' + (i + 1)
				});
				item.setStyles({
					'position': 'absolute'
				});
				item.grab(new Element('img', {
					src: f
				}));
				$(this).grab(item); // Adds the item to the container
			}.bind(this));
		},

		runShow: function() {
			// if there is only 1 item, just display it...no need to run the rotation
			if ($(this).getElements('.slideshow-item').length <= 1) {
				return;
			}

			// get all the slideshow items
			$(this).getElements('.slideshow-item').each(function(f, i) {
				if (i > 0) {
					$(f).set('opacity', 0);
				} else {
					this.active_item = $(f);
				}
			}.bind(this));

			this._periodical = this.rotate.periodical(this.options.visible_time, this);
		},

		rotate: function() {
			var active_number = this.active_item.getProperty('id').split('-');
			active_number = active_number[3];

			if (active_number == $(this).getElements('.slideshow-item').length) { //are we on the last item? if so start back at the beginning.
				var new_number = 1;
			} else { //otherwise just add 1 to the last active id
				var new_number = (parseInt(active_number) + 1);
			}

			$(this.slideshow_reference + '-item-' + new_number).set('tween', {
				duration: 3000
			});
			$(this.slideshow_reference + '-item-' + new_number).tween('opacity', 1);

			$(this.active_item).set('tween', {
				duration: 3000
			});
			$(this.active_item).tween('opacity', 0);
			this.active_item = $(this.slideshow_reference + '-item-' + new_number);
		},

		toElement: function() {
			return $(this.slideshow_reference);
		}

	});

	return pub;
} (POWERIT || {}));

/**
 * POWERIT namespaced module pattern, allowing for loose augmentation (it really
 * doesn't matter in what order methods/variable are added to this module)
 *
 * @see http://tinyurl.com/ydg9cvy
 */
POWERIT.utilities = (function(pub) {

	/**
	 * Generates a random string for the specified length
	 *
	 * @param integer string_length the length of the random string to generate
	 *
	 * @return string the random string
	*/
	pub.randomString = function(string_length) {
		var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
		var randomstring = '';
		for (var i = 0; i < string_length; i++) {
			var rnum = Math.floor(Math.random() * chars.length);
			randomstring += chars.substring(rnum, rnum + 1);
		}
		return randomstring;
	};

	return pub;
} (POWERIT.utilities || {}));


window.addEvent('domready', function(){
  if ($('large-slideshow')) {
    var powerit_large_slideshow = new POWERIT.SlideShow($('large-slideshow'), large_banner_images, {});
    var powerit_small_slideshow = new POWERIT.SlideShow($('small-slideshow'), small_banner_images, {});
  }
});

