/*!
 * CKBE Motion Functions
 * http://www.ckbe.com/
 *
 * Copyright 2010, CKBE
 * All Rights Reserved.
 *
 * Requires jQuery JavaScript Library v1.4.2
 * Copyright 2010, John Resig
 * http://www.jquery.com/
 * Released under the MIT or GPL Version 2 licenses.
 *
 * Date: Sun Jun 06 18:57:26 2010
 */

// ------------------------------------------------------------------------------------------------------------------------------------------------------------------ Global Variables.

var ckbe_timeout;
var ckbe_interval;

// ------------------------------------------------------------------------------------------------------------------------------------------------------------------ Slide Functions.

function ckbeMotion_slideDown(obj, speed) {

	$("#"+obj).slideDown(speed);

}

function ckbeMotion_slideUp(obj, speed) {

	$("#"+obj).slideUp(speed);

}

function ckbeMotion_slideSwap(obj, obj2, speed) {

	$("#"+obj).slideUp(speed, function() {
			$("#"+obj2).slideDown(speed);
		}
	);

}

function ckbeMotion_slideToggle(obj, speed) {

	$("#"+obj).slideToggle(speed);

}

// ------------------------------------------------------------------------------------------------------------------------------------------------------------------ Fade Functions.

function ckbeMotion_fadeIn(obj, speed) {

	$("#"+obj).fadeIn(speed);

}

function ckbeMotion_fadeOut(obj, speed) {

	$("#"+obj).fadeOut(speed);

}

function ckbeMotion_fadeOut2(obj, speed) {

	$("#"+obj).fadeTo(speed, 0, function() {
			document.getElementById(obj).style.visibility = "hidden";
			document.getElementById(obj).style.opacity = 1;
			document.getElementById(obj).style.filter = "alpha(opacity=100)"; // IE
		}
	);

}

function ckbeMotion_fadeSwap(obj, obj2, speed) {

	$("#"+obj).fadeOut(speed, function() {
			$("#"+obj2).fadeIn(speed);
		}
	);

}

// ------------------------------------------------------------------------------------------------------------------------------------------------------------------ Carousel Functions.

