try{Typekit.load();}catch(e){}

// easing 1.3 http://gsgd.co.uk/sandbox/jquery/easing
jQuery.easing['jswing'] = jQuery.easing['swing'];
jQuery.extend( jQuery.easing, {
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
		}
	});

$(document).ready(function() {

	if ( $.url().param('cat') ) {

		$('#sidebar.filters li.' + $.url().param('cat') + ' a').trigger('click');
		
		$('#models .model').hide();
		
		$('#sidebar.filters li').removeClass('current');
		$('#sidebar.filters li.' + $.url().param('cat')).addClass('current');
		
		$('#models .model.' + $.url().param('cat')).show();
		
		}

	$('#model #code').hide(1);
	var seen = {};

	$('#showcase-tags a').each(function() {
		var txt = $(this).text();
		if (seen[txt])
		$(this).remove();
		else
		seen[txt] = true;
		});

	// carousel

	$('#carousel li img, #model ul li > a img').removeAttr('title');
	var focus;

	$('#carousel nav').css('margin-left',  $('#carousel nav').width() / 2 - $('#carousel nav').width() ).show();

	function focusModel() {
		$('#carousel ol').stop().animate({ 'margin-left': -( focus * 325 - 110 ) }, 300, function() {
			$('#carousel ol li.focus .meta').show();
			});
		$('#carousel ol li, #carousel nav a').removeAttr('class').children('.meta').fadeOut(200, function() {
			$(this).removeAttr('style');
			})
		$('#carousel nav a:nth-child(' + focus + '), #carousel ol li:nth-child(' + focus + ')').addClass('focus');
		}

	$('#carousel a[href^="#"]').click( function(e) {
		e.preventDefault();
		});

	var iOS = ( navigator.userAgent.match(/iPad/i) != null || navigator.userAgent.match(/iPhone/i) != null || navigator.userAgent.match(/iPod/i) != null);

	$('#carousel ol li:not(.focus)').live('click', function() {
	
		if (!iOS) {
			focus = ( $('#carousel ol li').index(this)  + 1);
			focusModel();
			}
		
		});

	$('#carousel nav a[href="#"]:not(.focus)').live('click', function() {
	
		if (!iOS) {
			focus = ( $('#carousel nav a').index(this)  + 1);
			focusModel();
			}
		
		});

	if ( $.url().param('model') ) {
		focus = ( $('#' + $.url().param('model') ).index('#carousel ol li') + 1 );
		focusModel();
		}

	var IE = ( $.browser.msie && ( parseInt($.browser.version) == 6 || parseInt($.browser.version) == 7 || parseInt($.browser.version) == 8 ) );

	function loadModel() {

		$('#getsat').show();

		if ( !IE ) {

			$('#similar li:nth-child(-n+8)').show();
			$('#similar li#model-' + target ).addClass('current');
			var id = target;
			var dataString = 'id='+ id;

			$.ajax({
				type: "POST",
				url: '/wordpress/wp-content/themes/sumwise/assets/scripts/data-fetch.php',
				data: dataString,
				dataType: 'json',
				success: function(data) {
					$('#model').prepend(data[3]);
					$('#carousel').addClass('disabled');
					$('#model').removeAttr('class');
					$.scrollTo( '#model', 200 );
					$('#rum').css( 'margin-top', $('#model').height() + 88);
					}
				});

		} else {

			var id = target;
			var dataString = 'id='+ id;

			$.ajax({
				type: "POST",
				url: '/wordpress/wp-content/themes/sumwise/assets/scripts/fallback-fetch.php',
				data: dataString,
				dataType: 'json',
				success: function(data) {
					$('#model').prepend('<img src="' + data[3] + '">');
					$('#carousel').addClass('disabled');
					$('#similar').remove();
					$('#model').removeAttr('class');

					$('#model img').load(function () {
						$('#rum').css( 'margin-top', $('#model').height() + 80);
						});

					}
				});

			}

		}

	var target = '';
	var title;
	var sub;

	$('#carousel .focus .meta > a').live('click', function(e) {

		title = $(this).parent().children('h2').text();
		sub = $(this).parent().children('p').text();
		$('#model > h5').html( title + ' <span>' + sub + '</span>');

		$('#mask').fadeIn(150);
		e.preventDefault();
		target = this.hash.slice(1);
		loadModel();

		$('<iframe class="conversion" id="iframe-open" src="http://sumwise.com/wordpress/wp-content/themes/sumwise/assets/iframe-open.html">').appendTo('body');
		_gaq.push(['_trackEvent', 'Events', 'Conversions', 'Open Model']);

		});

	$('#similar li .meta > a').live('click', function(e) {

		title = $(this).parent().children('h2').text();
		sub = $(this).parent().children('p').text();
		$('#model > h5').html( title + ' <span>' + sub + '</span>');

		e.preventDefault();
		$('#similar li').removeClass('current');
		$(this).closest('li').addClass('current');
		var id = this.hash.slice(1);
		var dataString = 'id='+ id;

		$.ajax({
			type: "POST",
			url: '/wordpress/wp-content/themes/sumwise/assets/scripts/data-fetch.php',
			data: dataString,
			dataType: 'json',
			success: function(data) {
				$('#model iframe').remove();
				$('#model').prepend(data[3]);
				$.scrollTo( '#model', 200 );
				$('#rum').css( 'margin-top', $('#model').height() + 80);
				}
			});

		});

	$('#similar .all').toggle(function(e) {

		e.preventDefault();
		$(this).text('Hide similar models');

		// $.scrollTo( '#similar', 200 );

		$('#similar, #similar li').slideDown(100, function(){
			$('#similar').css('height', 'auto');
			$('#rum').css( 'margin-top', $('#model').height() + 80);
			});

	}, function(e) {

		e.preventDefault();
		$(this).text('View all similar models');

		$('#similar li:nth-child(n+9)').slideUp(100, function(){
			$('#similar').css('height', 128);
			$('#rum').css( 'margin-top', $('#model').height() + 80);
			});

		});

	});

