/**
 * Browser detection
 */

safariVersion = null;
if (navigator.userAgent.indexOf('Safari') != -1) {
	versionSearchString = "Version/";
	versionString = navigator.userAgent.substring(navigator.userAgent.indexOf(versionSearchString) + versionSearchString.length);
	
	major = 0;
	minor = 0;
	patch = 0;
	
	while (versionString[0] >= '0' && versionString[0] <= '9') {
		major *= 10;
		major += versionString[0] - '0';
		versionString = versionString.substring(1);
	}
	
	if (versionString[0] == '.') {
		versionString = versionString.substring(1);
		while (versionString[0] >= '0' && versionString[0] <= '9') {
			minor *= 10;
			minor += versionString[0] - '0';
			versionString = versionString.substring(1);
		}
		
		if (versionString[0] == '.') {
			versionString = versionString.substring(1);
			while (versionString[0] >= '0' && versionString[0] <= '9') {
				patch *= 10;
				patch += versionString[0] - '0';
				versionString = versionString.substring(1);
			}
		}
	}
	
	safariVersion = 16 * 16 * 16 * ((major - major % 10) / 10) + 16 * 16 * (major % 10) + 16 * minor + patch;
}

/**
 * Slideshow Arrows
 *
 */
 
$(document).ready(function(){
	var i = 1;
	var ssLink = $("#screenshot-link");
	if (!ssLink.length) {
		return;
	}
	updateScreenshotLink($(".screenshot-wrap img.img1")[0]);
	var active = 1;
	
	function animateImages(current, next, total) {
		var nextImg = $(".screenshot-wrap img.img" + next);
		var currentImg = $(".screenshot-wrap img.img" + current);
		
		nextEnd = "0px";
		if (next < current) {
			// Images scrolling to the right; next image moves in from the left, current image moves off to the right
			nextStart = "-226px";
			currentEnd = "226px";
		} else {
			// Images scrolling to the left; next image moves in from the right, current image moves off to the left
			nextStart = "226px";
			currentEnd = "-226px";
		}
		
		// first, move the next screenshot to the place from which it will be scrolling in
		nextImg.css({left: nextStart});
		
		// next, animate the current one out, and the next one in
		currentImg.animate({left: currentEnd}, 400);
		nextImg.animate({left: nextEnd}, 400, 
			function() { 
				if (next == 1) {
					$(".previous-arrow").css({visibility: "hidden"});
				} else {
					$(".previous-arrow").css({visibility: "visible"});
				}

				if (next == total) {
					$(".next-arrow").css({visibility: "hidden"});
				} else {
					$(".next-arrow").css({visibility: "visible"});
				}
				active = 1;
			}
		);

		// new active screenshot
		i = next;
		
		updateScreenshotLink(nextImg[0]);
	}
	
	function updateScreenshotLink(currentImg) {
		// Adjust the title and the href to match the new image
		ssHref = currentImg.src.replace(".jpg", "-lg.jpg")
		ssLink.attr("href", ssHref);
		
		ssLink.attr("title", currentImg.parentNode.title);
	}
	
	// back arrow starts hidden
	$(".previous-arrow").css({visibility: "hidden"});
	
	$(".previous-arrow").click(function(){
		if (active == 1){
			active = 0;
			var numImg = $(".screenshot-wrap a img").size();
			
			var nextImg = i - 1;
			if (nextImg >= 1) {
				animateImages(i, nextImg, numImg);
			}
		}
		
		return false;
	});

		
	$(".next-arrow").click(function(){
		if (active == 1){
			active = 0;
			var numImg = $(".screenshot-wrap a img").size();
			
			var nextImg = i + 1;
			if (nextImg <= numImg) {				
				animateImages(i, nextImg, numImg);
			}
		}
				
		return false;
	});
	

});




/**
 * Open/Close Balloons
 *
 */
 
