// CyberFuzion - Lightbox
// WordPress Edition

// Variable holder for current lightbox in place
var CFLB;
var CFLB_SLIDESHOW = new Array();
var CFLB_PLAY = false;
var CFLB_TIMEOUT;
var CFLB_SLIDESHOW_TIME = 5; // seconds

// KeyCodes
var KEYCODE_ESC = 27;	
var KEYCODE_LEFTARROW = 37;
var KEYCODE_RIGHTARROW = 39;

// Register Effects
$(document).ready(function() {
	// Create the overlay element
	var overlay = document.createElement('div');
	$(overlay).addClass('cflb_overlay');	
	$("head").after(overlay);
	
	// Create the content element
	var content = document.createElement('div');
	$(content).addClass('cflb_content');
	
	// Create content wrapper
	var wrapper = document.createElement('div');
	$(wrapper).addClass('cflb_content_wrapper');
	
	// Put wrapper inside content
	$(content).html(wrapper);
	$(content).prepend('<div class="cflbbar"><div class="cflbNext"></div><div class="cflbPrev"></div><div id="cflbPP" class="cflbPlay"></div></div>');
	$("body").prepend(content);

	// Hide content to be displayed in box
	$(".cflb_hide").hide();
	
	// The trigger click event for class cf_lb_trigger
	$(".cflb_trigger").click(function() {
		triggerCFLB('');
	});
	
	// Clicking outside of content box closes window
	$(".cflb_overlay").click(closeCFLB);
	
	// Add Bar Event Listeners
	$(".cflbNext").click(nextCFLB);
	$(".cflbPrev").click(prevCFLB);
	$("#cflbPP").click(playCFLB);
	
	// Find each element and add into slideshow array
	var cnt = 0;
	$(".cflb_slideshow").each(function() {
		var id = $(this).attr("id");
		CFLB_SLIDESHOW[cnt] = id;
		cnt = cnt+1;
	});
});

// The trigger function
// Pass in the ID of the element to display inside the lightbox
function triggerCFLB(ele) {
	// Get the id
	var id = $(ele).attr('id');
	CFLB = id;
	
	// Get the width and height of the content
	var width = $("#cflb_content_"+id).width();
	var height = $("#cflb_content_"+id).height();
	$(".cflb_content_wrapper").width = width;
	$(".cflb_content_wrapper").height = height;
	
	// Adjust the neutral area to full screen
	$(".cflb_overlay").fadeTo("fast", 0.6);
	
	// Calculate the size the lightbox needs to be depending on the size of the content passed in
	$("#cflb_content_"+id).width() ? w = $("#cflb_content_"+id).width() : w = $("#cflb_content_"+id).attr('width');
	$("#cflb_content_"+id).height() ? h = $("#cflb_content_"+id).height() : h = $("#cflb_content_"+id).attr('height');
	// Add Padding
	w = w+20;
	h = h+30 // Added for bar height
	
	// Get the window size for calculating
	var x = $(window).width()/2; 
	x = x+window.pageXOffset;
	var y = $(window).height()/2;
	y = y+window.pageYOffset;
	startW = h-y/2;
	startH = w-x/2;
	endTop = y - h/2;
	endLeft = x - w/2;
	
	endTop<0 ? endTop = 0 : endTop = endTop;
	endLeft<0 ? endLeft = 0 : endLeft = endLeft;
	
	// Get the element to display
	var content = $("#cflb_content_"+id).html();
	$(".cflb_content_wrapper").css({"width":w+"px","height":h+"px"});
	$(".cflb_content_wrapper").html(content);
	
	// Show the lightbox
	$(".cflb_content").css("display", "block");	
	
	$(".cflb_content").css({"left":x+"px","top":y+"px"});
	$(".cflb_content").css({"width":"0px","height":"0px"});
	
	// Start by animating
	$(".cflb_content").animate({
		opacity: 1,
		width: w+'px',
		height: h+'px',
		top: endTop+'px',
		left: endLeft+'px'},
		500,
		'linear');
	
	// Key Event Listeners
	$(document).keyup(function(e) {
	  if (e.keyCode == KEYCODE_ESC) { closeCFLB(); } 
	  if (e.keyCode == KEYCODE_LEFTARROW) { prevCFLB(); } 
	  if (e.keyCode == KEYCODE_RIGHTARROW) { nextCFLB(); } 
	});
	
	return false;
}

