/**
 * jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 5/25/2009
 * @author Ariel Flesler
 * @version 1.4.2
 *
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
 */
;(function(d){var k=d.scrollTo=function(a,i,e){d(window).scrollTo(a,i,e)};k.defaults={axis:'xy',duration:parseFloat(d.fn.jquery)>=1.3?0:1};k.window=function(a){return d(window)._scrollable()};d.fn._scrollable=function(){return this.map(function(){var a=this,i=!a.nodeName||d.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!i)return a;var e=(a.contentWindow||a).document||a.ownerDocument||a;return d.browser.safari||e.compatMode=='BackCompat'?e.body:e.documentElement})};d.fn.scrollTo=function(n,j,b){if(typeof j=='object'){b=j;j=0}if(typeof b=='function')b={onAfter:b};if(n=='max')n=9e9;b=d.extend({},k.defaults,b);j=j||b.speed||b.duration;b.queue=b.queue&&b.axis.length>1;if(b.queue)j/=2;b.offset=p(b.offset);b.over=p(b.over);return this._scrollable().each(function(){var q=this,r=d(q),f=n,s,g={},u=r.is('html,body');switch(typeof f){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=p(f);break}f=d(f,this);case'object':if(f.is||f.style)s=(f=d(f)).offset()}d.each(b.axis.split(''),function(a,i){var e=i=='x'?'Left':'Top',h=e.toLowerCase(),c='scroll'+e,l=q[c],m=k.max(q,i);if(s){g[c]=s[h]+(u?0:l-r.offset()[h]);if(b.margin){g[c]-=parseInt(f.css('margin'+e))||0;g[c]-=parseInt(f.css('border'+e+'Width'))||0}g[c]+=b.offset[h]||0;if(b.over[h])g[c]+=f[i=='x'?'width':'height']()*b.over[h]}else{var o=f[h];g[c]=o.slice&&o.slice(-1)=='%'?parseFloat(o)/100*m:o}if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],m);if(!a&&b.queue){if(l!=g[c])t(b.onAfterFirst);delete g[c]}});t(b.onAfter);function t(a){r.animate(g,j,b.easing,a&&function(){a.call(this,n,b)})}}).end()};k.max=function(a,i){var e=i=='x'?'Width':'Height',h='scroll'+e;if(!d(a).is('html,body'))return a[h]-d(a)[e.toLowerCase()]();var c='client'+e,l=a.ownerDocument.documentElement,m=a.ownerDocument.body;return Math.max(l[h],m[h])-Math.min(l[c],m[c])};function p(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);


/**
 * jQuery Plugin to obtain touch gestures from iPhone, iPod Touch and iPad, should also work with Android mobile phones (not tested yet!)
 * Common usage: wipe images (left and right to show the previous or next image)
 *
 * @author Andreas Waltl, netCU Internetagentur (http://www.netcu.de)
 * @version 1.0 (15th July 2010)
 */
(function($){$.fn.touchwipe=function(settings){var config={min_move_x:20,wipeLeft:function(){alert("left");},wipeRight:function(){alert("right");},preventDefaultEvents:true};if(settings)$.extend(config,settings);this.each(function(){var startX;var isMoving=false;function cancelTouch(){this.removeEventListener('touchmove',onTouchMove);startX=null;isMoving=false;}function onTouchMove(e){if(config.preventDefaultEvents){e.preventDefault();}if(isMoving){var x=e.touches[0].pageX;var dx=startX-x;if(Math.abs(dx)>=config.min_move_x){cancelTouch();if(dx>0){config.wipeLeft();}else{config.wipeRight();}}}}function onTouchStart(e){if(e.touches.length==1){startX=e.touches[0].pageX;isMoving=true;this.addEventListener('touchmove',onTouchMove,false);}}this.addEventListener('touchstart',onTouchStart,false);});return this;};})(jQuery);


