// count how many segs we have for javascript goodness
segs = window.location.pathname.substr(1).split('/');
// strip an empty seg of the end which could be caused by a trailing slash
if(segs[segs.length - 1] == ''){segs.pop();}
// remove index.php from the segs
if (segs[0] == 'index.php'){segs.shift();}

if(segs[0] == 'screencasts'){
	// cache the screencast elements jquery object
	$screencasts = $('#screencasts-list li');
	// add an event handler to each of the tabs
	$screencastTriggers = $("#screen-cast-filter a");
	$screencastTriggers.click(function(){ 
		//return filterElementsByClass(this.hash.substr(1), $screencastTriggers, $screencasts);
	}).parent().show();

	// if we have more than one segment... this isn't the index page
	if(segs.length > 1){
		// filter the screencasts and find the first selected element and update the tab
		$screencasts
			.filter('.selected')
			.each(function(index){
				filterElementsByClass(this.className.match(/cat-([a-z-]+)/)[0], $screencastTriggers, $screencasts);
			});
	}
	if(window.location.hash != '') { filterElementsByClass(window.location.hash.substr(1), $screencastTriggers, $screencasts); }
}

if(segs[0] == 'templates' || segs[1] == 'integrations'){

	// cache the screencast elements jquery object
	$emailTemplates = $('.filter-targets > *');
	// add an event handler to each of the tabs
	$emailTemplateTriggers = $(".filter-triggers a");
	$emailTemplateTriggers.click(function(){ 
		return filterElementsByClass(this.hash.substr(1), $emailTemplateTriggers, $emailTemplates); 
	}).parent().show();

	if(window.location.hash != '')
	{
		filterElementsByClass(window.location.hash.substr(1), $emailTemplateTriggers, $emailTemplates); 
	}

	var shadowbox_extra_width = 0;
	var shadowbox_extra_height = 0;
	Shadowbox.init({
		overlayColor: 'transparent',
		overlayOpacity: 1,
		viewportPadding: 30,
		displayNav: true,
		animate: false,
		onOpen: function(el){
			href = $('a[title='+el.title+']').nextAll('a:first').attr('href');
			$("#shadowbox_download_inner a").attr({'href':href});
		},
		onChange:function(el){
			href = $('a[title='+el.title+']').nextAll('a:first').attr('href');
			$("#shadowbox_download_inner a").attr({'href':href});
		}
	});
	$("#shadowbox_counter").insertAfter($("#shadowbox_nav"));
	$("#shadowbox").addClass("templates");
}

if(segs[0] == 'gallery' || segs[0] == 'blog'){
	var shadowbox_extra_width = 0;
	var shadowbox_extra_height = 0;
	Shadowbox.init({
		overlayColor: 'transparent',
		overlayOpacity: 1,
		viewportPadding: 30,
		displayNav: true,
		animate: false
	});
	$('#shadowbox_download_inner').remove();
	$('#comments-anchor').click(function() {
		$('#comments').focus();
		$('#comment').focus();
		return false;
	});
}

if(segs[0] == 'features')
{
	var shadowbox_extra_width = 194;
	var shadowbox_extra_height = 0;
	Shadowbox.init({
		overlayColor: 'transparent',
		overlayOpacity: 1,
		viewportPadding: 30,
		displayNav: false,
		displayCounter: true,
		counterType: "skip",
		animSequence: 'sync',
		animate: false,
		onChange: function(gallery)
		{
			$('#shadowbox_item_summary').text($("span", gallery.el).text()).show();
		},
		onOpen: function(gallery)
		{
			$('#shadowbox_item_summary').text($("span", gallery.el).text()).show();
		}
	});
	$('#shadowbox_download_inner, #shadowbox_nav').remove();
}

// filter the screencast
function filterElementsByClass(className, triggers, targets){
	triggers.parent().removeClass('selected');
	triggers.filter("[href=#"+className+"]").parent().addClass('selected');
	if(className == "all"){
		targets
			.fadeIn("fast");
	} else {
		targets
			.hide()
			.filter('.'+className)
			.fadeIn('fast');
	}
	return false;
}

