var xmlHttp = "";
var divTargetElement;
var cindex = 0;
var holdId = 0;

//called from default.aspx
function reposition(){
       var img_scrlup = document.getElementById('tmpimgup');
       var img_scrldown = document.getElementById('tmpimgdown');
       var img_width = img_scrlup.offsetWidth;
       var img_height = img_scrlup.offsetHeight;
       
    // x and y starting and ending coordinates for both up and down arrow
    //   alert(img_height);
       xs_scrlup = leftPos(img_scrlup);
       
       xe_scrlup = xs_scrlup + img_width;
       ys_scrlup = topPos(img_scrlup);
       ye_scrlup = ys_scrlup + img_height;   

      xs_scrldown = leftPos(img_scrldown);; 
      xe_scrldown = xs_scrldown + img_width; 
      ys_scrldown = topPos(img_scrldown);
      ye_scrldown = ys_scrldown + img_height;

//alert(xs_scrldown);
//alert(img_width);
//alert(ys_scrldown);
}

//called from default.aspx specifically - news, events, and announce xsl files
// will call ajax routine to populate the appropriate target element

function GetCMSContent(cid,TargetElement, anchor)
{
    divTargetElement = TargetElement;    
    xmlHttp = ExecuteCall('http://' + location.host + location.pathname + '?cid=' + cid, CallbackMethod);
}

// used in wunewshomepg.xsl, events.xsl, and announce.xsl for populating Tab section of WUdefault
function GetTabContent(typ,cid,xsl,TargetElement, anchor)
{
    divTargetElement = TargetElement;
    //alert('http://' + location.host + location.pathname + '?typ=' + typ + '&cid=' + cid + '&xsl=' + xsl);
    xmlHttp = ExecuteCall('http://' + location.host + location.pathname + '?typ=' + typ + '&cid=' +cid+ '&xsl=' + xsl, CallbackMethod);
}
//  function currently called from an xsl file which dynamically builds onmouseover functionality with CMS content
function ChangePic(imgurl,imgalt,TargetElement, anchor) 
{ 
    newpic='<img src="'+imgurl+'" alt="'+imgalt+'" title="'+imgalt+'"/>';
    document.getElementById(TargetElement).innerHTML = newpic;   
}

function GetBuilding(direction,TargetElement, anchor)
{
    var pos = '';
    try
    {
        cid = document.getElementById('ctl00_ContentBody_buildingIdForIndex').value;
        cindex = findArrayIndex(cid);
        if(direction == 'next')
        {
            if(cindex == cidlist.length-1)
            {
                pos="last";
            }
            else
            {
                cindex++;
                document.getElementById('ctl00_ContentBody_buildingIdForIndex').value = cidlist[cindex];
                if(cindex == cidlist.length-1)
                {
                    pos="last";
                }
            }
        }
        if(direction == 'prev')
        {
            if(cindex == 0)
            {
                pos = "first";
            }
            else
            {        
                cindex--;
                document.getElementById('ctl00_ContentBody_buildingIdForIndex').value = cidlist[cindex];
                if(cindex == 0)
                {
                    pos = "first";
                }
            }
        }
    }
    catch(e)
    {
        alert (e.message);
    }
    
    xmlHttp = '';
    divTargetElement = TargetElement;
    //alert(location.pathname);
   // alert('http://' + location.host + location.pathname + '?cid=' + cidlist[cindex] + '&pos=' + pos);
    xmlHttp = ExecuteCall('http://' + location.host + location.pathname + '?cid=' + cidlist[cindex] + '&pos=' + pos, CallbackMethod);
    
 
}

function findArrayIndex(buildingId)
{ 
    var index = 0;
    for ( i=0; i < cidlist.length; i++ ){
        if(cidlist[i] == buildingId)
        {
            index = i;
        }
    }
    return index;
}

function GetTourItem(direction, anchor) {
    var pos = '';
    try {
        cid = document.getElementById('ctl00_ContentBody_buildingIdForIndex').value;
        cindex = findArrayIndex(cid);
        if (direction == 'next') {
            if (cindex == cidlist.length - 1) {
                pos = "last";
            }
            else {
                cindex++;
                document.getElementById('ctl00_ContentBody_buildingIdForIndex').value = cidlist[cindex];
                if (cindex == cidlist.length - 1) {
                    pos = "last";
                }
            }
        }
        if (direction == 'prev') {
            if (cindex == 0) {
                pos = "first";
            }
            else {
                cindex--;
                document.getElementById('ctl00_ContentBody_buildingIdForIndex').value = cidlist[cindex];
                if (cindex == 0) {
                    pos = "first";
                }
            }
        }
    }
    catch (e) {
        alert(e.message);
    }

    if (pos=='')
    {
        window.location = 'http://' + location.host + location.pathname + '?id=' + cidlist[cindex];        
    }
    else 
    {
        window.location = 'http://' + location.host + location.pathname + '?id=' + cidlist[cindex] + '&pos=' + pos;
    }

}

// cid - if passed then return that item to page
// direction - if passed, then find element of array and return item to page
// enter either one or the other
function GetGalleryItem(p_lb_selidx, direction, anchor) {
    var pos = '';
    try {

        if (p_lb_selidx == '' && p_lb_selidx != '0') {
            p_lb_cnt = document.getElementById('ctl00_ContentBody_ListBox1').length;
            p_lb_selidx = document.getElementById('ctl00_ContentBody_ListBox1').selectedIndex;
            if (direction == 'next') {
                if (p_lb_selidx == p_lb_cnt - 1) {
                    alert("No more items after current image.");
                    return;
                }
                else {
                    p_lb_selidx++;
                }
            }
            if (direction == 'prev') {
                if (p_lb_selidx == 0) {
                    alert("No more items before current image.");
                    return;
                }
                else {
                    p_lb_selidx--;
                }
            }
        }
    }
    catch (e) {
        alert(e.message);
    }
    //define image and determine width & height - max values are determined in aspx file
    var newImg = new Image();
    newImg.src = null;
    newImg.src = 'http://www.winthrop.edu' + document.getElementById('ctl00_ContentBody_ListBox1')[p_lb_selidx].text;
    var height = newImg.height;
    var width = newImg.width;
    // alert('The image size is ' + width + '*' + height);
    // return
    var height_str = "";
    var width_str = "";

    if (height > max_height) {
        height_str = " height='" + max_height + "'";
    }
    if (width > max_width) {
        width_str = " width='" + max_width + "'";
    }
    // hi-light listbox1's item
    document.getElementById('ctl00_ContentBody_ListBox1').selectedIndex = p_lb_selidx;
    // build & assign new image to page
    mystr = "<img " + width_str + height_str + "src='http://www.winthrop.edu" + document.getElementById('ctl00_ContentBody_ListBox1')[p_lb_selidx].text + "'></img>"
    document.getElementById('gallery_img').innerHTML = mystr;
    // hi-light listbox2's item
    mystr = document.getElementById('ctl00_ContentBody_ListBox2')[p_lb_selidx].text
    //assign new caption to page
    document.getElementById('gallery_caption').innerHTML = mystr
}
