$(document).ready(function() {
	// First load thumbs, then images
	var t_total_thumbs = $("#thumbnails-wrapper img").length;
	var t_total_loaded = 0;
	$(".loadfirst").each(function() {
		$(this).attr('src', $(this).attr('filesrc')).bind('load', function() { t_total_loaded++; if(t_total_thumbs == t_total_loaded) { 
			$(".loadsecond").each(function() { 
				$(this).attr('src', $(this).attr('filesrc'));

				$(this).next().remove();
				$(this).removeClass("main-image-loading");				
			});
		} });
	});

	// Caluclate window sizes
	reset_view();

	// Start the resizer
	$(window).bind("resize", function(){ calculate_element_sizes(); resizer(); });

	// Clients List Page
	$(".icon").hover(function() { $(this).addClass("icon-hover"); }, function() { $(this).removeClass("icon-hover"); });
	$("#client-overlay").live('mouseover', function() {
		if (!$(".client_images li:visible").is(":first-child")) { $(".client_photo_left").show(); }	
		if (!$(".client_images li:visible").is(":last-child")) { $(".client_photo_right").show(); }
	});
	$("#client-overlay").live('mouseout', function() { $(".client_photo_nav").hide(); });
	$("#text ul li a").bind('mouseenter', function() { Cufon.refresh(); })
	$("#text ul li").bind('mouseout', function() { Cufon.refresh(); });

	// Contacts Overlay
	$("#site-by").hover(function() {
		$("#onvi-media-logo").stop().fadeIn();
	}, function() { });
	$(".contact-content-left").hover(function() { }, function() {
		$("#onvi-media-logo").stop().fadeOut();
	});

	// Lower Menu Bar
	$("#menu").data('amion', 'true').hover(
		function() { $(this).data('mouseover', 'true'); toggle_menubar("on"); },
		function() { $(this).data('mouseover', 'false'); toggle_menubar("off"); }
	);

	// Init the menubar
	if (show_bar) {
		toggle_menubar("on");
		$("#thumbnails-window").hide();
		setTimeout(function() { if ($("#menu").data('mouseover') != "true") { toggle_menubar("off"); } }, 3000);
		setTimeout(function() { $("#thumbnails-window").stop().animate({ width: 'show' }, 1500); }, 1000);
	}

	// Init the slideshow controls
	if (enable_slideshow) {
		toggle_slideshow(slideshow_autoplay ? "on" : "off");
		$(".slideshow-img").bind('mousemove', function(e) {
			$("#slideshow-controls").css({
		    top: (e.pageY + 5) + "px",
		    left: (e.pageX + 5) + "px"
			});
		})
		.bind('mouseenter', function() { $("#slideshow-controls").show(); })
		.bind('mouseout', function() { $("#slideshow-controls").hide(); })
		.bind('click', function() { toggle_slideshow(); });
	}

	// Setup overlays and placeholders
	$(".overlay_button").bind("mouseover", function() { $(this).addClass("overlay_button_hover"); }).bind("mousedown", function() { $(this).addClass("overlay_button_active"); }).bind("mouseout", function() { $(this).removeClass("overlay_button_hover").removeClass("overlay_btn_active"); } ).bind("mouseup", function() { $(this).removeClass("overlay_button_active"); });
	$(".close").bind("mouseover", function() { $(this).addClass("close_hover"); }).bind("mouseout", function() { $(this).removeClass("close_hover").removeClass("overlay_btn_active"); } );
	$("#galleries-close").bind("mouseover", function() { $(this).addClass("galleries-close-hover"); }).bind("mouseout", function() { $(this).removeClass("galleries-close-hover"); } );

	// Side Hotspots
	$("#hotspot-right").hover(
		function() {
			$(this).addClass("hotspot-right-arrow");

			if (is_relaxed()) {
				if (use_sneakpeek) {
					$(".slideshow-img").eq(current_image).css('zIndex', '199').show();
					var tmp = sneakpeek;
					if (compensate) { tmp += compensate; }
					$(".slideshow-img").eq(current_image - 1).stop().animate({left: '-'+tmp+'px'}, sneak_peek_duration);
				}
			}
		},
		function() {
			$(this).removeClass("hotspot-right-arrow");

			if (is_relaxed()) {
				if (!just_clicked && use_sneakpeek) {
					$(".slideshow-img").eq(current_image - 1).stop().animate({left: '0px'}, function() {
						$(".slideshow-img").not(this).css('zIndex', '198').hide();
					});
				}
				just_clicked = false;
			}
		}
	).click(
		function() {
			next_image();
		}
	).mouseenter(
		function() {
			$(this).data('iamhere', 'true');
		}
	).mouseleave(
		function() {
			$(this).data('iamhere', 'false');
		}
	);

	$("#hotspot-left").hover(
		function() {
			$(this).addClass("hotspot-left-arrow");

			if (is_relaxed()) {
				if (use_sneakpeek) {
					$(".slideshow-img").eq(current_image - 2).css('zIndex', '199').show();
					$(".slideshow-img").eq(current_image - 1).stop().animate({left: sneakpeek+'px'}, sneak_peek_duration);
				}
			}
		},
		function() {
			$(this).removeClass("hotspot-left-arrow");

			if (is_relaxed()) {
				if (!just_clicked && use_sneakpeek) {
					$(".slideshow-img").eq(current_image - 1).stop().animate({left: '0px'}, function() {
						$(".slideshow-img").not(this).css('zIndex', '198').hide();
					});
				}
				just_clicked = false;
			}
		}
	).click(
		function() { 
			prev_image();
		}
	).mouseenter(
		function() {
			$(this).data('iamhere', 'true');
		}
	).mouseleave(
		function() {
			$(this).data('iamhere', 'false');
		}
	);

	// Thumbnail Hotspots
	$("#thumbnails-window").mousemove(function(e) {
		if ($("#galleries-window:visible").size() == 0) {
			if (ratio < 0) { calculate_element_sizes(); }
			var offset = $("#thumbnails-window").offset();
			
			var x = e.pageX - offset.left;
			var move_to = ratio * x;
			$('#thumbnails-wrapper').css({ left: "-" + move_to + "px" });
		}
	});
});