// registration validation and submission
/*

var $email = $("#subscribe-form input#z-z");
var emailReg = /^([\w-+\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
var $emailFormDiv = $('#subscribe-form .text');
$email.removeAttr('disabled');

$('#subscribe-form').submit(function() {
	var emailToVal = $email.val();
	$(".error", $(this)).remove();
	var hasError = false;
	if(emailToVal == '') {
		$emailFormDiv.after('<em class="error">You forgot to enter your email address.</em>');
	} else if(!emailReg.test(emailToVal)) {
		$emailFormDiv.after('<em class="error">The email address you entered is invalid.</em>');
	}
	else {
		//$('#subscribe-form').submit();

		$('#subscribe-form').ajaxForm({
			type: "POST",
			dataType: 'json',
			beforeSubmit: function(){
				console.log("before send");
				$('<span class="loading">loading</span>').insertAfter('input#subscribe');
				$email.attr({'disabled': 'disabled'});
			},
			complete: function() {
				console.log("complete");
				$email.removeAttr('disabled');
			},
			success: function(ret) {
				console.log("success");
				$('.loading').remove();
				console.log(ret.Code);
				switch(ret.Code)
				{
					case 0:
						$email.val('');
						$('<span class="success">success</span>')
							.insertAfter('input#subscribe')
							.animate({opacity: 2.0}, 1000)
							.fadeOut('slow', function() {
								$(this).remove();
								$('#subscribe-form')
								.hide()
								.css({width:'350px'})
								.html('<h5>Congratulations!</h5><p>You just joined thousands of designers that get our tips on email marketing plus the skinny on the latest at Campaign Monitor. You&rsquo;ll hear from us in the next month or so.</p>')
								.fadeIn();
							});
					break;
					default:
						$email.addClass('error');
						$('<em class="error" title="'+ret.Message+'">'+ret.Message+'</em>').insertAfter($email);
					break;
				}
			},
			error: function() {
				$email.addClass('error');
				$('<em class="error" title="There was an issue connecting. Please try again.">There was an issue connecting. Please try again.</em>').insertAfter($email);
				$('.loading').remove();
			}
		});
		params = {
			'ApiKey': '589d682e396577285bdd6a493f25ee83',
			'ListID': 'ae21b89395641a35',
			'Email': emailToVal,
			'Name': ''
		};	
		$.ajax({
			url: 'http://api.createsend.com/api/api.asmx/Subscriber.Add',
			type: "GET",
			dataType: "xml",
			data: $.param(params),
			beforeSend: function(){
				$('<span class="loading">loading</span>').insertAfter('input#subscribe');
				$("#z-z").attr({'disabled': 'disabled'});
			},
			complete: function() {
				$("#z-z").removeAttr('disabled');
			},
			success: function(xml) {
				$('.loading').remove();
				switch($(xml).find('Code:eq(0)').text())
				{
					case '0':
						$('<span class="success">success</span>')
							.insertAfter('input#subscribe')
							.animate({opacity: 2.0}, 1000)
							.fadeOut('slow', function() {
								$(this).remove();
								$('#subscribe-form')
								.hide()
								.css({width:'350px'})
								.html('<h5>Congratulations!</h5><p>You just joined thousands of designers that get our tips on email marketing plus the skinny on the latest at Campaign Monitor. You&rsquo;ll hear from us in the next month or so.</p>')
								.fadeIn();
							});
					break;
					default:
						$emailFormDiv.after('<em class="error">'+$(xml).find('Message:eq(0)').text()+'</em>');
					break;
				}
				$("#z-z").val('');
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				// alert(textStatus);
				$emailFormDiv.after('<em class="error">There was an issue connecting. Please try again.</em>');
				$('.loading').remove();
			}
		}); 
	}
	return false;
});
*/

// setup external links
$('a[@rel=external nofollow]').click(function() {
	$(this).attr('target', 'blank');
});

prettyPrint();