// JavaScript Document

function imgpreload(imgs,settings)
{
	// settings = { each:Function, all:Function }
	if (settings instanceof Function) { settings = {all:settings}; }

	// use of typeof required
	// https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Operators/Special_Operators/Instanceof_Operator#Description
	if (typeof imgs == "string") { imgs = [imgs]; }

	var loaded = [];
	var t = imgs.length;
	var i = 0;

	for (i; i<t; i++)
	{
		var img = new Image();
		img.onload = function()
		{
			loaded.push(this);
			if (settings.each instanceof Function) { settings.each.call(this); }
			if (loaded.length>=t && settings.all instanceof Function) { settings.all.call(loaded); }
		};
		img.src = imgs[i];
	}
}

if (typeof jQuery != "undefined")
{
	(function($){

		// extend jquery (because i love jQuery)
		$.imgpreload = imgpreload;

		// public
		$.fn.imgpreload = function(settings)
		{
			settings = $.extend({},$.fn.imgpreload.defaults,(settings instanceof Function)?{all:settings}:settings);

			this.each(function()
			{
				var elem = this;

				imgpreload($(this).attr('src'),function()
				{
					if (settings.each instanceof Function) { settings.each.call(elem); }
				});
			});

			// declare urls and loop here (loop a second time) to prevent
			// pollution of above closure with unnecessary variables

			var urls = [];

			this.each(function()
			{
				urls.push($(this).attr('src'));
			});

			var selection = this;

			imgpreload(urls,function()
			{
				if (settings.all instanceof Function) { settings.all.call(selection); }
			});

			return this;
		};

		// public
		$.fn.imgpreload.defaults =
		{
			each: null // callback invoked when each image in a group loads
			, all: null // callback invoked when when the entire group of images has loaded
		};

	})(jQuery);
}

$(document).ready(function() {

	$('a').focus(function(){
		$(this).blur();
	});
	
	// browser detection
	
	var opera = $.browser.opera;
	
	// bacground images
	
	var bgImage = new Array();
	
	bgImage = [ 'bg1.jpg', 'bg2.jpg', 'bg3.jpg', 'bg4.jpg', 'bg5.jpg' ];
	
	// images no patterns
	
	$('.project .img img').each(function(){
	
		if(opera){
			var imgWidth = $(this).attr('width');
			var imgHeight = $(this).attr('height');
			// add px
			imgWidth = imgWidth + 'px';
			imgHeight = imgHeight + 'px';
		}
		else
		{
			var imgWidth = $(this).css('width');
			var imgHeight = $(this).css('height');			
		}
		
		//alert($(this).attr('src') + ' - ' + $(this).attr('height'));
		
		$(this).parent().css({ 'width': imgWidth, 'height': imgHeight });
		
		var imgHeightPx = imgHeight.slice(0,-2);
		var imgWidthPx = imgWidth.slice(0,-2);
		
		var preloader = '<img class="preloader" style="top:' + parseInt((imgHeightPx/2)-16) + 'px; left:' + parseInt((imgWidthPx/2)-16) + 'px;" src="http://bratn.com/img/preloader.gif" alt="preloader"/>';
		$(this).parent().append(preloader);
	
	});
	
	// images preloader
	
	$('.project .img img').not('img[class=preloader]').imgpreload
	({
		each: function()
		{
			var fadeSpeed = 600;
			$(this).parent().find('.preloader').fadeOut(fadeSpeed, function(){
				$(this).parent().children('img').not('img[class=preloader]').fadeIn(500);
			})
		}
	});
	
	$('.hit-area').mouseenter(function(){
		Cufon.replace('span.email', { color: '#6C6C6C' });
	}).mouseleave(function(){
		Cufon.replace('span.email', { color: '#ffffff' });
	}).click(function(){
		document.location = 'mailto:contact@bratn.com';
	});

	$('.hit-flic').mouseenter(function(){
		$(this).parent().find('img').attr('src', 'http://bratn.com/img/flickr-on.jpg');
	}).mouseleave(function(){
		$(this).parent().find('img').attr('src', 'http://bratn.com/img/flickr.jpg');
	});
	
	$('.hit-beh').mouseenter(function(){
		 $(this).parent().find('img').attr('src', 'http://bratn.com/img/behance-on.jpg');
	}).mouseleave(function(){
		$(this).parent().find('img').attr('src', 'http://bratn.com/img/behance.jpg');
	});
	
	// for IE 
	
	$('.site img[alt=flickr]').mouseenter(function(){
		$(this).attr('src', 'http://bratn.com/img/flickr-on.jpg');
	}).mouseleave(function(){
		$(this).attr('src', 'http://bratn.com/img/flickr.jpg');
	});
	
	$('.site img[alt=behance]').mouseenter(function(){
		 $(this).attr('src', 'http://bratn.com/img/behance-on.jpg');
	}).mouseleave(function(){
		$(this).attr('src', 'http://bratn.com/img/behance.jpg');
	});
	
});	

Cufon.replace('h1, span.for, span.grey, .categories span', { 
	fontFamily: 'Kozuka Gothic Pro OpenType M'
});

Cufon.replace('span.email', { 
	fontFamily: 'Kozuka Gothic Pro OpenType M',
	hover: {color: '#6C6C6C'}
});
