﻿//------------------------------------------------------------------------------------
// 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 = true;
//
// requests the selected image from the ajax server
//
function showSelectedImage(imageIndex) 
{
   
	var id = document.getElementById("artid").value;
	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>';
        }  
		elemCopy = document.getElementById('photo_copy');
        if (elemCopy)
        {
            elemCopy.innerHTML = '<p> </p>';
        }   

       var ajaxPath = GLOBAL_AJAX_PATH + imageIndex + '&artid='+id;
		//alert(ajaxPath);
       (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]);
		//alert(imgInfo[1]);
        $("#photo_frame img").attr("src", imgInfo[1]);

       // $("#sm-rt-btn-next a").attr("onclick", showSelectedImage(2));
		var nextIndex = parseInt(imgInfo[2])+1;
		var prevIndex = parseInt(imgInfo[2])-1;
        //
        // set caption
        //
		 $("#imageindex").text(nextIndex);
		 $("#imageindex2").text(nextIndex);
        elemCaption = document.getElementById('photo_text');
        if (elemCaption)
        {
            elemCaption.innerHTML = '<p>' + imgInfo[0] + '</p>';
            elemCaption.style.display='block';
        }  
		elemCopy = document.getElementById('photo_copy');
        if (elemCopy)
        {
            elemCopy.innerHTML = '&copy;&nbsp;' + imgInfo[4];
			elemCopy.style.display='block';
        }
		elemTitle = document.getElementById('photo_title'); 
	    if (elemTitle) 
	    {
		    elemTitle.innerHTML = imgInfo[3];
		    elemTitle.style.display='block';
	    }
		var btnNextPath = "/rt/btn-next.gif";
		var btnPrevPath = "/rt/btn-previous.gif";
		var btnpath = document.getElementById('module');
		if(btnpath)
		{
			if(btnpath.value == "tl")
			{
				btnNextPath = "/tl/tl-back.gif";
				btnPrevPath = "/tl/tl-previous.gif";
			}
		}
		var pn = document.getElementById('pn').value;
		var mlc = document.getElementById('mlc').value;
		elemPrevButton = document.getElementById('sm-rt-btn-previous');
		var prevIndex = parseInt(imgInfo[2])-1;
        if (elemPrevButton)
        {
            if(parseInt(imgInfo[2]) == 0)
			{
				elemPrevButton.innerHTML = '';
			}
			else
			{
				elemPrevButton.innerHTML = '<a href="javascript:void();" onclick="showSelectedImage(' + prevIndex +'); InformWSS(\'' + pn + '\',\'' + mlc + '\'); RefreshAds(); return false;" title="View Previous Image" ><img src="../assets/img' + btnPrevPath + '" alt="View Previous Image" /></a>';
			}
            elemPrevButton.style.display='block';
        } 
		elemNextPrevText =  document.getElementById('sm-rt-nextbtn');
		if (elemNextPrevText)
        {
			var strPrev = "";
			var strNext = "";
			if(GLOBAL_IMAGES_COUNT > nextIndex )
				 strNext = '<a href="javascript:void();" onclick="showSelectedImage(' + nextIndex +'); InformWSS(\'' + pn + '\',\'' + mlc + '\'); RefreshAds(); return false;">Next&raquo;</a>';
			
			if(parseInt(imgInfo[2]) > 0) strPrev = '<a href="javascript:void();" onclick="showSelectedImage(' + prevIndex +'); InformWSS(\'' + pn + '\',\'' + mlc + '\'); RefreshAds(); return false;">&laquo;Previous</a> &nbsp;|&nbsp;';
			
			elemNextPrevText.innerHTML = strPrev + strNext;
		}
        elemNextButton = document.getElementById('sm-rt-btn-next');
		
		
        if (elemNextButton)
        {
            if(GLOBAL_IMAGES_COUNT > nextIndex )
			{
				elemNextButton.innerHTML = '<a href="javascript:void();" onclick="showSelectedImage(' + nextIndex +'); InformWSS(\'' + pn + '\',\'' + mlc + '\'); RefreshAds(); return false;" title="View Next Image" ><img src="../assets/img'+ btnNextPath + '" alt="View Next Image" /></a>';
			}
			else
			{
				elemNextButton.innerHTML = '';
			}
            elemNextButton.style.display='block';
        } 

		var currentItem = document.getElementById("currentItem");
		if(currentItem)
		{
			var selectedImage = document.getElementById("sm-rt-thumbimg-" + currentItem.value);
			
			if(selectedImage)
			{
				$("#sm-rt-thumbimg-" + currentItem.value).removeClass("sm-pp-thumbimg-active");
				$("#sm-rt-thumbimg-" + currentItem.value).addClass("sm-pp-thumbimg");
				$("#sm-rt-thumbimg-" + parseInt(imgInfo[2])).removeClass("sm-pp-thumbimg");
				$("#sm-rt-thumbimg-" + parseInt(imgInfo[2])).addClass("sm-pp-thumbimg-active");
			}
			currentItem.value = parseInt(imgInfo[2]);
		}
        //
        // 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';
	    }*/

		if (!skipPageTitle) {
			document.title = GLOBAL_PAGE_TITLE_FIRST + imgInfo[4];
		}
    }
}



