/**
* Toxic Galaxy UI (GUI)
* 
* @copyright 	: Toxic Media Ltd. Sofia, Bulgaria
* @author 	: Angel Kostadinov (angel.kostadinov@toxicmedia.bg)
* @version	: 1.0
*/
var GUI = function() /* Galaxy UI */
{
	this.fx = 
	{
		fadeIn: 	$.browser.msie ? 0 : 250,
		fadeOut: 	$.browser.msie ? 0 : 250,
		fade: 		$.browser.msie ? 0 : 250,
		scroll: 	300,
		rebuid:		200,
		loop: 		6000
	}
	
	this.opacity =  0.0018;
	
	/* Plugins */
	$.fn.OE = this.OE; 
	$.fn.showroom = this.showroom;
	
	this.running = false;
	
	this.timeout = null;
};

GUI.prototype.getDomain = function()
{
	return preloader.getDomain();
}

GUI.prototype.prePreload = function()
{
	return this;
};

GUI.prototype.postPreload = function()
{
	return this;
};

GUI.prototype.preload = function( callback ) /* Preload large files */
{
	return callback();
	
	var complete = callback;
	
	/* Cache array */
	var cache = [];
	
	/* Number of loaded items */
	var items = 0;
	
	/* Load sheet */
	var sheet = $.ajax({
		url: this.getDomain() + '/templates/themes/galaxy/styles/default.css',
		async:false
	}).responseText;
	
	/*  Regex */
	var resources = sheet.match(/[^\(]+\.(gif|jpg|jpeg|png)/g);
	
	var onComplete = function( callback )
	{
		for (var c in cache)
		{
			if (cache[c].complete && cache[c].loaded)
			{
				items++;
			}
		}
		 
		var percent = Math.abs((Math.abs(items)/cache.length)*100).toPrecision(3);
		
		$('.progress').html(percent + '% complete');
		
		if (items < cache.length)
		{
			setTimeout(onComplete,30);
		}
		else 
		{
			setTimeout(complete,8000);
			
			return true;
		}
	};
	
	for (var i in resources)
	{
		image = new Image();
		image.onload = function()
		{
			this.loaded = true;
		};
		image.onerror = function()
		{
			this.loaded = false;
		};
		image.src = null;
		image.src = this.getDomain() + '/templates/themes/galaxy/' + resources[0].substr(3);
		
		cache.push(image);
	}
	
	setTimeout(onComplete);
};

/**
* (OE) Add Styles
* Loads runtime CSS stylesheet(s)
*/
GUI.prototype.addStyles = function( styles )
{
	if (!$('link[href$="' + styles + '"]','head').length)
	{
		jQuery(document.createElement('link')).attr(
		{
	            href: styles,
	            type: 'text/css',
	            rel: 'stylesheet'
	    }).appendTo('head'); 
	}
	return this;
};

GUI.prototype.addBlockStyles = function()
{
	if ($.browser.msie && 6.0 == $.browser.version)
	{
		this.addStyles(this.getDomain() + '/templates/themes/galaxy/styles/stop.css');
	}
};

/**
* (OE) Overfade effect
* Applies fadeIn/Out hover effect to elements
*/
GUI.prototype.OE = function( options ) 
{
	$(this).append('<div></div>').each(function()
	{
		var $fade = $('> div', this).css('opacity', 0);
		var $link = $('a',this).clone(true);
		
		$('> div',this).append($link);
		
		$(this).hover(function () 
		{
			$fade.stop(true,true).fadeTo(gx.fx.fadeIn, 1, 
			function()
			{
				if ($.browser.msie)
				{
					this.style.removeAttribute('filter');
				}
			});
		}, function () 
		{
			$fade.stop(true,true).fadeOut(gx.fx.fadeOut);
		}).andSelf().disableTextSelect();
	});
};

GUI.prototype.dataText = function( text, caption )
{
	var data = $('.monitor-data');
	
	if (typeof text != "undefined" && typeof caption != "undefined")
	{
		var w = parseInt(text.toString().length + caption.toString().length) * 7.2;
	
		setTimeout(function()
		{
			data.stop().empty().animate({ width: w, queue: false}, gx.fx.rebuild, function()
			{
				$(this).html(text + ' <span>' + caption + '</span>');
			});
		},0);
	}
	
};

GUI.prototype.unblockUI = function()
{
	$(window).unbind();
		
	$('.overlay-layer, .overlay-message').remove();

	$('.overlay').fadeOut(gx.fx.rebuild, function()
	{
		$(this).remove();
	});
};

GUI.prototype.blockUI = function( useMessage )
{
	var overlay = $('<div></div>').addClass('overlay');
	var message = $('<div></div>').addClass('overlay-message');
	
	var size = function()
	{
		var w, h; 
		
		h = Math.max(document.body.scrollHeight, document.body.offsetHeight);
		
		w = jQuery.boxModel && document.documentElement.clientWidth || document.body.clientWidth;

		w = w <= document.body.scrollWidth ? document.body.scrollWidth : w;

		return { width: w, height: h };
	};
	
	var resize = function()
	{
		dimension = size();
		
		overlay.css(
		{ 
			width: dimension.width,
			height: dimension.height
		});
	};
	
	dimension = size();

	overlay.bind('click',gx.unblockUI).css(
	{ 
		opacity: .7,
		width: dimension.width,
		height: dimension.height
	}).appendTo(document.body).disableTextSelect();
	
	if (useMessage) $(document.body).append(message);
	
	$(window).bind('resize',resize);
	
	return this;
};

GUI.prototype.stopAll = function()
{
	if (this.timeout) clearTimeout(this.timeout);
	
	return this;
}

GUI.prototype.showroom = function( options )
{
	var obj = jQuery(this);
	
	var loop = function( delay )
	{
		if (!gx.running)
		{
			gx.stopAll();
			
			gx.timeout = setTimeout(nextSlide,delay);
		}
	};

	var loadSlide = function(source, callback)
	{
		if (!gx.running)
		{
			gx.running = true;
			
			var slide = new Image();
	
			/* Attach slide load callback */
			slide.onload = callback;
			
			/* Image source */
			slide.src = source;
		}
	};
	
	var showSlide = function(slide, image)
	{	
		$('li.active',obj).stop(true,true).fadeOut(gx.fx.fade, function()
		{
			$(this).removeClass('active');
			
			$('li',obj).eq(--slide).stop(true,true).fadeIn(gx.fx.fadeIn, function()
			{
				$(this).addClass('active');
				
				gx.running = false;
			});
		});
	};
	var nextSlide = function()
	{
		var ul = $('.monitor-page ul');
		
		$('li.active', obj).stop(true,true).fadeOut(gx.fx.timeout,function()
		{
			var collection = $('li:first',obj).add($(this).next());
			
			var slide = collection.slice(-1);
			
			/* Parent control */
			var parent = $('li',ul).eq(slide.index());
			
			if (!gx.running && !parent.hasClass('active'))
			{
				parent.addClass('active').siblings().removeClass('active');
				
				/* Update data text */	
				gx.dataText(slide.attr('title'), slide.attr('technologies'));
				
				showBaloon(parent.position().left, slide.index());

				/* Load & display slide */
				loadSlide($('img:first',slide).attr('src'), function()
				{
					showSlide(slide.index() + 1, this);
				});
			}
		});
		
		loop(gx.fx.loop);
	};
	
	var loadSequence = function( items, ul )
	{
		if (items.length)
		{
			var item = items.shift();
			
			ul.animate(
			{
				width: '+=32'
			},250,function()
			{
				item.appendTo(ul).OE();
				
				return loadSequence(items,ul);
			});
		}
		else 
		{
			var li = $('li:first',obj);
			
			gx.dataText(li.attr('title'), li.attr('technologies'));
			
			showBaloon(li.position().left, 0);
			
			/* Autoslide */
			loop(gx.fx.loop);
		}
		
		return false;
	}
	
	var showBaloon = function(offset, item)
	{
		var baloon = $('.baloon');
		
		baloon.find('a:first').unbind().bind('click',
		function()
		{
			gx.stopAll().selectPrimaryButton('portfolio').portfolio(item);
			
			return false;
		}).text(gx.texts.details);
		
		baloon.hide(0).css({ left: offset}).fadeIn(gx.fx.fadeIn);
	};
	
	return this.each(function()
	{
		var ul = $('.monitor-page ul');
		
		var items = [];
		
		$('li',this).each(function(index)
		{
			var li = $(this).clone();

			li.empty().append('<a href="javascript:void(0)">' + (++index) + '</a>');
			
			var source = $('img:first',this).attr('src');
			
			$('a',li).bind('click', function()
			{
				var parent = $(this).parents().filter('li:first');
				
				if (!gx.running && !parent.hasClass('active'))
				{
					loop(10000);
					
					parent.addClass('active').siblings().removeClass('active');
					
					showBaloon(parent.position().left, index - 1);
					
					/* Update data text */	
					gx.dataText(li.attr('title'), li.attr('technologies'));

					/* Load & display slide */
					loadSlide(source, function()
					{
						showSlide(index, this);
					});
				}
			});
			
			items.push(li);
		});
		
		loadSequence(items, ul);
	});
};