function ckbeMotion_carousel(obj, start, speed) {

	var stage = "crlStage";
	var stage_w = parseInt(document.getElementById(stage).style.width);
	
	document.getElementById(stage).style.width = stage_w + "px";
	document.getElementById(stage).style.overflow = "hidden";
	
	var prevbtn = stage + "PrevBtn";
	var nextbtn = stage + "NextBtn";
	
	var menu = stage + "Menu";
	var menu_a = false;
	
	var mnus = document.getElementById(menu).childNodes;
	var mnus_c = 0;
	
	var herebtn = stage + "Here";
	var herebtn_f = false;
	var herebtn_o;
	var herebtn_s;
	
	var notbtn = stage + "NotHere";
	var notbtn_f = false;
	var notbtn_o;
	var notbtn_s;
	
	var wrapper = stage + "Wrapper";
	var wrapper_w = 0;
	
	var itm = stage + "Item";
	var itm_w = 0;
	
	var itms = document.getElementById(wrapper).childNodes;
	var itms_c = 0;
	
	for (var i = 0, element; element = itms[i]; i++) {
		
		wrapper_w = wrapper_w + parseInt(element.style.width);
		
		itms_c = itms_c + 1;
		
		if (itm_w == 0) {
			itm_w = parseInt(element.style.width);
		}
		
	}
	
	var step = stage_w / itm_w;
	
	document.getElementById(wrapper).style.width = wrapper_w + "px";
	
	if (wrapper_w > stage_w) {
		
		function ckbeMotion_carousel_stageAnimate() {
		
			$("#"+stage).animate({ "scrollLeft" : currentPos + "px" }, speed);
		
		}
		
		function ckbeMotion_carousel_menuState() {
		
			for (var f = 0, element; element = mnus[f]; f++) {
				
				if (element.targetPos == currentPos) {
				
					element.src = herebtn_s;
					element.style.cursor = "default";
				
				} else {
				
					element.src = notbtn_s;
					element.style.cursor = "pointer";
				
				}
				
			}
		
		}
		
		function ckbeMotion_carousel_buttonState() {
		
			if (currentPos == 0) {
			
				prevbtn_disabled = true;
				document.getElementById(prevbtn).onmouseover = "";
				document.getElementById(prevbtn).onmouseout = "";
				document.getElementById(prevbtn).style.opacity = 0.5;
				document.getElementById(prevbtn).style.filter = "alpha(opacity=50)"; // IE
				document.getElementById(prevbtn).style.cursor = "default";
			
			} else {
			
				if (prevbtn_disabled == true) {
				
					prevbtn_disabled = false;
					document.getElementById(prevbtn).onmouseover = prevbtn_on_over;
					document.getElementById(prevbtn).onmouseout = prevbtn_on_out;
					document.getElementById(prevbtn).style.opacity = 1;
					document.getElementById(prevbtn).style.filter = "alpha(opacity=100)"; // IE
					document.getElementById(prevbtn).style.cursor = "pointer";
				
				}
			
			}
			
			if (currentPos == endPos) {
			
				nextbtn_disabled = true;
				document.getElementById(nextbtn).onmouseover = "";
				document.getElementById(nextbtn).onmouseout = "";
				document.getElementById(nextbtn).style.opacity = 0.5;
				document.getElementById(nextbtn).style.filter = "alpha(opacity=50)"; // IE
				document.getElementById(nextbtn).style.cursor = "default";
			
			} else {
			
				if (nextbtn_disabled == true) {
				
					nextbtn_disabled = false;
					document.getElementById(nextbtn).onmouseover = nextbtn_on_over;
					document.getElementById(nextbtn).onmouseout = nextbtn_on_out;
					document.getElementById(nextbtn).style.opacity = 1;
					document.getElementById(nextbtn).style.filter = "alpha(opacity=100)"; // IE
					document.getElementById(nextbtn).style.cursor = "pointer";
				
				}
			
			}
		
		}
		
		var step_w = itm_w * step;
		var steps = Math.ceil(wrapper_w / step_w);
		
		if (wrapper_w < (step_w * steps)) {
			wrapper_w = step_w * steps;
			document.getElementById(wrapper).style.width = wrapper_w + "px";
		}
		
		var startPos = 0;
		var currentPos = startPos;
		var endPos = (steps - 1) * step_w;
		
		if (start != 0 && start <= itms_c) {
			
			for (var a = 1; a < (steps + 1); a++) {
				
				if ((start * itm_w) <= (a * step_w)) {
					
					startPos = (a * step_w) - step_w;
					currentPos = startPos;
					break;
					
				}
				
			}
			
		}
		
		var prevbtn_on_over = document.getElementById(prevbtn).onmouseover;
		var prevbtn_on_out = document.getElementById(prevbtn).onmouseout;
		var prevbtn_disabled = false;
		
		var nextbtn_on_over = document.getElementById(nextbtn).onmouseover;
		var nextbtn_on_out = document.getElementById(nextbtn).onmouseout;
		var nextbtn_disabled = false;
		
		document.getElementById(prevbtn).style.cursor = "pointer";
		document.getElementById(nextbtn).style.cursor = "pointer";
		
		ckbeMotion_carousel_buttonState();
		
		document.getElementById(stage).scrollLeft = startPos;
		
		for (var b = 0, element; element = mnus[b]; b++) {
			
			mnus_c = mnus_c + 1;
			
			if (element.id == herebtn) {
				
				herebtn_f = true;
				
			}
			
			if (element.id == notbtn) {
				
				notbtn_f = true;
				
			}
			
		}
		
		if (herebtn_f == true && notbtn_f == true) {
			
			menu_a = true;
			
			herebtn_o = document.getElementById(herebtn).cloneNode(true);
			herebtn_s = herebtn_o.src;
			
			notbtn_o = document.getElementById(notbtn).cloneNode(true);
			notbtn_s = notbtn_o.src;
			
			for (var c = 0; c < mnus_c; c++) {
				
				document.getElementById(menu).removeChild(document.getElementById(menu).childNodes[0]);
				
			}
			
			for (var d = 1; d <= steps; d++) {
				
				notbtn_o.id = notbtn + d;
				
				document.getElementById(menu).appendChild(notbtn_o);
				notbtn_o = document.getElementById(notbtn_o.id).cloneNode(true);
				
				document.getElementById(notbtn_o.id).targetPos = ((step_w * d) - step_w);
				
				if (document.getElementById(notbtn_o.id).targetPos == currentPos) {
				
					document.getElementById(notbtn_o.id).src = herebtn_s;
					document.getElementById(notbtn_o.id).style.cursor = "default";
				
				} else {
				
					document.getElementById(notbtn_o.id).style.cursor = "pointer";
				
				}
				
				document.getElementById(notbtn_o.id).onclick = function () {
				
					if (this.targetPos != currentPos) {
					
						currentPos = this.targetPos;
						
						ckbeMotion_carousel_stageAnimate();
						ckbeMotion_carousel_menuState();
						ckbeMotion_carousel_buttonState();
					
					}
				
				}
				
				mnus = document.getElementById(menu).childNodes;
				
			}
			
		}
		
		if (mnus_c > 0 && herebtn_f == false && notbtn_f == false) {
		
			menu_a = true;
			
			// Reserved for future image based menus.
		
		}
		
		document.getElementById(prevbtn).onclick = function () {
		
			if (prevbtn_disabled != true) {
				
				if ((currentPos - step_w) >= 0) {
					
					currentPos = currentPos - step_w;
					
					ckbeMotion_carousel_stageAnimate();
					
				}
				
				ckbeMotion_carousel_menuState();
				ckbeMotion_carousel_buttonState();
				
			}
			
		}
			
		document.getElementById(nextbtn).onclick = function () {
		
			if (nextbtn_disabled != true) {
				
				if ((currentPos + step_w) <= endPos) {
					
					currentPos = currentPos + step_w;
					
					ckbeMotion_carousel_stageAnimate();
					
				}
				
				ckbeMotion_carousel_menuState();
				ckbeMotion_carousel_buttonState();
				
			}
			
		}
		
	} else {
		
		document.getElementById(prevbtn).style.visibility = "hidden";
		document.getElementById(nextbtn).style.visibility = "hidden";
		document.getElementById(menu).style.visibility = "hidden";
		
	}
	
	document.getElementById(obj).style.visibility = "visible";

}

