/************************************************************
 FLASH DETECTION 
************************************************************/ 
// Globals
// Major version of Flash required
var requiredMajorVersion = 9;
// Minor version of Flash required
var requiredMinorVersion = 0;
// Minor version of Flash required
var requiredRevision = 115;
// ----------------------------------------------------------


/************************************************************
 SITE TOOLS
************************************************************/ 

var opm_temp = window.location.href.split("?"); // get base url
var opm_base = opm_temp[0];
var q_count = 15; // number of help questions (this seems like something that could be dynamic)


// functions for opening new tool windows
function addPhoto(photoId){
    window.location = opm_base + '?action=main.addPhotoOPM';
}

function sharePhoto(photoId){
    window.location = opm_base + '?action=main.emailPhoto&event_photo_id=' + photoId + '&' + urlSession;
}

function enlargePhoto(photoId){	
    window.location = opm_base + '?&action=main.enlargePhoto&event_photo_id=' + photoId + '&' + urlSession;
}

function printPhoto(photoId){
    window.location = opm_base + '?action=main.enlargePhoto&event_photo_id=' + photoId + '&' + urlSession;
}

function help(photoId){
    window.location = opm_base + '?action=main.help' + '&' + urlSession;
}

// toggles answer div on help page (should be jquery'd)
function getAnswer(id){
	for (i = 0; i < q_count; i++)
	{
		document.getElementById('help_a' + (i+ 1)).style.display = 'none';
	}
	document.getElementById(id).style.display = 'block';
}

// auto popluates id form 
function autotab(original, destination){
	if (original.getAttribute && original.value.length == original.getAttribute("maxlength")){
		destination.focus();
		}
}

// popup window code
function myPopup(url,windowname,w,h,x,y){
			window.open(url,windowname,"resizable=no,toolbar=no,scrollbars=no,menubar=no,status=no,directories=no,width="+w+",height="+h+",left="+x+",top="+y+"");
}

/************************************************************
NO FLASH
************************************************************/
var aGallery = new Array(), 
    nf_index = 0, 
    thumbsPerPage = 6,
    currentPage=0,
    totalPages,
    currentId,
    detailHeight=480;

var urlSession = "";

function setUrlSession( currentSession ){
    urlSession=currentSession;
}

// inits the gallery
function setGallery(){
    currentId=aGallery[nf_index].id; //sets id for tool links
    totalPages = Math.ceil(aGallery.length / thumbsPerPage); 
    var pageCount = 'Page ' + (currentPage + 1) + ' of ' + totalPages;
    $("#page-count").html(pageCount); // puts page count into pagination control
 	setThumbs();
 	$("#noflash-detail").fadeTo("fast", .01, function () {setDetail(nf_index)});
}

// creates the thumbs
function setThumbs(){
	// empties container
    $("#noflash-thumbs").empty();    
    var thumbCount = thumbsPerPage;
    // finds how many thumbs go on the last page if it is not full
    if (currentPage == totalPages - 1 && aGallery.length % thumbsPerPage != 0)
    {
    	thumbCount = aGallery.length % thumbsPerPage;
    } 
    // pushes thumbs into container
    for (j=0; j<thumbCount; j++){
    	var x = j + (currentPage * thumbsPerPage);
 		var temp = "<li id='thumb" + x + "'><a href='#' onclick='fadeDetail(" + x + "); return false;'><img src='" + imageBaseURL + aGallery[x].thumbURL + "' ></a></li>";
 		$("#noflash-thumbs").append(temp);
 	} 
 	$("#noflash-thumbs").fadeTo("slow", 1);
  	 	
}

// ugly function used to chain detail animation
function fadeDetail(image){
	$("#noflash-detail").fadeTo("fast", .01, function () {setDetail(image)});
}

// sets the selected image's thumb to #ccc NOTE: might make this a dynamic var, but would then need that defined in the head file. 
function setThumbBg(image){
	$('#noflash-thumbs li').css('background-color', 'transparent')
    $(eval("'#thumb" + image + "'")).css('background-color', '#cca')
}

// makes the large thumb
function setDetail(image){
    nf_index = image;
    setThumbBg(image);
    currentId = aGallery[image].id;
    var temp = "<img src='" + imageBaseURL + aGallery[image].fullURL + "&t=" + detailHeight + "'>";
    $("#noflash-detail").html(temp);
    $("#noflash-detail").fadeTo("slow", 1);
}

// pagination control functions
function nfBack(){
    if (currentPage > 0)
    {
        currentPage --;
        var pageCount = 'Page ' + (currentPage + 1) + ' of ' + totalPages;
        $("#page-count").html(pageCount);
        $("#noflash-thumbs").fadeTo("fast", .01, function () {setThumbs()});
    }
}

function nfForward(){
    if (currentPage != totalPages - 1)
    {
        currentPage ++;
        var pageCount = 'Page ' + (currentPage + 1) + ' of ' + totalPages;
        $("#page-count").html(pageCount);
        $("#noflash-thumbs").fadeTo("fast", .01, function () {setThumbs()});
    }			
}
