/**
 * @see http://github.com/NV/placeholder.js
 */
jQuery.fn.textPlaceholder = function () {

	return this.each(function(){

		var that = this;

		if (that.placeholder && 'placeholder' in document.createElement(that.tagName)) return;

		var placeholder = that.getAttribute('placeholder');
		var input = jQuery(that);

		if (that.value === '' || that.value == placeholder) {
			input.addClass('text-placeholder');
			that.value = placeholder;
		}

		input.focus(function(){
			if (input.hasClass('text-placeholder')) {
				this.value = '';
				input.removeClass('text-placeholder')
			}
		});

		input.blur(function(){
			if (this.value === '') {
				input.addClass('text-placeholder');
				this.value = placeholder;
			} else {
				input.removeClass('text-placeholder');
			}
		});

		that.form && jQuery(that.form).submit(function(){
			if (input.hasClass('text-placeholder')) {
				that.value = '';
			}
		});

	});

};

// preload images: http://stackoverflow.com/questions/476679/preloading-images-with-jquery
$.fn.preload = function() {
    this.each(function(){
        $('<img/>')[0].src = "/images/hi/"+this;
    });
}

$(document).ready(function() {
	
	$("[placeholder]").textPlaceholder();
	
	$('a.iframe').fancybox({
		titleShow: false,
		overlayShow: false,
		width: 600,
		height: 533
	});
	
	// pricing page
	if ($('.main-container').eq(0).attr('id') === 'page-pricing') {
		
		$('#sign-up-now').click(function() {
			$(window).scrollTo($('#pricing-blurb-sign-up-button'),400,{
				onAfter: function() {
					$('#pricing-blurb-sign-up-button').click();
				}
			});
			// return false;
		});
		
		$('#pricing-blurb-sign-up-button').click(function() {
			$(this).slideUp('fast');
			$('#pricing-blurb .simple-form').slideDown('fast',function(){
				// scrollTo the form
				$(window).scrollTo($('#pricing-blurb .simple-form'),400);
			}).find('input:first').focus();
			
			// return false;
		});
		
		if (window.location.hash === "#signup") {
			$('#pricing-blurb-sign-up-button').click();
		};
		
	}
	
	// extras page
	if ($('.main-container').eq(0).attr('id') === 'page-extras') {
		
		// local dev
		// var bmlink = "javascript:(function(){_my_script=document.createElement('SCRIPT');_my_script.type='text/javascript';_my_script.src='http://specify.localhost/javascripts/bookmarklet.js?x='+Math.random();document.getElementsByTagName('head')[0].appendChild(_my_script);})();"; // local dev
		
		// remote dev
		// var bmlink = "javascript:(function(){_my_script=document.createElement('SCRIPT');_my_script.type='text/javascript';_my_script.src='http://173-203-97-67.static.cloud-ips.com/javascripts/bookmarklet.js?x='+Math.random();document.getElementsByTagName('head')[0].appendChild(_my_script);})();"; // staging

		// production
		var bmlink = "javascript:(function(){_my_script=document.createElement('SCRIPT');_my_script.type='text/javascript';_my_script.src='http://specifyapp.com/javascripts/bookmarklet.js?x='+Math.random();document.getElementsByTagName('head')[0].appendChild(_my_script);})();"; // staging
		
		
		$('#bookmarklet').attr('href',"javascript:" + encodeURI(bmlink));		
		$('#bookmarklet').click(function() {
			alert("Drag this bookmark to you browser's bookmark bar.");
			return false;
		});
	};
	
	// tour page
	if ($('.main-container').eq(0).attr('id') === 'page-tour') {
		
		$([
			'tour-1-off.gif',
			// 'tour-1-on.gif',
			// 'tour-2-off.gif',
			'tour-2-on.gif',
			// 'tour-3-off.gif',
			'tour-3-on.gif',
			// 'tour-title-1.gif',
			'tour-title-2.gif',
			'tour-title-3.gif'
		]).preload();
		
		var tourPageNum = 1;
				
		function nextTourPage() {
			if (tourPageNum++ >= 3) {
				showTourPage(1);
			} else {
				showTourPage(tourPageNum);
			}
		}
		function prevTourPage() {
			if (tourPageNum-- <= 1) {
				showTourPage(3);
			} else {
				showTourPage(tourPageNum);
			}
		}
		
		function fixTourScrollerHeight() {
			var newHeight = $('#tour-content-inner .tour-page').eq(tourPageNum-1).height();
			$('#tour-content-scroller').height(newHeight);
		}
		fixTourScrollerHeight();
		
		function showTourPage(pagenum) {
			tourPageNum = pagenum;
			
			// scroll the content
			var left = ((790*pagenum)-790)+'px';
			$('#tour-content-scroller').stop(true).scrollTo({left:left, top:"0"}, 700, {
				onAfter: fixTourScrollerHeight
			});
			
			// move the hand
			var handy = $('#tour-header-right .tour-item').eq(tourPageNum-1).position().top+20;
			$('#tour-hand').animate({top: handy+'px'}, 200);
			
			// switch the menu bubble image
			$('#tour-header-right .tour-item').each(function(index) {
				$(this).find('img').eq(0).attr('src', '/images/hi/tour-'+(index+1)+'-off.gif');
			});
			$('#tour-header-right .tour-item').eq(tourPageNum-1).find('img').eq(0).attr('src', '/images/hi/tour-'+tourPageNum+'-on.gif');
			
			// switch the header image
			$('#tour-header-left img').fadeOut(350, function() {
				$(this).attr("src", "/images/hi/tour-header-"+tourPageNum+".gif");
				$(this).fadeIn(350);
			});
		}
		
		$('#tour-item-1 a').click(function(e) { e.preventDefault(); showTourPage(1); });
		$('#tour-item-2 a').click(function(e) { e.preventDefault(); showTourPage(2); });
		$('#tour-item-3 a').click(function(e) { e.preventDefault(); showTourPage(3); });
		
		$('.tour-content-nav-prev a').click(function(e) {
			e.preventDefault();
			if ($(this).closest('#tour-content-nav-bottom').length > 0) {
				$(window).scrollTo({left:0,top:0},500,{
					onAfter: prevTourPage
				});
			} else {
				prevTourPage();
			};
		});
		$('.tour-content-nav-next a').click(function(e) {
			e.preventDefault();
			if ($(this).closest('#tour-content-nav-bottom').length > 0) {
				$(window).scrollTo({left:0,top:0},500,{
					onAfter: nextTourPage
				});
			} else {
				nextTourPage();
			};
		});
	};

	
});

