/*
*	Author:		Jelle van der Coelen
*				jelle@jellevandercoelen.com
*
*	Version: 	1.5.0.5
*/
var coolbox = null; 		 		 // --- coolbox default = null
var tempdir = '/coolbox/templates/'; // --- templates directory

/*======================================*/
/*	common functions					*/ 
/*======================================*/
// --- getScrollY is used to correctly position the cb_viewport in IE (also used in FF, but isn't necessary)
function getScrollY()
{
	var scrOfY = 0;
	
	if(typeof(window.pageYOffset) == 'number')
		scrOfY = window.pageYOffset;
	else if(document.body && document.body.scrollTop)
		scrOfY = document.body.scrollTop;
	else if(document.documentElement && document.documentElement.scrollTop)
		scrOfY = document.documentElement.scrollTop;

	return scrOfY;
};

// --- killCoolBox
function killCoolBox()
{
	if(coolbox)
	{
		if(WARN == true)
			coolbox.warn();
		else
		{
			coolbox.kill();
			
			coolbox = null;
		};
	};
};

// --- triggerCoolBox can be called seperately
function triggerCoolBox(title, msg, options, img)
{
	$('object').hide();
	
	if(coolbox != null)
		coolbox.kill();

	coolbox = new CoolBox(title, msg, options, img);
		
	coolbox.trigger();
};

/*======================================*/
/*	document ready functions			*/ 
/*======================================*/
$(document).ready(function()
{
	// --- append viewport to determine absolute height and kill CoolBox on click event
	$('body').append('<div id="cb_viewport" onclick="killCoolBox();"></div>');
	
	// --- set on click event to all elements with class CoolBox
	$('.CoolBox').click(function()
	{
		WARN = ($(this).hasClass('cb_warn') ? true : false);
		
		$('#cb_viewport').css(
		{
			'zIndex': 		'1999',
			'background': 	'#000000',
			'opacity':		0.7,
			'top':			getScrollY()+'px'
		});
		
		var title = $(this).attr('title');
		var href  = $(this).attr('href');

		if(href == null || href == '')
			href = $(this).attr('alt');

		if(href.indexOf('?') != -1)
		{
			url = href.split('?');
			url = url[1].split('&');
			
			var args = new Array();
			for(var i = 0; i < url.length; i++)
			{
				var arg = url[i].split('=');
				
				args[arg[0]] = arg[1];
			};
		};
		
		var img = 0;
		if(href.indexOf('.jpg') != -1)
			img = 1;
				
		triggerCoolBox(title, href, args, img);
		
		return false;
	});
	
	$(document).scroll(function()
	{
		$('#cb_viewport').css('top', $(document).scrollTop());
	});
	
	$(document).keyup(function(e)
	{
		if(e.keyCode == 27)
			killCoolBox();
	});
});

