/**
 * (c) adrianbradford.com - by fabric creative <fabriccreative.com>
 * @author	Jonathan Cochran <jono.cochran@gmail.com>
 */
$(document).ready(function() {
	
/**
 * BROWSER RELATED
 * --------------------------------------------------------- */
	var docLoc = window.location.href;
	var badBrowser = (/MSIE ((5\.5)|6|7|8)/.test(navigator.userAgent) && navigator.platform == "Win32");

/**
 * BROWSER RELATED
 * --------------------------------------------------------- */
	function replaceFonts()
	{
		Cufon.replace('.date, .contact_widget li');
	}
	replaceFonts();
 	
/**
 * POST IMAGES
 * --------------------------------------------------------- */
	if ( $('.entry img').length > 0 )
	{
		//$('.entry img').attr('width', '660').attr('height', 'auto');
	}

/**
 * SWF ADDRESS
 * --------------------------------------------------------- */	
	/*
	function replaceLinks()
	{
		$('a').each(function() {
			if ( $(this).attr('rel') != "external" )
			{
				if ( $(this).attr('href').indexOf('http://jdesktop.com') != -1 )
				{
					var href = $(this).attr('href').replace('http://jdesktop.com', '');
					$(this).attr('rel', 'address:'+ href);
				}
			}
		})
		$('a').address();	
	}
	replaceLinks();
	
	var title = document.title;
	
 	// Simple log
    var log = function(msg) {
        if (!$('.log').length) {
            $('<div class="log"></div>').appendTo('body');
        }
        $('.log').append(msg.replace(/^([^:]*):(.*)$/, '<p><b>$1:</b> <span class="$1">$2</span></p>'))
            .attr({scrollTop: $('.log').attr('scrollHeight')})
            .find('p:nth-child(even)').addClass('even');
    }
    
    // Default tracker mock
    var pageTracker = {
        _trackPageview: function() {
            log('track: ' + arguments[0]);
        }
    }

    // Serialization utility
    var serialize = function(obj, re) {
        var result = [];
        $.each(obj, function(i, val) {
            if ((re && re.test(i)) || !re)
                result.push(i + ': ' + (typeof val == 'object' ? val.join 
                    ? '\'' + val.join(', ') + '\'' : serialize(val) : '\'' + val + '\''));
        });
        return '{' + result.join(', ') + '}';
    }
    
    // Init and change handlers
    $.address.init(function(event) {
        log('init: ' + serialize({
            value: $.address.value(), 
            path: $.address.path(),
            pathNames: $.address.pathNames(),
            parameterNames: $.address.parameterNames(),
            queryString: $.address.queryString()
        }));
    }).change(function(event) {
        log('change: ' + serialize(event, /parameters|parametersNames|path|pathNames|queryString|value/));
        var names = $.map(event.pathNames, function(n) {
            return n.substr(0, 1).toUpperCase() + n.substr(1);
        }).concat(event.parameters.id ? event.parameters.id.split('.') : []);
        var links = names.slice();
        var match = links.length ? links.shift() + ' ' + links.join('.') : 'Home';
        $('a').each(function() {
            $(this).toggleClass('selected', $(this).text() == match);
        });
        $.address.title([title].concat(names).join(' | '));
		if (event.value != "/" )
		{
			$('#content').load(event.value +' #content > *', function() {
			//$(document).load(event.value, function() {
				replaceFonts();
				replaceLinks();
			});
		}
    });
	*/
	
/**
 * RESIZABLE BACKGROUND IMAGE
 * --------------------------------------------------------- */
	var objDocBg = $('#background-image img'); //Background image obj
	function resizeBackground()
	{
		//Gather browser size
		var browserwidth = $(window).width();
		var browserheight = $(window).height();

		//Show the background image
		objDocBg.show();

		//Define image ratio
		var ratio = 1071 / 1281;

		if ( browserheight > (browserwidth * ratio) )
		{
			objDocBg.height(browserheight);
			objDocBg.width(browserheight / ratio);
		}
		else
		{
			objDocBg.height(browserwidth * ratio);
			objDocBg.width(browserwidth);
		}
	}	
	resizeBackground();
	$(window).resize(function() { resizeBackground(); })

/**
 * STREAM PAD, CHANGE STYLES, (wait 1 second first)
 * --------------------------------------------------------- */	
	$('#header').fadeTo(1000, 1, function() {
		if ( $('#streampadBottomBar').length > 0 )
		{
			$('#streampadBottomBar').css({ 
					'background' : '#333', 
					'background-image' : 'none'
			});
		}
	});

});
