var leftside;
var rightside;
var topside;
var bottomside;

var max_right;
var max_bottom;

var viewportwidth;
var viewportheight;

var t;
var css_left;
var css_top;
var current_direction;

function reset_vars(){

get_viewport_dimensions();
leftside = viewportwidth / 3;
rightside = leftside * 2;
max_right = -3401 + viewportwidth;
topside = viewportheight / 3;
bottomside = topside * 2;
max_bottom = -900 + viewportheight;

// nav pos
var header_pos_left = (viewportwidth - 960) / 2;
$("#navigation_container").css("left", header_pos_left);
var header_post_left_2 = header_pos_left + 17;
$("#space_container h1").css("left", header_post_left_2);

}

$(document).ready(function(){

get_viewport_dimensions();

// nav pos
var header_pos_left = (viewportwidth - 960) / 2;
$("#navigation_container").css("left", header_pos_left);
var header_post_left_2 = header_pos_left + 17;
$("#space_container h1").css("left", header_post_left_2);

// project hologram gallery

$("#ph_left").click(function(){

	if(!$("#project_hologram_ss_inner").is(":animated")){

	var current_left = parseInt($("#project_hologram_ss_inner").css("left"));
	var new_left = (current_left == 0 ) ? -4734 : current_left + 789 ;
	$("#project_hologram_ss_inner").animate({left: new_left});
	
	}
	
});

$("#ph_right").click(function(){

	if(!$("#project_hologram_ss_inner").is(":animated")){

	var current_left = parseInt($("#project_hologram_ss_inner").css("left"));
	var new_left = (current_left == -4734 ) ? 0 : current_left - 789 ;
	$("#project_hologram_ss_inner").animate({left: new_left});
	
	}
	
});

// gallery

$("#view_detail").click(function(){

	if($(this).html() == 'VIEW DETAIL IMAGE'){
		$("#main_image").hide();
		$("#makro_image").show();
		$(this).html("VIEW FULL IMAGE");
	}
	
	else if($(this).html() == 'VIEW FULL IMAGE'){
		$("#main_image").show();
		$("#makro_image").hide();
		$(this).html("VIEW DETAIL IMAGE");	
	}
	
	else{
	// do nothing
	}

});

/*
var pane = $('.scrollable');

pane.jScrollPane(
{
			verticalDragMinHeight: 5,
			verticalDragMaxHeight: 5,
			showArrows: true,
			animateScroll: true
});

var api = pane.data('jsp');
*/

// subnav

$("#subnav ul li a").click(function(){

	//api.scrollToY(0);
	var rel = $(this).attr("rel");

	$('#subnav ul li a').each(function(index){
	var temp_rel = $(this).attr("rel");
	$(this).removeClass("active");
	$(temp_rel).hide();
	});
	
	if($(this).hasClass("gallery_subnav")){
	// change dimensions of page container
	
		switch(rel){
		
		case '#gallery':
		var cont_width = 'gallery_container_width';
		break;
		
		case '#publications':
		var cont_width = 'publications_container_width';
		break;
		
		case '#reviews':
		var cont_width = 'reviews_container_width';
		break;
		
		case '#art_biography':
		var cont_width = 'art_biography_container_width';
		break;
		
		case '#exhibitions':
		var cont_width = 'exhibitions_container_width';
		break;
		
		case '#available_to_buy':
		var cont_width = 'available_to_buy_container_width';
		break;
		
		case '#screensavers':
		var cont_width = 'screensavers_container_width';
		break;

		}
		
		//$("div.the_gallery #content").removeClass();
		//$("div.the_gallery #content").addClass(cont_width);

	}

	$(this).addClass("active");
	$(rel).show();
	//api.reinitialise();

});

// gallery dropdown

$("#gallery_top_left>ul>li").mouseenter(function(){
	$("#gallery_top_left ul li ul").show();
});

$("#gallery_top_left>ul>li").mouseleave(function(){
	$("#gallery_top_left ul li ul").hide();
});

$("#gallery_top_right>ul>li").mouseenter(function(){
	$("#gallery_top_right ul li ul").show();
});

$("#gallery_top_right>ul>li").mouseleave(function(){
	$("#gallery_top_right ul li ul").hide();
});

// bg start pos

if(viewportwidth <= 2572){

var positive_left = ((2572 - viewportwidth) / 2) + 829;
var space_left = positive_left - (positive_left * 2);

$("#space").css("left", space_left);

}

// window resize

$(window).bind('resize', function(){ reset_vars(); });

// home nav

$("#home_nav a").hover(
function(){
$("#home_nav_bg").fadeIn();
},
function(){
$("#home_nav_bg").fadeOut();
});

leftside = viewportwidth / 3;
rightside = leftside * 2;
max_right = -3401 + viewportwidth;

topside = viewportheight / 3;
bottomside = topside * 2;
max_bottom = -900 + viewportheight;

   $("#space_container").mousemove(function(e){
   
   css_left = ($("#space").css("left") != 'auto') ? parseInt($("#space").css("left")) : 0;
   css_top = ($("#space").css("top") != 'auto') ? parseInt($("#space").css("top")) : 0;

	var x = e.pageX - this.offsetLeft;
	var y = e.pageY - this.offsetTop;
	
	if(x <= leftside && css_left < 0){
	// move left
	
	if(current_direction != 'left'){
	$("#space").clearQueue();
	$("#space").stop(true);
	}
	
	current_direction = 'left';
	var distance = css_left - (css_left * 2);
	var time = distance * 4;
	$("#space").animate({left: 0}, time);
	}
	
	else if(x >= rightside && css_left > max_right){
	// move right
	
	if(current_direction != 'right'){
	$("#space").clearQueue();
	$("#space").stop(true);
	}
	
	current_direction = 'right';
	var distance = (max_right - css_left) - ((max_right - css_left) * 2);
	var time = distance * 4;
	$("#space").animate({left: max_right}, time);
	}
	
	else if(y <= topside && css_top < 0){
	// move up
	
	if(current_direction != 'up'){
	$("#space").clearQueue();
	$("#space").stop(true);
	}
	
	current_direction = 'up';
	$("#space").animate({top: 0}, "slow");
	}
	
	else if(y >= bottomside && css_top > max_bottom){
	// move down
	
	if(current_direction != 'down'){
	$("#space").clearQueue();
	$("#space").stop(true);
	}
	
	current_direction = 'down';
	$("#space").animate({top: max_bottom}, "slow");
	}
	
	else{
	// stop animation
	
		if($("#space").is(':animated') && current_direction != null){
		
		$("#space").clearQueue();
		$("#space").stop(true);
		
			if(current_direction == 'left'){
			var distance = css_left - (css_left * 2);
			
				if(distance > 55){
				$("#space").animate({left: '+=45'}, 350, 'linear', function(){
					$("#space").animate({left: '+=10'}, 200, 'linear');
				});
				}
				
				else{
				var time = distance * 10;
				$("#space").animate({left: 0}, time, 'linear');
				}
			
			}
			
			if(current_direction == 'right'){
			var distance = (max_right - css_left) - ((max_right - css_left) * 2);
			
				if(distance > 55){
				$("#space").animate({left: '-=45'}, 350, 'linear', function(){
					$("#space").animate({left: '-=10'}, 200, 'linear');
				});
				}
				
				else{
				var time = distance * 10;
				$("#space").animate({left: max_right}, time, 'linear');
				}
			
			}
			
		}
	
	current_direction = null;
	
	}
	
   });
   
});

function get_viewport_dimensions(){

	if(typeof window.innerWidth != 'undefined'){
	viewportwidth = window.innerWidth,
	viewportheight = window.innerHeight
	}
	 
	else if(typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0){
	viewportwidth = document.documentElement.clientWidth,
	viewportheight = document.documentElement.clientHeight
	}
	 
	else{
	viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
	viewportheight = document.getElementsByTagName('body')[0].clientHeight
	}

}
