var screenshotsOpen = false;
var currentScreenshot = "Screenshot-1";
var currentScreenshotn = 1;
var numScreenshots = 1;
var baseUrl = "";
var alt = "";
var newImg;
var newImgn = 0;
var imgw = 1; var imgh = 1;
var needsLoader = true;

$(function() {
	numScreenshots =  $('.page .main .gallery ul > li').length;
	
	if (numScreenshots > 0) {
	    baseUrl =  $('.page .main .gallery ul > li#Screenshot-1 > div > div > a').attr("href");
	    alt =  $('.page .main .gallery ul > li#Screenshot-1 > div > div > a > img').attr("alt");
	    baseUrl = baseUrl.substring(0, baseUrl.length - 1);
	
	    $('.page .main .gallery ul > li > div > div > a').click( function() {
		currentScreenshot = $(this).parent().parent().parent().attr("id");
		currentScreenshotn = parseInt(currentScreenshot.match(/\d+$/)[0]);
		
		if (!screenshotsOpen) { openScreenshots(); }
		
		setScreenshot(currentScreenshotn);
		
		this.blur();
		
		return false;
	    });
	    
	    $(window).resize(function(){
		if (screenshotsOpen) {
		    resizeImg();
		}
	    })
	}
	
	$('.page .main .downloadbutton-hover').css({visibility: "visible", opacity: 0.0});
	$('.page .main .downloadbutton-down').css({visibility: "visible", opacity: 0.0});
	
	$('.page .main .downloadbutton a').hover(function(){
	    $('.page .main .downloadbutton-hover').stop().animate({opacity: 1},{queue: false, duration: 150});
	    $('.page .main .downloadarrow').stop().animate({top: "15px"},{queue: false, duration: 200});
	}, function(){
	    $('.page .main .downloadbutton-hover').stop().animate({opacity: 0},{queue: false, duration: 2000});
	    $('.page .main .downloadbutton-down').stop().animate({opacity: 0},{queue: false, duration: 100});
	    $('.page .main .downloadarrow').stop().animate({top: "0px"},{queue: false, duration: 200});
	})
	
	$('.page .main .downloadbutton a').mousedown(function(){
	    $('.page .main .downloadbutton-down').stop().animate({opacity: 1},{queue: false, duration: 100});
	})
	$('.page .main .downloadbutton a').mouseup(function(){
	    $('.page .main .downloadbutton-down').stop().animate({opacity: 0},{queue: false, duration: 100});
	})
});

function openScreenshots()
{
    screenshotsOpen = true;
    $('.page .main .gallery ul > li > div > div > a > img').stop().animate({opacity:'0.5'},{queue:false,duration:500});
    $('.page .main .gallery ul > li#'+currentScreenshot+' > div > div > a > img').stop().animate({opacity:'1.0'},{queue:false,duration:100});
    
    if (numScreenshots > 1) {
    
	$('<a href="#" id="PreviousButton" class="nolink"><span>&lt;</span></a>').click(function() {
	    setScreenshot(currentScreenshotn > 1 ? currentScreenshotn - 1 : numScreenshots);
	    this.blur();
	    return false;
	}).prependTo('.page .main .gallery .thumbs-inner').hide().fadeIn(500);
	
	$('<a href="#" id="NextButton" class="nolink"><span>&gt;</span></a>').click(function() {
	    setScreenshot(currentScreenshotn < numScreenshots ? currentScreenshotn + 1 : 1);
	    this.blur();
	    return false;
	}).appendTo('.page .main .gallery .thumbs-inner').hide().fadeIn(500);
	
    } else {
	$('.page .main .gallery ul > li').fadeOut(200);
    }
    $('<a href="#" id="CloseButton" class="nolink"><span>x</span></a>').click(closeScreenshots).appendTo('.page .main .gallery .thumbs-inner').hide().fadeIn(500);
   
    if ($('.page .main .gallery .thumbs').get(0).style.width != "100%") {
	 //Exclude IE
	$('.page .main .gallery .thumbs').stop().animate({left: "50%"}, {queue:false, duration:500, easing: "easeInOutQuad"});
	$('.page .main .gallery .thumbs-inner').stop().animate({marginLeft: "-50%"}, {queue:false, duration:500, easing: "easeInOutQuad"});
	$('.page .main .gallery .mainpicture').stop().animate({minHeight: "50px", marginBottom: "10px", marginTop: "10px"}, {queue:false, duration: 100});
   }
    
    //preload nav buttons
    preload("left"); preload("right"); preload("close");
}

