jQuery(function($) {

	// Dropdown Timer
	var dropDownTimer = {};


	// Hide elements
	$('.covertF').fadeTo(1, 0).removeClass('covertF');
	$('.covertH').hide().removeClass('covertH');
	

	// Prepare Overlay
	$('#overlay').hide().fadeTo('fast', 0);
	$('#overlay').fixedPosition({ vpos: 'top', hpos: 'left' });
	
	
	// Wire up Gallery Controls
	$('#gallery-controls .slideshow').click(function() {
		if($(this).html() == "Play Slideshow") {
			$('#gallery-content').trigger('start');
			$(this).html('Pause Slideshow');
		}
		else {
			$(this).html('Play Slideshow');
			$('#gallery-content').trigger('stop');
		}
		return false;
	});

	// Set Search Watermark
	$('#s').watermark('Search Talent');
	
	
	// Set Search Behavior
	$('#searchform').submit(function() {
		$('#bottom-container').fadeOut('slow', function() {
			$.ajax({
				type: 'GET',
				data: 's='+$('#s').val(),
				url: $(this).attr('action'),
				success: function(html) {
					var bottomContent = $('#bottom-text', html);
					$('#bottom-container').html(bottomContent);
					$('#bottom-container').fadeIn('slow', function() {
						if(($.browser.mozilla && $('html').scrollTop() < $('#top-wrapper').position().top) ||
						   (($.browser.safari || $.browser.msie) && $('body').scrollTop() < $('#top-wrapper').position().top)) {
							$.scrollTo('#top-wrapper', 750);
						}
					});
				}
			});
		});
		return false;
	});
	
	
	// Dropdown
	$('#nav ul > li').hover(function() {
		var obj = $(this);
		dropDownTimer = $.timer(250, function() {
			obj.children('ul').slideDown('fast');
		});
	}, function() {
		$.clearTimer(dropDownTimer);
		$(this).children('ul').slideUp('fast');
	});
	
	
	// Handle Pages
	$('.page-link a').live('click', function() {	
		var isNextProfileLink = $(this).parent().hasClass('next');
		var isPrevProfileLink = $(this).parent().hasClass('prev');
		var isMainNavLink = $(this).parents('#nav').length > 0;
	
		// If page link lives in Area 3
		if($(this).parents('#a3').length > 0) {
			$('#a3 ul li a').removeClass('hit');
			$(this).addClass('hit');
		}
		
		var href = $(this).attr('href');
		
		$('#bottom-container').fadeOut('slow', function() {
			$.post(href, { is_async: "true" }, function(data) {
				var topContent = $('#top-text', data);
				if(topContent.children().length > 0)
					$('#top-text-container').fadeOut('fast', function() {
						$(this).html(topContent);
						$(this).fadeIn();
					});
				var bottomContent = $('#bottom-text', data);
				$('#bottom-container').html(bottomContent);
				$('#bottom-container').fadeIn('slow', function() {
					if(isMainNavLink)
						return;
						
					// BEGIN Comment if there is a problem
					if(($.browser.mozilla && $('html').scrollTop() < $('#top-wrapper').position().top) ||
					   (($.browser.safari || $.browser.msie) && $('body').scrollTop() < $('#top-wrapper').position().top)) {
						$.scrollTo('#top-wrapper', 750);
					}
					// END Comment if there is a problem
				});
				
				if(isNextProfileLink || isPrevProfileLink) {
					$('#a3 ul li a').removeClass('hit');
					$("#a3 ul li a[href='"+href+"']").addClass('hit');
				}
			});
		});
		return false;
	});
	
	
	// Handle Category Listing
	$('.category-listing a').live('click', function() {	
		var href = $(this).attr('href');
	
		$('.category-listing a').removeClass('hit');
		$(".category-listing a[href='"+href+"']").addClass('hit');
		
		$('#top-text-container').fadeOut('fast', function() {
			$.post(href, { is_async: "true" }, function(data) {
				$('#top-text-container').html(data);
				$('#top-text-container').fadeIn('fast');
				if($('#category-container').find('li').size() > 10)
					$('#category-container').columnize({ width: 225, height: 200 });
			});
		});
		return false;
	});
	
	
	// Close Gallery Window
	$('#gallery-close a').click(function() {
		hideGallery();
		return false;
	});
	
	
	// Go to Top Link
	$('.top-link a').live('click', function() {
		goToTop(function() {});
		return false;
	});
	
	
	// Show Reel
	$('.reel-link a').live('click', function() {
		$('#gallery-controls').hide();
		
		var href = $(this).attr('href');
		
		goToTop(function() {
			showGallery();
			
			var so = new SWFObject('/wp-content/themes/eta/jwplayer/player.swf','mpl','854','500','9', '#000000');
			so.addParam("wmode", "transparent");
			so.addParam('allowfullscreen','true');
			so.addParam('flashvars','file='+href+'&autostart=true&skin=/wp-content/themes/eta/jwplayer/skins/nacht.swf');
			so.write('gallery-content');
		});
		
		return false;
	});
	
	
	// Show Images
	$('.images-link a').live('click', function() {	
		$('#gallery-controls').show();
		
		var href = $(this).attr('href');

		goToTop(function() {
			$.post(href, { is_async: "true" }, function(data) {
				$('img:first', data).load(function() {
					$('#gallery-content').html($('#bottom-content', data).html());
					showGallery();
					$('#gallery-content').serialScroll({
						items: 'li',
						prev: '#gallery-controls .prev',
						next: '#gallery-controls .next',
						axis: 'x',
						duration: 750,
						force: true,
						constant: false,
						jump: true,
						cycle: true,
						lazy: true,
						stop: true,
						lock: true,
						start: 0, 
						interval: 5000,
						onBefore: function(e, ele, $pane, $items, pos) {
							/**
							 * 'this' is the triggered element 
							 * e is the event object
							 * elem is the element we'll be scrolling to
							 * $pane is the element being scrolled
							 * $items is the items collection at this moment
							 * pos is the position of elem in the collection
							 * if it returns false, the event will be ignored
							 */
						}
					});
					$('#gallery-content').trigger('stop');
				});
			});
		});
		return false;
	});
	
	
	// Start animation after window loads
	$(window).load(function() {
		if(!($.browser.msie && $.browser.version == 6) && !$('body').hasClass('page-id-1796'))
			beginAnimation();
		else
			beginNonAnimation();
	});
	
	
	function hideGallery() {
		$('#gallery-container').slideUp('slow');
		$('#overlay').fadeTo('slow', 0, function() {
			$('#gallery-content').html('');
			$('#gallery-controls .slideshow').trigger('click');
			$('#overlay').hide();
		});
	}
	
	function showGallery() {
		$('#gallery-container').slideDown('slow', function() {
			$('#overlay').show().fadeTo('slow', 0.45);
		});
	}
	
	function beginNonAnimation() {
		$('#top-right-container').show();
		$('#top-text-container').show();
		$('#bottom-container').show();
	}
	
	function beginAnimation() {
		var a1Pos = $('#a1').position();
		var a1Width = $('#a1').width();
		var a2Pos = $('#a2').position();
		var a2Width = $('#a2').width();
		var a3Pos = $('#a3').position();
		var a3Width = $('#a3').width();
		var a4Pos = $('#a4').position();
		var a4Width = $('#a4').width();

		$('#top-text-container, #top-right-container, #bottom-container').hide();
		$('#a1, #a2, #a3, #a4, .b1, .b2').css('width', '0').css('left', '0').fadeTo(1, 0);
		$('#a4').animate({ width: a4Width, left: a4Pos.left, opacity: 1 }, 2200, 'swing', function() {
			$('#top-right-container').fadeIn();
			$('#top-text-container').fadeIn();
			$('#bottom-container').fadeIn();
		});
		$('#a3').animate({ width: a3Width, left: a3Pos.left, opacity: 1 }, 2100, 'swing');
		$('#a2').animate({ width: a2Width, left: a2Pos.left, opacity: 1 }, 1500, 'swing');
		$('#a1').animate({ width: a1Width, left: a1Pos.left, opacity: 1 }, 2800, 'swing');
		$('.b1').animate({ width: '50%', opacity: 1 }, 2200, 'swing');
		$('.b2').animate({ width: '50%', left: $.browser.msie ? '49.99%' : '50%', opacity: 1 }, 2100, 'swing');
	}
	
	function goToTop(callback) {
		if($('body').scrollTop() > 10 || $('html').scrollTop() > 10) {
			$('html, body').animate({
				scrollTop: 0
			}, 750, callback);
		}
		else
			callback();
	}
});