/**
 * @package WOW-FactorHomeStaging.com
 * @author {@link mailto:ss@toolboxstudios.com Steve Simons, Toolbox Studios, Inc.}
 * @license http://toolboxstudios.com Contact about usage
 * @revision $Revision: 2.0 $
 * @copyright Copyright 2009
**/



$(document).ready( function() {
	
	$('a.openWindow')
		.click( function(e) {
			var dims = this.className.match( /w(\d+)h(\d+)$/);
			if ( dims) {
				var width = parseInt( dims[1]) + 20;
				var height = parseInt( dims[2]) + 20;
				var win = window.open( this.href, this.href.replace( /.*([^\/]+$)/, '$1'), 'width=' + width.toString() + ',height=' + height.toString() + ',resizable=1');
				win.focus();
				e.stopPropagation();
				return false;
			}
		});

	$('#enewsletter input.text')
		.focus( function(e) {
			if ( this.defaultValue == this.value)
				this.value = '';
			this.select();
		})
		.blur( function(e) {
			if ( this.value.replace(/\s/g, '') == '')
				this.value = this.defaultValue;
		});
		
	$('#enewsletter')
		.submit( function(e) {
			var good = false;
			var email = this.subscribeEmail;
			if ( email.defaultValue == email.value)
				alert( 'Enter your email address');
			else if ( !email.value.match(/^\s*[\w\._-]+@([\w_-]+\.)+\w+\s*$/))
				alert( email.value + ' does not appear to be a valid email address');
			else {
				good = confirm( 'Subscribe ' + email.value + '?');
				if ( good)
					alert( 'Thank you for subscribing.');
			}
			if ( !good)
				e.stopPropagation();
			return good;
		});

	if ( $('#topImage > li').length > 5) {
		$('#topImage > li')
			.each( function(index) {
				if ( index <= 5)
					$(this).addClass( 'preloaded');
				if ( index == 5) {
					var src = $(this).find('img').attr('src');
					$('<img />')
						.attr( 'src', $(this).find('img').attr('src'))
						.load( function() {
							$(this).remove();
						});
				}
			});
		setInterval( 
			function() {
				var current = $('#topImage li:first');
				// Move to end
				current.appendTo( '#topImage');
				var next = $('#topImage li').eq(5);
				next.hide();
				$('#topImage > li:not(.preloaded)')
					.eq(0)
						.each( function(index) {
							var src = $(this).find('img').attr('src');
							$('<img />')
								.attr( 'src', src)
								.load( function() {
									$(this).remove();
								});
						})
						.addClass( 'preloaded');
				var lastClass = next.get(0).className;
				next.get(0).className = current.get(0).className;
				current.fadeOut( 'slow', function() {
					current.get(0).className = lastClass;
					next.fadeIn( 'slow');
				});
			},
			5000
		);
	}
	
	if ( jQuery( 'body').hasClass( 'portfolioPg') && FB) {
		
		FB.init({ status: true, cookie: true, xfbml: true });
		
		var jLike = jQuery( '<div />')
			.css( { position: 'absolute', top: '-40px', right: '40px', width: '360px'})
			.html( '<fb:like href="' + document.URL + '" layout="standard" show_faces="false" width="360" action="like" colorscheme="light" />')
			.appendTo( '#mainContent');
		
		var jImg = jQuery( '.After img').first();
		if ( jImg.length == 0)
			jImg = jQuery( '#rightColumn img').first();
		
		jQuery( '<meta />')
			.attr( 'property', 'og:image')
			.attr( 'content', 'http://' + document.location.host + jImg.attr( 'src'))
			.appendTo( 'head');
		
		FB.XFBML.parse( jLike.get(0));
		
	}
});