function preload(s)
{
    a1 = new Image(); a1.src = "/assets/navigate-"+s+"-hover.png";
    a2 = new Image(); a2.src = "/assets/navigate-"+s+"-down.png";
}

function closeScreenshots()
{
    screenshotsOpen = false;
    $('.page .main .gallery .mainpicture img').fadeOut(200, function() {
	$('.page .main .gallery ul > li > div > div > a > img').stop().animate({opacity:'1.0'},{queue:false,duration:100});
	if ($('.page .main .gallery .thumbs').get(0).style.width != "100%") {
	    //Exclude IE
	    $('.page .main .gallery .thumbs').stop().animate({left: "0%"},{queue:false, duration:400, easing: "easeInOutQuad"});
	    $('.page .main .gallery .thumbs-inner').stop().animate({marginLeft: "-0%"},{queue:false, duration:400, easing: "easeInOutQuad"});
	    $('.page .main .gallery .mainpicture').stop().animate({minHeight: "0px", marginBottom: "0px", marginTop: "0px"},{queue:false,duration:400});
	}
	
	$('.page .main .gallery .thumbs #PreviousButton').fadeOut(200, function() { $(this).remove(); });
	$('.page .main .gallery .thumbs #NextButton').fadeOut(200, function() { $(this).remove(); });
	$('.page .main .gallery .thumbs #CloseButton').fadeOut(200, function() { $(this).remove(); });
	
	if (numScreenshots == 1) $('.page .main .gallery ul > li').fadeIn(500);
    });
    return false;
}

function setScreenshot(n)
{
    newImgn = n;
    currentScreenshotn = n;
    currentScreenshot = "Screenshot-" + n;
    if ($('.page .main .gallery .mainpicture img').length > 0) {	
	    $('.page .main .gallery .mainpicture img').fadeOut(200, function() {
		$('.page .main .gallery .mainpicture img').remove();
		loadNewScreenshot();
	    });
    } else loadNewScreenshot();
}

function loadNewScreenshot()
{
    needsLoader = true;
    setTimeout(showLoader,50);
    var img = new Image();
    $(img)
    .load(function () { 
	$(this).hide();
	newImg = this;
	
	imgw = this.width;
	imgh = this.height;
	
	$('.page .main .gallery ul > li > div > div > a > img').stop().animate({opacity:'0.5'},{queue:false,duration:500});
	$('.page .main .gallery ul > li#'+currentScreenshot+' > div > div > a > img').stop().animate({opacity:'1.0'},{queue:false,duration:400});
	
	viewNewScreenshot();
	
    }).attr('src', baseUrl + (newImgn + "")).attr('alt', alt);
}

function showLoader()
{
    if (needsLoader) {
	needsLoader = false;
	$('.page .main .gallery .mainpicture').addClass("loading");
    }
}

function viewNewScreenshot()
{
    needsLoader = false;
    resizeImg();
    if ($('.page .main .gallery .mainpicture img').length > 0) { $('.page .main .gallery .mainpicture img').remove(); }
    $('.page .main .gallery .mainpicture').removeClass("loading").append(newImg);
    $(newImg).fadeIn(200);
}

function resizeImg()
{
    if ($('.page .main .gallery .thumbs').get(0).style.width != "100%") //Exclude IE
	$('.page .main .gallery .mainpicture').stop().animate({minHeight: (Math.min($('.page .main .gallery').width() - 40, imgw)/imgw)*imgh}, 100);
}
