$(function() {

	var photos = new Object;
	var jsonData;

	$('.album').hover(function() {
		$('a', this).fadeTo('fast', 1);
	},
	function() {
		$('a',this).fadeTo('fast',0);
	});

	$('#pictureWrapper').cycle({
		next: "#next",
		prev : "#prev",
		timeout: 0,
		speed: 300,
		onPrevNextEvent : function(isNext, i, el) {
			$('.currentNumber').text(i + 1);
			$('.photoTitle').text(photos[i]['name']);
		}	
	});




	var contentHeight = $('#content').height(),
		 boxHeight = $('#photoSidebar').height(),
		 overlayHeight = $('#overlayInner').height();
	
	// get the values from php ad store in an object
	jsonData = $('input#jsonPhotos').attr('value');
			 
	if (jsonData) { 
		photos = JSON.parse(jsonData);		
		// set initial values
		$('.total').text(countProperties(photos));
		$('.currentNumber').text('1');
		$('.photoTitle').text(photos[0]['name']);
		$('#photoSidebar').css({ marginTop : ( (overlayHeight - boxHeight) / 2)});
	}
		
$('.vimeo').click(function(e) {
      var overlay = $('#overlay'),
      vimeo_id = $(this).data('id'),
      vimeo_desc = $(this).data('desc'),
      vimeo_title = $(this).data('title');
      $('#videoWrapper').html('<iframe src="http://player.vimeo.com/video/'+vimeo_id+'?title=0&byline=0&portrait=0&autoplay=1&color=FFFFFF" width="700" height="400" frameborder="0"></iframe><div class="videoMeta"> <span class="title">'+vimeo_title+'</span> <p>'+vimeo_desc+'</p></div>');
      overlay.fadeIn();

      var overlayHeight = $('#overlayInner').height(),
      windowHeight = $(window).height();

      $('#overlayInner').css({ marginTop : ( (windowHeight - overlayHeight) / 2)});
      e.preventDefault();
});
	
	// add in nth child
	$('.album:nth-child(3n+1)').addClass('col1');
	$('.album:nth-child(3n+2)').addClass('col2');
  

$('.close').click(function() {
  $('#overlay').fadeOut();
  $('#videoWrapper').html('');
});


$(document).keydown(function(e){
    if (e.keyCode == 39) { 
       $('a#next').trigger('click');
    }
    
    else if (e.keyCode == 37) {
    	 $('a#prev').trigger('click');
    }
});


$('.page-id-11 #left p:first').addClass('first');


	// projects page sliders
	var htmlimg ="",
	    els = $('.parent-pageid-9 #left img');
	
	els.fadeOut().each(function(i,el) {
	
		htmlimg += "<img src='"+el.src+"'  />";
		
		if (i == els.length - 1) { $('#left').append( '<div id="projectPhotosWrapper"><div id="projectPhotosNav"></div><div id="projectPhotos" >'+htmlimg+'</div></div>' ).find('#projectPhotos')
			.cycle({
				pager : '#projectPhotosNav'
			})
		}
		
	});
	
	

});

/* ============================================================
     utils
   ============================================================ */
function countProperties(obj) {
    var count = 0;

    for (var prop in obj) {
        if (obj.hasOwnProperty(prop))
            ++count;
    }
    return count;
}

