function set_scroll(){
					// Add the scroll if the content is higher then the container
			if(sublim.scroll.content.outerHeight()/sublim.scroll.container.height() > 1){

				if(!$('.slider').length > 0)
					$('.image_wrapper').append('<div class="slider"><div class="indicator"></div></div>');
				
				$('div.slider')
					.css('top', 0 + 'px')
					.css('height', sublim.scroll.container.height() -135 + 'px');
				
				$('div.indicator')
					.css('height', Math.floor( (sublim.scroll.container.height() / sublim.scroll.content.outerHeight() * $('div.slider').height()) ).toString() + 'px');
				
				$('.indicator').draggable(
					{
						containment : '.slider',
						drag : function(e)
						{
							var move = Math.floor( parseInt($(this).css('top').replace('px','') ) / $('div.slider').height() * sublim.scroll.content.outerHeight() )
							sublim.scroll.content.css('top', -move + 'px');
						}
					}
				);	
			}else{
				if($('.slider').length > 0) $('.slider').remove(); 
			}
}

function toggle_project_overview(){
	
	project_overview = $(".projects_overview");
	
	if(project_overview.is(':visible')){
		project_overview.fadeHide();
		$('.project_nav .overview').css('background-position', '');
	}else{
		project_overview.fadeShow();
		$('.project_nav .overview').css('background-position', '0 -17px');
	}
	return false
}

function toggle_zoom(e, options){
    var z_img_w =  2500;
	var z_img_h =  2500;
	
	var options = $.extend({force_hide: false}, options);
	if($(".image_zoom").length > 0){
		var image_zoom = $(".image_zoom");
		if(image_zoom.is(':visible') || options.force_hide ){
			image_zoom.fadeHide();
			$('.project_nav .zoom').css('background-position', '');
		}else{
			if($(this).attr('href') != ""){ 
				image_zoom.fadeShow();	
				$('.project_nav .zoom').css('background-position', '0 -40px');	
			}
		}
		
	}else{
		
		function show_zoom(){
			$(document.createElement("div")).attr("class","image_zoom").prependTo(".image_wrapper").html('<div class="limiter"><div class="drag_wrapper" style="background: transparent url('+sublim.zoom_img_src+') no-repeat;width: '+z_img_w+'px;height: '+z_img_h+'px;"><div class="spaceball"></div></div></div><div class="l_border"></div><div class="r_border"></div>').animate({'opacity':1}, 500); 
			
			var width = $('div.image_wrapper').width();
			var height = $('div.image_wrapper').height();
			
			var limit_width = ((z_img_w - width) * 2 + width);
			// The 67px .nav .project_nav .helperis the height of the nav/2 because we reposition 
			// the image_wrapper based on it
			var limit_height= (z_img_h - height ) * 2 + height;
			var limit_t_margin =  (z_img_w - height) + $('.nav').height();
			var limit_l_margin =  (z_img_h - width);

			// Offsets to center the image in the viewport
			var left_center = Math.floor( (z_img_w - $('.image_zoom').width())/2 ); 
			var top_center  = Math.floor( (z_img_h - $('.image_zoom').height())/2 );
			
			$(".image_zoom .limiter").css({'width' :  limit_width.toString()+'px', 'height' :  limit_height.toString()+'px', 'margin-left' :  '-'+limit_l_margin.toString()+'px', 'margin-top' :  '-'+limit_t_margin.toString()+'px' });
						
			$('.image_zoom .drag_wrapper').draggable({containment: '.image_zoom .limiter'} ).css({'left': '-'+left_center.toString() + 'px', 'top': '-'+top_center.toString() + 'px'});
			$('.project_nav .zoom').css('background-position', '0 -40px');
			stop_load();
		}
		if (!options.force_hide){
			if($(this).attr('href') != ""){
				start_load();
				$('<img src="'+$(this).attr('href')+'">').load(show_zoom);
			}
		}
		
	}
	return false;
}

function reposition_zoomed_image(){
	
	var z_img_w =  2500;
	var z_img_h =  2500;
	// Offsets to center the image in the viewport
	var left_center = Math.floor( (z_img_w - $('.image_zoom').width())/2 ); 
	var top_center  = Math.floor( (z_img_h - $('.image_zoom').height())/2 );
	
	$('.image_zoom .drag_wrapper').css({'left': '-'+left_center.toString() + 'px', 'top': '-'+top_center.toString() + 'px'});
	
}

