﻿//------------------------------------------------------------------------------------
// ajaximage.js
// tdinh
//
// javascript file that contains functions used for loading the images dynamically with ajax for the global photo player.
// Code Behind files can pass parameters in via global vars : 
//      GLOBAL_IMAGES_COUNT : number of images in the gallery
//      GLOBAL_AJAX_PATH : path to the ajax server
//      GLOBAL_AJAX_DELIM : the string that speficies the delimeter used to separate the ajax response.
//      GLOBAL_PAGE_TITLE_FIRST : the first part of what will be the page title, combine with PageTitle
//------------------------------------------------------------------------------------
var skipPageTitle = false;
//
// requests the selected image from the ajax server
//
function showSelectedImage(imageIndex) 
{
    if (imageIndex <= GLOBAL_IMAGES_COUNT)
    {

        //
        // clear the current text.  Seems to reset the scroll bars.
        //
        elemCaption = document.getElementById('photo_text');
        if (elemCaption)
        {
            elemCaption.innerHTML = '<p> </p>';
        }
        
        //
        //
        //        
        var relatedSection = document.getElementById('related_section');
        if (relatedSection != null)
        {
            relatedSection.style.display = 'none';
        }

       var ajaxPath = GLOBAL_AJAX_PATH + imageIndex;
       (new AjaxRequest(ajaxPath, null, endRequest)).run();
       
       
    }
}

//
// handle the response from the server...
//
function endRequest(sender, statusCode, rawResponse)
{
    //
    // check for success
    //
    if (statusCode == 200)
    {
        //
        // Split the response into imgInfo array: 
        // 0-Caption, 1-Img URL, 2-Credit, 3-PhotoTitle, 4-Friendly Photo Text, 5-Alt Tag
        //
        var imgInfo = rawResponse.split(GLOBAL_AJAX_DELIM);
        //
        // set the image...alt and title both get set to alt for firefox and ie
        //
        $("#photo_frame img").attr("title", imgInfo[5]);
        $("#photo_frame img").attr("alt", imgInfo[5]);
        $("#photo_frame img").attr("src", imgInfo[1]);

        //
        // set caption
        //
        elemCaption = document.getElementById('photo_text');
        if (elemCaption)
        {
            elemCaption.innerHTML = '<p>' + imgInfo[0] + '</p>';
            elemCaption.style.display='block';
        }        
        
        //
        // photo credit
        //
        elemCredit = document.getElementById('photo_credit');
        if (elemCredit) 
	    {
	        elemCredit.innerHTML = imgInfo[2];
		    elemCredit.style.display='block';
	    }
        
        //
        // title
        //
        elemTitle = document.getElementById('photo_title'); 
	    if (elemTitle) 
	    {
		    elemTitle.innerHTML = imgInfo[3];
		    elemTitle.style.display='block';
	    }
	    
	    
        var relatedSection = document.getElementById('related_section');
        if (relatedSection != null)
        {
            relatedSection.style.display = 'block';
        }	    

		if (!skipPageTitle) {
			document.title = GLOBAL_PAGE_TITLE_FIRST + imgInfo[4];
		}
    }
}



