/*
	Main JavaScript for CooperBoating.com
	By <hutzmedia.com>
	September 15, 2009
*/

/* 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 */

/* Enhanced Select DropDown */
function EnhancedSelectClass(ids) {
	this.opened = false;
	this.ids = ids;
}

EnhancedSelectClass.prototype.start = function() {
	document.getElementById(this.ids.hide).style.display = "none";
	document.getElementById(this.ids.main).style.display = "block";
};

EnhancedSelectClass.prototype.click = function() {
	if (this.opened) {
		this.close();
	} else {
		this.open();
	}
};

EnhancedSelectClass.prototype.externalClick = function(e) {

	if (this.opened) {

		var targ;
		if (!e) { var e = window.event; }
		if (e.target) { targ = e.target; } else if (e.srcElement) { targ = e.srcElement; }
		if (targ.nodeType == 3) { targ = targ.parentNode; }

		
		"enhancedSelect"
		var obj = targ;
		var inside = false;
		
		try {
			while (obj != null) {
				var parent = obj.parentNode;
				
				if (parent.className == "enhancedSelect") {
					inside = true;
					break;
				}
				obj = parent;
			}
		} catch (e) { }
		
		if (!inside) {
			
			this.close();
			
		}
		
	}
	
};

EnhancedSelectClass.prototype.open = function() {
	document.getElementById(this.ids.dropdown).style.display = "block";
	this.opened = true;
};

EnhancedSelectClass.prototype.close = function() {
	document.getElementById(this.ids.dropdown).style.display = "none";
	this.opened = false;
};

EnhancedSelectClass.prototype.select = function(className, title, value) {

	document.getElementById(this.ids.focus).innerHTML = "<a style=\"cursor: default;\" class=\"" + className + "\" href=\"#\" onclick=\"return false;\">" + title + "</a>";
	
	// set value
	var obj = document.getElementById(this.ids.selectValue);
	var l = obj.length;
	for (var i=0;i<l;i++) {
		if (obj[i].value == value) { obj.selectedIndex = i; }
	}
	
	// close
	this.close();
};





/* Normal JS Functions */

// init classes
//Calendar.startDate = new CalendarClass("startDate", "Calendar.startDate");
//Calendar.endDate = new CalendarClass("endDate", "Calendar.endDate");

var EnhancedSelect = new EnhancedSelectClass({main: "course_level_enhanced", hide: "course_level_div", dropdown: "enhanced_dropdown", focus: "course_level_focus", selectValue: "course_level"});
document.onclick = function(e){EnhancedSelect.externalClick(e);}


function cropMenu(x1, x2) {
	var obj = this.document.getElementById("menuClip");
	obj.style.visibility = 'visible';
	obj.style.clip = "rect(1px," + x2 + "px,36px," + x1 + "px)";
}
function cropMenuOut() {
	var obj = this.document.getElementById("menuClip");
	obj.style.clip = "rect(0px, 0px, 0px, 0px)";
}
var dropDown_open = {};
function dropDown(objName, e) {
	var obj = document.getElementById(objName);
	obj.style.display = "block";
}
function dropDown_out(objName, e) {
	var obj = document.getElementById(objName);
	
	var targ;
	if (!e) { var e = window.event; }
	if (e.target) { targ = e.target; } else if (e.srcElement) { targ = e.srcElement; }
	if (targ.nodeType == 3) { targ = targ.parentNode; }
	
//	if (targ.className != "stayOpen") {
		dropDown_open = {};
		obj.style.display = "none";
//	}
}

function openGalleryImage(url, w, h) {

	w = w + 64;
	h = h + 136;
	h = h + 160; // for description
	if (w < 480) { w = 480; }
	if (h > 768) { h = 768; }
	
	var win = window.open(url, "_blank", "alwaysRaised=1,resizable=yes,scrollbars=yes,width=" + (w) + ",height=" + (h));
	return false;
		
}


function initSlideshow(ids) {
	
	var SS = new SlideShowClass(ids);
	SS.pause = 3000; // 3s
	SS.fade = 1000; // 1s
	SS.start();
	
}