function toggle_credits(e, options){
	var options = $.extend({force_hide: false}, options);
	if($('.project_content').is(':visible') || options.force_hide){
		$('.project_content').fadeHide(500);	
		$('.project_nav .credits').css('background-position', '');
	}else{
		$('.project_content').fadeShow(500);	
		$('.project_nav .credits').css('background-position', '0 -19px');	
	}
	// Prevent the propagation of the location change
	return false;
	
}

function toggle_infos(e, options){
	return false;
	var options = $.extend({force_hide: false}, options);	
	
	var info = $('.project_content .infos');
	if(info.hasClass('more')){
		$('.project_content *').show(0);
		info.removeClass('more');
		info.addClass('less')
		$('.project_content .infos li').text(sublim.lang.less_i);
	}else{
		$('.project_content *').hide(0);
		info.removeClass('less');
		info.addClass('more')
		info.show();	
		$('.project_content ul *').show(0);
		$('.project_content ul').show(0);
		$('.project_content .infos li').text(sublim.lang.more_i)
	}	
	if(options.force_hide){
		$('.project_content *').hide(0);
		info.removeClass('less');
		info.addClass('more')
		info.show();	
		$('.project_content ul *').show(0);
		$('.project_content ul').show(0);
		$('.project_content .infos li').text(sublim.lang.more_i)	
	}
}



// Dynamicaly change the current image being displayed
// Gather the date from the return of the ajax fonction
// Update the sublim.cur_project, sublim.cur_image and sublim.cur_section data
function update_project_page(){
	start_load();
	
	//console.log( $(this).attr('href') + "?ajax=1&t=json&a=gpc" );
	
	toggle_credits(NaN, {force_hide:true});
	toggle_zoom(NaN, {force_hide:true});
	
	// We get the href of the this object and ask for it in JSon
	var url = $(this).attr('href');
	
	// AJAX cool to get the new data
	$.getJSON(url, {'ajax':1,'t':'json', 'a':'gpc'}, function(data, satus){
		
		function reload_img_page(){
			$('.image_wrapper').css({'background-image': "url(./)", 'background-position': data.align});		
			$('<img src="'+data.page+'" />').load(function(){$('.image_wrapper').css('background-image', 'url('+data.page+')');$('.image_wrapper').animate({'opacity': 1}, 500);stop_load();})
		}
		
		$('.image_wrapper').animate({'opacity': 0}, 500, reload_img_page);
		// Cahgne teh source of the zoomed image
		$('.image_zoom img').attr('src',data.zoom);
		
		// Update the data for the project overview function to 
		// perform correctly
		sublim.zoom_img_src = data.zoom;
		sublim.cur_page_id = data.id;
		
		// Handle things not handled by refrsh_project_overview		
		$('.project_content').html(data.content);
		// Uncomment to activate the more info thingy
		//$('.project_content').html(data.content).append('<ul class="infos more"><li>'+sublim.lang.more_i+'</li></ul>');
		$('.project_nav .prev a').attr('href', data.prev);
		$('.project_nav .next a').attr('href', data.next);
		$('.project_nav .zoom a').attr('href', data.zoom);
		if($('.project_nav .zoom a').attr("href") == ""){
			$('.project_nav .zoom').css({'background-position': '0 -60px'});
			$('.project_nav .zoom a').css({'cursor': 'default'});
		}else{
			$('.project_nav .zoom').css({'background-position': ''});
			$('.project_nav .zoom a').css({'cursor': 'pointer'});
			reposition_zoomed_image();
		}
		//Change the active picture in the thumbnails
		$('.projects_overview .thumbnails li').removeClass("active");
		$('#thumb_'+data.id).addClass("active");
		
		$('.image_zoom').remove();
				
		// Rebind the events
		//$('.project_content .infos').click(toggle_infos).css('cursor','pointer');
		//toggle_infos(NaN, {force_hide: true});
		
		});
		
	return false;
}


function start_load(){
	// Calculate the center of the screen based on .image_wrapper 
	// -69 is the width of the ajax loader -7 is the height (well half
	$('.loading').css({'left': (Math.floor($('.image_wrapper').width()/2)-69).toString()+'px', 'top':(Math.floor($('.image_wrapper').height()/2 ) + 80 ).toString()+'px'  } ).fadeShow();
}

