/*-----------------------------Variables----------------------------------*/
var viewWidth,
	viewHeight,
	resizeDelay,
	portfolioLength,
	portfolioArray = [],
	titleArray = [],
	activeParent,
	$html = '',
	$href = window.location.href,
	currHash,
	urlHash;

jQuery.easing.def = 'easeOutExpo';

String.prototype.capitalize = function(){
   return this.replace( /(^|\s)([a-z])/g , function(m,p1,p2){ return p1+p2.toUpperCase(); } );
};

/*-----------------------------Initialize----------------------------------*/
function initModel()
{
	var index = 0;
	worksLength =  $('.portfolio-entry').length;
	
	$('.portfolio-entry').each(function()
	{
		var id = 'portfolio-entry-' + index;
		$(this).attr({ 'id' : id });
		$('#' + id + ' .portfolio-preview').attr({ 'id' : 'portfolio-preview-' + index });
		$('#' + id + ' .portfolio-details-holder').attr({ 'id' : 'portfolio-details-holder-' + index });
		
		var portfolioTitle = $('#' + id + ' .info-h2').text();
		titleArray.push(portfolioTitle);
		portfolioTitle = portfolioTitle.replace(' - ', '-');
		portfolioTitle = portfolioTitle.replace(/[\s]/g, '-');
		portfolioTitle = portfolioTitle.replace(/[^A-Za-z0-9&\-]/g, '');
		portfolioArray.push(portfolioTitle);
		
		var $facebook = '<a href="http://www.facebook.com/sharer.php?u=http://www.philleep.com/?work=' + portfolioArray[index] + '" target="_blank">share on facebook</a>';
		$('#' + id + ' .portfolio-facebook').html($facebook);
		index++;
	});
	
	listNum('.portfolio-entry');
	
	$controlHtml = $('#portfolio-control-holder').html();
	$('#masthead-email').html('<strong>Email:</strong> <a href="mailto:email@philleep.com?subject=Hello Philleep Florence" target="_blank">email@philleep.com</a>');
	$('#masthead-telephone').html('<strong>Telephone:</strong> +1 800 985 9250');
	var $marginTop = $('#content').height() * -1;
	$('#content').css({ 'margin-top' : $marginTop });
}

function initLocation()
{
	if($href.search('work=') > 0)
	{
		$href = $href.replace('?work=', '#/') + '/';
		window.location = $href;
		urlHash = window.location.hash;
		currHash = urlHash;
		currHash = currHash.replace('#',  '');
		initScroll();
	}
	else
	{
		if(window.location.hash == '')
		{
			urlHash = '/home/';
			window.location.hash = urlHash;
			document.title = 'Philleep Florence - Interactive Digital Studio';
			initScroll();
		}
		else
		{
			urlHash = window.location.hash;
			currHash = urlHash;
			currHash = currHash.replace('#',  '');
			initScroll();
		}	
	}
}

/*-------------------------------Load XML-----------------------------------*/
function loadXml()
{
	try
	{
		xmlhttp = new XMLHttpRequest();
	}
	catch(e)
	{
		try
		{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)
			{
				alert('Unable to load data. Your browser is not supported!');
				return false;
			}
		}
	}
	
	xmlhttp.open('GET', 'xml/portfolio.xml', false);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.send();
	xmlDoc = xmlhttp.responseXML;
}

/*-----------------------------Load Images----------------------------------*/
function loadContent($parent, $index, $holder, $details, $targetOffset)
{
	var $entry = xmlDoc.getElementsByTagName('entry');
	var $len = $entry[$index].getElementsByTagName('image').length;
	
	var $image = $entry[$index].getElementsByTagName('image');

	if($parent != activeParent)
	{
		$($parent + ' .portfolio-details-num-p').text('1 - ' + $len);
		
		$html = '\n' + '<div class="portfolio-swipe"></div>' + '\n';
		var $zIndex = 10;
		var $imageID = 'portfolio-image-' + $index + '-';
		
		for(var i = 0; i < $len; i++)
		{
			$zIndex = $zIndex - i;
			$html = $html + '<img src="' + $image[i].childNodes[0].nodeValue + '" class="portfolio-image" id="' + $imageID + i + '" alt=""/>' + '\n';
		}
		
		activeParent = $parent;
		$holder.html($html);		
		
		$details.slideDown(800);
		$('#' + $imageID + 0).load(function()
		{ 
			$('html, body').animate({scrollTop : $targetOffset}, 600, function()
			{
				$('#' + $imageID + 0).fadeIn(600);		
			});
		});
	}
	
	enableClicks();
}