$(function() {

	$('#screenshots li a').fancybox({
			cyclic: false,
			padding: 0,
			centerOnScroll: false,
			overlayShow: false,
			titleShow: false,
			transitionIn: 'fade',
			transitionOut: 'fade',
			showCloseButton: true,
			showNavArrows: true,
			enableEscapeButton: true,
			margin: 70,
	});

	iload('/art/her-go-tagline.png', function() {
			$('#claim').animate({'top' : 0}, 400);
	});

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

	$('#tabs a').click(function() {

			$(this).blur();

			$('#tabs a.active').removeClass('active');
			$(this).addClass('active');

			switch($(this).attr('href').replace('#', '')) {
					case 'tab1':
							var text = 0;
							var indicator = 85;
							break;
					case 'tab2':
							var text = -620;
							var indicator = 285;
							break;
					case 'tab3':
							var text = -1240;
							var indicator = 485;
							break;
					case 'tab4':
							var text = -1860;
							var indicator = 685;
							break;
					case 'tab5':
							var text = -2480;
							var indicator = 850;
							break;
			}

  		$('#text').stop().animate({'left' : text}, 400);
  		$('#indicator').stop().animate({'left' : indicator}, 300);

			return false;

	});

	$('a[href=#extended]').click(function() {

			$('#data').removeClass('hide');
			$('#footer').addClass('extended');

			$(window).scrollTo($('#data'), 300);
			return false;
	});

	$('a[href=#credits]').click(function() {

			$('#data').removeClass('hide');
			$('#footer').addClass('extended');

			$(window).scrollTo($('#data h2.credits'), 300);
			return false;
	});

	$('a[href=#past_releases]').click(function() {

			$('#data').removeClass('hide');
			$('#footer').addClass('extended');

			$(window).scrollTo($('#data h2.past_releases'), 300);
			return false;
	});

	if($.browser.webkit) {

			$('.box').touchwipe({
					wipeRight: function() {
							$('#tabs a.active').parent().prev().find('a').trigger('click');
					},
					wipeLeft: function() {
							$('#tabs a.active').parent().next().find('a').trigger('click');
					}
			});
	}

	$('#newsletter .text')
			.focus(function() {
					if($(this).val() == $(this).attr('title')) $(this).val('');
					$(this).parent().addClass('focus');
			})
			.blur(function() {
					if($(this).val() == '') $(this).val($(this).attr('title'));
					$(this).parent().removeClass('focus');
			});

	$('#newsletter .field').click(function() {
			$(this).find('.text').trigger('focus');
	});

	$('#newsletter .submit')
			.hover(
					function() {
							$(this).parent().addClass('hover');
					},
					function() {
							$(this).parent().removeClass('hover');
					}
			)
			.mousedown(function() {
					$(this).parent().addClass('active');
			})
			.mouseup(function() {
					$(this).parent().removeClass('active');
			});


	$('#ipad').click(function() {
			$('#video').removeClass('hide');
	});

	$('#signup').submit(function() {

			var f = {
					fname : $('input[name=fname]'),
					lname : $('input[name=lname]'),
					email : $('input[name=email]'),
			};

			var fname = f.fname.val();
			var lname = f.lname.val();
			var email = f.email.val();

			if(fname.length == 0 || fname == f.fname.attr('title')) {
					f.email.trigger('blur');
					f.fname.trigger('focus');
					$('#response').removeClass('hide').html('Please enter your first name');
					return false;
			}

			if(lname.length == 0 || lname == f.lname.attr('title')) {
					f.email.trigger('blur');
					f.lname.trigger('focus');
					$('#response').removeClass('hide').html('Please enter your last name');
					return false;
			}

			if(email.length == 0 || email == f.email.attr('title')) {
					f.email.trigger('focus');
					$('#response').removeClass('hide').html('Please enter a valid email address');
					return false;
			}

			var data  = {
					'fname' : fname,
					'lname' : lname,
					'email' : email
			};

			$('#signup').addClass('loading');
			$('#signup .field').attr('disabled', 'disabled').fadeTo(100, .3);

			$.getJSON('/php-signup.php', data, function(response) {

					$('#signup').removeClass('loading');
					$('#signup .field').removeAttr('disabled').fadeTo(100, 1);

					if(response.status == 'error') {
							$('#response')
									.removeClass('hide')
									.html(response.msg);
							return false;
					}

					$('#newsletter .content').html('<div id="signupSuccess"><h2 class="thanks">Thank you for signing up!</h2></div>');

			});

			return false;

	});

	// hover animations
	$('#screenshots li a').hover(
			function() {
					$(this).find('span').stop().animate({'opacity' : 1}, 200);
			},
			function() {
					$(this).find('span').stop().animate({'opacity' : .5}, 200);
			}
	);

	$('#tabs a').hover(
			function() {
					$(this).stop().animate({'opacity' : 1}, 200);
			},
			function() {
					$(this).stop().animate({'opacity' : .5}, 200);
			}
	);

	$('#footer a span').hover(
			function() {
					$(this).stop().animate({'opacity' : 1}, 300);
			},
			function() {
					$(this).stop().animate({'opacity' : 0}, 300);
			}
	);

	$('#corelink').hover(
			function() {
					$('#coretitle').stop().animate({'opacity' : 1}, 150);
			},
			function() {
					$('#coretitle').stop().animate({'opacity' : 0.6}, 150);
			}
	);

	$('#golink').hover(
			function() {
					$('#gotitle').stop().animate({'opacity' : 1}, 150);
			},
			function() {
					$('#gotitle').stop().animate({'opacity' : 0.6}, 150);
			}
	);
});

function iload(src, onload) {
		var image = new Image();
		$(image).attr('src', src).load(onload);
}

if (window.location.search.substring(1) == "international") {
	$('#data').removeClass('hide');
	$('#footer').addClass('extended');
	$(window).scrollTo($('#data'), 300);
}

if (window.location.search.substring(1) == "past_releases") {
	$('#data').removeClass('hide');
	$('#footer').addClass('extended');
	$(window).scrollTo($('#data h2.past_releases'), 300);
}

if(WebKitDetect.mobileDevice() == 'iPod' || WebKitDetect.mobileDevice() == 'iPhone') {
	document.getElementById("viewport").setAttribute('content','width=1024; maximum-scale=1.0; user-scalable=1;');
}

if(WebKitDetect.mobileDevice() == 'iPod' || WebKitDetect.mobileDevice() == 'iPhone' || WebKitDetect.mobileDevice() == 'iPad') {
	// Working around a positioning bug in Mobile Safari by temporarily removing the Lightbox spinners.
	document.write('<style type="text/css">#fancybox-loading { display: none !important; }</style>');
}