function calculateBalloonPositions() {
	$(".left-balloon-link").each(function(){

		// calculate positions
		var topPos = ($(this).offset().top);
		var leftPos = ($(this).offset().left);

		// move to the end of the document
		var balloon = $($(this).attr("href"));
		$('body').append(balloon);

		// position again
		var delta = Math.min(topPos - 85, 0);
		$('body').css('position', 'relative');
		balloon.css('position', 'absolute');
		balloon.css('left', leftPos - balloon.width() - 83);
		balloon.css('top', topPos - 75 - delta);
		balloon.children('.balloon-arrow').css('left', balloon.width() + 53);
		balloon.children('.balloon-arrow').css('top', 73 + delta);
	});

	$(".right-balloon-link").each(function(){

		// calculate positions
		var topPos = ($(this).offset().top);
		var leftPos = ($(this).offset().left);

		// move to the end of the document
		var balloon = $($(this).attr("href"));
		$('body').append(balloon);

		// position again
		var delta = Math.min(topPos - 85, 0);
		$('body').css('position', 'relative');
		balloon.css('position', 'absolute');
		balloon.css('left', leftPos + $(this).width() + 25);
		balloon.css('top', topPos - 75 - delta);
		balloon.children('.balloon-arrow').css('top', 73 + delta);
	});
}

$(document).ready(function() {
	
	// Create pop-up divs
	$(".right-balloon, .left-balloon").each(function() {
		$(this).children().wrapAll('<div class="balloon-content"></div>');
	})
	$(".right-balloon").prepend('<div class="balloon-top"></div><div class="balloon-arrow"></div>');
	$(".right-balloon").append('<div class="balloon-bottom"></div>');
	$(".left-balloon").prepend('<div class="balloon-top"></div>');
	$(".left-balloon").append('<div class="balloon-arrow"></div><div class="balloon-bottom"></div>');
	$(".left-balloon, .right-balloon").hide();
	
	calculateBalloonPositions();
	
	$(".right-balloon-link, .left-balloon-link").click(function(){
		var balloonBlock = $($(this).attr("href"))
		
		if(balloonBlock.is(':hidden')){		
			balloonBlock.fadeIn('fast');
			
			// hide <select> in IE6; 
			if($.browser.msie){
				if($.browser.version == 6.0){
					$('select').css('visibility', 'hidden');
				}
			}
			
		}
		else {
			balloonBlock.fadeOut('fast');
			
			// bring back <select> in IE6; 
			if($.browser.msie){
				if($.browser.version == 6.0){
					$('select').css('visibility', 'visible');
				}
			}
		}
			
		return false;
	});
	
	
	$(".close-balloon").click(function(){
		$(this).parents(".left-balloon, .right-balloon").fadeOut('fast');
		
		if($.browser.msie){
			if($.browser.version == 6.0){
				$('select').css('visibility', 'visible');
			}
		}
					
		return false;
	});

});


/**
 * Comment Preview
 *
 */

if (safariVersion == null || safariVersion >= 0x300) {
 
$(document).ready(function() {	
	$('#comment-name').keyup(function() {
		if($(this).val() != ''){
			$('.comment-preview-head span').html( $(this).val() ); 
		}
		else {
			$('.comment-preview-head span').html( "Fetch visitor" ); 
		}
			
	});
	
	$('#comment-website').keyup(function() {
		var commentWebsite = $(this).val();
		var commentName = $('.comment-preview-head span').html();
		var commentNameA = $('.comment-preview-head span a').html();
		
		if(commentWebsite != ''){
			$('.comment-preview-head span').html( '<a href="'+commentWebsite+'">'+(commentNameA?commentNameA:commentName)+'</a>' ); 
		}
		else {
			$('.comment-preview-head span').html( commentNameA ); 
		}
			
	});
	
	var comment = '';
	$('#comment').keyup(function() {
		comment = $(this).val();
		comment = comment.replace(/\n/g, "<br />")
		   .replace(/\n\n+/g, '<br /><br />');
		$('#comment-preview').html( '<p>'+comment+'</p>' );
	});
});

}

/**
 * Workaround for FireFox legend rendering bugs
 */

$(document).ready(function() {
	if ($.browser.mozilla) {
		$('legend').wrapInner($(document.createElement("span")).attr("class", "moz-legend-wrap"));
	}
})