// Detect current page filename in order to highlight appropriate main-menu button
var filenameLength = location.pathname.length;
var page = location.pathname.substr(location.pathname.lastIndexOf("/")+1, filenameLength);
if (page=="index" || page=="") { page="home"; }

(function($) {

$(document).ready(function() {
    
	// open links marked external in new windows
	$('a[rel="external"]').click( function() {
		window.open( $(this).attr('href') );
		return false;
	});
	
	// resize all user images to fit user window
	$('.user img').imgscale({
		parent : '.user'
	}); 	
/*
	$('.article .entry img').imgscale({
		parent : '.entry'
	}); 	
*/


	// Add 'current' class for detected page to appropriate main-menu button
	$('ul.nav-main li.' + page + ' a').addClass('current');

	// Add PrettyPhoto to all links with rel=prettyPhoto
	$("a[rel^='prettyPhoto']").prettyPhoto({theme:'facebook', overlay_gallery: false, allow_resize: true, show_title: false});	

	// PrettyPhoto - iFrame config
	$("a[rel^='prettyIframe']").prettyPhoto({theme:'facebook', overlay_gallery: false, allow_resize: false, show_title: true, social_tools: false});	

	// PrettyPhoto - inline config
	$("a[rel^='prettyInline']").prettyPhoto({theme:'facebook', overlay_gallery: false, allow_resize: false, show_title: true, social_tools: false, default_width: 900, default_height: 500});	

	// add PrettyPhoto to all img links in blog posts (gif, png and jpg types only)
	$("#blog .article .entry a img").parent().each(function() {
		var filename = $(this).attr("href");
		var filelength = filename.length;
		var extension = filename.substr(filelength-3, filelength).toUpperCase();
		if (extension=='PNG'||extension=='JPG'||extension=='GIF'||extension=='PEG') {
			$(this).prettyPhoto({theme:'facebook', overlay_gallery: false, allow_resize: true, show_title: false});
		}	
	});
	
	// Who uses? hover animation
	$('.user img[title]').removeAttr('title'); //hide title
	$(".user").hover(
	function(){
		$(this).find('.details').animate({"top": "-=160px"}, 300);
	}, 
	function(){
		$(this).find('.details').animate({"top": "+=160px"}, 300);
	});

	// Home hero hover animation
	$(".hero").hover(
	function(){
		$(this).find('.content').animate({"top": "-=390px"}, 300);
		$(this).find('img.up').fadeOut(300);
		ipad = $(this).hasClass('ipad');
		if (ipad==true) { $('.badge-awards').fadeOut(); }
		
	}, 
	function(){
		$(this).find('.content').animate({"top": "+=390px"}, 300);
		$(this).find('img.up').fadeIn(300);
		ipad = $(this).hasClass('ipad');
		if (ipad==true) { $('.badge-awards').fadeIn(); }
	});
	
	// Carousel functions
	
	var imgNo = 1;
	var imgMax = $('#main .screenshots ul.image-list li').length;
	var carWidth = 620;
	
	$("#main .screenshots ul.image-list").css("width", imgMax*carWidth);
	$("a#carousel-left").hide();
	
	$("a#carousel-right").click(function(){
		if(imgNo!==imgMax) {
			if (imgNo==1) { $("a#carousel-left").fadeIn(); }
			$("ul.image-list").animate({"left": "-=620px"}, 300);
			imgNo++;
			if (imgNo==imgMax) { $("a#carousel-right").fadeOut(); }
		}
		return false;
	});
	$("a#carousel-left").click(function(){
		if (imgNo==imgMax) { $("a#carousel-right").fadeIn(); }
		if(imgNo!==1) {
			$("ul.image-list").animate({"left": "+=620px"}, 300);
			imgNo--;
			if (imgNo==1) { $("a#carousel-left").fadeOut(); }
		}
		return false;
	});
	

	// FAQ slide up & down	
	$("ul.faq p.answer").hide();

	$("ul.faq li h4").each(function()
	{
		var expanded = false;
		
		$(this).click(function()
		{
			if(expanded) {
				$(this).removeClass('expanded');
				$(this).next("p.answer").slideUp(400);
				expanded = false;
			} else {
				$(this).addClass('expanded');
				$(this).next("p.answer").slideDown(400);
				expanded = true;
			}
		});
	});
		
});

})(jQuery);

