// ----------------------------------------------------------------------------
// Tag Cloud Plugin - A jQuery Plugin for creating tag cloud
// v 1.0 Beta
// Dual licensed under the MIT and GPL licenses.
// ----------------------------------------------------------------------------
// Copyright (C) 2010 Rohit Singh Sengar
// http://rohitsengar.cueblocks.net/
// ----------------------------------------------------------------------------
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// ----------------------------------------------------------------------------

var tcTimer = new Array();

jQuery.fn.extend({
	tagCloud: function(options) {
		
		// default configuration properties
		var defaults = {
			direction: 'random',  //
			easein:	'linear', //  "easin option from easin plugin"
			speed: 12000		// increase for slow animation, decrease for faster animation	
		}; 
		
		var options = jQuery.extend(defaults, options); // mapping user options with default options
		
		var offset = jQuery(this).offset();

		
		var idDiv = "#"+jQuery(this).attr("id");	// getting id of the div where plugin is applied.
		
		var tcWidth = parseInt(jQuery(this).css("width"));//-70;
		var tcHeight = parseInt(jQuery(this).css("height"))-30;	
		var direction = '';
		
		jQuery(this).css("list-style","none");
		jQuery(idDiv).children().each(function(i){
			 jQuery(this).css({"z-index":i, "position":"absolute", "top": ((Math.random()*tcHeight)+offset.top)+"px", "left":((Math.random()*(tcWidth - parseInt(jQuery(this).width())))+offset.left)+"px"});
			 jQuery(this).children().each(function(j) {
				 jQuery(this).css({fontSize: (parseInt(jQuery(this).css("font-size"))*1.2)+"px"});
			 });
		});
		jQuery(idDiv).children().each(function(){
			jQuery(this).animate({"top": ((Math.random()*tcHeight)+offset.top)+"px", "left":((Math.random()*(tcWidth - parseInt(jQuery(this).width())))+offset.left)+"px"}, options.speed, options.easein);
		});
		
		jQuery(this).mouseover(function(){ jQuery(this).stop(); clearInterval(tcTimer[idDiv]); });




		
		switch(options.direction)
		{
				case 'vertical' : {		direction = 'jQuery("'+idDiv+'").children().each(function(){jQuery(this).animate({"top": (Math.random()*'+tcHeight+')+"px"}, '+options.speed+', "'+options.easein+'" );});'; 
										break;
							  	  }
				case 'horizontal' : {	direction = 'jQuery("'+idDiv+'").children().each(function(){jQuery(this).animate({"left":(Math.random()*'+tcWidth+')+"px"}, '+options.speed+', "'+options.easein+'" );});'; 
										break;
									}
				default : {	direction = 'jQuery("'+idDiv+'").children().each(function(){jQuery(this).animate({"top": ((Math.random()*'+tcHeight+')+'+offset.top+')+"px", "left":((Math.random()*'+'('+tcWidth+'-parseInt(jQuery(this).width())))+'+offset.left+')+"px"}, '+options.speed+', "'+options.easein+'" );});'; 
								}
		
		}
		tcTimer[idDiv] = setInterval(direction, options.speed+50);
		
		jQuery(this).mouseout(function(){ 
			tcTimer[idDiv] = setInterval(direction, options.speed+50);
		});	
		
	}
		
});