function post_splash() {
	// Hide the loading bar
	$("#loading-bar").css("width", "100%").hide();
	
	// Fade out the splash page
	$("#splash").fadeOut("slow");
	
	// Fade in the gallery
	$("#site-wrapper").fadeIn();
	
	// Adjust view to screen size
	reset_view();

	// Bring up the history
	// Load gallery
	if (gid = $.address.parameter('gallery')) { load_gallery(gid); }
	
	// Load client list
	if ($.address.parameter('clientlist') == "1") { show_clients(); }
	
	// Load photo
	var photo = ($.address.parameter('photo')) ? parseInt($.address.parameter('photo')) : 1;
	if (current_image != photo) { load_image(photo, false, false); }
}

function toggle_menubar(state) {
	if (($("#menu").data('amion') == "true" && state == null) || state == "off") {
		$("#menu").stop().animate({bottom: '-' + menubar_bottom + 'px'}, 500);
	} if (($("#menu").data('amion') == "false" && state == null) || state == "on") {
//		realign_thumbnails(1); FIX IE FIRST
		toggle_galleries("instant-off");
		$("#menu").stop().animate({bottom: '0px'}, 500);
	}
}

function realign_thumbnails(pageX) {
	var offset = $("#thumbnails-window").offset();
	
	var x = pageX - offset.left;
	var move_to = ratio * x;
	$('#thumbnails-wrapper').css({ left: "-" + move_to + "px" });
}

function load_image(imgpos, hotspot_origin, wait_until_relaxed) {
	// Default params
	if (wait_until_relaxed == null) { wait_until_relaxed = true; }
	
	// Quick check
	if (current_image == imgpos || (wait_until_relaxed && !is_relaxed())) return false;

	// Hide the clients overlay, if open
	hide_clients();

	// Disable the hotspot buttons
	$("#hotspot-left").addClass("hotspot-left-arrow-disabled");
	$("#hotspot-right").addClass("hotspot-right-arrow-disabled");

	// Default values
	if (hotspot_origin == null) hotspot_origin = false;

	// Check if the image has been cached already
  var image = new Image();

	// Sneakpeak to the relavant side
	if (current_image < imgpos) {
		// The desired image is to the right of the current image
		$(".slideshow-img").eq(current_image - 1).animate({paddingLeft: '-=11px', paddingRight: '+=11px'}, 200, function() {
			$("#right-image-loader-bar").slideDown();
		});
	} else if (current_image > imgpos) {
		// The desired image is to the left of the current image
		$(".slideshow-img").eq(current_image - 1).animate({paddingLeft: '+=11px', paddingRight: '-=11px'}, 200, function() {
			$("#left-image-loader-bar").slideDown();
		});
	}

	$(image).bind("load", null, function() {
		var tmp = setTimeout(function() {
			// Move, now
			move_to_image(imgpos, hotspot_origin);
	
			// Set the current image marker
			current_image = imgpos;
			resizer(true);			
		}, 200);
  });
  
  // Load it now!
  var src = $(".slideshow-img").eq(imgpos - 1).attr('src');
	image.src = src;
}

