/**
 * Hanzell JS
 *
 * @author Kyle Robinson Young <kyle at dontkry.com>
 * @copyright Kyle Robinson Young 2011
 */
$(function() {

	// LOGO ROLLOVER
	$('#logo').mouseover(function(){
		$(this).attr('src', '/public/images/logo/logo-black.png');
	}).mouseout(function(){
		$(this).attr('src', '/public/images/logo/logo.png');
	});
	
	// SEAL ROLLOVER
	$('#seal').mouseover(function(){
		$(this).attr('src', '/public/images/logo/seal-rollover.png');
	}).mouseout(function(){
		$(this).attr('src', '/public/images/logo/seal.png');
	});
	
	// JOIN ROLLOVER
	$('.join').mouseover(function(){
		$(this).attr('src', '/public/images/buttons/button-join-rollover.png');
	}).mouseout(function(){
		$(this).attr('src', '/public/images/buttons/button-join.png');
	});
	
	// JOIN TO PURCHASE ROLLOVER
	$('.join-purchase').mouseover(function(){
		$(this).attr('src', '/public/images/buttons/button-join-to-purchase-rollover.png');
	}).mouseout(function(){
		$(this).attr('src', '/public/images/buttons/button-join-to-purchase.png');
	});
	
	// PURCHASE ROLLOVER
	$('.purchase').mouseover(function(){
		$(this).attr('src', '/public/images/buttons/button-purchase-rollover.png');
	}).mouseout(function(){
		$(this).attr('src', '/public/images/buttons/button-purchase.png');
	});

	/** BOUNCY! **/
	$.easing.bouncy = function (x, t, b, c, d) {
		var s = 1.70158;
		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
	}
	$.tools.tooltip.addEffect("bouncy",
		function(done) {
			this.getTip().animate({top: '+=15'}, 1000, 'bouncy', done).show();
		},
		function(done) {
			this.getTip().animate({top: '-=15'}, 250, 'bouncy', function()  {
				$(this).hide();
				done.call();
			});
		}
	);

	// TRACK MOUSE
	var mouse = {x:0,y:0};
	$(window).mousemove(function(e) {
		mouse.x = e.pageX;
		mouse.y = e.pageY;
	})
	
	/** HERO ARROW **/
	//var heroTimer = null;
	var heroTrigger = '#myhero';
	var heroDiv = $('<div id="hero-arrow"></div>');
	$(heroTrigger).after(heroDiv).mousemove(function(e) {
		/*if (heroTimer) {
			clearTimeout(heroTimer);
			heroTimer = 0;
		}*/
		heroDiv.css({
			'top':e.pageY-(heroDiv.height()/2),
			'left':e.pageX-(heroDiv.width()/2)
		}).stop(true).fadeTo(200,.9);
		heroTimer = window.setTimeout(function() {
			heroDiv.stop(true).fadeOut(1000);
		}, 2000);
	});
	heroDiv.click(function(e) {
		e.preventDefault();
		$.scrollTo('1000px', 500, {'axis':'y'});
	});

	/** DYNAMIC SLIDESHOWS W/ ARROWS **/
	/*var ssImages = [];
	$('.slideshow > a').each(function() {
		ssImages.push($(this).attr('href'));
		ssImages.reverse();
		$(this).remove();
	});*/
	$('.slideshow').each(function() {
		var fx = 'fade';
		if ($(this).hasClass('scrollHorz')) {
			fx = 'scrollHorz';
		}
		if ($(this).parent().hasClass('slideshow-wrapper') == false) {
			$(this)
				.wrap('<div class="slideshow-wrapper"></div>')
				.before('<div class="slideshow-left"></div>')
				.before('<div class="slideshow-right"></div>');
		}
		$(this)
		.cycle({
			fx: fx,
			timeout: 0
			/*before: function(curr, next, opts) {
				if (!opts.addSlide) {
					return;
				}
				if (ssImages.length) {
					opts.addSlide('<img src="' + ssImages.pop() + '" />');
				}
			}*/
		});
	});
	
	/** SLIDESHOW NEXT/PREV **/
	$('.slideshow-right').live('click', function() {
		var parent = $(this).parent().find('.slideshow');
		parent.cycle('next');
		$('.slideshow-left').fadeIn();
		return false;
	});
	$('.slideshow-left').live('click', function() {
		var parent = $(this).parent().find('.slideshow');
		parent.cycle('prev');
		return false;
	});

	/** MODALS **/
	$('.modal').overlay({
		mask: {
			color: '#000000',
			loadSpeed: 200,
			opacity: 0.9
		}
	});
	$('.modalme').live('click', function() {
		var href = $(this).attr('href');
		//var close = $('<div class="close">X</div>');
		$('.modal').html('Loading...');
		$('.modal').overlay().load();
		$.get(href, function(res) {
			$('.modal').html(res);
		});
		return false;
	});
	$('.modal .close').live('click', function() {
		$('.modal').overlay().close();
		return false;
	});

	/** TOOLTIPS **/
	jQuery.fn.tipOnBeforeShow = function() {
		var tip = this.getTip();
		var title = tip.text();
		if (title.substr(0, 1) == '#' || title.substr(0, 1) == '.') {
			tip.attr('style', $(title).attr('style'));
			tip.html($(title).html());
		}
		if (tip.find('.tooltip-wrapper').length == 0) {
			tip
			.wrapInner('<div class="tooltip-wrapper"></div>')
			.append('<div class="tooltip-bottom"></div>');
		}
	};
	$(".tipme").tooltip({
		effect: 'bouncy',
		position: 'center left',
		onBeforeShow: jQuery.fn.tipOnBeforeShow
	}).click(function() {
		return false;
	});
	$(".tipmeclick").tooltip({
		effect: 'bouncy',
		position: 'center left',
		events: { def: 'click, mouseleave' },
		onBeforeShow: jQuery.fn.tipOnBeforeShow
	});
	/*$(".tipmouse").tooltip({
		position: 'center left',
		onBeforeShow: jQuery.fn.tipOnBeforeShow,
		onShow: function() {
			var tip = this.getTip();
			tip.css({
				'top': mouse.y-(tip.height())-20,
				'left': mouse.x-(tip.width())+50
			});
		}
	}).click(function() {
		return false;
	});*/
	
});