// The close lb function
function closeCFLB() {
	stopCFLB();
	// Hide the lightbox
	$(".cflb_overlay").fadeTo("fast", 0, function() {
		$(".cflb_overlay").css("display", "none");
	});
	$(".cflb_content").css("display", "none");
	
	// Remove the keyevent listeners
	$(document).unbind('keyup');
}

// Go to next item
function nextCFLB() {
	var id;
	// Find the next item in CFLB_SLIDESHOW
	for(i=0;i<CFLB_SLIDESHOW.length;i++) {
		if(CFLB_SLIDESHOW[i]==CFLB) {
			if(i+1==CFLB_SLIDESHOW.length) {
				id = CFLB_SLIDESHOW[0];
			} else {
				id = CFLB_SLIDESHOW[i+1];
			}
		}
	}
	
	displaySlideshow(id);

	return false;	
}

// Go to prev item
function prevCFLB() {
	var id;
	// Find the next item in CFLB_SLIDESHOW
	for(i=0;i<CFLB_SLIDESHOW.length;i++) {
		if(CFLB_SLIDESHOW[i]==CFLB) {
			if(i==0) {
				id = CFLB_SLIDESHOW[CFLB_SLIDESHOW.length-1];
			} else {
				id = CFLB_SLIDESHOW[i-1];
			}
		}
	}
	
	displaySlideshow(id);

	return false;	
}

// Display using id
function displaySlideshow(id) {
	// Set current to existing id
	CFLB = id;
	
	// Get the width and height of the content
	var width = $("#cflb_content_"+id).width();
	var height = $("#cflb_content_"+id).height();
	//$(".cflb_content_wrapper").width = width;
	//$(".cflb_content_wrapper").height = height;
	
	// Adjust the neutral area to full screen
	$(".cflb_overlay").fadeTo("fast", 0.6);
	
	// Calculate the size the lightbox needs to be depending on the size of the content passed in
	$("#cflb_content_"+id).width() ? w = $("#cflb_content_"+id).width() : w = $("#cflb_content_"+id).attr('width');
	$("#cflb_content_"+id).height() ? h = $("#cflb_content_"+id).height() : h = $("#cflb_content_"+id).attr('height');
	w = w+20;
	h = h+30; // Bar height
	
	// Get the window size for calculating
	var x = $(window).width()/2; 
	x = x+window.pageXOffset;
	var y = $(window).height()/2;
	y = y+window.pageYOffset;
	startW = h-y/2;
	startH = w-x/2;
	endTop = y - h/2;
	endLeft = x - w/2;
	endTop<0 ? endTop = 0 : endTop = endTop;
	endLeft<0 ? endLeft = 0 : endLeft = endLeft;
	
	// Get the element to display
	var content = $("#cflb_content_"+id).html();
	//$(".cflb_content_wrapper").css({"width":w+"px","height":h+"px"});
	$(".cflb_content_wrapper").html(content);

	// Start by animating 
	$(".cflb_content").animate({
		opacity: 1,
		width: w+'px',
		height: h+'px',
		top: endTop+'px',
		left: endLeft+'px'},
		500,
		'linear');
	
	return false;
}

function playCFLB() {
	// Show pause button by adding new class
	$("#cflbPP").removeClass("cflbPlay").addClass("cflbPause");
	
	// Go to next
	nextCFLB();
	
	// Start timeout
	CFLB_TIMEOUT = setTimeout("playCFLB()", CFLB_SLIDESHOW_TIME*1000);
	
	// Add stop to this listener
	$("#cflbPP").unbind('click');
	$("#cflbPP").click(stopCFLB);
}

function stopCFLB() {
	// Stop time
	clearTimeout(CFLB_TIMEOUT);
	
	// Show play button
	$("#cflbPP").addClass("cflbPlay").removeClass("cflbPause");
	
	// Add play to this listener
	$("#cflbPP").unbind('click');
	$("#cflbPP").click(playCFLB);
}
