/*
*                  _         _                   _ _         _ 
*      /\  /\_   _| |_ ____ / \   /\/\   ___  __| (_) __ _  / \
*     / /_/ / | | | __|_  //  /  /    \ / _ \/ _` | |/ _` |/  /
*    / __  /| |_| | |_ / //\_/  / /\/\ \  __/ (_| | | (_| /\_/ 
*    \/ /_/  \__,_|\__/___\/    \/    \/\___|\__,_|_|\__,_\/   
*
*	PrairieAircraft.com
*	Javascript
*
*	Developed By Hutz! Media! <hutzmedia.com>
*	under contract for WebStager <webstager.com>
*
*	November 02, 2010
*
*/


/* Class: SlideShowClass
		Creates a fade-in and out slide show.
*/
function SlideShowClass(objList) {
	this.objList = objList;
	this.current = 0;
	this.fps = 30;
	this.pause = 3000;
	this.fade = 2000;
	this.zIndex = 15;
}

SlideShowClass.prototype.start = function() {
	// go to next after pause
	var self = this;
	setTimeout(function(){self.next(self);}, self.pause);
};

SlideShowClass.prototype.next = function(self) {
	// start fade out and in
	
	var a = self.current;
	var b = a + 1;
	if (b >= self.objList.length) {
		b = 0;
	}
	self.current = b;
	
	var objA = self.getID(self.objList[a]);
	var objB = self.getID(self.objList[b]);
	
	// make sure A is in front of B
	objA.style.zIndex = self.zIndex + 0;
	objB.style.zIndex = self.zIndex + 1;
	
	// set starting opacity
	self.setOpacity(objA, 99);
	self.setOpacity(objB, 1);
	
	// make both visible
	self.setVisible(objA, true);
	self.setVisible(objB, true);
	
	// start fade
	self.fadeStep(self, objA, objB, 0);
	
};

SlideShowClass.prototype.fadeStep = function(self, objA, objB, alpha) {
	// step through fade until complete
	
	var delay = 1000 / self.fps;
	var step = 100 / self.fade * delay;
	
	alpha = Math.min(alpha + step, 100);
	
	// set alpha
	self.setOpacity(objA, (100 - Math.floor(alpha)));
	self.setOpacity(objB, Math.floor(alpha));
	
	if (alpha >= 100) {
		// done
		self.setVisible(objA, false);
		setTimeout(function(){self.next(self);}, self.pause);
	} else {
		// continue step
		setTimeout(function(){self.fadeStep(self, objA, objB, alpha);}, delay);
	}
	
};		

SlideShowClass.prototype.getID = function(objname) {
	return document.getElementById(objname);
};

SlideShowClass.prototype.setVisible = function(obj, v) {
	if (v) {
		obj.style.display = "block";
	} else {
		obj.style.display = "none";
	}
};
SlideShowClass.prototype.setOpacity = function(obj, alpha) {
	try { obj.style.opacity = (alpha / 100); } catch (e) { }
	try { obj.style.MozOpacity = (alpha / 100); } catch (e) { }
	try { obj.style.KhtmlOpacity = (alpha / 100); } catch (e) { }
	try { obj.style.filter = "alpha(opacity=" + alpha + ")"; } catch (e) { }
};
SlideShowClass.prototype.debug = function(s) {
	var obj = document.getElementById("debug");
	obj.value = s + "\n" + obj.value; 
};

/* Class: GalleryClass
	Controls gallery veiw on front-end
*/
function GalleryClass() {
	this.objects = [];
	this.featuredObjName = "gallery_featured";
}
GalleryClass.prototype = {
	viewImage: function(id) {
		// open image
		var image = this.objects[id];
		
		var html = '';
		html += '		<img src="' + image.imageURL + '" alt="' + image.title + '" />\n';
		document.getElementById(this.featuredObjName).innerHTML = html;
		
		return false;
	
	},
	viewVideo: function(id) {
		// open video
		var video = this.objects[id];
		var html = '';
		html += '		<iframe title="YouTube video player" class="youtube-player" type="text/html" width="100%" height="100%" src="http://www.youtube.com/embed/' + video.tag + '?rel=0&amp;hd=1&amp;autoplay=1" frameborder="0"></iframe>\n';
		document.getElementById(this.featuredObjName).innerHTML = html;

		return false;
	},
	setObjects: function(obj) {
		this.objects = obj;
	}
};
var Gallery = new GalleryClass();
/* End Gallery Class */

/* Class: DropDownClass */
function DropDownClass() {
}
DropDownClass.prototype = {
	over: function(e, objName) {
		if (!e) var e = window.event;
		var relTarg = e.relatedTarget || e.toElement;
	
		var obj = document.getElementById(objName + "_hidden");
		obj.style.display = 'block';
		this.isOver = true;
	},
	out: function(e, objName, callBack) {
	
		if (!e) var e = window.event;
		var relTarg = e.relatedTarget || e.toElement;
		
		var found = false;
		var node = relTarg;
		
		if (!this.isOver) { return; }
		
		var debug = "";
		try {
			
			while (true) {
				debug += node.className + "\n";
				if (node.className == "forSale") {
					found = true;
					break;
				} else {
					if (node.parentNode) {
						node = node.parentNode;
					} else {
						break;
					}
				}
			}
		
		} catch (e) { }	

		if (!found) {
			this.isOver = false;
			var self = this;
			setTimeout(function(){self.outDelay(self, objName, callBack);}, 500);
		}
	},
	outDelay: function(self, objName, callBack) {
		if (!self.isOver) {
			var obj = document.getElementById(objName + "_hidden");
			obj.style.display = 'none';
			if (callBack) { callBack(); }
		}
	}

};
var DropDown = new DropDownClass();
/* End DropDownClass */

/* Global Functions */
function cropMenu(x1, x2) {
	var obj = this.document.getElementById("menuClip");
	obj.style.visibility = 'visible';
	obj.style.clip = "rect(0px," + x2 + "px,34px," + x1 + "px)";
}
function cropMenuOut() {
	var obj = this.document.getElementById("menuClip");
	obj.style.clip = "rect(0px, 0px, 0px, 0px)";
}

function initSlideshow(ids) {
	
	var SS = new SlideShowClass(ids);
	SS.pause = 3000; // 3s
	SS.fade = 1000; // 1s
	SS.start();
	
}
