/**
 * Yay! JS!
 */

function pageInit() {

	// attach automagic class-named events
	$("a.external", this).click(function() {
		this.target = "_blank";
		trackLink(this);
	});

	$("a.print", this).click(function() {
		printThis(this);
	});

	setupThumbnailLinks();

	// setup the slider
	if ($('.anythingSlider')) {
		$('.anythingSlider').anythingSlider( {
			easing : "easeInOutExpo", // Anything other than "linear" or
			// "swing" requires the easing plugin
			autoPlay : true, // This turns off the entire FUNCTIONALY,
			// not
			// just if it starts running or not.
			delay : 5000, // How long between slide transitions in
			// AutoPlay
			// mode
			startStopped : false, // If autoPlay is on, this can force it
			// to
			// start stopped
			animationTime : 1000, // How long the slide transition takes
			hashTags : true, // Should links change the hashtag in the
			// URL?
			buildNavigation : true, // If true, builds and list of anchor
			// links
			// to link to each slide
			pauseOnHover : true, // If true, and autoPlay is enabled, the
			// show will pause on hover
			startText : "Go", // Start text
			stopText : "Stop", // Stop text
			navigationFormatter : formatText
		// Details at the top of the file on this use (advanced use)
				});

		$("#slide-jump").click(function() {
			$('.anythingSlider').anythingSlider(6);
		});
	}

}

function formatText(index, panel) {
	return index + "";
}

function doItemLink(element) {
	setLoadingState();
	if (element.tagName != 'A') {
		var myElement = element.parentNode;
	} else {
		var myElement = element;
	}

	var pants = myElement.href;
	$.ajax( {
		type : "GET",
		url : '/ajax/get-content',
		data : {
			content : pants
		},
		dataType : "text/html",
		success : function(html) {
			$("#video").removeClass();
			$("#video").html(html);
			setupTitle();
			setupExtraLinks();
			$.scrollTo($('#main_content'), 500, {
				queue : true
			});
		}
	});
}

function setupThumbnailLinks() {
	$('#doNextLink').click(function(e) {
		e.preventDefault();
		doNextLink(this);
	});

	$('#doPrevLink').click(function(e) {
		e.preventDefault();
		doPrevLink(this);
	});

	if ($('#video').html()) {
		$('a.doItemLink').click(function(e) {
			e.preventDefault();
			doItemLink(this);
		});
		setupExtraLinks();
	}
}
function setupTitle() {
	// if a new title came along in the ajax content, set it to the header.
	if ($('#doItemTitle') && $('#itemHeader')) {
		// change the classes
		var segment = $('#doItemSegment').val();
		$('#itemHeaderTop').removeClass();
		$('#itemHeaderTop').addClass('header_top_' + segment); // header_top_
		$('#itemHeader').removeClass();
		$('#itemHeader').addClass('header_' + segment); // header_
		$('#itemHeaderBottom').removeClass();
		$('#itemHeaderBottom').addClass('header_bottom_' + segment); // header_bottom_
		// set the title
		$('#itemHeader').html($('#doItemTitle').html());
		if ($('#doItemExtrasSegment')) {
			$('#doItemExtrasSegment').html(segment);
			$('#doItemMoreMomentsLink').attr('href', "/index/" + segment);
		}
	}
}

function setupExtraLinks() {
	// doItemCommentsLink
	$('#doItemCommentsLink').click(function(e) {
		e.preventDefault();
		doCommentsLink(this);
	});
	// doItemMoreInfoLink
	$('#doItemMoreInfoLink').click(function(e) {
		e.preventDefault();
		doMoreInfoLink(this);
	});
}

function doPrevLink(element) {
	if (element.tagName != 'A') {
		var myElement = element.parentNode;
	} else {
		var myElement = element;
	}

	var pants = myElement.href;
	$.ajax( {
		type : "GET",
		url : '/ajax/get-thumbs',
		data : {
			content : pants
		},
		dataType : "text/html",
		success : function(html) {
			$("#thumbs_nav_holder").html(html);
			setupThumbnailLinks();
			// $.scrollTo($('#thumbs_nav_holder'), 300, {queue:true});
	}
	});

}

function doNextLink(element) {
	if (element.tagName != 'A') {
		var myElement = element.parentNode;
	} else {
		var myElement = element;
	}

	var pants = myElement.href;
	$.ajax( {
		type : "GET",
		url : '/ajax/get-thumbs',
		data : {
			content : pants
		},
		dataType : "text/html",
		success : function(html) {
			$("#thumbs_nav_holder").html(html);
			setupThumbnailLinks();
			// $.scrollTo($('#thumbs_nav_holder'), 300, {queue:true});
	}
	});

}

function doCommentsLink(element) {
	$("#doItemDropZone").removeClass();
	$("#doItemDropZone").addClass('loadingbgblack');

	if (element.tagName != 'A') {
		var myElement = element.parentNode;
	} else {
		var myElement = element;
	}

	var pants = myElement.href;
	$.ajax( {
		type : "GET",
		url : '/ajax/get-comments',
		data : {
			content : pants
		},
		dataType : "text/html",
		success : function(html) {
			$("#doItemDropZone").html(html);
			$("#doItemDropZone").removeClass();
			setupCommentForm();
		}
	});
}

function setupCommentForm() {
	$('#commentSubmit').click(function(e) {
		e.preventDefault();
		postComments(this);
	});
}

function postComments(element) {
	$("#doItemDropZone").removeClass();
	$("#doItemDropZone").addClass('loadingbgblack');
	// disable form
	$("#commentSubmit").attr('disabled', 'disabled');
	// send only the comment box contents, the rest is figured out by url &
	// something.
	$.ajax( {
		type : "POST",
		url : '/ajax/post-comment',
		data : $("#commentForm").serialize(),
		dataType : "text/html",
		success : function(html) {
			$("#doItemDropZone").html(html);
			$("#doItemDropZone").removeClass();
			setupCommentForm();
		}
	});
}

function doMoreInfoLink(element) {
	$("#doItemDropZone").removeClass();
	$("#doItemDropZone").addClass('loadingbgblack');

	if (element.tagName != 'A') {
		var myElement = element.parentNode;
	} else {
		var myElement = element;
	}

	var pants = myElement.href;
	$.ajax( {
		type : "GET",
		url : '/ajax/get-more-info',
		data : {
			content : pants
		},
		dataType : "text/html",
		success : function(html) {
			$("#doItemDropZone").html(html);
			$("#doItemDropZone").removeClass();
		}
	});
}

function setLoadingState() {
	$("#video").html('');
	$("#video").addClass('loadingbgblack');
}

function trackLink(event) {
	if (event.element(event).tagName != 'A') {
		var myElement = event.element(event).parentNode;
	} else {
		var myElement = event.element(event);
	}
	try {
		pageTracker._trackPageview('/external/' + encodeURI(myElement));
	} catch (e) {
	}
}

function pageViewLink(page) {
	try {
		pageTracker._trackPageview(encodeURI(page));
	} catch (e) {
	}
}

function printThis(e) {

}
$(document).ready(pageInit);
// Event.observe(window, 'load', pageInit);
