if(typeof window['console'] === 'undefined') {
	window.console = { log: function(){}, warn: function(){}, error: function(){}, info: function(){}, debug: function(){} };
}

if (jQuery) { jQuery.noConflict(); }
jQuery(document).ready(function() {
	var $ = jQuery;
	
	var switchToScreen = function(screenId) {
		var currentScreen = $('.portfolio .details .screenshot img.displaying');
		var targetScreen = $('.portfolio .details .screenshot img#screenshot-' + screenId);
		
		if (currentScreen.length) {
			currentScreen.fadeOut('slow', function() {
					currentScreen.removeClass('displaying');
					targetScreen.addClass('displaying');
					targetScreen.fadeIn('slow');
				});
		} else {
			targetScreen.addClass('displaying');
			targetScreen.show();
		}
		
		$('.controller .pages .button.selected').removeClass('selected');
		$('.controller .pages .button[href=#ss-' + screenId + ']').addClass('selected');
	}
	
	/**
	* Clicking on the buttons swaps out the active screenshot img
	* being shown in the .portfolio .details .screenshot area.
	*/
	var screenshots = $('.portfolio .details .screenshot img');
	screenshots.hide();
	
	var screenHash = document.location.hash.replace('#ss-', '');
	if (screenHash != parseInt(screenHash)) screenHash = '1';
	switchToScreen(screenHash);
	
	$('.controller .pages .button').click(function(event) {
		event.preventDefault();
		event.stopPropagation();
		var buttonTarget = $(this).attr('href').replace('#ss-', '');
		switchToScreen(buttonTarget);
	});
	
	/**
	* Replace .portfolio .details with the flash player.
	*/
	$('.controller .links .control#play-demo').click(function(event) {
		event.preventDefault();
		event.stopPropagation();
		
		var button = $(this);
		
		if (!button.hasClass('disable')) {
			
			button.toggleClass('selected');
			if (button.hasClass('selected')) {
				// Show demo player (show details)
				$('.portfolio .details').fadeOut('slow', function() {
					$('.portfolio .demo').fadeIn('slow');
				});
			} else {
				// Hide demo player (show details)
				$('.portfolio .demo').fadeOut('slow', function() {
					$('.portfolio .details').fadeIn('slow');
				});
			}
		}
	});
	
	/**
	* Hover states for the navigation buttons
	*/
	$('.nav-main ul li a').hover(function() {
		if (!$(this).hasClass('selected')) {
			$(this).addClass('hover');
		}
	}, function() {
		if ($(this).hasClass('hover')) {
			$(this).removeClass('hover');
		}
	});
	
	/**
	* The quotes should just cycle endlessly in a pleasing fadein/out loop.
	*/
	var quoteDelay = 8000;
	var quotes = $('blockquote.quote');
	if (quotes.length) {
		var cycleQuote = function() {
			var currentQuote = $('blockquote.quote.displaying');
			var nextQuote = currentQuote.next();
			if (!nextQuote.length) {
				nextQuote = $(currentQuote.siblings()[0]);
			}
			currentQuote.animate({dummy: 1}, quoteDelay).fadeOut('slow', function() {
						currentQuote.removeClass('displaying');
						nextQuote.addClass('displaying');
						nextQuote.fadeIn('slow', function() {
							cycleQuote();
						});
					});
		}
		quotes.each(function() {
			$(this).hide();
		});
		$(quotes[0]).addClass('displaying');
		$(quotes[0]).show();
		cycleQuote();
	}
	
	// Set up the email input for .hint plugin
	$('.header #subscription-form input[name="email"]').addClass('hinter');
	$('.header #subscription-form input[name="email"]').attr('title', 'Enter email here...');
	$('.header #subscription-form input[name="email"]').hint('hinter');
	
	$('#subscriber #subscription-form input[name="email"]').addClass('hinter');
	$('#subscriber #subscription-form input[name="email"]').attr('title', 'Enter email here...');
	$('#subscriber #subscription-form input[name="email"]').hint('hinter');
	
	/**
	* Fade in/out the portfolio sheet items
	*/
	// If there are problems rendering PNGs then we shouldn't 
	// it fade in/out, as black artefacts in the alpha regions
	// will appear and ruin the effect. Primarily affects IE7.
	if (typeof $.support == 'undefined' || ! $.support.opacity) {
		$('.page-portfolio .content .main .sheet .item .overlay').hover(function() {
			$(this).children('img').show();
		}, function() {
			$(this).children('img').hide();
		});
	} else {
		$('.page-portfolio .content .main .sheet .item .overlay').hover(function() {
			$(this).children('img').fadeIn('slow');
		}, function() {
			$(this).children('img').fadeOut('fast');
		});
	}
	
});

jQuery.fn.hint = function (blurClass) {
	if (!blurClass) { 
		blurClass = 'blur';
	}
	
	return this.each(function () {
		// get jQuery version of 'this'
		var $input = jQuery(this),
	
		// capture the rest of the variable to allow for reuse
		title = $input.attr('title'),
		$form = jQuery(this.form),
		$win = jQuery(window);
	
		function remove() {
			if ($input.val() === title && $input.hasClass(blurClass)) {
				$input.val('').removeClass(blurClass);
			}
		}
	
		// only apply logic if the element has the attribute
		if (title) { 
			// on blur, set value to title attr if text is blank
			$input.blur(function () {
				if (this.value === '') {
					$input.val(title).addClass(blurClass);
				}
			}).focus(remove).blur(); // now change all inputs to title
		
			// clear the pre-defined text when form is submitted
			$form.submit(remove);
			$win.unload(remove); // handles Firefox's autocomplete
		}
	});
};

