// Logo Blend
function logoBlend() {
	$('#logo a').blend(400);
}

// Link Hover
function linkHover() {
	$('#top-nav li').not('.current').find('a').css({
		color: '#eee'
	});
	
	$('#top-nav li').not('.current').find('a').hover(function() {
		$(this).stop().animate({
			color: '#ef8200'
		}, 200);
	}, function() {
		$(this).stop().animate({
			color: '#eee'
		}, 200);
	});
	
	$('#breadcrumb li').not('.you-are-here').find('a').css({color: '#eee'}).hover(function() {
		$(this).stop().animate({
			color: '#ef8200'
		}, 200);
	}, function() {
		$(this).stop().animate({
			color: '#eee'
		}, 200);
	});
}

// Sidebar Accordion
function sideNavAccordion(page) {
	
	var cfg = ($.hoverintent = {
		sensitivity: 7,
		interval: 150
	});
		
	$.event.special.hoverintent = {
		setup: function() {
			$(this).bind("mouseover", jQuery.event.special.hoverintent.handler);
		},
		teardown: function() {
			$(this).unbind("mouseover", jQuery.event.special.hoverintent.handler);
		},
		handler: function(event) {
			event.type = "hoverintent";
			var self = this,
				args = arguments,
				target = $(event.target),
				cX, cY, pX, pY;
				
			function track(event) {
				cX = event.pageX;
				cY = event.pageY;
			};
			
			pX = event.pageX;
			pY = event.pageY;
			
			function clear() {
				target.unbind("mousemove", track).unbind("mouseout", arguments.callee);
				clearTimeout(timeout);
			}
			function handler() {
				if ( ( Math.abs(pX-cX) + Math.abs(pY-cY) ) < cfg.sensitivity ) {
					clear();
					jQuery.event.handle.apply(self, args);
				} else {
					pX = cX; pY = cY;
					timeout = setTimeout(handler, cfg.interval);
				}
			}
			var timeout = setTimeout(handler, cfg.interval);
			target.mousemove(track).mouseout(clear);
			return true;
		}
	};

	if (page === 'topLevel') {
		$('#side-nav').accordion({
			collapsible: 'true',
			active: 'false',
			event: 'mouseover',
			navigation: 'true'
		});
	} else if (page === 'content') {
		$('#side-nav').accordion({
			animated: 'easeslide',
			navigation: 'true',
			event: 'hoverintent'
		});
	}
}

function theRotator() {
	//Set the opacity of all images to 0
	$('div#rotator ul li').css({opacity: 0.0});
	
	//Get the first image and display it (gets set to full opacity)
	$('div#rotator ul li:first').css({opacity: 1.0});
		
	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('rotate()',6000);
}

function rotate() {	
	//Get the first image
	var current = ($('div#rotator ul li.show')?  $('div#rotator ul li.show') : $('div#rotator ul li:first'));

	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div#rotator ul li:first') :current.next()) : $('div#rotator ul li:first'));	
	
	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
	
};

// Video Thumbnail Carousel
function thumbCarousel() {
	$('#slide-thumbs').jcarousel({
		visible: 5,
		wrap: 'circular'
	});
}

var cOffset;

$(window).resize(function(event) {
	dropForm();
});

// Sweet Drop-down Form
function dropForm() {

	var cPos = $('#contact a').position(),
		cWidth = $('#contact a').outerWidth(),
		contactForm = $('#contact-drop-form'),
		formWidth = contactForm.outerWidth();
		
	cOffset = $('#contact a').offset();
		
	contactForm.css({
		display: 'none',
		opacity: 0,
		left: (cOffset.left - formWidth/2) + cWidth/2 + 'px'
	}).mouseleave(function() {
		$(this).stop().animate({
			top: '20px',
			opacity: 0
		}, function() {
			$(this).css({
				display: 'none',
				opacity: 0//,
			});
		});
	});
	$('li#contact a').bind('hoverintent', function() {
		if (contactForm.css('opacity') == 0) {
			contactForm.css({
				display: 'block',
				top: cOffset.top,
				zIndex: 2
			}).stop().animate({
				top: cOffset.top + 30,
				opacity: 1
			});
		}
	}).click(function() {
		return false;
	});
}

function rkcForm() {

	// Ajax Form Loader
	function loadForm() {
	
		if ($('body').attr('class') === 'demo') {
			$('#container').css({position: 'relative'}).append('<div id="contact-drop-form"/>');
		} else {
			$('body').append('<div id="contact-drop-form"/>');	
		}
		
		$('#contact-drop-form').load('/contact.html #contact-info');
		
	}
	
	// Ajax Form Submitter
	function submitForm() {
		$('form').live('submit', function() {
			if ( $(this).attr('id') == 'freelancer-form' ) {
				return;
			}
			$.post('../send-mail.php', $('#contact-info').serialize(), function() {
				$('body').append('<div id="sent"><h4>Thanks! We\'ll get back to you as soon as possible.</h4></div>');
				$('#sent').css({
					display: 'none',
					opacity: 0
				});
				$('#contact-drop-form').trigger('mouseleave');
				$('#sent')
					.css({
						display: 'block',
						zIndex: 999999999})
					.stop().animate({
						opacity: 1
					})
					.delay(3000)
					.animate({opacity: 0}, function() {
						$(this).remove();
					});
				$('input[type="text"], textarea').val('');
			});
			return false;
		});
	}
	
	loadForm();
	submitForm();

}

