function GetThumbnailAreaWidth() { return $('#thumbnailarea').width(); }
function GetNumImagesPerRow() { return Math.floor( GetThumbnailAreaWidth() / 120 ); }
function GetNumImagesPerPage() { return 4 * GetNumImagesPerRow(); }
function GetMainImageAreaWidth() { return $("#mainimagearea").width(); }
function GetMainImageAreaHeight() { return $(window).height() - 300; }

function StartSpinners() {
    $(".spinner").html( "<img src='/images/ajax-loader.gif' />" );
}

var timerId = 0;

function ClearTimer() {
    if( timerId != 0 ) {
	clearTimeout( timerId );
	timerId = 0;
    }
}

function SetTimer( tag, curpage, curimgindex, num_pages ) {
    var func;

    if( timerId != 0 ) {
	ClearTimer();
    }
    func = "NextImage( '" + tag + "', " + curpage + ", " + curimgindex + ", " +
	num_pages + ", 1 )";
    timerId = setTimeout( func, 5000 );
}

function toggleAuto( tag, curpage, curimgindex, num_pages ) {
    var id = $("#autocheckbox")[0];
    var auto;
    if( id == null ) {
	auto = 0;
    } else {
	auto = id.checked;
    }
    if( auto ) {
	SetTimer( tag, curpage, curimgindex, num_pages );
    } else {
	ClearTimer();
    }
}

function LoadGallery( tag, curpage ) {
    var w = GetThumbnailAreaWidth();
    var images_per_row = GetNumImagesPerRow();
    var images_per_page = GetNumImagesPerPage();
    var main_w = GetMainImageAreaWidth();
    var main_h = GetMainImageAreaHeight();

    ClearTimer();
    StartSpinners();

    // Initialize the page count area
    $.post( "/ajax.php", {
		"action": "SetPageCount",
		"tag": tag,
		"curpage": curpage,
		"images_per_row": images_per_row,
		"images_per_page": images_per_page },
	    function( data, status ) {
		if( status == "success" ) {
		    $("#pagecount").html( data );
		}
	    },
	    "html" );

    // Initialize the image count area
    $.post( "/ajax.php", {
		"action": "SetImageCount",
		"tag": tag,
		"curpage": curpage,
		"images_per_row": images_per_row,
		"images_per_page": images_per_page,
		"curimgindex": 1,
		"auto": 0 },
	    function( data, status ) {
		if( status == "success" ) {
		    $("#imgcount").html( data );
		}
	    },
	    "html" );

    // Intialize the thumbnail area
    $.post( "/ajax.php", {
     		"action": "SetThumbnails",
		"tag": tag,
		"curpage": curpage,
		"images_per_row": images_per_row,
		"images_per_page": images_per_page },
	    function( data, status ) {
		if( status == "success" ) {
		    $("#thumbnailarea").html( data );
		}
	    },
	    "html" );

    // Initialize the main image
    $.post( "/ajax.php", {
		"action": "SetMainImage",
		"tag": tag,
		"curpage": curpage,
		"images_per_row": images_per_row,
		"images_per_page": images_per_page,
		"curimgindex": 1,
		"main_w": main_w,
		"main_h": main_h },
	    function( data, status ) {
		if( status == "success" ) {
		    $("#mainimagearea").html( data );
		}
	    },
	    "html" );
}

function PrevImage( tag, curpage, curimgindex ) {
    var w = GetThumbnailAreaWidth();
    var images_per_row = GetNumImagesPerRow();
    var images_per_page = GetNumImagesPerPage();
    var main_w = GetMainImageAreaWidth();
    var main_h = GetMainImageAreaHeight();

    ClearTimer();

    if( curimgindex > 1 ) {
	// prev image
	curimgindex--;
	$.post( "/ajax.php", {
		    "action": "SetImageCount",
		    "tag": tag,
		    "curpage": curpage,
		    "images_per_row": images_per_row,
		    "images_per_page": images_per_page,
		    "curimgindex": curimgindex,
		    "auto": 0 },
		function( data, status ) {
		    if( status == "success" ) {
			$("#imgcount").html( data );
		    }
		},
		"html" );
	$.post( "/ajax.php", {
		    "action": "SetMainImage",
		    "tag": tag,
		    "curpage": curpage,
		    "images_per_row": images_per_row,
		    "images_per_page": images_per_page,
		    "curimgindex": curimgindex,
		    "main_w": main_w,
		    "main_h": main_h },
		function( data, status ) {
		    if( status == "success" ) {
			$("#mainimagearea").html( data );
		    }
		},
		"html" );
    } else if( curpage > 1 ) {
	// prev page, last image
	curpage--;
	$.post( "/ajax.php", {
		    "action": "SetPageCount",
		    "tag": tag,
		    "curpage": curpage,
		    "images_per_row": images_per_row,
		    "images_per_page": images_per_page },
		function( data, status ) {
		    if( status == "success" ) {
			$("#pagecount").html( data );
		    }
		},
		"html" );
	$.post( "/ajax.php", {
		    "action": "SetImageCount",
		    "tag": tag,
		    "curpage": curpage,
		    "images_per_row": images_per_row,
		    "images_per_page": images_per_page,
		    "curimgindex": images_per_page,
		    "auto": 0 },
		function( data, status ) {
		    if( status == "success" ) {
			$("#imgcount").html( data );
		    }
		},
		"html" );
	$.post( "/ajax.php", {
     		    "action": "SetThumbnails",
		    "tag": tag,
		    "curpage": curpage,
		    "images_per_row": images_per_row,
		    "images_per_page": images_per_page },
		function( data, status ) {
		    if( status == "success" ) {
			$("#thumbnailarea").html( data );
		    }
		},
		"html" );
	$.post( "/ajax.php", {
		    "action": "SetMainImage",
		    "tag": tag,
		    "curpage": curpage,
		    "images_per_row": images_per_row,
		    "images_per_page": images_per_page,
		    "curimgindex": images_per_page,
		    "main_w": main_w,
		    "main_h": main_h },
		function( data, status ) {
		    if( status == "success" ) {
			$("#mainimagearea").html( data );
		    }
		},
		"html" );
    } else {
	// do nothing
    }
}


