$(document).ready(function() {
	
	ajaxWorkLinks();
	
	isIphone();
	
	//had to put this in an if/else cause IE was rendering the carousel buggy on pageload otherwise
	if (getParameterByName('project_id')) {
		var i = getParameterByName('project_id');
		$('.projects-list li a').eq(i).trigger('click');
	} else {
		$('div.carousel').carousel({loop: true, pagination: true});
		$('.navigation li a').corner('5px');

		if (navigator.appName != "Microsoft Internet Explorer") {
			$('.carousel').corner('5px');
		}
	}
	
 });


//function that checks if you're using a mobile device
function isIphone() {
	if ((navigator.platform.indexOf("iPhone") != -1) || (navigator.platform.indexOf("iPad") != -1) || (navigator.platform.indexOf("iPod") != -1)) {
		$('.projects-list li a').hover(function() {
			$(this).trigger('click');
		});
	}
}


//function that updates the coutner in the carousel
function updateCounter() {
	var v = $('.carousel-pagination a').index($('.carousel-pagination a.active'))+1+'/'+$('.carousel-pagination a').length
	$('span.counter').text(v);
}

//function that handles the project selector
function ajaxWorkLinks() {
	
	$('a.close').unbind('click').click(function(){
		$('#content').slideUp();
	});
	
	$('.projects-list li a, ul.navigation li a').unbind('click');
	$('.projects-list li a, ul.navigation li a').click(function(){
		//variables
		var clickedElement = $(this);
		
		
		if (!$(this).hasClass('active')) {
			
			var scrollSpeed = 600;
			if ($('#content').hasClass('hide')) {scrollSpeed = 0;}
			
			//check if info is expanded
			$('#content').removeClass('hide').slideUp(scrollSpeed, function() {
				
				// Animation complete
				$(window).scrollTop(350);
				$('body').append('<div class="ajax-content"></div>');

				$('.ajax-content').load(clickedElement.attr('href')+' #content', function(response, status, xhr) {
				  if (status == "error") {
				  } else {

					$('#content').html($('.ajax-content div:first').html());
					$('#content').delay(20).slideDown(600);
					$('.ajax-content').remove();

					$('#content div.carousel').carousel({loop: true});
					$('.navigation li a').corner('5px');
					
					if (navigator.appName != "Microsoft Internet Explorer") {
						$('.carousel').corner('5px');
					}
					
					$('.info').removeClass('hide');

					window.location.hash = clickedElement.attr('href');

					$('.projects-list li a').removeClass('active');
					clickedElement.addClass('active');
					
					ajaxWorkLinks();
				}
				});
			});
			
		}
		
		return false;
	})
}

//function that checks if there is a param after the hash tag
function getParameterByName( name ) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

