/* ----------------------------------
jQuery Blox 0.9

tested with jQuery v1.2+
©2009 CSSLab.cl
free for any use, of course... :D
instructions: http://www.csslab.cl/2009/05/25/jquery-blox/
---------------------------------- */

jQuery.fn.blox = function(options){

	settings = jQuery.extend({
		container: '#blox',
		content: '.blox_content',
		box: '.blox_box',
		close: '.blox_close',
		fadein: 0.2,
		fadeout: 1,
		timein: 200,
		timeout: 200,
		animation: 'yes',		// values: [yes/no]
		transparency: 'yes',	// values: [yes/no]
		direction: 'lefty' 		// values: [lefty/righty]
	}, options);
			
	var fadeIn = settings.fadein;
	var fadeOut = settings.fadeout;
	var timeIn = settings.timeIn;
	var timeOut = settings.timeOut;
	var widthContainer = $(settings.container).width();
	var heightContainer = $(settings.container).height();

	$(settings.container+'>'+settings.content).each(function(){
		
		// if hover
		if(settings.transparency == 'yes') {
			$(this).find(settings.box).hover(
				function(){
					$(settings.box).animate({'opacity': fadeIn},{queue:false,duration:timeIn});
					$(this).animate({'opacity': 1},{queue:fadeOut,duration:timeOut})
				},
				function(){
					$(settings.box).animate({'opacity': fadeOut},{queue:false,duration:timeOut})
				}	
			);
		};
		// endif hover
		
		
		// == lefty
		if(settings.direction == 'lefty'){
			$(settings.box).css({'float':'left'});
		};
		// == end lefty
		
		// == righty
		if(settings.direction == 'righty'){
			$(settings.box).css({'float':'right'});
		};
		// == end righty
		
		var widthContent = $(settings.content).width();
		var heightContent = $(settings.content).height();
		
		var upper = $(this).css('top');
		var lefter = $(this).css('left');
		
		$(this).find(settings.box).click(function(){
			// if animation
			if(settings.animation == 'yes'){
				$(this).parent().animate({'top':0, 'left':0, 'width':widthContainer, 'height':heightContainer},{queue: false, duration: settings.timeIn});
			} else {
				$(this).parent().css({'top':0, 'left':0, 'width':widthContainer, 'height':heightContainer});
			};
			// endif animation
			$(this).parent().css({'z-index': 20});
		});
		
		$(this).find(settings.close).click(function(){
			// if animation
			if(settings.animation == 'yes'){
				$(this).parent().animate({'top':upper, 'left':lefter, 'width':widthContent, 'height':heightContent},{queue: false, duration: settings.timeOut, complete: zindex});
			} else {
				$(this).parent().css({'top':upper, 'left':lefter, 'width':widthContent, 'height':heightContent, 'z-index': 0});
			};
			function zindex(){
				$(this).css({'z-index': 0});
			};
			// endif animation
		});

	});

};