$(document).ready(function() {

var assets = 'http://contrastfilms.net/cfweddings/wp-content/themes/cfweddings/assets/';

if (location.href.indexOf('grid=y') != -1) {
	$('<div.>').css({
		'width': '100%',
		'height': '1500px',
		'position': 'fixed',
		'left': '0',
		'top': '0',
		'background': 'url(' + assets + '/img/ui/grid.png) center repeat-y',
	}).appendTo('body');
}

if (location.href.indexOf('message=sent') != -1) {
	$('<div />').css('display', 'none').html('<div id="sent-alert"><h1>Message sent</h1><p>Thanks for your interest. We\'ll be in touch!</p></div>').appendTo('body');
	$('#header').colorbox({
		inline: true,
		href: '#sent-alert',
		opacity: 0.25,
		open: true
	});
}

function asset(path) {
	return(assets + path);
}

var numTestimonials = 0;

$.each($('#footer .praise .testimonials li'), function(i) {
	$(this).attr('id', i);
	numTestimonials ++;
});

var rand = Math.floor(Math.random() * numTestimonials);

$('#footer .praise .testimonials li:eq(' + rand + ')').addClass('current').css('display', 'block');

numTestimonials--;

$('#footer .praise .next').click(function() {
	$('#footer .praise .testimonials li.current').css('display', 'none');
	var cur = parseInt($('#footer .praise li.current').attr('id'));
	if (cur == numTestimonials) {
		$('#footer .praise .testimonials li.current').removeClass('current');
		$('#footer .praise .testimonials li:first-child').addClass('current').css('display', 'block');
	} else {
		$('#footer .praise .testimonials li:eq(' + (cur + 1) + ')').addClass('current').css('display', 'block');
		$('#footer .praise .testimonials li.current:eq(0)').removeClass('current')
	}
	return false;
});

$('#footer .praise .prev').click(function() {
	$('#footer .praise .testimonials li.current').css('display', 'none');
	var cur = parseInt($('#footer .praise li.current').attr('id'));
	if (cur == 0) {
		$('#footer .praise .testimonials li.current').removeClass('current');
		$('#footer .praise .testimonials li:last-child').addClass('current').css('display', 'block');
	} else {
		$('#footer .praise .testimonials li:eq(' + (cur - 1) + ')').addClass('current').css('display', 'block');
		$('#footer .praise .testimonials li.current:eq(1)').removeClass('current');
	}
	return false;
});

var imgRoot = assets + '/img/site/still-';
var slide = 1;

$('.slideshow .controls .next').click(function() {
	if (slide == 5) {
		slide = 1;
	} else {
		slide ++;
	}
	var img = imgRoot + slide + '.jpg';
	$('<img />').attr('src', img).css('display', 'none').appendTo('.slideshow').fadeIn(500, function() {
		$('.slideshow img:eq(0)').remove();
	});
	return false;
});

$('.slideshow .controls .prev').click(function() {
	if (slide == 1) {
		slide = 5;
	} else {
		slide --;
	}
	var img = imgRoot + slide + '.jpg';
	$('<img />').attr('src', img).css('display', 'none').appendTo('.slideshow').fadeIn(500, function() {
		$('.slideshow img:eq(0)').remove();
	});
	return false;
});

var demoHeight = 535;
var demo = '<object width="950" height="535"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=8591953&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=ffffff&amp;fullscreen=1&amp;autoplay=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=8591953&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=ffffff&amp;fullscreen=1&amp;autoplay=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="950" height="535"></embed></object>';

$('.front .intro .button').click(function() {
	$('<div/>').addClass('video').html(demo).appendTo('.front .intro');
	$('.front .intro').animate({ height: demoHeight + 'px' }, 3000);
	$('.front .intro .blurb').fadeOut(2000);
	$('.front img').fadeOut(4000);
	return false;
});

$('.contact .extra-info a').click(function() {
	$('#extra-info').slideDown(500);
	$(this).remove();
});

$('#collections .master .button').colorbox({
	inline: true,
	href: '#master-info',
	opacity: 0.25
});

$('#options .master').colorbox({
	inline: true,
	href: '#master-info',
	opacity: 0.25
});

$('#collections .ultimate .button').colorbox({
	inline: true,
	href: '#ultimate-info',
	opacity: 0.25
});

$('#options .ultimate').colorbox({
	inline: true,
	href: '#ultimate-info',
	opacity: 0.25
});

$('#collections .essential .button').colorbox({
	inline: true,
	href: '#essential-info',
	opacity: 0.25
});

$('#options .essential').colorbox({
	inline: true,
	href: '#essential-info',
	opacity: 0.25
});

function vimeo(id) {
	var moogaloop = $.flash.create({
		width: 930,
		height: 523,
		swf: 'http://www.vimeo.com/moogaloop.swf',
		flashvars: {
			clip_id: id,
			width: 930,
			height: 523,
			autostart: 0
		}
	});
	return moogaloop;
}

var thumbs = 0;

$.getJSON('http://www.vimeo.com/api/v2/album/970933/videos.json?callback=?', function(data) {
	$.each(data, function(i, item) {
		if (i == 0) {
			$('#vimeo .viewer').html(vimeo(item.id))
		}
		var a = $('<a/>');
		a.attr('href', '#vimeo-' + item.id);
		a.html('<img src="' + item.thumbnail_medium + '" alt="' + item.title + '" />');
		var left = 180 * i;
		$('<li />')
		.css('left', left + 'px')
		.append(a)
		.appendTo('#vimeo .thumbnails ul');
		thumbs++;
	});
});

$('#vimeo .thumbnails a').live('click', function() {
	var id = $(this).attr('href');
	id = id.substring(7, id.length);
	$('#vimeo .viewer').html(vimeo(id));
	return false;
});

var panes = thumbs / 4;
var current = 0;

$('#vimeo .next').click(function() {
	$('#vimeo .thumbnails li').animate({ left: '-=700px' }, 500);
	$('#vimeo .prev').removeClass('inactive').addClass('active');
	current++;
	return false;
});

$('#vimeo .prev').click(function() {
	if ($(this).attr('class').indexOf('inactive') == -1 ) {
		$('#vimeo .thumbnails li').animate({ left: '+=700px' }, 500);
		$('#vimeo .next').removeClass('inactive').addClass('active');
		if (current == 1) {
			$('#vimeo .prev').removeClass('active').addClass('inactive');
		}
		current--;
	}
	return false;
});

var okayToSubmit;

$('.contact select#how').change(function() {
	if ($(this).val() == 'Other (specify below)') {
		$(this).parent('div').after($('<div />').html('<input id="ifother" name="ifother" type="text" placeholder="If you chose \'other,\' please specify" />'));
	} else {
		$('#ifother').parent('div').remove();
	}
});

$('.contact #name, .contact #weddate').focusout(function() {
	var url;
	var title;
	if ($(this).val() == '') {
		url = asset('img/ui/x_14x14.png');
		title = 'We need to know your name';
		okayToSubmit = false;
	} else {
		url = asset('img/ui/check_16x13.png');
		title = 'Thanks!';
		okayToSubmit = true;
	}
	$(this).next('.valid').remove();
	$(this).after($('<div />')
		.addClass('valid')
		.text(title)
		.attr('title', title)
		.css('background-image', 'url(' + url + ')')
	);
});

function checkEmail(id) {
	var valid = true;
	var email = $('#' + id).val();
	var user = email.substring(0, email.lastIndexOf('@'));
	var domain = email.substring(email.lastIndexOf('@') + 1, email.lastIndexOf('.'));
	var tld = email.substring(email.lastIndexOf('.') + 1, email.length);
	var arr = [user, domain, tld];
	if (email.indexOf('@') == -1 || email.indexOf('.') == -1) {
		valid = false;
	}
	$.each(arr, function() {
		if ( this == '' ) {
			valid = false;
		}
	});
	return valid;
}

$('.contact #email').focusout(function () {
	var url;
	var title;
	if (checkEmail('email') == false) {
		url = asset('img/ui/x_14x14.png');
		title = 'We need a valid email address';
		okayToSubmit = false;
	} else {
		url = asset('img/ui/check_16x13.png');
		title = 'Thanks!';
		okayToSubmit = true;
	}
	$(this).next('.valid').remove();
	$(this).after($('<div />')
		.addClass('valid')
		.text(title)
		.attr('title', title)
		.css('background-image', 'url(' + url + ')')
	);
});

if ($('.contact #name').val() == '' || $('.contact #email').val() == '') {
	okayToSubmit = false;
}

$('.contact #contact-form').submit(function() {
	if (okayToSubmit) {
		return true;
	} else {
		return false;
	}
});

var twitterUrl = 'http://twitter.com/statuses/user_timeline/cfwed.json?count=20&callback=?';

$.getJSON(twitterUrl, function(data) {
	$.each(data, function(i, tweet) {
		if (tweet.text.substring(0, 1) != '@' && $('#footer .tweets li').length < 1) {
			var username = tweet.user.screen_name;
			// Replacement method from Twitter Blogger script (http://twitter.com/javascripts/blogger.js)
			var status = tweet.text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
		      return '<a href="'+url+'">'+url+'</a>';
		    }).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
		      return reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
		    });
			var html = '<p class="entry-content">' + status + '</p> <p class="entry-time"><a href="http://twitter.com/' + username + '/statuses/' + tweet.id +'">' + relative_time(tweet.created_at) + '</a> from ' + tweet.source + '</p>';
			$('<li/>').html(html).appendTo('#footer .tweets');
		}
	})
});

// Relative time function from Twitter Blogger script (http://twitter.com/javascripts/blogger.js)
function relative_time(time_value) {
  var values = time_value.split(" ");
  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
  var parsed_date = Date.parse(time_value);
  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
  delta = delta + (relative_to.getTimezoneOffset() * 60);
  if (delta < 60) {
    return 'less than a minute ago';
  } else if(delta < 120) {
    return 'about a minute ago';
  } else if(delta < (60*60)) {
    return (parseInt(delta / 60)).toString() + ' minutes ago';
  } else if(delta < (120*60)) {
    return 'about an hour ago';
  } else if(delta < (24*60*60)) {
    return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
  } else if(delta < (48*60*60)) {
    return '1 day ago';
  } else {
    return (parseInt(delta / 86400)).toString() + ' days ago';
  }
}

});
