/**
 * Site wide scripts
 */

$(document).ready(function(){
	
	$(window).load(function() {
		$('#home').masonry();
		$('#work').masonry();
		$('#news').masonry();
	});
	
	$('div.slideshow').each(function(){
		if($(this).find('ul').children().length > 1)
		{
			$(this).jcarousel({
				wrap: 'circular',
				scroll: 1,
				start: 1
			});
		}
	});

	$('div.post').hover(function () {
     $(this).siblings().stop().animate({ opacity: 0.3 }, 'slow' );
     }, function () {
     $(this).siblings().stop().animate({ opacity: 1 }, 'slow' );
  });



	if (navigator.userAgent.match(/MSIE/))
	{
		$('.entry').hover(function() {
			$('.image',this).stop().fadeTo('slow', 0.3).siblings().stop().show();
		  },
		  function() {
		  	$('.image',this).stop().fadeTo('slow', 1).siblings().stop().hide();
		  });
	}
	else
	{
		$('.entry').hover(function() {
			$('.image',this).stop().fadeTo('slow', 0.3).siblings().stop().fadeTo('slow', 1, function(){
				$(this).css('filter','');
			});
		  },
		  function() {
		  	$('.image',this).stop().fadeTo('slow', 1).siblings().stop().fadeTo('slow', 0);
		  });	
	}




	$('form').submit(function(){
		var out = true;
		$('input.required, textarea.required').each(function(){
			if($(this).val() == '')
			{
				$(this).css('border-color',  '#f15452');
				out = false;
			}
			else
			{
				$(this).css('border-color',  '#bbb');
			}
			
			if($(this).attr('id') == 'email')
			{
				reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
				if(!reg.test($(this).val()))
				{
					$(this).css('border-color',  '#f15452');
					out = false;
				}
			}
		});
		return out;
	});
	
	$('#client-list').makeacolumnlists({cols:2,colWidth:325,equalHeight:false,startN:1});
	
	if($('#map').is('div'))
	{
		$(window).load(function() {
			load();
		});
		
		$(window).unload(function() {
			GUnload();
		});
	}
	
	if($('#detail').is('div'))
	{
		$(window).load(function() {
			$.scrollTo({ top:147, left:0 }, {duration:800});
		});
		
		$('a.anchor').click(function(){
			linkid = '#' + $(this).attr('title');
			route = $(this).attr('href');
			direction = ($(this).hasClass('up'))?	'up' : 'down';

			if(!$(linkid).is('div'))
			{
				$(this).parent().parent().find('li.activity').addClass('busy');
				loadProject(route, direction);
				$.scrollTo($('#detail').find('div.detail:last'), {duration:800});
				
			}
			else
			{
				$.scrollTo($(linkid), {duration:800});
			}

			return false;
		});
	}
	
	
});

function loadProject(route, direction)
{
	$.ajax({
		url: route + '/format/html',
		success: function(response)
		{
			$('li.activity').removeClass('busy');
			if(direction == 'down')
			{
				$('#detail').prepend(response).find('div.detail:first').slideDown('slow');
				$.scrollTo({ top:147, left:0 }, {duration:800});

				slideshow = $('#detail').find('div.slideshow:first');
				target = $('#detail').find('div.detail:first');
			}
			else
			{
				$('#detail').append(response).find('div.detail:last').slideDown('slow');
				$.scrollTo($('#detail').find('div.detail:last'), {duration:800});

				slideshow = $('#detail').find('div.slideshow:last');
				target = $('#detail').find('div.detail:last');
			}

			refreshList(slideshow, target);
		}
	});
}

function refreshList(slideshow, target)
{
	slideshow.jcarousel({
		wrap: 'circular',
		scroll: 1,
		start: 1
	});

	target.find('a.anchor').click(function(){
		route = $(this).attr('href');
		linkid = '#' + $(this).attr('title');

		direction = ($(this).hasClass('up'))?	'up' : 'down';
		if(!$(linkid).is('div'))
		{
			$(this).parent().parent().find('li.activity').addClass('busy');
			loadProject(route, direction);
		}
		else
		{
			$.scrollTo($(linkid), {duration:800});
		}

		return false;
	});
}

function pipeCallback(obj) 
{ 
	// var x;
	// 	for (x = 0; x < 1 ; x++) {
	//     	var buildstring = "<a id='lastfm' href=" + obj.value.items[x].link + " target='_blank'><strong>Studio hi-fi:</strong> " + obj.value.items[x].title + "</a>";
	// 		document.write(buildstring);
	//     	buildstring = null;
//	console.log(obj.value.items);
	$('li.studio-hi-fi').append('<ul id="hi-fi"></ul>');
	
	$.each(obj.value.items, function(i){
	//	console.log(this);
		if(i == 1)	return false;
		$('#hi-fi').append('<li><a href="' + this.link + '" target="_blank">' + this.title + '</a></li>')
	});
}
