/* RESEN */
/* Master Javascript File
------------------------------------------------------------------------ */


/* EVENTS
------------------------------------------------------------------------ */


	$(document).ready(function() {
	
		browser();
		selectors();
				
		if (page != "index" || window.location.hash && window.location.hash != "#home") {

			bookmark();
		
		} else {
		
			$('div#content').remove();
			cufon();

		}

		$('#navigation a, h1 a').click(navigation);
		email();
		
		easterEggs();
	
	});

	$(window).load(function() { });
	

/* FUNCTION: READ HASH BOOKMARK
----------------------------------------------------------------------------- */


	function bookmark() {
				
		// Get Link
		var href = window.location.hash.replace('#', '');
		if (href == "") var href = page;

		// Set Title
		setTitle(href);
		
		if (href.match('home')) {
		
		
/* Home Page */		
		

			if ($('#content').length > 0) $('#content').remove();
			
				
		} else if (!href.match('work-')) {		
				
				
/* Standard Pages */				
				
				
			// Set Selected Section Indicator
			$('#navigation').children('.' + href).addClass('selected');
			$('body').addClass('content');
			
			// Move Heading
			$('#header h2').css('top', '-260px');
	
			// Move Marks
			$('<h1 class="small"><a href="/" title="home">Resen</a></h1>').prependTo('#header').css('top', '24px');
			$('h1:not(.small)').css('top', '-71px');
			
			// Move Logotype
			$('#navigation .resen a').css('margin-top', '-61px');			
			
			// Hide Clients
			$('#clients > div').hide();
			
			// Create Content Area
			$('#content').remove();
			$('<div id="content" class="' + href + '"><em class="loading">Loading...</em></div>').insertAfter('#header-container').css('height', '200px').css('display', 'block');
			
			// Move Header
			$('#header').css('marginTop', '-260px');
			
			// Load Content
			$('#content').load(href + '?ajax=true', function() {
	
				postLoad();
				$(this).css('height', 'auto');
			
			});
			
		} else {
		
		
/* Portfolio Pages */


			// Set Selected Section Indicator
			$('#navigation').children('.work').addClass('selected');
			$('body').addClass('content').addClass('detail');
			
			// Move Heading
			$('#header h2').css('top', '-260px');
	
			// Move Navigation
			$('#navigation').css('margin-top', '-58px');
	
			// Move Mark
			$('<h1 class="small"><a href="/" title="home">Resen</a></h1>').prependTo('#header').css('top', '24px');
			$('h1:not(.small)').css('top', '-71px');

			// Move Logotype
			$('#navigation .resen a').css('margin-top', '-61px');			
			
			// Hide Clients
			$('#clients > div').hide();
			
			// Create Content Area
			$('#content').remove();
			$('<div id="content" class="work"><em class="loading">Loading...</em></div>').insertAfter('#header-container').css('height', '200px').css('display', 'block');
			
			// Move Header
			$('#header').css('margin-top', '-260px');
			
			// Move Navigation
			$('#navigation').css('marginTop', '-58px');
			
			// Load Content: Work
			$('#content').hide().load('/work?ajax=true', function() {
	
				postLoad();
				$(this).css('height', 'auto');
			
			});
			
			// Load Content: Piece
			$('<div id="detail"><div><div><em class="loading">Loading...</em></div></div></div>').insertAfter('#content');
			$('#detail > div > div').load(href + '?ajax=true', function() {
			
				postLoad();
				$('#footer').show();
			
			});
			
			// Back Button
			backButton();
	
		}
	
	}
	