// ------------------------------------------------------------------------------------------------------------------------------------------------------------------ Bubble Functions.

function ckbeMotion_bubble(obj, id, name, icon, desc, lib, width, pointer) {
	
	clearTimeout(ckbe_timeout);
	
	var bub = "bubble";
	
	function ckbeMotion_bubble_hide() {
		
		ckbeMotion_fadeOut(bub, 400);
		
		clearTimeout(ckbe_timeout);
		
	}
	
	function ckbeMotion_bubble_show(obj, id, name, icon, desc, lib, width, pointer) {
		
		var bub_o = document.createElement('div');
		var bub_x = 0;
		var bub_y = 0;
		var bub_w = width;
		var bub_h = 0;
		var bub_m = 25; // The horizontal margin of the bottom left and bottom right pointers from there edges.
		
		var obj_x = ckbeCommon_getValue_position(obj).x;
		var obj_y = ckbeCommon_getValue_position(obj).y;
		var obj_w = ckbeCommon_getValue_dimension(obj).w;
		var obj_h = ckbeCommon_getValue_dimension(obj).h;
		
		var doc_w = ckbeCommon_getValue_dimension(document.body).w;
		
		var row_t = "<tr><td style='width:19px; height:19px; background-image:url(" + lib + "bbl_edge_TL.png);'>&nbsp;</td><td style='width:21px; height:19px; background-image:url(" + lib + "bbl_edge_TC.png);'>&nbsp;</td><td style='height:19px; background-image:url(" + lib + "bbl_edge_TC.png);'>&nbsp;</td><td style='width:21px; height:19px; background-image:url(" + lib + "bbl_edge_TC.png);'>&nbsp;</td><td style='width:19px; height:19px; background-image:url(" + lib + "bbl_edge_TR.png);'>&nbsp;</td></tr>";
		
		var row_b = "";
		var row_b_np = "<tr><td style='width:40px; height:27px; background-image:url(" + lib + "bbl_edge_BL.png);' colspan='2'>&nbsp;</td><td style='height:27px; background-image:url(" + lib + "bbl_edge_BC.png);'>&nbsp;</td><td style='width:40px; height:27px; background-image:url(" + lib + "bbl_edge_BR.png);' colspan='2'>&nbsp;</td></tr>";
		var row_b_pl = "<tr><td style='width:40px; height:27px; background-image:url(" + lib + "bbl_pointer_BL.png);' colspan='2'>&nbsp;</td><td style='height:27px; background-image:url(" + lib + "bbl_edge_BC.png);'>&nbsp;</td><td style='width:40px; height:27px; background-image:url(" + lib + "bbl_edge_BR.png);' colspan='2'>&nbsp;</td></tr>";
		var row_b_pc = "<tr><td style='width:40px; height:27px; background-image:url(" + lib + "bbl_edge_BL.png);' colspan='2'>&nbsp;</td><td style='height:27px; background-image:url(" + lib + "bbl_pointer_BC.png); background-position:center; background-repeat:no-repeat;'>&nbsp;</td><td style='width:40px; height:27px; background-image:url(" + lib + "bbl_edge_BR.png);' colspan='2'>&nbsp;</td></tr>";
		var row_b_pr = "<tr><td style='width:40px; height:27px; background-image:url(" + lib + "bbl_edge_BL.png);' colspan='2'>&nbsp;</td><td style='height:27px; background-image:url(" + lib + "bbl_edge_BC.png);'>&nbsp;</td><td style='width:40px; height:27px; background-image:url(" + lib + "bbl_pointer_BR.png);' colspan='2'>&nbsp;</td></tr>";
		
		var row_m = "<tr><td style='width:19px; background-image:url(" + lib + "bbl_edge_ML.png);'>&nbsp;</td><td style='width:21px; background-color:#FFFFFF;'>&nbsp;</td><td style='background-color:#FFFFFF;'><table style='margin-bottom:6px;'><tr><td style='width:50px;'><img src='" + icon + "' style='width:40px; height:40px;' title='" + name + "' /></td><td style='vertical-align:middle; font-weight:bold;'>" + name + "</td></tr></table>" + desc + "</td><td style='width:21px; background-color:#FFFFFF;'>&nbsp;</td><td style='width:19px; background-image:url(" + lib + "bbl_edge_MR.png);'>&nbsp;</td></tr>";
		
		bub_x = (obj_x + (obj_w / 2)) - (bub_w / 2);
		
		row_b = row_b_pc;
		
		if ((bub_x + bub_w) >= doc_w) {
			
			row_b = row_b_pr;
			bub_x = ((obj_x + (obj_w / 2)) - bub_w) + bub_m;
			
		}
		
		if (bub_x <= 0) {
			
			row_b = row_b_pl;
			bub_x = (obj_x + (obj_w / 2)) - bub_m;
			
		}
		
		bub_o.setAttribute("id", bub);
		bub_o.innerHTML = "<table style='width:inherit;'>" + row_t + row_m + row_b + "</table>";
		
		document.body.appendChild(bub_o);
		document.getElementById(bub).jid = id;
		
		document.getElementById(bub).style.visibility = "hidden";
		document.getElementById(bub).style.width = width + "px";
		document.getElementById(bub).style.position = "absolute";
		document.getElementById(bub).style.zIndex = 1;
		
		bub_h = ckbeCommon_getValue_dimension(document.getElementById(bub)).h;
		bub_y = obj_y - bub_h;
		
		document.getElementById(bub).style.left = bub_x + "px";
		document.getElementById(bub).style.top = bub_y + "px";
		document.getElementById(bub).style.display = "none";
		document.getElementById(bub).style.visibility = "visible";
		
		ckbeMotion_fadeIn(bub, 600);
		
		clearTimeout(ckbe_timeout);
		
		obj.onmouseout = function () {
				
			ckbe_timeout = setTimeout(ckbeMotion_bubble_hide, 1000);
			
		}
		
		document.getElementById(bub).onmouseout = function () {
				
			ckbe_timeout = setTimeout(ckbeMotion_bubble_hide, 1000);
			
		}
		
		document.getElementById(bub).onmouseover = function () {
				
			clearTimeout(ckbe_timeout);
			
		}
	
	}
	
	if (document.getElementById(bub) != null) {
		
		if (document.getElementById(bub).jid != id || document.getElementById(bub).jid == id && document.getElementById(bub).style.display == "none") {
		
				document.body.removeChild(document.getElementById(bub));
		
		}
		
	}
	
	if (document.getElementById(bub) == null || document.getElementById(bub) != null && document.getElementById(bub).jid != id || document.getElementById(bub).jid == id && document.getElementById(bub).style.display == "none") {
	
		ckbeMotion_bubble_show(obj, id, name, icon, desc, lib, width, pointer);
	
	}
	
}