/*======================================*/
/*	CoolBox								*/
/*======================================*/
function CoolBox(title, href, options, img)
{
	/*
	 */
	var defaults =
	{
		/*
		 */
		'title'				: title,
		'href'				: href,
		
		/*
		 */
		'tempDir'			: tempdir,
		'type'				: 'default.html',
		
		/*
		 */
		'width'				: 350,
		'height'			: 150,
		
		/*
		 */
		'post'				: false,
		
		/*
		 */
		'animate'			: false,
		'animationDelay'	: 500
	};
	
	/* 
	 */
	var options = $.extend(defaults, options);
	
	/*
	 */
	var screen_height = document.getElementById('cb_viewport').offsetHeight;
	
	defaults.left	= (((parseInt($('#cb_viewport').css('width'))  - defaults.width)  / 2) + $(document).scrollLeft());
	defaults.top	= (((screen_height - defaults.height) / 2) + $(document).scrollTop());

	/*
	*
	*/
	this.trigger = function()
	{
		$('body').append('<div id="cb_container"></div>');
		
		$('#cb_container').css(
		{
			'height' 	: defaults.height+'px',
			'width' 	: defaults.width+'px',
			'zIndex'	: '2000'
		});
		
		if(img == 0)
		{
			$.post(defaults.tempDir+defaults.type, {}, function(data)
			{
				$('#cb_container').html(data);
			});
		}
		else if(img == 1)
		{
			var img_title = '';
			
			current_img = new Image();
			current_img.src = defaults.href;
		
			if(current_img.height > parseInt($('#cb_viewport').css('height')) - 100)
			{
				current_img.height = parseInt($('#cb_viewport').css('height')) - 100;
				
				var percent = (((current_img.height * 100) / defaults.height) / 100);
				
				current_img.width = (parseInt(defaults.width) * percent);
			};
		
			if (defaults.title != null && defaults.title != '')
				var img_title = '<div id="cb_title">' + defaults.title + '</div>';
					
			new_height = current_img.height;
			
			if(img_title)
				new_height = parseInt(current_img.height) + 26;
					
			$('#cb_container').html
			(
				'<div id="cb" style="background: none;">'+
					img_title+
				'	<div id="cb_window">'+
				'		<div id="cb_content">'+
				'			<img style="width: '+current_img.width+'px;height: '+current_img.height+'px;" id="cb_image" src="'+defaults.href+'" alt="'+defaults.title+'" onclick="coolbox.kill();" />'+
				'		</div>'+
				'	</div>'
			).css(
			{
				'height': new_height+"px",
				'width': current_img.width+"px"
			});
			
			defaults.height = new_height+"px";
			defaults.width = current_img.width+"px";
		};
		
		this.CurrentHeight = (defaults.Height - 28);
		
		$('body').append('<div id="cb_overlay"></div>');
		$('#cb_overlay').css(
		{
			'height' 		: (parseInt(defaults.height) + 20)+'px',
			'width' 		: (parseInt(defaults.width) + 20)+'px',
			'opacity' 		: 0.4,
			'position' 		: 'absolute',
			'background' 	: '#000000',
			'top' 			: (defaults.top-10)+'px',
			'left' 			: (defaults.left-10)+'px'
		});
		
		$('#cb_container').css({'left': defaults.left, 'top': defaults.top}).fadeIn(150);
		
		if(defaults.animate != false)
			coolbox.animate();
	};
	
	/*
	*
	*/
	this.setVars = function()
	{
		$('#cb').css({'width': defaults.width+'px'});
		$('#cb_window').css({'height': ((defaults.height - 9) - parseInt($('#cb_title').css('height')))+'px'});
		$('#cb_title').prepend(defaults.title);
		
		$.post(defaults.href, (defaults.post == 'true' ? options : {}), function(data)
		{
			$('#cb_content').html(data);
		});
	};
	
	/*
	* 
	*/
	this.animate = function()
	{
		if (defaults.animate == 'slideIn')
		{
			$('#cb_container').css('width', '0px').hide().animate({'width': defaults.width + 'px'}, eval(defaults.animationDelay));
			$('#cb_overlay').css('width', '0px').hide().animate({'width': (parseInt(defaults.width)+20) + 'px'}, eval(defaults.animationDelay));
		}
		else if(defaults.animate == 'fadeIn')
		{
			$('#cb_container').hide().animate({'opacity': 'toggle'}, eval(defaults.animationDelay));
			$('#cb_overlay').hide().animate({'opacity': 'toggle'}, eval(defaults.animationDelay));
		}
		else if(defaults.animate == 'insideOut')
		{
			var screen_center_left = (defaults.left + (defaults.width / 2));
		
			$('#cb_container').css(
			{
				'left'		: screen_center_left + 'px',
				'width'		: '0px'
			}).animate(
			{
				'left'		: defaults.left+'px',
				'width'		: defaults.width+'px'
			}, eval(defaults.animationDelay));
			
			$('#cb_overlay').css(
			{
				'left'		: screen_center_left + 'px',
				'width'		: '0px'
			}).animate(
			{
				'height' 	: (parseInt(defaults.height) + 20)+'px',
				'left'		: defaults.left-10+'px',
				'top'		: defaults.top-10+'px',
				'width' 	: (parseInt(defaults.width) + 20)+'px'
			}, eval(defaults.animationDelay));
		};
	};
	
	/*
	*
	*/
	this.shake = function()
	{
		var o = $('#cb_window').parent();
		
		d = 30;
		
		o.animate({'marginLeft' : '-=20px', 'marginTop' : '-=10px'}, d)
		.animate({'marginLeft' : '+=20px', 'marginTop' : '+=10px'}, d)
		.animate({'marginLeft' : '+=20px', 'marginTop' : '+=10px'}, d)
		.animate({'marginLeft' : '-=20px', 'marginTop' : '-=10px'}, d)
		.animate({'marginLeft' : '-=20px', 'marginTop' : '-=10px'}, d)
		.animate({'marginLeft' : '+=20px', 'marginTop' : '+=10px'}, d)
		.animate({'marginLeft' : '+=20px', 'marginTop' : '+=10px'}, d)
		.animate({'marginLeft' : '-=20px', 'marginTop' : '-=10px'}, d);
	};
	
	/*
	*
	*/
	this.kill = function()
	{
		$('#cb_container').remove();
		$('#cb_overlay').remove();
		
		$('object').show();
				
		$('#cb_viewport').animate({'opacity': 'toggle'}, eval(defaults.animationDelay), function()
		{
			$(this).css({
				'zIndex': '-1',
				'background': 'none',
				'opacity': 1,
				'display': 'block'
			});
		});
		
		coolbox = null;
	};
	
	/*
	*
	*/
	this.warn = function()
	{
		if(confirm('Weet u zeker dat u dit scherm wilt sluiten?\n\nAangebrachte wijzigingen zullen niet worden opgeslagen.'))
			this.kill();
	};
};