function NextImage( tag, curpage, curimgindex, num_pages, fromTimer ) {
    var w = GetThumbnailAreaWidth();
    var images_per_row = GetNumImagesPerRow();
    var images_per_page = GetNumImagesPerPage();
    var main_w = GetMainImageAreaWidth();
    var main_h = GetMainImageAreaHeight();

    ClearTimer();

    if( curimgindex < images_per_page ) {
	// next image
	curimgindex++;
	$.post( "/ajax.php", {
		    "action": "SetImageCount",
		    "tag": tag,
		    "curpage": curpage,
		    "images_per_row": images_per_row,
		    "images_per_page": images_per_page,
		    "curimgindex": curimgindex,
		    "auto": fromTimer },
		function( data, status ) {
		    if( status == "success" && data != "" ) {
			$("#imgcount").html( data );
		    }
		},
		"html" );
	$.post( "/ajax.php", {
		    "action": "SetMainImage",
		    "tag": tag,
		    "curpage": curpage,
		    "images_per_row": images_per_row,
		    "images_per_page": images_per_page,
		    "curimgindex": curimgindex,
		    "main_w": main_w,
		    "main_h": main_h },
		function( data, status ) {
		    if( status == "success" && data != "" ) {
			$("#mainimagearea").html( data );
		    }
		},
		"html" );
	if( fromTimer ) {
	    SetTimer( tag, curpage, curimgindex, num_pages );
	}
    } else if( curpage < num_pages ) {
	// next page, first image
	curpage++;
	$.post( "/ajax.php", {
		    "action": "SetPageCount",
		    "tag": tag,
		    "curpage": curpage,
		    "images_per_row": images_per_row,
		    "images_per_page": images_per_page },
		function( data, status ) {
		    if( status == "success" ) {
			$("#pagecount").html( data );
		    }
		},
		"html" );
	$.post( "/ajax.php", {
		    "action": "SetImageCount",
		    "tag": tag,
		    "curpage": curpage,
		    "images_per_row": images_per_row,
		    "images_per_page": images_per_page,
		    "curimgindex": 1,
		    "auto": fromTimer },
		function( data, status ) {
		    if( status == "success" ) {
			$("#imgcount").html( data );
		    }
		},
		"html" );
	$.post( "/ajax.php", {
     		    "action": "SetThumbnails",
		    "tag": tag,
		    "curpage": curpage,
		    "images_per_row": images_per_row,
		    "images_per_page": images_per_page },
		function( data, status ) {
		    if( status == "success" ) {
			$("#thumbnailarea").html( data );
		    }
		},
		"html" );
	$.post( "/ajax.php", {
		    "action": "SetMainImage",
		    "tag": tag,
		    "curpage": curpage,
		    "images_per_row": images_per_row,
		    "images_per_page": images_per_page,
		    "curimgindex": 1,
		    "main_w": main_w,
		    "main_h": main_h },
		function( data, status ) {
		    if( status == "success" ) {
			$("#mainimagearea").html( data );
		    }
		},
		"html" );
	if( fromTimer ) {
	    SetTimer( tag, curpage, 1, num_pages );
	}
    } else {
	// do nothing
    }
}

function LoadMainImage( tag, curpage, file, h, w, text, curimgindex ) {
    var images_per_row = GetNumImagesPerRow();
    var images_per_page = GetNumImagesPerPage();
    var main_w = GetMainImageAreaWidth();
    var main_h = GetMainImageAreaHeight();

    ClearTimer();
    $.post( "/ajax.php", {
		"action": "SetImageCount",
		"tag": tag,
		"curpage": curpage,
		"images_per_row": images_per_row,
		"images_per_page": images_per_page,
		"curimgindex": curimgindex,
		"auto": 0 },
	    function( data, status ) {
		if( status == "success" ) {
		    $("#imgcount").html( data );
		}
	    },
	    "html" );
    $.post( "/ajax.php", {
		"action": "SetMainImage",
		"tag": tag,
		"curpage": curpage,
		"images_per_row": images_per_row,
		"images_per_page": images_per_page,
		"curimgindex": curimgindex,
		"main_w": main_w,
		"main_h": main_h },
	    function( data, status ) {
		if( status == "success" ) {
		    $("#mainimagearea").html( data );
		}
	    },
	    "html" );
}

function ResizeWindow( tag ) {
//    LoadGallery( tag, 1 );
}
