/*
*	===========
*	  CoolBox
*	===========
*
*	Version: 3.0.0.1
*	Date: 14th April, 2010 - 18:52 GMT+1
*
*	This JavaScript function uses:	jQuery 1.4.2
*
*	(c)2010 Jelle van der Coelen. All rights reserverd
*	http://www.jellevandercoelen.com | jelle@jellevandercoelen.com
*
*	Distributed under the Creative Commons Attribution-Noncommercial-Share Alike 3.0 Netherlands
*	http://creativecommons.org/licenses/by-nc-sa/3.0/nl/deed.en_US
*
*/
var tempdir  	= ROOT_PATH+'js/coolbox/templates/'; 				// --- templates directory
var loaderpath  = ROOT_PATH+'image/jlightbox/loading.gif'; 			// --- path to loading image
var warnmsg	 	= 'Weet u zeker dat u dit scherm wilt sluiten?';	// --- message to show when closing coolbox

//init
var coolbox  		 = null;
var current_coolbox  = null;
var warn	 		 = false;

var CoolBox_postvars = new Array();


/*======================================*/
/*	CoolBox								*/ 
/*======================================*/
function CoolBox(options)
{
	/*
		Set default variables which will be replaced by options
	 */
	var defaults =
	{
		group			: null,
		height			: 385,
		href			: null,
		image_template	: 'default.html',
		is_image		: false,
		post			: false,
		postvars		: {},
		temp_dir		: tempdir,
		template		: 'default.html',
		title			: null,
		width			: 480
	};
	
	var options = $.extend(defaults, options);
	
	/*
		Set optional postvars
	 */
	if(options.cbpv != null)
		options.postvars = CoolBox_postvars[options.cbpv];
	
	/**
	 * 
	 */
	this.initialize = function(force_position)
	{
		/*
			Calculate viewport dimensions and fix IE
		 */
		options.viewport_height = parseInt($('#cb_viewport').css('height').replace("px", ""));
		options.viewport_width  = parseInt($('#cb_viewport').css('width').replace("px", ""));
	
		if($.browser.msie)
			options.viewport_height = (options.viewport_height / 2);
		
		/*
			Set position 
	 	*/
		if(options.left == null || force_position === true) options.left = (((options.viewport_width  - options.width)  / 2) + $(window).scrollLeft());
		if(options.top == null  || force_position === true) options.top  = (((options.viewport_height - options.height) / 2) + $(window).scrollTop());
	};
	
	/**
	 * 
	 */
	this.trigger = function()
	{
		if(options.left == null) this.initialize(); //so you can forget to initialize
		
		/*
			Append and position loader
		 */
		this.appendLoader();
		
		/*
			Fade in #cb_viewport
		 */
		$('#cb_viewport').css
		({
			backgroundColor : '#000000',
			opacity			: 0.7,
			zIndex			: 1999
		});
		
		/*
			Append and position #cb_container
		 */
		$('body').append
		(
			$('<div></div>')
			.attr('id', 'cb_container')
			.css
			({
				height : options.height+"px",
				left   : options.left+"px",
				top    : options.top+"px",
				width  : options.width+"px"
			})
		);
		
		/*
			Load #cb_container
		 */
		$.post(options.temp_dir+(options.is_image ? options.image_template : options.template), {}, function(data)
		{
			$('#cb_container').html(data);
			
			//set title
			if(options.title != null && options.title !="")
			{
				var pl = parseInt($('#cb_title').css('paddingLeft'));
				var pr = parseInt($('#cb_title').css('paddingRight'));
				var pt = parseInt($('#cb_title').css('paddingTop'));
				var pb = parseInt($('#cb_title').css('paddingBottom'));

				$('#cb_title')
				.prepend(options.title)
				.css
				({
					display : 'block',
					width   : (options.is_image ? "auto" : ((options.width - pl) - pr)+"px")
				});
			};
		});
		
		
		/*
			Load content
		 */
		if(!options.is_image)
		{
			$.post(options.href, (options.post == true ? options.postvars : {}), function(data)
			{
				$('#cb_content').html(data);
				
				$('#cb_loader').remove();
				
				$('#cb_container').fadeIn(300);
			});
		}
		
		/*
			Load image
		 */
		else
		{
			var current_image = new Image();
			
			var _date = new Date();
			var _time = _date.getTime();
			
			current_image.src = options.href+"?"+_time;
			
			current_image.onload = function()
			{
				//to high
				if(current_image.height > (options.viewport_height - 100))
				{
					options.height = current_image.height;
					
					current_image.height = (options.viewport_height - 100);
					
					current_image.width = ((parseInt(current_image.width) * current_image.height) / options.height);
				}
				
				//to wide
				else if(current_image.width > (options.viewport_width - 100))
				{
					options.width = current_image.width;
					
					current_image.width = (options.viewport_width - 100);
					
					current_image.height = ((parseInt(current_image.height) * current_image.width) / options.width);
				};
				
				options.height = parseInt(current_image.height);
				options.width  = parseInt(current_image.width);
				
				//append image
				var img = new Image();
				
				$(img)
				.attr('id',    'cb_image')
				.attr('src',   options.href)
				.attr('alt',   options.title)
				.bind('click', function(){killCoolBox()})
				.css
				({
					width  : options.width,
					height : options.height
				});
				
				$('#cb_content').append(img);
				
				$('#cb_window').css
				({
					height   : options.height,
					overflow : 'hidden'
				});
				
				$('#cb_loader').remove();
				
				//fade in container
				$('#cb_container').fadeIn(300);
				
				options.title_height = parseInt($('#cb_title').outerHeight(true));
				
				//initialize to recalculate position
				coolbox.initialize(true);

				$('#cb_container').css
				({
					height : (options.height + (options.title != null && options.title !="" ? options.title_height : 0))+"px",
					left   : options.left+"px",
					top    : (options.top - (options.title != null && options.title !="" ? (options.title_height / 2) : 0))+"px",
					width  : options.width+"px"
				});
			};
		};
	};
	
	/**
	 * 
	 */
	this.kill = function(keep)
	{
		if(keep != true)
		{
			/*
				Fade out #cb_viewport
		 	*/
			$('#cb_viewport').css({zIndex: -1}).animate({opacity: 0}, 500);
		
			/*
				Fade out and remove #cb_container
		 	*/
			$('#cb_container').fadeOut(500, function()
			{
				$(this).remove();
			});
		}
		else
			$('#cb_container').remove();
		
		$('#cb_loader').remove();
	};
	
	/**
	 * 
	 */
	this.switchImage = function(type)
	{
		if(options.group != null && options.group != "")
		{
			coolboxes = $('.CoolBox[rel='+options.group+']').get();
			
			for(var i = 0; i < coolboxes.length; i++)
			{
				if(current_coolbox.attr('href') == $(coolboxes[i]).attr('href'))
					break;
			};
			
			var index = (type == "previous" ? (i - 1) : (i + 1));
			
			if(coolboxes[index] == null)
				index = (type == "next" ? 0 : (coolboxes.length - 1));
			
			killCoolBox(true);
			
			$(coolboxes[index]).trigger('click');
		}
		else
			return false;
	};
	
	/**
	 * 
	 */
	this.appendLoader = function()
	{
		$('body').append('<img id="cb_loader" src="'+loaderpath+'" alt="Loader" />');
		
		$('#cb_loader').css(
		{
			'left':	(((options.viewport_width  - 220) / 2) + $(document).scrollLeft()),
			'top':	(((options.viewport_height - 19)  / 2) + $(document).scrollTop()),
			'position':	'absolute',
			'zIndex': 3000
		});
	};
};