function enableClicks()
{
	$('.portfolio-image').unbind('click');
	$('.portfolio-image').click(function()
	{
		var $num = $(this).parent().find('img').length;
		var $parent = '#' + $(this).parent().attr('id');
		var $class = $($parent + ' .portfolio-image');
		var $numClass = $($(this).parent().parent().find('.portfolio-details-num-p'));
		var $index = $class.index(this);
		var $targetOffset = $(this).parent().offset().top;
		$('html, body').animate({scrollTop : $targetOffset}, 600);

		if($num > 1)
		{
			rotateImages($num, $index, $class, $numClass);	
		}
	});	
}

/*-----------------------------Window Hash Change----------------------------------*/
function hashChanged()
{
	currHash = window.location.hash;
	currHash = currHash.replace('#',  '');
	if(currHash != urlHash)
	{		
		loadHashContent();
	}
}

function loadHashContent()
{
	var $currHash = currHash.replace(/\//g, '');
	var $index = $.inArray($currHash, portfolioArray);
	
	if($index > -1)
	{
		document.title = 'Philleep Florence - ' + titleArray[$index];
		var $parent = '#' + $($('.portfolio-entry').get($index)).attr('id');
		resetPortfolio();
		var scrollDelay = setTimeout(function()
		{
			scrollContent($parent);	
		}, 1000);				
	}
	else if($currHash == 'home')
	{
		document.title = 'Philleep Florence - Interactive Digital Studio';
		var $parent = '#container';
		resetPortfolio();
		var scrollDelay = setTimeout(function()
		{
			scrollContent($parent);	
		}, 1000);	
	}
	else if($currHash.indexOf('search') > -1)
	{
		var $val = $currHash.replace('search', '');		
		searchWorks($val);
	}
}

/*--------------------------------Search Works-------------------------------------*/
function searchWorks($val)
{	
	$('.portfolio-entry').removeClass('search-portfolio');
	
	$('.portfolio-entry').each(function()
	{
		var $text = $(this).text();
		var $pattern = new RegExp($val, 'i');
		
		if($text.search($pattern) > -1)
		{
			$(this).addClass('search-portfolio');
			$(this).show();
		}
		else
		{
			$(this).hide();
		}
	});
	
	var $len = $('.search-portfolio').length;
	if($len > 0)
	{
		$('#portfolio-search-p').html('Showing search results for <span class="search-span">' + $val + '</span>.<span class="search-close">show all works</span>');
		$('.portfolio-controls').hide();
	}
	else
	{
		$('#portfolio-search-p').html('Your search for <span class="search-span">' + $val + '</span> yielded no results.<span class="search-close">show all works</span>');
		$('.portfolio-controls').show();
	}
	
	$('#portfolio-search').slideDown(500);
	document.title = 'Philleep Florence - Interactive Digital Studio';
	urlHash = '/' + 'search' + '/' + $val + '/';
	window.location.hash = urlHash;
	listNum('.search-portfolio');
}

function clearSearch()
{
	resetPortfolio();
	
	document.title = 'Philleep Florence - Interactive Digital Studio';
	urlHash = '/home/';
	window.location.hash = urlHash;
}

function resetPortfolio()
{
	$('#portfolio-search').slideUp(500);
	$('.portfolio-entry').removeClass('search-portfolio');
	listNum('.portfolio-entry');
	$('.portfolio-entry').show();
	$('#masthead-input').val('search works');
	$('.portfolio-controls').show();	
}

function listNum($class)
{
	var index = 1,
		$len = $($class).length;
	
	$($class).each(function()
	{
		var $id = '#' + $(this).attr('id');
		$($id + ' .portfolio-num').text(index + ' - ' + $len);
		index++;
	});
}




