function stop_load(){
	$('.loading').fadeHide();	
}


// Extending jQuery functions
jQuery.fn.fadeHide = function(options) {
 
  // default settings
  var options = jQuery.extend( {
    delay : 500
  },options);
 
	function none_display()
	{
		$(this).css('display', 'none');
	} 
 
  return this.each(function() {
  	$(this).animate({'opacity': 0}, options.delay, NaN, none_display);
  });
 
};

jQuery.fn.fadeShow = function(options) {
 
  // default settings
  var options = jQuery.extend( {
    opacity: 1,
    delay : 500
  },options);
 
  return this.each(function() {
   $(this).css({'display': 'block', 'opacity': 0}).animate({'opacity': options.opacity}, options.delay);
  });
 
};

function set_dynamic_ui(){
	if(!$(document.body).hasClass("normal_page"))
	{
		// Bind the click event on the project icon
		$(".project_nav .overview a").click(toggle_project_overview);
		// Bind the click event on the zoom icon
		if($('.project_nav .zoom a').attr("href") == ""){
			$('.project_nav .zoom').css({'background-position': '0 -60px'});
			$('.project_nav .zoom a').css({'cursor': 'default'});
		}
		$(".project_nav .zoom a").click(toggle_zoom);
		
		$(".nav .project_nav a").click( function() { $(".nav .project_nav .helper").animate({opacity: 0}, 500); return false; });
		//Bind the active project links to a dynamic loading
		$('.projects_overview .thumbnails a').click(update_project_page);
		$('.projects_overview').draggable({
										/*containment: '.content',*/
										start: function(){$(this).css({'right': 'auto', 'bottom' :'auto',  'margin-right': 0})}
									}).css({'cursor':'move'});
		//Bind the project links to a dynamic loading
		$('.projects_overview .project_list a').click(update_project_page);
		//Bind the click event on the Camera icon (credits toggle)
		$('.project_nav .credits a').click(toggle_credits);
		//Bind the more info click event
		$('.project_content .infos').click(toggle_infos).css('cursor','pointer');
		/*$('.project_content').draggable({
										containment: '.image_wrapper',
										start: function(){$(this).css({'right': 'auto', 'bottom' :'auto'})}
									}).css('cursor','move');*/
		$('.project_nav .next a').click(update_project_page);
		$('.project_nav .prev a').click(update_project_page);
	}else{
		sublim.scroll.container = $('div.image_wrapper');
		sublim.scroll.content = $('div.page_content');
		$('.current_page_item a').attr("href", '/').attr("title", '');
		set_scroll();
		setTimeout(set_scroll, 300);
		$(window).wresize(set_scroll);
		$('div.image_wrapper').mousewheel(function(e,d){
							var visible_ratio = sublim.scroll.container.height() / sublim.scroll.content.outerHeight();
							
							var move = Math.floor( parseInt(sublim.scroll.content.css('top').replace('px','') ) + (d * (sublim.scroll.content.height()/100 ) ) );
							
							var slider_move = Math.floor( parseInt($('.slider .indicator').css('top').replace('px','') ) + -(d * $('.slider').height() /100 )  );
							// Limit the movement of the scrolling
							//Min
							slider_move = slider_move < 0 ? 0 : slider_move;
							move = slider_move <= 0 ? 0 : move;
							//Max
							slider_move = slider_move > $('.slider').height() - $('.slider .indicator').height() ? $('.slider').height() - $('.slider .indicator').height() : slider_move;
							move = slider_move >= $('.slider').height() - $('.slider .indicator').height() ? -(sublim.scroll.content.outerHeight()- sublim.scroll.container.height()) : move;
							sublim.scroll.content.css('top', move + 'px');
							$('.slider .indicator').css('top', slider_move + 'px')
			
			 });
	}
	/*$(".nav_wrapper").hover(function() { 
		$("#legalinfo").animate({ opacity: "1" }, 300);
	}, function() {
		$("#legalinfo").animate({ opacity: "0" }, 300);
	});*/
}
//Bind the load event on the document
$(document).ready(set_dynamic_ui);