/* FUNCTION: NAVIGATION
------------------------------------------------------------------------ */

	
	function navigation() {
		
		if ($(this).parent('li.resen').length > 0 || $(this).parent('h1.large').length > 0) {
		
			return false;			
		
		} else if ($(this).parent('h1.small').length > 0) {
		

/* Return to Home Page */

	
			$('#navigation li').removeClass('selected');
			$('body').removeClass('content').removeClass('detail');
			window.location.hash = "home";

			// Set Title
			setTitle('Home');
	
			// Animate Heading
			$('#header h2').animate({ top: '0' }, 800);
			
			// Animate Header
			$('#header').animate({ marginTop: '0' }, 600);
			
			// Animate Navigation
			$('#navigation').animate({ marginTop: '0' }, 600);

			// Animate Marks
			$('h1.small').animate({ top: '-38' }, 400, function() {
			
				$(this).remove();
				$('h1:not(.small)').animate({ top: '135px' }, 700);
				
			});
			
			// Animate Logotype
			$('#navigation .resen a').animate({ marginTop: '0' }, 500);			
			
			// Remove Content Area
			$('#content').remove();
			if ($('#detail').length > 0) $('#detail').remove();
			if ($('a.back').length > 0) $('a.back').remove();
			
			// Unhide Client List
			$('#clients > div').slideDown('slow');

			// Analytics
			pageTracker._trackPageview('index');

		} else if ($(this).parent('li').length > 0 && $(this).parent('.selected').length == 0 && $('#content').length == 0) {


/* Leave Home Page */

			
			$(this).parent().siblings().removeClass('selected');
			$(this).parent().addClass('selected');
			$('body').addClass('content');

			// Get Link
			var href = $(this).attr('href');
			var section = href.split('.')[0].replace('/', '');
			window.location.hash = section;
			
			// Set Title
			setTitle(section);
	
			// Animate Heading
			$('h2').animate({ top: '-260px' }, 700);

			// Animate Marks
			$('h1:not(.small)').animate({ top: '-71px' }, 700, function() {

				$('<h1 class="small"><a href="/" title="home">Resen</a></h1>').prependTo('#header').animate({ top: '24px' }, 400, function() {
				
					// Load Content
					$('#content').load(href + '?ajax=true', function() {
	
						postLoad();
						$(this).css('height', 'auto');
				
					});
				
				});
				
				$('h1.small a').click(navigation);
				
			});
			
			// Animate Logotype
			$('#navigation .resen a').animate({ marginTop: '-61px' }, 500);			
			
			// Animate Clients
			$('#clients > div').slideUp(500);
			
			// Create Content Area
			$('<div id="content" class="' + section + '"><em class="loading">Loading...</em></div>').insertAfter('#header-container').hide().css({ height: '200px' }).slideDown('500');
			
			// Animate Header
			$('#header').animate({ marginTop: '-260px' }, 500);
			
			// Analytics
			pageTracker._trackPageview(href);

		} else if (!$(this).parent().hasClass('selected')) {
		

/* Navigate Internal Pages */

			
			$(this).parent().siblings().removeClass('selected');
			$(this).parent().addClass('selected');
			$('body').addClass('loading');
			$('#content').attr('class', '');
			
			// Hide Footer
			$('#footer').hide();

			// Get Link
			var href = $(this).attr('href');
			var section = href.split('.')[0].replace('/', '');
			window.location.hash = section;
			
			// Set Title
			setTitle(section);
		
			// Content
			$('#content').empty().append('<em class="loading">Loading...</em>').addClass(section).load(href + '?ajax=true', function() {

				postLoad();
				$('body').removeClass('loading');
				$('#footer').show();

			});
			
			// Analytics
			pageTracker._trackPageview(href);
					
		}
	
		return false;
	
	}
	
	
/* FUNCTION: CUFON
------------------------------------------------------------------------ */


	function cufon() {
	
		var replace = "#detail h2, #content h3, #content h4";
		Cufon.replace(replace);
		$(replace).css('visibility', 'visible');

	}
	
	
/* FUNCTION: IMAGES
------------------------------------------------------------------------ */
	
	
	function images() {
	
		if ($('div.fourths.thumbs').length > 0) {
		
		
/* Thumbnail Grid */


			$('div.fourths.thumbs li').hover(function() {

				$(this).addClass('hover');
				
			}, function() {
			
				$(this).removeClass('hover');

			});

		}
	
		if ($('div.images').length > 0) {
		
		
/* Tooltip */


			$('div.images > div > ul > li:not(:first-child)').hover(function() {
			
				$(this).addClass('hover');
				$(this).children('div.info, a').fadeIn('fast');
				$(this).find('div div > strong').fadeIn('fast');

			}, function() {

				$(this).removeClass('hover');
				$(this).children('div.info, a').fadeOut('fast');
				$(this).find('div div > strong').fadeOut('fast');

			});
			
			$('div.images > div > ul > li:first-child').hover(function() {
			
				$(this).addClass('hover');
				if ($(this).find('div.info').length > 0) $(this).closest('div').children('h3').fadeOut('fast');
				$(this).children('div.info, a').fadeIn('fast');
				$(this).find('div div > strong').fadeIn('fast');

			}, function() {
			
				$(this).removeClass('hover');
				if ($(this).find('div.info').length > 0) $(this).closest('div').children('h3').fadeIn('fast');
				$(this).children('div.info, a').fadeOut('fast');
				$(this).find('div div > strong').fadeOut('fast');

			});
		
			
/* Slideshow */

			
			$('.slideshow:not(.active)').each(function(index) {
			
				var next = 'next' + index;
				var prev = 'prev' + index;
				
				$(this).siblings('a.next').attr('id', next);
				$(this).siblings('a.prev').attr('id', prev);

				$(this).cycle({ 
				
					fx: 'scrollHorz', 
					speed: 'fast', 
					timeout: 0, 
					next: '#' + next,
					prev: '#' + prev 
				
				});
				
				$(this).addClass('active');
			
			});
		
		}
	
	}
	
	
