/*global $, document, window, navigator*/

$(document).ready(function ()
{
	// cloudify the products cloud
	$("#products-cloud li a").cloudify();
		
	// zoomify '.zoomee' images
	$('.zoomee').jqzoom({zoomWidth: 400, zoomHeight: 400, title: true,	position: 'right', xOffset: 15, yOffset: -140});
	
	// add to the title of lightbox images
	$('a[rel="lightbox[\'pageset\']"]').each(function (index, el)
	{
		var title = $(el).attr('title');
		$(el).attr('title', title + ' [Larger view]');
	});
	
	// blend the 'portfolio'
	$('#portfolio-thumbs a').portfolioBlend({container: '#portfolio-item'});
	
	if(jQuery().slimbox) if (!/android|iphone|ipod|series60|symbian|windows ce|blackberry/i.test(navigator.userAgent)) {
		jQuery(function($) {
			$("a[rel^='lightbox']").slimbox(
			{
				initialWidth: 300,
				initialHeight: 300,
				resizeDuration: 300,
				captionAnimationDuration: 200,
				loop: true,
				imageFadeDuration: 300,
				overlayOpacity: 0.8,
				overlayFadeDuration: 200
			}, null, function(el) 
			{
				return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
			});	 
		});
	};		
});

// need to wait for images to load
$(window).load(function ()
{
	// set news columns to the greatest height
	$('#news li').greatestHeight();	
});

$.fn.cloudify = function () 
{
	$(this).each(function (index, el)
	{
		$(el).css('font-size', Math.floor(Math.random() * (140 - 89)) + 90 + '%');
	});
	
	return this;
};

$.fn.portfolioBlend = function (options) 
{
	var settings = $.extend(
	{
		container: "",
		seperator: "|"
		
	}, options), item_title, current_url, current_url_array;
	
	$(this).click(function (ev) 
	{
		// if container exists
		if ($(settings.container).length)
		{
			// send an ajax request for the item
			$.post($(this).attr('href'), false, function (return_data)
			{
				if (return_data.content !== undefined)
				{
					$(settings.container).hide(0, function () 
					{ 
						$(this).html(return_data.content).fadeIn(1500);
						
						// AUTOLOAD CODE BLOCK FOR SLIMBOX
						if (!/android|iphone|ipod|series60|symbian|windows ce|blackberry/i.test(navigator.userAgent))
						{
							$("a[rel^='lightbox']").slimbox({initialWidth: 300, initialHeight: 300, resizeDuration: 200, captionAnimationDuration: 300, loop: true, imageFadeDuration: 300, overlayOpacity: 0.8, overlayFadeDuration: 200}, null, function (el) 
							{
								return (this === el) || ((this.rel.length > 8) && (this.rel === el.rel));
							});
						}
					});				
				}
				
				if (return_data.title !== undefined)
				{
					item_title = $('title').text().split(settings.seperator);
					
					item_title[0] = return_data.title.replace("&amp;", "&") + ' ';
					
					$('title').text(item_title.join(settings.seperator));
				}
			}, 'json');		
		
			ev.preventDefault();
		}
	});
	
	return this;
};