$(window).load(function () {

	// touch functionality

	if ( $.url().param('model') )
		var focus = ( $('#' + $.url().param('model') ).index('#carousel ol li') + 1 );
	else
		var focus = 2;

	var IMG_WIDTH = 325;
	var maxImages= ($('#carousel li').length);
	var speed=500;
	var imgs;

	var swipeOptions= {
		triggerOnTouchEnd : true,	
		swipeStatus : swipeStatus,
		allowPageScroll:"vertical",
		threshold: 75
		}

	$(function() {				
		imgs = $("#carousel ol");
		imgs.swipe( swipeOptions );
		});

	function swipeStatus(event, phase, direction, distance) {

		if( phase=="move" && (direction=="left" || direction=="right") ) {
			var duration=0;
			if (direction == "left")
				scrollImages((IMG_WIDTH * focus) + distance, duration);
			else if (direction == "right")
				scrollImages((IMG_WIDTH * focus) - distance, duration);
			}
		
		else if ( phase == "cancel") {
			scrollImages(IMG_WIDTH * focus, speed);
			}
		
		else if ( phase =="end" ) {
			if (direction == "right")
				previousImage()
			else if (direction == "left")
				nextImage()
				}
		}

	function previousImage() {

		$('body').animate({ scrollTop: $("#carousel").offset().top }, 150);

		focus = Math.max(focus-1, 1);
		scrollImages();
		$('nav a.focus, #carousel li.focus').prev().addClass('focus').next().removeClass('focus');
		}

	function nextImage() {

		$('body').animate({ scrollTop: $("#carousel").offset().top }, 150);

		focus = Math.min(focus+1, maxImages);
		scrollImages();
		$('nav a.focus, #carousel li.focus').next().addClass('focus').prev().removeClass('focus');
		}

	function scrollImages() {

		$('#carousel ol').stop().animate({ 'margin-left': -( focus * 325 - 110 ) }, 300, 'easeOutQuad', function() {
			$('#carousel ol li.focus .meta').show();
			});

		}

	$('#header').css('visibility', 'visible');

	$('#social li a').mouseenter( function() {
		$(this).stop().animate({ 'opacity': 0.8 }, 100 );
		$(this).parent().children('span').stop().css('display', 'block').animate({ 'opacity': 1 }, 10 );
		});
	$('#social li a').mouseleave( function() {
		$(this).stop().animate({ 'opacity': 1 }, 200, function() { $(this).removeAttr('style'); });
		$(this).parent().children('span').stop().delay(100).animate({ 'opacity': 0 }, 200, function() { $(this).removeAttr('style'); });
		});

	$('a.model').click(function() {
		$('a.model').removeClass('active');
		$(this).addClass('active');
		setTimeout(function() {
			$('a.model').removeClass('active');
			}, 3000);
		});

	$('#video .video-link').click(function() {
		$('<iframe class="conversion" id="iframe-video" src="http://sumwise.com/wordpress/wp-content/themes/sumwise/assets/iframe-video.html">').appendTo('body');
		_gaq.push(['_trackEvent', 'Events', 'Conversions', 'Launch Videos']);
		});

	$('#tools-top .cta').click(function() {
		$('<iframe class="conversion" id="iframe-login" src="http://sumwise.com/wordpress/wp-content/themes/sumwise/assets/iframe-login.html">').appendTo('body');
		_gaq.push(['_trackEvent', 'Events', 'Conversions', 'Sumwise Login']);
		});

	$('#sidebar.filters li a').click(function(event) {
		event.preventDefault();
		$('#models .model').hide();
		$('#sidebar.filters li').removeClass('current');
		$(this).parent().addClass('current');
		});
	$('#sidebar.filters li.all a').click(function(event) {
		$('#models .model').show();
		});

	$('#playlist div a').not('.current').click(function(event) {
		event.preventDefault();
		$('#playlist .current iframe').detach().appendTo('#playlist .current');
		$('#playlist iframe').hide(0);
		$(this).parent().children('iframe').show(0);
		$('#playlist div ').removeClass('current');
		$(this).parent().addClass('current');
		});

	//search engine

	function resetFilters() {
		$('#models a.model').show( 0, function() { $(this).removeAttr('style'); });
		$('#search').each ( function() { this.reset(); });
		$('#search, #search button').removeAttr('class');
		}

	$('#search input').focus(function() {
		$(this).parent().addClass('active');
		$(this).val('').quicksearch('#models a.model', {
			//selector: 'th'
			});		
		});

	$('#search button').click( function() { resetFilters(); });

	$('a[href=#register-checked], a[href=#register-unchecked], a[href=#popup], a.fancy, a[href=#playlist]').fancybox({
		'titlePosition'		: 'inside',
		'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'overlayColor'		: '#000',
		'overlayOpacity'	: '0.8'
		});

	$('#commentform').submit(function() {
		$form = $(this).closest('form');
		$email = $('input[name=url]', $form);
		if ($email.val() != $email.attr('data-default')) {
		} else {
			$('input[name=url]', $form).val('');
			}
		});

	$('input[type=text], textarea').each(function() {

		var field = $(this);
		var default_value = field.val();

		field.focus(function() {
			if (field.val() == default_value) {
				field.val('').removeClass('inactive');
				}
			});

		field.blur(function() {
			if (field.val() == '') {
				field.val(default_value).addClass('inactive');
				}
			});

		});

	$("#subscribe").validate({
		invalidHandler: function(form, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				 $("a[href=#popup]").trigger('click');
			} else {
				}
			}
		});

	$('#register').validate({
		invalidHandler: function(form, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
			} else {
				}
			}
		});

	$('#popup').validate({
		invalidHandler: function(form, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
			} else {
				}
			}
		});


	});