function move_to_image(imgpos, hotspot_origin) {
	// Default values
	if (hotspot_origin == null) hotspot_origin = false;

	// Set the address
	$.address.parameter("photo", imgpos);

	// Hide the loading bar
	$(".image-loader-bar").slideUp().hide();

	// Hide the menubar now
	toggle_menubar("off");

	// Prepare the new image for 
	$(".slideshow-img").eq(imgpos - 1).css('zIndex', '199').show();

	// Are we moving right or left?
	if (current_image < imgpos) {
		// Moving right it is
		var tmp = $(window).width();
		if (compensate) { tmp += compensate; }

		$(".slideshow-img").eq(current_image - 1).stop().animate({left: '-'+tmp+'px'}, animation_length, function() {
			// Enable the hotspot buttons
			$("#hotspot-left").removeClass("hotspot-left-arrow-disabled");
			$("#hotspot-right").removeClass("hotspot-right-arrow-disabled");

			$(".slideshow-img").not(".slideshow-img:eq("+(imgpos-1)+")").hide().css('zIndex', '198').css('left', '0');

			$(".slideshow-img").eq(imgpos - 1).css('zIndex', '200');
			if (hotspot_origin) {
				if (total_images > current_image) {
					setTimeout(function(){
						if ($("#hotspot-right").data('iamhere') == "true") {
							$(".slideshow-img").eq(current_image).css('zIndex', '199').show();
							$(".slideshow-img").eq(current_image - 1).stop().animate({left: '-'+sneakpeek+'px'}, sneak_peek_duration);
							just_clicked=false;
						}
					}, time_to_wait_after_move);
				}
			}
		});
	}
	else if (current_image > imgpos) {
		// Moving left it is
		var tmp = $(window).width();
		if (compensate) { tmp += compensate; }
		$(".slideshow-img").eq(current_image - 1).stop().animate({left: tmp+'px'}, animation_length, function() {
			// Enable the hotspot buttons
			$("#hotspot-left").removeClass("hotspot-left-arrow-disabled");
			$("#hotspot-right").removeClass("hotspot-right-arrow-disabled");

			$(".slideshow-img").not(".slideshow-img:eq("+(imgpos-1)+")").hide().css('zIndex', '198').css('left', '0');
	
			$(".slideshow-img").eq(imgpos - 1).css('zIndex', '200');
			if (hotspot_origin) {
				if (current_image > 1) {
					setTimeout(function(){
						if ($("#hotspot-left").data('iamhere') == "true") {
							$(".slideshow-img").eq(current_image - 2).css('zIndex', '199').show();
							$(".slideshow-img").eq(current_image - 1).stop().animate({left: sneakpeek+'px'}, sneak_peek_duration);
							just_clicked=false;
						}
					}, time_to_wait_after_move);
				}			
			}			
		});
	}
}

function init_clients_nav() {
	// Text list nav
	$("#clients-text-arrow-down").hover(function() {
		move_text_down = setInterval( "clients_text_list_move_text_down()", 4 );
	}, function() {
		clearInterval(move_text_down);
	});

	$("#clients-text-arrow-up").hover(function() {
		move_text_up = setInterval( "clients_text_list_move_text_up()", 4 );
	}, function() {
		clearInterval(move_text_up);
	});

	// Icon grid nav
	$("#clients-icons-arrow-down").hover(function() {
		move_icons_down = setInterval( "clients_icons_list_move_text_down()", 4 );
	}, function() {
		clearInterval(move_icons_down);
	});

	$("#clients-icons-arrow-up").hover(function() {
		move_icons_up = setInterval( "clients_icons_list_move_text_up()", 4 );
	}, function() {
		clearInterval(move_icons_up);
	});
}

function clients_text_list_move_text_down() {
	var top = parseInt($('#text').css('top'));
	var total_height = $("#text").height() - $("#text-wrapper").height();
	
	if(Math.abs(top) < total_height) {
		var new_pos = top - 3;
		$('#text').css({ top: new_pos });	
		$('#clients-text-arrow-up').show();
	} else {
		$('#clients-text-arrow-down').hide();	
		$('#clients-text-arrow-up').show();
	}
}

function clients_text_list_move_text_up() {
	var top = parseInt($('#text').css('top'));

	if(Math.abs(top) > 0) {
		var new_pos = top + 3;
		$('#text').css({ top: new_pos });	
		$('#clients-text-arrow-down').show();
	} else {
		$('#clients-text-arrow-down').show();	
		$('#clients-text-arrow-up').hide();	
	}
}

function clients_icons_list_move_text_down() {
	var top = parseInt($('#icons').css('top'));
	var total_height = $("#icons").height() - $("#icons-wrapper").height();

	if(Math.abs(top) < total_height) {
		var new_pos = top - 3;
		$('#icons').css({ top: new_pos });	
		$('#clients-icons-arrow-up').show();
	} else {
		$('#clients-icons-arrow-down').hide();	
		$('#clients-icons-arrow-up').show();
	}
}

function clients_icons_list_move_text_up() {
	var top = parseInt($('#icons').css('top'));

	if(Math.abs(top) > 0) {
		var new_pos = top + 3;
		$('#icons').css({ top: new_pos });	
		$('#clients-icons-arrow-down').show();
	} else {
		$('#clients-icons-arrow-down').show();	
		$('#clients-icons-arrow-up').hide();	
	}
}