/* FUNCTION: DETAIL PAGE
------------------------------------------------------------------------ */


	function detailPage() {
	
		// Coordinates and Dimensions
		var href = $(this).attr('href');
		var section = href.split('.')[0].replace('/', '');
		window.location.hash = section;
		var windowWidth = window.innerWidth;
		var contentWidth = $('#content').width();
			
		// Header
		$('#navigation').animate({ marginTop: '-58px' }, 500, function() {
		
			// Content Area
			var contentLeft = windowWidth * -1;
			$('#content').hide();
			
			$('<div id="detail"><div><div><em class="loading">Loading...</em></div></div></div>').insertAfter('#content');
			$('#detail > div > div').load(href + '?ajax=true', function() {
				
				postLoad();
				$('#footer').show();
				
			});		
		
			// Scroll to Top
			window.scrollTo(0, 0);
		
		});

		// Body Class
		$('body').addClass('detail');
		$('#footer').hide();
		
		// Back Button
		backButton();
		
		// Analytics
		pageTracker._trackPageview(href);
		
		return false;
	
	}
	
	
/* FUNCTION: BACK BUTTON
------------------------------------------------------------------------ */

	
	function backButton() {

		$('body').append('<a class="back" href="#work"><span>Go Back</span></a>');
		var backWidth = $('body > a.back').outerWidth() * -1;		
		$('body > a.back').css('left', backWidth).animate({ left: 0 });
		
		$('body > a.back').click(function() {
		
			if ($('#lightbox').length > 0) {
		
				// Close Lightbox
				$('#lightbox').fadeOut('fast', function() { $(this).remove(); });
				
			} else {
			
				// Hash
				window.location.hash = 'work';
				
				// Header
				$('#navigation').animate({ marginTop: '0' }, 500);
				
				// Browse Area
				$('#content').show();
				
				// Detail Area
				$('#detail').remove();
				
				// Body Class
				$('body').removeClass('detail');
				
				// Fix Slideshows
				images();
				
			}
		
			// Remove Button
			var backWidth = $(this).outerWidth() * -1;
			$(this).animate({ left: backWidth }, function() { $(this).remove(); });
			
			return false;
		
		});

	}
	
	
/* FUNCTION: LIGHTBOX
------------------------------------------------------------------------ */


	function lightbox() {
	
		// Create Lightbox
		$('body').append('<a id="lightbox" href="#"></a>');
		$('#lightbox').hide().fadeIn('fast');
				
		// Get Image
		var image = $(this).attr('href');
		imgLoader = new Image();
		imgLoader.src = image;
	
		// Load
		imgLoader.onload = function() {
				
			var height = imgLoader.height;
			var top = ($('#lightbox').height() - imgLoader.height) / 2;
			$('#lightbox').append('<img src="' + image + '" alt="" />');
			$('#lightbox img').css('display', 'inline').css('margin-top', top + 'px');
		
		}
		
		// Back Button
		backButton();

		// Close Lightbox
		$('#lightbox').click(function() {
		
			// Remove Lightbox
			$(this).fadeOut('fast', function() { $(this).remove(); });
			
			// Remove Back Button
			$('body > a.back').each(function() {
			
				var backWidth = $(this).outerWidth() * -1;
				$(this).animate({ left: backWidth }, function() { $(this).remove(); });
				
			});
			
			return false;
		
		});

		return false;

	}

	
/* FUNCTION: POST-LOAD
------------------------------------------------------------------------ */


	function postLoad() {
		
		cufon();
		images();
		email();
		$('div.fourths.thumbs h4 a[rel=detail]').click(detailPage);
		$('a[rel=lightbox]').click(lightbox);
		$('a[rel=dead]').click(function() { return false; });

	}


