/*
 * jQuery Plugins
 * http://marostakac.co.uk/
 *
 * Coded by Maros Takac
 
 * Includes: 
 * - fullscreen | maxWidth & maxHeight to be added
 * - tooltips
 * - accordion | scope issue to be sorted out
 * - tabulator
 *
 * Date: Thu Jun 01  2011
*/

(function($){

$.Body = $('body');
    
$.Window = $(window);	

$.Document =  $(document);

		
$.fn.fullscreen = function(options) {
	
	if(options && typeof options == 'object')
	{		
		var $window =  $(window);	
		var elementW =  options.elementW || 1;
		var elementH = options.elementH || 1;
		
		var stageW = screen.width || 1200;
		var stageH = screen.height || 800;	
		
		var ratio = 0;	
		
		if(options['byScreen']!= null && options['byScreen'])
		{
			ratio = stageH/stageW;
			
		} else {
			
			if(elementW == 1 || elementH == 1)
			{
				ratio = 3/4;
			} else {
				ratio = elementH/elementW
			}			
		}
		
		// Get browser window size
		var browserwidth = $window.width();
		var browserheight = $window.height();
		
		var targetWidth = options.targetW || browserwidth;
		var targetHeight = options.targetH || browserheight;
			
		var minWidth = options.minW || null;
		var minHeight = options.minH || null;
		
		if ((browserheight/browserwidth) > ratio)
		{
			if(minWidth && minHeight) // EVALUATE THE MIN WIDTH and HEIGHT INDIVIDIUALLY, USE RATIO TO CALCULATE BOTH
			{
				if(targetWidth < minWidth ){ targetWidth = minWidth;  targetHeight = minHeight;}
				if(targetHeight < minHeight ){ targetHeight = minHeight; targetWidth = minWidth}
			}		
		
			$(this).height(targetHeight);
			$(this).width(targetHeight / ratio);
			
			if(options['maintainRatio'] != null && options['maintainRatio'])
			{
				if((targetHeight / ratio) > targetWidth)
				{				
					$(this).width(targetWidth);
					$(this).height(targetWidth * ratio);		
				}
			}							
					
		} else {		
			
			if(minWidth && minHeight)
			{
				if(targetHeight < minHeight ){ targetHeight = minHeight; targetWidth = minWidth}
				if(targetWidth < minWidth ){ targetWidth = minWidth;  targetHeight = minHeight;}
			}			
					
			$(this).width(targetWidth);
			$(this).height(targetWidth * ratio);
			
			if(options['maintainRatio'] != null && options['maintainRatio'])
			{
				if((targetWidth * ratio) > targetHeight)
				{
					$(this).height(targetHeight);
					$(this).width(targetHeight / ratio);		
				}
			}			
		}	
		
		var _top = (browserheight -  $(this).height())/2;
		var _left = (browserwidth -  $(this).width())/2;
		
		if(options['target'])
		{
			_top = (options.target.height() -  $(this).height())/2;
			_left = (options.target.width() -  $(this).width())/2;	
		}			
		
		if(options.fixEdge && _left < 0)
		{
			$(this).css('left',0);
		} else {
			$(this).css('left',_left);		
		}
				
		if(options.fixEdge && _top < 0)
		{
			$(this).css('top',0);
		} else {
			$(this).css('top',_top);		
		}
	}
	return this; 		
};

$.fn.extend({
	tabulator: function (options) {
		
		var opts = jQuery.extend({}, jQuery.fn.tabulator.defaults, options);
				
		return this.each(function () {
			var $this = $(this);
			var $tabs = $this.find('.tab_controls');
			
			$tabs.find('a').live('click', function(ev){	showElement($(this)); return false;	})
			
			if(opts.first){	showElement($this.find('.tab_controls a:first-child'));}
							
			function showElement(element)
			{
				if(!element) return false;										
					element.addClass('active')
						.siblings().removeClass('active')
						.end()							
					.parents('.tab_controls').next('.tab_content')
						.children("[data-element='" + element.attr('href').replace(/^#/, '') + "']").show().css('opacity',0).animate({'opacity':1}, 600, 'swing')
						.siblings('.tab_block').hide()								
			}
		})
	}
})

jQuery.fn.tabulator.defaults = {
	
	first: false		
}

$.fn.extend({
        accordion: function (options) {
					
			var opts = jQuery.extend({}, jQuery.fn.accordion.defaults, options);
			
            return this.each(function () {
                var $accordion = $(this);
				var $children = $accordion.children('.AccordionPanel');
				var $content = null;
                $.each($children, function () {
					
					if($(this).has('.AccordionPanelTab'))
					{
						$content = $(this).children('.AccordionPanelTab').click(function (e) {
							activate(this);
							return false;
						}).next('.AccordionPanelContent');
						
						$content.data('properties', {							
							'height': $content.outerHeight(true),
							'margin-top': $content.css('margin-top'),
							'margin-bottom': $content.css('margin-bottom'),
							'padding-top': $content.css('padding-top'),
							'padding-bottom': $content.css('padding-bottom')							
						}).hide().css({'height':0, 'padding-top':0, 'padding-bottom':0, 'margin-top':0, 'margin-bottom':0})													
					}										
                });					
				
				if($.isArray(opts.slide) && opts.scope == 'local')
				{
					$.each(opts.slide, function(index,element){
												
						if($children.eq(element).length)
						{
							activate($children.eq(element).children('.AccordionPanelTab'))
						}
					})				
				}									
               
                function activate(a) {
                    var _this = $(a);
					var _parent = _this.parents('.AccordionPanel');
					var _content = _this.next('.AccordionPanelContent');
					
					if(opts.scope == 'local')
					{					
						if(!_content.is(":hidden"))
						{						
							_content.stop().animate({
								'height':0, 'padding-top':0, 'padding-bottom':0, 'margin-top':0, 'margin-bottom':0
							}, {
								duration: opts.speed,
								easing: 'swing',
								step: function()
								{
									if(opts['onStep'] && typeof opts['onStep'] == 'function')
									{
										opts.onStep.call()
									}	
								},
								complete: function()
								{ 
									$(this).css('display','none')
									
									_parent.toggleClass('active')								
								
									if(opts['onComplete'] && typeof opts['onComplete'] == 'function')
									{
										opts.onComplete.call()
									}
								}
							})
							
						} else {
														
							var properties = _content.data('properties');
														
							_content.stop().animate({
								'height':properties['height'], 
								'padding-top':properties['padding-top'],
								'padding-bottom':properties['padding-bottom'],
								'margin-top':properties['margin-top'], 
								'margin-bottom':properties['margin-bottom']
							}, {
								duration: opts.speed,
								easing: 'swing',
								step: function()
								{
									if(opts['onStep'] && typeof opts['onStep'] == 'function')
									{
										opts.onStep.call()
									}	
								},
								complete: function()
								{ 
									_parent.toggleClass('active')						
									if(opts['onComplete'] && typeof opts['onComplete'] == 'function')
									{
										opts.onComplete.call()
									}
								}
							})
							
						}								
						
						
					} else {
						
						_this.parent('.AccordionPanel').toggleClass('active')
						     .siblings().removeClass('active')
							 .children('.AccordionPanelContent').slideUp(opts.speed);
						_this.next('.AccordionPanelContent').slideToggle(opts.speed, function()
						{
							opts.onComplete.call()
						});	
					}																	
                }
            })
        }
    })
	
$.fn.accordion.defaults = {
	
	scope: 'local',
	speed: 800,
	slides: []			
}


$.fn.tooltip = function(options) {

	var opts = jQuery.extend({}, jQuery.fn.tooltip.defaults, options);
	
	return this.each(function(){
		
		$('body').append("<div id='tooltip_outer'><div id='tooltip_inner'></div></div>");
		
		var $tt_outer = $('#tooltip_outer');
		var $tt_inner = $('#tooltip_inner');		
		var $tt_title = '';	
		var $tt_alt = '';
		
		// BODY coordinates		
		var $bd_x = $('body').outerWidth();
		var $bd_y = $('body').outerHeight();
		
		
		$(this).bind('mouseover mouseenter', function(ev) {
							
				if($(this).attr('title'))
				{				
					$tt_title = $(this).attr('title');
					$(this).attr('title', '');							
				}
				
				if($(this).attr('alt'))
				{				
					$tt_alt = $(this).attr('alt');
					$(this).attr('alt', '');							
				}				
				
				if($tt_title != "")
				{
					$tt_inner.html($tt_title);
					$tt_outer.stop(true).css({
						'display': 'block',
						'opacity':0, 
						'top': (ev.pageY - $(this).outerHeight() - 50) + 'px', 
						'left': ev.pageX - $(this).outerWidth()
						
					}).animate({'opacity': 1},500, 'swing');	
				}
								
				
			}).bind('mouseleave mouseout',function(ev){ 
				
				if($tt_title != "")
				{				
					$tt_outer.stop(true).animate({'opacity': 0, 'top': "-=10px"},500, 'swing', function()
					{						
						$tt_outer.hide();
						$tt_inner.html('');					
					});	
				
					if($tt_title){$(this).attr('title',$tt_title)}
					if($tt_alt){$(this).attr('alt',$tt_alt)}
				}
				
		}).mousemove(function(ev){  
	 	
			// TOOL TIP coordinates
			var $tt_x = $tt_outer.outerWidth();
			var $tt_y = $tt_outer.outerHeight();
				 
			$tt_outer.css({'top':  ev.pageY - $tt_y - 50, 'left': ev.pageX - $tt_x/2});		 
		})	  	
	})
}

$.fn.tooltip.defaults = {
	
	orientation: 'center bottom',
	offset: 20		
}

})(jQuery);


