var lk = {

/* Constants */

menu : {
	panel : {
		delay : 300,
		speed : 90,
		fx : 150
	}
},

/* Variables */

background : {
	color : '',
	url : ''
},

open : {
	games : false,
	photography : false,
	graphics : false
},

/* Functions */

onPageLoaded : function() {
	// Set the page background color.
	lk.background = lk.chooseBackground();
	$('body').css('background-color', lk.background.color);

	// Prepare the menu for use.
	lk.prepareMenu(function() {
		// Set the page background image, after the menu is ready.
		lk.setBackground(lk.background.color, lk.background.url);
	});

	sda.resize($('#background img'));
	$(window).resize(function(){ sda.resize($('#background img')); });

	// Load gallery thumbnails.
	lk.loadThumbs('games');
	lk.loadThumbs('photography');
	lk.loadThumbs('graphics');

	// Set contact form send button.
	$('#contact .submit')
		.unbind()
		.click(lk.submitContactForm)
		.hover(function() {
			$(this).stop(true).animate({top: '-27px'}, 400);
		},
		function() {
			$(this).stop(true).animate({top: '0px'}, 400);
		});
	$('#contact .cancel').unbind().click(lk.closeContactForm);
},

showSDA : function() {
	$('#sda')
		.hide()
		.find('img')
			.hide()
			.fadeIn(500, 'linear')
			.end()
		.show();
},

hideSDA : function() {
	$('#sda img').fadeOut(500, 'linear', function() {
		$(this)
			.hide()
			.parent()
				.hide();
	});
},

openContactForm : function() {
	lk.setBackground($('body').css('background-color'), '/images/contact-back.jpg');
	lk.stopSlideShow();
	$('#thumbs').hide();
	$('#menu').fadeOut(400, function() {
		$('#menu').hide();
		$('#frame').css({top: '0px'});
		$('#contact .loading').hide();
		$('#contact').fadeIn(600);
	});
},

closeContactForm : function() {
	lk.background = lk.chooseBackground();
	lk.setBackground(lk.background.color, lk.background.url);
	$('#contact').fadeOut(600, function() {
		$('#menu .panel').stop(true).css({top: '-50px'});
		$('#menu .page-home').show();
		$('#menu .page-gallery').hide();
		$('#menu').fadeIn(400);
	});
},

submitContactForm : function() {
	var name = $('#contact .name').val();
	var email = $('#contact .email').val();
	var phone = $('#contact .phone').val();
	var message = $('#contact textarea.message').val();
	if ((name == '') || (email == '') || (message == '')) {
		$('#contact .loading')
			.text('Please fill in all required fields.')
			.css({color: 'red'})
			.show()
			.stop(true)
			.animate({color: 'white'}, 1000, 'linear');
		return;
	}

	$('#contact .buttons').hide();
	$('#contact .loading').text('Submitting..').show();
	$.ajax({
		url: '/contact.aspx',
		type: 'POST',
		dataType: 'json',
		data: {
			name: name,
			email: email,
			phone: phone,
			message: message
		},
		async: false,
		success: lk.onContactFormSuccess,
		error: lk.onContactFormFailure
	});
},

onContactFormSuccess : function(data, status) {
	if (data && data.success) {
		$('#contact .loading')
			.text('Thank you. Your message has been sent.')
			.css({color: 'green'})
			.show()
			.stop(true)
			.animate({color: 'white'}, 1000, 'linear');
		$('#contact .buttons').show();
	}
	else {
		lk.onContactFormFailure(undefined, status, undefined);
	}
},

onContactFormFailure : function(xhr, status, ex) {
	$('#contact .loading')
		.text('Error sending message. Please try again later.')
		.css({color: 'red'})
		.show()
		.stop(true)
		.animate({color: 'white'}, 1000, 'linear');
	$('#contact .buttons').show();
},

chooseBackground : function() {
	// Set the background color and image.
	var background = null;
	if (backgrounds && backgrounds.length && (backgrounds.length > 0)) {
		background = backgrounds[sda.random(backgrounds.length - 1)];
	}
	return background;
},

loadThumbs : function(cls) {
	if ($('#thumbs .' + cls + ' ul').children().length > 0) {
		return;
	}

	$.each(thumbs[cls], function(i, item) {
		var div = 
			$('<div/>')
				.addClass('wrapper')
				.appendTo(
					$('<li/>').appendTo('#thumbs .' + cls + ' ul'));
		var shadowdiv =
			$('<div/>')
				.addClass('shadow')
				.css({'z-index': '-1'})
				.appendTo(div);
		var shadow =
			$('<img/>')
				.hide()
				.addClass('shadow')
				.css({'z-index': '-1'})
				.attr('src', '/images/thumb-shadow.png')
				.appendTo(shadowdiv);
		var thumbdiv =
			$('<div/>')
				.addClass('thumb')
				.css({'z-index': ''})
				.appendTo(div);
		$('<img/>')
			.hide()
			.addClass('thumb')
			.css({'z-index': ''})
			.appendTo(thumbdiv)
			.bind('load readystatechange', function(e) {
				if (this.complete || ((this.readyState == 'complete') && (e.type == 'readystatechange'))) {
					$(this)
						.click(function() {
							lk.thumbClicked.call(this, cls);
						})
						.hover(function() {
								lk.thumbOver.call(this, cls);
							},
							function() {
								lk.thumbOut.call(this, cls);
							});
				}
			})
			.attr('src', item.url);
	});

	$('#thumbs .' + cls).css({height: thumbs[cls].length * 121 + 'px'});
},

openThumb : function(cls, div) {
	$('#thumbs .' + cls + ' .thumb').addClass('grayedout');
	$('#thumbs .' + cls + ' img.thumb')
		.stop(true)
		.stopTime()
		.css('opacity', 0.3);

	$(div)
		.addClass('opening')
		.css({'z-index': 1000})
		.parent()
			.css({'z-index': 999});

	var shadowdiv = div.childNodes[0];
	$(shadowdiv)
		.css({left: '-21px', top: '-13px', width: '283px', height: '149px', 'z-index': 1001})
		.show();

	var shadow = shadowdiv.childNodes[0];
	$(shadow)
		.css({width: '283px', height: '149px', 'z-index': 1002})
		.show();

	var thumbdiv = div.childNodes[1];
	$(thumbdiv)
		.css({left: '-7px', top: '-1px', width: '255px', height: '121px', 'z-index': 1003})
		.stopTime()
		.oneTime(220, function() {
			$(this)
				.stop(true)
				.stopTime()
				.animate({width: '510px'}, 250, function() {
					$(this).parent().removeClass('opening');
				});
		});

	var thumb = thumbdiv.childNodes[0];
	$(thumb)
		.removeClass('grayedout')
		.css('opacity', 1)
		.css({width: '510px', height: '121px', 'z-index': 1004})
		.show();
},

closeThumb : function(div) {
	$(div)
		.css({'z-index': ''})
		.parent()
			.css({'z-index': ''});

	var shadowdiv = div.childNodes[0];
	$(shadowdiv)
		.css({left: '-13px', top: '-12px', width: '267px', height: '142px', 'z-index': '-1'})
		.hide();

	var shadow = shadowdiv.childNodes[0];
	$(shadow)
		.css({width: '267px', height: '142px', 'z-index': '-1'})
		.hide();

	var thumbdiv = div.childNodes[1];
	$(thumbdiv).css({left: '0px', top: '0px', width: '241px', height: '115px', 'z-index': ''});

	var thumb = thumbdiv.childNodes[0];
	$(thumb)
		.addClass('grayedout')
		.css('opacity', 0.3)
		.css({width: '482px', height: '115px', 'z-index': ''});
},

menuPanelOver : function() {
	$(this).stop(true).animate({top: '-100px'}, 400);
},

menuPanelOut : function() {
	$(this).stop(true).animate({top: '-50px'}, 400);
},

menuGamesOver : function(cls) {
	lk.pauseSlideShow();

	lk.open[cls] = true;

	$('#thumbs .' + cls)
		.hide()
		.css({'top': '0px', 'cursor': 'pointer'})
		.unbind()
		.mousemove(function(e) {
			var div = $('#thumbs .' + cls);
			var ul = $('#thumbs .' + cls + ' ul');
			var ulHeight = ul.height();
			var offset = 50;
			var deadspace = 100;
			var pageHeight = $('body').height();
			if (ulHeight <= pageHeight - offset) {
				return;
			}

			var y = e.pageY - (offset + deadspace / 2);
			var height = pageHeight - (offset + deadspace);
			if (y > height) y = height;
			var ny = y / height * (ulHeight - deadspace);
			if (ny < y) ny = y;
			var top = y - ny;
			div.css('top', top + 'px');
		})
		.hover(function() {
			},
			function(e) {
				var btn = $('#menu .page-gallery .' + cls + ' .panel');
				var pos = btn.offset();
				if ((e.pageX < pos.left) || (e.pageY < pos.top) ||
					(e.pageX >= pos.left + btn.width()) || (e.pageY >= pos.top + btn.height()))
				{
					lk.thumbsHide(cls, $(this), btn);
				}
				else {
					btn.unbind().hover(function(){}, function(e) {
						var thumbs = $('#thumbs .' + cls);
						var pos = thumbs.offset();
						if ((e.pageX < pos.left) || (e.pageY < pos.top) ||
							(e.pageX >= pos.left + thumbs.width()) || (e.pageY >= pos.top + thumbs.height()))
						{
							lk.thumbsHide(cls, thumbs, $(this));
						}
					});
				}
			});

	$(this)
		.unbind()
		.hover(function(){}, function(e) {
			var thumbs = $('#thumbs .' + cls);
			var pos = thumbs.offset();
			if ((e.pageX < pos.left) || (e.pageY < pos.top) ||
				(e.pageX >= pos.left + thumbs.width()) || (e.pageY >= pos.top + thumbs.height()))
			{
				lk.thumbsHide(cls, thumbs, $(this));
			}
		});

	$('#thumbs .' + cls + ' *').css({'z-index': ''});
	$('#thumbs .' + cls + ' .shadow').css({'z-index': '-1'});
	var imgs =
		$('#thumbs .' + cls + ' ul img.thumb')
			.hide()
			.removeClass('grayedout')
			.css('opacity', 1)
			.unbind()
			.click(function() {
				lk.thumbClicked.call(this, cls);
			})
			.hover(function() {
					lk.thumbOver.call(this, cls);
				},
				function() {
					lk.thumbOut.call(this, cls);
				});

	if (lk.open[cls]) {
		//imgs.fadeIn(400, 'linear');
		imgs.show();
	}
	else {
		imgs.hide();
	}

	$('#thumbs').show();
	$('#thumbs .' + cls).show();
},

thumbOver : function(cls) {
	$('#thumbs .' + cls + ' .opening')
		.removeClass('opening')
		.find('*')
			.stopTime()
			.stop(true)
			.end()
		.each(function(i) {
			lk.closeThumb(this);
		});
	lk.openThumb(cls, this.parentNode.parentNode);
},

thumbOut : function(cls) {
	var shouldClose = true;
	var thumb = this;
	$('#thumbs .' + cls + ' .opening')
		.removeClass('opening')
		.find('*')
			.stopTime()
			.stop(true)
			.end()
		.each(function(i) {
			if (thumb.parentNode.parentNode == this) {
				shouldClose = false;
			}
			lk.closeThumb(this);
		});
	if (shouldClose) {
		lk.closeThumb(this.parentNode.parentNode);
	}
},

thumbsHide : function(cls, thumbs, btn) {
	lk.resumeSlideShow();

	lk.open.games = false;
	lk.open.photography = false;
	lk.open.graphics = false;

	btn.unbind();

	thumbs.css({'cursor': 'default'}).unbind().find('img').unbind();
	thumbs.stop(true).stopTime();
	$('#thumbs .' + cls + ' div.grayedout').removeClass('grayedout');
	$('#thumbs .' + cls + ' .grayedout').css({opacity: 0.3}).removeClass('grayedout');
/*
	$('#thumbs .' + cls + ' img.thumb').fadeOut(500, function() {
		thumbs.hide();
		btn
			.unbind()
			.hover(lk.menuPanelOver, function(){})
			.hover(function(){ lk.menuGamesOver.call(this, cls); }, function(){});
	});
*/
	$('#thumbs .' + cls + ' img.thumb').hide();
	thumbs.hide();
	btn
		.unbind()
		.hover(lk.menuPanelOver, function(){})
		.hover(function(){ lk.menuGamesOver.call(this, cls); }, function(){});

/*
	$('#thumbs .' + cls + ' img.thumb').each(function(i, item) {
		if ($(item).css('opacity') > 0) {
			$(item).fadeOut(500, function() {
				thumbs.hide();
				btn
					.unbind()
					.hover(lk.menuPanelOver, function(){})
					.hover(function(){ lk.menuGamesOver.call(this, cls); }, function(){});
			});
		}
		else {
			thumbs.hide();
			btn
				.unbind()
				.hover(lk.menuPanelOver, function(){})
				.hover(function(){ lk.menuGamesOver.call(this, cls); }, function(){});
		}
	});
*/
	btn.stop(true).animate({top: '-50px'}, 400);
},

prepareMenu : function(onMenuReady) {
	// When the page first loads, show empty buttons on the menu.
	$('#menu .panel').css('top', '0px');

	// Show the menu bar.
	$('#menu').show();

	// Then animate in the up state for buttons.
	$('#menu .page-home')
		.oneTime(150, function() {
			$('#menu .page-home .agency .panel').animate({top: 0}, 90, function() {
				$(this).animate({top: '-50px'}, 150, function() {
					$('#menu .page-home .agency .fx').show();
					$('#menu .page-home .agency .fx').fadeOut(500);
				});
			});
		})
		.oneTime(300, function() {
			$('#menu .page-home .gallery .panel').animate({top: 0}, 90, function() {
				$(this).animate({top: '-50px'}, 150, function() {
					$('#menu .page-home .gallery .fx').show();
					$('#menu .page-home .gallery .fx').fadeOut(500);
				});
			});
		})
		.oneTime(450, function() {
			$('#menu .page-home .contact .panel').animate({top: 0}, 90, function() {
				$(this).animate({top: '-50px'}, 150, function() {
					$('#menu .page-home .contact .fx').show();
					$('#menu .page-home .contact .fx').fadeOut(500);
					if (onMenuReady) {
						onMenuReady();
					}
				});
			});
		});

	var delay = 300;
	var pause = 90;
	var speed = 150;

	// Lastly, set up hover handler for buttons.
	$('#menu .page-home .panel').hover(lk.menuPanelOver, lk.menuPanelOut);
	$('#menu .page-gallery .back .panel').hover(lk.menuPanelOver, lk.menuPanelOut);
	$('#menu .page-gallery .contact .panel').hover(lk.menuPanelOver, lk.menuPanelOut);
	$('#menu .page-gallery .games .panel').hover(lk.menuPanelOver, function(){});
	$('#menu .page-gallery .photography .panel').hover(lk.menuPanelOver, function(){});
	$('#menu .page-gallery .graphics .panel').hover(lk.menuPanelOver, function(){});

	// Agency button is a special case.
	$('#menu .page-home .agency .panel').hover(function() {
		$('#about .bio').stop(true);
		$(this)
			.stop(true)
			.animate({top: '-100px'}, 400, function() {
				$('#about .bio').animate({height: '214px'}, sda.timeToMax($('#about .bio').height(), 0, 214, 300));
			});
	}, function() {
		$(this).stop(true);
		$('#about .bio').stop(true);
		var height = $('#about .bio').height();
		if (height > 0) {
			$('#about .bio').animate({height: '0px'}, sda.timeToMin($('#about .bio').height(), 0, 214, 300), function() {
				$('#menu .page-home .agency .panel').animate({top: '-50px'}, 400);
			});
		}
		else {
			$('#menu .page-home .agency .panel').animate({top: '-50px'}, 400);
		}
	});

	// Hide the SDA logo.
	$('#menu .panel').click(function() {
		lk.hideSDA();
	});

	// On click handler for the gallery page.
	$('#menu .page-home .gallery .panel').click(function() {
		var height = $('#frame').height();
		$('#frame')
			.css('height', height + 'px')
			.animate({top: -(height - 50)/2 + 'px'}, 700, function() {
				$('#menu').animate({width: 0}, 500, function() {
					$('#menu .page-home').hide();
					$('#menu .page-gallery')
						.css('top', '0px')
						.show();
					$('#menu').animate({width: '100%'}, 650);
					$('#menu .page-gallery .back .panel').animate({top: '-50px'}, delay);
					$('#menu .page-gallery .games .panel').animate({top: '-50px'}, delay + pause);
					$('#menu .page-gallery .photography .panel').animate({top: '-50px'}, delay + 2 * pause);
					$('#menu .page-gallery .graphics .panel').animate({top: '-50px'}, delay + 3 * pause);
					$('#menu .page-gallery .contact .panel').animate({top: '-50px'}, delay + 4 * pause, function() {
						// On hover handler for gallery panels.
						$('#menu .page-gallery .games .panel, #menu .page-gallery .photography .panel, #menu .page-gallery .graphics .panel').hover(lk.menuPanelOver, function(){});
						$('#menu .page-gallery .games .panel').hover(function(){ lk.menuGamesOver.call(this, 'games'); }, function(){});
						$('#menu .page-gallery .photography .panel').hover(function(){ lk.menuGamesOver.call(this, 'photography'); }, function(){});
						$('#menu .page-gallery .graphics .panel').hover(function(){ lk.menuGamesOver.call(this, 'graphics'); }, function(){});
					});

					lk.setBackground($('body').css('background-color'), '/images/gallery-back.jpg');
				});
			});
	});

	// On click handler for back to home page.
	$('#menu .page-gallery .back .panel').click(function() {
		lk.stopSlideShow();

		$('#thumbs').hide();
		$('#menu .page-gallery .games .panel, #menu .page-gallery .photography .panel, #menu .page-gallery .graphics .panel').unbind();

		$('#frame').animate({top: '0px'}, 700, function() {
			$('#menu .page-gallery')
				.animate({top: '50px'}, 500, function() {
					$(this).hide();

					// When the page first loads, show empty buttons on the menu.
					$('#menu .panel').css('top', '0px');

					$('#menu .page-home')
						.css('left', '0px')
						.css('top', '50px')
						.show()
						.oneTime(50, function() {
							$('#menu .page-home .agency .panel').animate({top: 0}, 90, function() {
								$(this).animate({top: '-50px'}, 150, function() {
									$('#menu .page-home .agency .fx').show();
									$('#menu .page-home .agency .fx').fadeOut(500);
								});
							});
						})
						.oneTime(200, function() {
							$('#menu .page-home .gallery .panel').animate({top: 0}, 90, function() {
								$(this).animate({top: '-50px'}, 150, function() {
									$('#menu .page-home .gallery .fx').show();
									$('#menu .page-home .gallery .fx').fadeOut(500);
								});
							});
						})
						.oneTime(350, function() {
							$('#menu .page-home .contact .panel').animate({top: 0}, 90, function() {
								$(this).animate({top: '-50px'}, 150, function() {
									$('#menu .page-home .contact .fx').show();
									$('#menu .page-home .contact .fx').fadeOut(500);

									$('#background').oneTime(500, function() {
										lk.background = lk.chooseBackground();
										lk.setBackground(lk.background.color, lk.background.url);
									});
								});
							});
						})
						.animate({top: '0px'}, 500);
				});
		});
	});

	// On click handler for contact page.
	$('#menu .page-home .contact .panel, #menu .page-gallery .contact .panel').click(function() {
		lk.openContactForm();
	});
},

setBackground : function(color, url, dir) {
	// Optional/default parameters.
	if (typeof dir == 'undefined') {
		dir = 'rtl';
	}

	// Set the page background color.
	$('body').css('background-color', color);

	// Load the background image.
	var img = new Image();
	$(img)
		// once the image has loaded, execute this code
		.bind('load readystatechange', function(e) {
			if (this.complete || ((this.readyState == 'complete') && (e.type == 'readystatechange'))) {
				// set the image hidden by default    
				$(this).hide();

				// with the holding div #loader, apply:
				$('#background')
					// remove the loading class, 
					.removeClass('loading')
					// fade out the container
					.animate({opacity: 0}, 0, function() {
						$('#background')
							.empty()
							.append($(img));

						sda.resize($(img));

						$('#background').animate({opacity: 1}, 900);
						$(img).show();

						if (dir == 'rtl') {
							var left = $(img).position().left;
							$(img)
								.css({left: left + 150 + 'px'})
								.animate({
										left: left
									},
									450,
									'easeOutQuad');
						}
						else if (dir == 'ltr') {
							var left = $(img).position().left;
							$(img)
								.css({left: left - 150 + 'px'})
								.animate({
										left: left
									},
									450,
									'easeOutQuad');
						}

						// Show the SDA logo.
						if (lk.background && lk.background.url && (url == lk.background.url)) {
							$('body').oneTime(1000, lk.showSDA);
						}
					});
			}
		})

		// if there was an error loading the image, react accordingly
		.error(function () {
			// notify the user that the image could not be loaded
		})

		// *finally*, set the src attribute of the new image to our image
		.attr('src', url);
},

thumbClicked : function(cls) {
	lk.thumbOut.call(this, cls);
	lk.thumbsHide(cls, $('#thumbs .' + cls), $('#menu .page-gallery .' + cls + ' .panel'));

	var li = this.parentNode.parentNode.parentNode;
	var siblings = li.parentNode.childNodes;
	var len = siblings.length;

	var found = -1;
	for (var i = 0; i < len; i++) {
		if (siblings[i] === li) {
			found = i;
			break;
		}
	}

	if (found >= 0) {
		lk.slide.current = -1;
		lk.startSlideShow.call(this, cls, found);
	}
},

slide : {
	cls: '',
	i: 0,
	item: null,
	current: -1
},

startSlideShow : function(cls, i) {
	if ((typeof cls != 'undefined') && (typeof i != 'undefined')) {
		lk.slide.cls = cls;
		lk.slide.i = i;
		lk.setSlide();
		lk.nextSlide();
	}

	if (lk.slide.item == null) {
		return;
	}

	$('#slideshow .prev').unbind().hover(function() {
			$(this).stop(true).animate({left: '-35px'}, 400);
		},
		function() {
			$(this).stop(true).animate({left: '0px'}, 400);
		})
		.click(function() {
			lk.prevSlide();
		});
	$('#slideshow .next').unbind().hover(function() {
			$(this).stop(true).animate({right: '-35px'}, 400);
		},
		function() {
			$(this).stop(true).animate({right: '0px'}, 400);
		})
		.click(function() {
			lk.nextSlide();
		});

	$('#slideshow').show();
},

stopSlideShow : function() {
	$('#slideshow .prev').unbind();
	$('#slideshow .next').unbind();
	$('#slideshow').hide();
},

pauseSlideShow : function() {
	lk.stopSlideShow();
},

resumeSlideShow : function() {
	lk.startSlideShow();
},

setSlide : function(dir) {
	lk.slide.item = thumbs[lk.slide.cls];
	if (lk.slide.item && (lk.slide.i < lk.slide.item.length)) {
		lk.slide.item = lk.slide.item[lk.slide.i];
	}

	if (lk.slide.item && lk.slide.item.slides && (lk.slide.current >= 0) && (lk.slide.current < lk.slide.item.slides.length)) {
		lk.setBackground('#000000', lk.slide.item.slides[lk.slide.current], dir);
	}
},

prevSlide : function() {
	lk.slide.current--;
	if (lk.slide.current < 0) {
		lk.slide.current = lk.slide.item.slides.length - 1;
	}
	lk.setSlide('ltr');
},

nextSlide : function() {
	lk.slide.current++;
	if (lk.slide.current >= lk.slide.item.slides.length) {
		lk.slide.current = 0;
	}
	lk.setSlide('rtl');
}

}
