$(document).ready(function () {
	// Set Vars
	$('#details').hide();
	var backtxt = '< Back to Work';
	var count = 99;
    var sxml;
    var fxspeed = 300;
	var showingdetails = false;
	var date = new Date();
	var ticks = date.getTime();
    $.ajax({
        type: "GET",
        url: "data/showcase.xml?v=" + ticks,
        dataType: "xml",
        success: function (xml) {
            sxml = xml;
			loaditems();
        }
    });
	// Check for count
	var limit = $('#showcase').attr('rel');
	if(limit != undefined){
		count = limit;
	}
    function loaditems() {
		$(sxml).find('item:lt(' + count + ')').each(function(){
			var node = $(this);
			var tmbpath = '../showcase/images/' + $(node).find('thumbnail').text();
			var id = $(node).find('id').text();
			var title = $('<h3></h3>').html($.trim($(node).find('title').text()));
			var agency = ($.trim($(node).find('agency').text()) != '') ? $.trim($(node).find('agency').text()) : '';
			var description = $('<p></p>').html($.trim($(node).find('description').text()));
			var preview = $('<span></span>').addClass('preview').append(title,agency,description).hide();
			var img = $('<img>').hide().attr('src', tmbpath).load(function(){
				$(this).fadeIn();
			});
			var item = $('<a></a>').addClass('showcaseitem floatleft').attr({'rel':id, 'href':'#'}).append(img, preview)
				.hover(function(){
					$(this).find('.preview').stop(true,true).fadeIn(fxspeed);
				},function(){
					$(this).find('.preview').stop(true,true).fadeOut(fxspeed);
				})
				.click(function(){
					if($('#showcase').hasClass('work')){
						// Work page
						showDetails(id);
					}else{
						// Homepage
						window.location = 'work.html#' + id;
					}
					return false;
				}).hide();
			$('#showcase').append(item);
		});
		// Check Hash
		var hashArr = window.location.hash.substring(1).split('#');
		var activeHash = hashArr[0].toLowerCase();
		$('#showcase a[rel="' + activeHash + '"]').trigger('click');
		fadeItems('in');
    }
	
	// Fade In Items
	function fadeItems(direction){
		var currentcount = 0;
		$('.showcaseitem').each(function(){
			if(direction == "in"){
				$(this).delay(currentcount*66).css('visibility','visible').fadeTo(200,1)
			}else{
				$(this).delay(currentcount*66).fadeTo(200, 0, function(){
					$(this).css('visibility','hidden');
				})
			}
			currentcount ++;
		});
	}
	
	// Show Details
	var showcasenavtxt = $('#showcasenav').text();
	function showDetails(id){
		window.location.hash = id;
		fadeItems('out');
		//$('#showcasenav').delay(500).text(backtxt).addClass('back');
		$('#showcasenav').animate({
				marginLeft: '-400px'
			}, 200, function(){
				$(this).delay(500).text(backtxt).addClass('back').animate({
					marginLeft: 0
				}, 200);
			});
		showingdetails = true;
		// load html
		$('#showcase #details').load('showcase/' + id + '.html', function() {
			$('#showcase #details').delay(500).fadeIn();
			initVideoLinks();
			$('html, body').animate({ scrollTop: 0 }, 500);
		});
	}
	
	// Hide Details
	function hideDetails(){
		window.location.hash = '';
		$('#showcase #details, #showcase #videoplayer').hide().html('');
		fadeItems('in');
		$('#showcasenav').text(showcasenavtxt).removeClass('back');
	}
	$('#showcasenav').click(function(){
		if(showingdetails){
			showingdetails = false;
			hideDetails();
		}
	});
	
	// Videoplayer
	function initVideoLinks(){
		$('#details a[rel="videoplayer"]').click(function(){
			if($(this).hasClass('close')){
				// Close Video
				$(this).text('Launch Video').removeClass('close');
				$('#videoplayer').html('').fadeOut();
			}else{
				// Open Video
				$(this).text('Close Video').addClass('close');
				var href = $(this).attr('href');
				var vid = href.substring(href.lastIndexOf('/')+1);
				var iframe = '<iframe width="894" height="413" src="http://www.youtube.com/embed/' + vid + '?rel=0" frameborder="0" allowfullscreen></iframe>';
				$('#videoplayer').append(iframe).fadeIn();
			}
			return false;
		})
	}
	
});