/* FUNCTION: EMAIL
------------------------------------------------------------------------ */


	function email() {
	
		var eat = "info@";
		var wheat = ".com";
		var shredded = "resenmedia";
		var never = "mailto:";
		var alwaysRemember = never + eat + shredded + wheat;
		
		$('a.email').each(function() {
					
			if ($(this).text().match('Demo')) {
			
				$(this).attr('href', alwaysRemember + '?subject=Product Demo Request');
			
			} else {
			
				$(this).attr('href', alwaysRemember);

			}
			
		});
	
	}
	
	
/* FUNCTION: BROWSER DETECT
------------------------------------------------------------------------ */


	function browser() {
	
		if (navigator.userAgent.match('MSIE 6')) {
		
			browser = "ie6";
			window.location = "/ie6";
			
		} else if (navigator.userAgent.match('MSIE')) {
		
			browser = "ie";
			
		} else if (navigator.userAgent.match('Firefox')) {
		
			browser = "gecko";
			
		} else if (navigator.userAgent.match('AppleWebKit')) {
		
			browser = "webkit";
		
		}
		
		$('body').addClass(browser);

	}
	
	
/* FUNCTION: SET PAGE TITLE
------------------------------------------------------------------------ */
	
	
	function setTitle(section) {
	
		if (browser == "webkit") {
		
			if (section == "Home") {
			
				$('title').text('Resen™');

			} else {
		
				var title = section.substr(0, 1).toUpperCase() + section.substr(1);
				$('title').text(title + ' / Resen™');
			
			}
		
		}
		
	}
	
	
/* FUNCTION: ADD IE7 HELPER SELECTORS
------------------------------------------------------------------------ */


	function selectors() {
	
		if (navigator.userAgent.match('MSIE')) {
		
			$('div.fourths > div > ul > li:nth-child(4n)').addClass('clear');
			
		}
		
	}
	
	
/* FUNCTION: BRAP
------------------------------------------------------------------------ */


	function easterEggs() {
	
	
/* Brap */	
	
	
		var kkeys = [], konami = "66,82,65,80";
		
		$(document).keydown(function(e) {
		
			kkeys.push(e.keyCode);
		
			if (kkeys.toString().indexOf(konami) >= 0) {
		
				$(document).unbind('keydown');

				$('body').append('<div id="brap"></div>');
				var so = new SWFObject('/flash/game.swf', 'game', '850', '350', '9', '#FFFFFF');
				so.addParam('allowScriptAccess', 'always');
				so.addParam('wmode', 'transparent');

				so.write('brap');
		
			}
		
		});
		
		
/* Will */	
	
		
		var kkeys3 = [], konami3 = "67,72,73,76,76,87,73,76,76";
		
		$(document).keydown(function(e) {
		
			kkeys3.push(e.keyCode);
		
			if (kkeys3.toString().indexOf(konami3) >= 0) {
		
				$(document).unbind('keydown');

				$('body').append('<div id="will"></div>');
				
				var windowWidth = $(window).width();
				
				$('#will').animate({ right: windowWidth }, 30000, function() {
				
					$('#will').remove();
				
				});
		
			}
		
		});
		
		
		var kkeys2 = [], konami2 = "87,73,76,76";
		
		$(document).keydown(function(e) {
		
			kkeys2.push(e.keyCode);
		
			if (kkeys2.toString().indexOf(konami2) >= 0) {
		
				$(document).unbind('keydown');

				$('body').append('<div id="will"></div>');
				
				var windowWidth = $(window).width();
				
				$('#will').animate({ right: windowWidth }, 1500, function() {
				
					$('#will').remove();
				
				});
		
			}
		
		});
		
		
/* MTX Discount */	
	
		
		var kkeys4 = [], konami4 = "77,84,88";
		
		$(document).keydown(function(e) {
		
			kkeys4.push(e.keyCode);
		
			if (kkeys4.toString().indexOf(konami4) >= 0 && $('#content.products').length > 0) {
				
				$(document).unbind('keydown');

				var $this = $('#content').find('input[value^=ECT Graphics]').parent();
				
				$this.children('input[name=amount]').val('25.00');
				$this.children('em').html('<del>$50</del> $25 (MTX Discount)').addClass('discount');

		
			}
		
		});
		

	}
	