/*======================================*/
/*	document functions					*/ 
/*======================================*/
$(document).ready(function()
{
	/*
		Append viewport
	 */
	$('body').append('<div id="cb_viewport" onclick="killCoolBox();"></div>');
	
	/*
		.CoolBox click event
	 */
	$('.CoolBox').click(function()
	{
		var a = $(this);
		
		current_coolbox = a;
		
		var options = new Array();
		
		//warn before close
		warn = (a.hasClass('warn') ? true : false);
		
		//href
		var href = a.attr('href');
		
		//input instead of a
		if(href == null || href == "")
			href = a.attr('alt');
		
		//parameters
		if(href.indexOf('?') != -1)
		{
			var url = href.split("?");
			    url = url[1].split("&");
			
			for(var i = 0; i < url.length; i++)
			{
				var option = url[i].split('=');
				
				options[option[0]] = option[1];
			};
		};
		
		options['title'] = " " + a.attr('title');
		options['group'] = a.attr('rel');
		options['href']  = href;
		
		//is image
		if(href.indexOf('.jpg') != -1 || href.indexOf('.jpg') != -1 || href.indexOf('.jpg') != -1)
			options['is_image'] = true;
		
		//trigger CoolBox
		coolbox = new CoolBox(options);
		
		coolbox.initialize();
		
		coolbox.trigger();
		
		return false;
	});
});

/*
	Key funcions
 */
$(document).keyup(function(e)
{
	//esc = kill
	if(e.keyCode == 27)
	{
		killCoolBox();
	};
	
	//right arrow = next
	if(e.keyCode == 39)
	{
		if(coolbox != null)
			coolbox.switchImage('next');
	};
	
	//left arrow = previous
	if(e.keyCode == 37)
	{
		if(coolbox != null)
			coolbox.switchImage('previous');
	};
});


/*======================================*/
/*	extra functions						*/ 
/*======================================*/
function killCoolBox(keep)
{
	if(coolbox)
	{
		coolbox.kill(keep);
	};
	
	coolbox = null;
};


/*======================================*/
/*	CoolBox	plugin						*/ 
/*======================================*/
jQuery.fn.coolbox = function(options)
{
	if(options == null)
		options = new Array();
	
	$(this).each(function()
	{
		var href = ($(this).attr('href') != null ? "href" : "alt");
		var uri  = "";
		
		for(key in options)
		{
			if(key != "postvars")
				uri += key+"="+options[key]+"&";
		};
		
		uri = uri.substr(0, (uri.length - 1));
		
		uri = ($(this).attr(href).indexOf('?') == -1 ? "?" : "&")+uri;

		if(options.postvars != null)
		{
			CoolBox_postvars.push(options.postvars);
			
			uri += "&cbpv="+(CoolBox_postvars.length - 1)+"&post=1";
		};

		$(this).attr(href, $(this).attr(href)+uri);
	});
	
	return jQuery;
};
