function IsNumeric(val) { if (isNaN(parseFloat(val))) { return false; } return true }

function checkHash(loc) {
	var hash = window.location.hash.split('/');
	if(hash[1] == '') {
		$('ul.vidlist li:first a').attr('id','activated');
		var sel = $('ul.vidlist li:first').attr('id');
	} else if(IsNumeric(hash[1])) {
		//Put selected video in the box
		$('ul.vidlist li.link-'+hash[1]+' a').attr('id','activated');
		var sel = $('ul.vidlist li.link-'+hash[1]).attr('id');
	} else {
		//Put the first video in the box
		var sel = $('ul.vidlist li:first').attr('id');
	}
	showVideo(sel,loc,hash[1]);
}

function showVideo(what,cat,id) {
		console.log(what+' '+cat+' '+id);
		//Reset boxes
		$('#video').empty();
		$('ul.vidlist li a').attr('id','');
		//Load current box
		var v = what.split('-');
		var iframe = '<iframe src="http://player.vimeo.com/video/'+v[1]+'?title=0&amp;byline=0&amp;portrait=0&amp;color=969696" width="640" height="480" frameborder="0"></iframe>';
		$('#video').append(iframe);
		$('li#'+what+' a').attr('id','activated');
		if(id == undefined) { var id = $('ul.vidlist li:first').attr('class').split('-'); var post = id[1]; } else { var post = id; }
		var url = window.location.href.split('#');
		window.location.href = url[0]+'#!/'+post+'/';
		return false;			
}

$(document).ready(function(){
	/* EXTERNAL LINKS */
	$('a[rel="external"], a[class="external"]').attr('target','_blank');
	/* NO LINKS */
	$('ul#nav a[href="#"]').click(function(){ return false; });
	
	/* PAGE-SPECIFIC FUNCTIONS */
	var path = document.location.pathname.split('/');
	var loc = path[1];
	if(loc == '') {
		//Do Nothing
	} if(loc == 'moving-pictures' || loc == 'valentines') {
		checkHash(loc);
		//Change the video on click
		$('ul.vidlist li a').click(function() {
			var now = $(this).parent().attr('id');
			var nowID = $(this).parent().attr('class').split('-');
			showVideo(now,loc,nowID[1]);
			return false;
		});

		$('#gallery-next').click(function() {
			var $chosen = $('a#activated').parent();
			if(!$chosen.next().length) {
				$nextup = $('ul.vidlist li:first');
 			} else {
 				$nextup = $chosen.next();
 			}
			var nextID = $nextup.attr('class').split('-');
 			showVideo($nextup.attr('id'),loc,nextID[1]);
 			return false;
 		});
 		
		$('#gallery-prev').click(function() {
			var $chosen = $('a#activated').parent();
			if(!$chosen.prev().length) {
				$nextup = $('ul.vidlist li:last');
 			} else {
 				$nextup = $chosen.prev();
 			}
			var nextID = $nextup.attr('class').split('-');
 			showVideo($nextup.attr('id'),loc,nextID[1]);
 			return false;
 		});

	} else if(loc == 'clients' || loc == 'contact') {
		$(".block").equalHeights();
	} else {
		//Gallery functions
		$('#gallery').cycle({
			fx: 'fade',
			timeout:  0,
			prev: '.prev',
			next: '.next'
		});
		$('#gallery-hover-prev').hover(
			function(){ $('#gallery-prev a').addClass('hov'); },
			function(){ $('#gallery-prev a').removeClass('hov'); }
		);
		$('#gallery-hover-next').hover(
			function(){ $('#gallery-next a').addClass('hov'); },
			function(){ $('#gallery-next a').removeClass('hov'); }
		);
		$(window).keydown(function(e){
			if(e.which == 37){ $('#gallery-prev a').click(); }
			else if(e.which == 39){ $('#gallery-next a').click(); }
		});
	}
});