function introBoxPage() {
	
	var triggerForm = function triggerForm() { $('#contact a').trigger('mouseover'); }
	
	if ($('#intro-wrap').find('a[href="#contact"]').length == 1 ) {
	
		$('#intro-box').localScroll({
			onAfter: triggerForm
		});
	
	}

	$('.you-are-here').click(function(event) {
		event.preventDefault();
	});
}

// 50% opacity to 100% opacity on hover
function halfToFull(e) {
	$(e).css({opacity: .5}).hover(function() {
		$(this).stop().fadeTo(400, 1);
	}, function() {
		$(this).stop().fadeTo(400, .5);	
	});
}

// Footer "glow streak" hover animation

function glowStreak() {
	
	var footCol1 = $('#foot-col-1'),
		footCol2 = $('#foot-col-2'),
		footCol3 = $('#foot-col-3');
		
	$('#footer').append('<div id="follower" />');
	
	$('#follower').css({
		width: '83px',
		height: '27px',
		position: 'absolute',
		background: 'url(/images/glow.png) no-repeat',
		display: 'block',
		top: '4px',
		left: '8px'
	});
	
	var follower = $('#follower');
	
	footCol1.mouseover(function() {
		
		follower.stop().animate({
			left: '8px'
		});
	});
	
	footCol2.mouseover(function() {
		follower.stop().animate({
			left: '148px'
		});
	});
	
	footCol3.mouseover(function() {
			
		follower.stop().animate({
			left: '321px'
		});
	});
}

// Bookmarklet Background Shift

function bookmarklet() {
	$('a.bookmark-us-btn').mousedown(function(event) {
		event.preventDefault();
		$(this).css({
			backgroundPosition: '0 -33px'
		});
		return false;
	});
	$('a.bookmark-us-btn').mouseup(function(event) {
		event.preventDefault();
		$(this).css({
			backgroundPosition: '0 0'
		});
		return false;
	});
}

function dynamicActiveNavItem() {
	$('#top-nav li').each(function(i) {
		var childHref = $(this).find('a').attr('href');
		if ( location.pathname.search(childHref) !== -1 ) {
			$(this).addClass('current');
		}
	});
}

// Top-Level Page Functions
function topLevelPage() {
	if (!$.browser.msie) {
		logoBlend();
	}
	if ( $('body').hasClass('jobs') ) {
		dynamicActiveNavItem();
	}
	linkHover();
	sideNavAccordion('topLevel');
	thumbCarousel();
	rkcForm();
	dropForm();
	glowStreak();
	$.preloadCssImages();
	introBoxPage();
	$('#ajax-news').empty().load('/news/index.php div.entry-content:eq(0) p:eq(0)', function() {
		$(this).find('img').css({display: 'none', visibility: 'hidden'});
		$(this).append('<p><a href="/news/">Read More...</a></p>');
	});
};

// Content Page Functions
function contentpage() {
	if (!$.browser.msie) {
		logoBlend();
	}
	linkHover();
	sideNavAccordion('content');
	thumbCarousel();
	rkcForm();
	dropForm();
	halfToFull('#breadcrumb');
	introBoxPage();
	glowStreak();
	$.preloadCssImages();
	$('#ajax-news').empty().load('/news/index.php div.entry-content:eq(0) p:eq(0)', function() {
		$(this).find('img').css({display: 'none', visibility: 'hidden'});
		$(this).append('<p><a href="/news/">Read More...</a></p>');
	});
	bookmarklet();
};

function jobsPage() {
	theRotator();
	$('#rotator a').bind('click', function() {
		window.location.href = this.href;
		return false;
	});
	$('#rotator h3').css('cursor', 'pointer').click(function() {
		$('#rotator li.show a').trigger('click');
	});
	$('#jobs-wrap, #jobs-list').localScroll({
		//hash: true
		onAfter: function(elem) {
			var elemId = $(elem).attr('id');

			if ( location.hash !== elemId ) {
				location.hash = elemId;
			}
		}
	});
	
	var open = false;
	$('#form-show-btn').click(function(event) {
		event.preventDefault();
		$('#freelancer-form').stop().slideToggle('fast', function() {
			if ( open === false ) {
				$(this).next('.bevel').stop().animate({
					opacity: 0
				}, 'fast', function() {
					$('#freelancer-form').find('input').eq(0).focus();
					open = true;
				});
			} else {
				
				$(this).next('.bevel').stop().animate({
					opacity: 1
				}, function() {
					open = false;
				});
			}
		});
	});
	
	$('#freelancer-form input, #freelancer-form textarea').focus(function(e) {
		$(this).parent().find('label').addClass('focused');
	}).blur(function() {
		$(this).parent().find('label').removeClass('focused');
	});
	
	var origBorderColor;
	
	$('#fl_url').focus(function(event) {
		var $prefix = $(this).prev('span');
		origBorderColor = $prefix.css('border-color');
		$(this).prev('span').css({
			borderColor: '#ef8200'
		});
	}).blur(function() {
		$(this).prev('span').css({
			borderColor: origBorderColor
		});
	});
	
	$('.success-bubble').click(function() {
		$(this).stop().fadeTo('fast', 0, function() {
			$(this).remove();
		});
	});
}
