/*******************************************************************************
 * controller.js is a safarikstudios specific client controller function library
 * TODO: rewrite in oop and make moe general
 *
 * The only public functions contained in this package are the factory functions
 * that are used to create the stage and the animation action objects.
 *
 *
 *
 *
 *
 * Nov 7 2020 : L.Tietz
 * copyright 2020 : Laurence Tietz
 *******************************************************************************
 *******************************************************************************/
var xmlPageContenObj;
var xmlMenuStateObj;
var xmlContexualHelpObj;
var formRequestXMLDataHttp;
var imageRequestXMLDataHttp;


// onload function handler
function initPage(page) {
    page = "home_0";
    // to start things off we need to call the create stage factory function
    createStage();
    menuClick(page);
    // delay init of faux flash elements until page elements are established in the DOM
    if (page=="home_0") {
        window.setTimeout(initHomePageFauxFlash, 1000);
    }
}

// onclick function handler : 
function nextGalleryImageClick ( artworkID ) {
    
    console.log("next image "+artworkID);
    alert("next image "+artworkID);
    // image data request
    imageRequestXMLDataHttp=GetXmlHttpObject();
    // if any one of the objs failed to be created we need to bail
    if (imageRequestXMLDataHttp==null)
    {
        alert ("Your browser does not support AJAX!!!");
        return;
    }
    var dataUrl = "v02/asp/aspGalleryImageData.php";
    
    
    //----------------------------------------------
    // make the main content updata request  
    dataUrl=dataUrl+"?q="+artworkID;
    dataUrl=dataUrl+"&sid="+Math.random();
    imageRequestXMLDataHttp.onreadystatechange=galleryImageDataReadyCallBackF;
    // make the data request
    imageRequestXMLDataHttp.open("GET",dataUrl,true);
    imageRequestXMLDataHttp.send(null);
    
}

function galleryImageDataReadyCallBackF () {
    if (imageRequestXMLDataHttp.readyState==4) {
        //closeGalleryWindow();
        alert("galleryImageDataReadyCallBackF");
        document.getElementById("galleryWindowFixed").innerHTML=imageRequestXMLDataHttp.responseText;
    }     
}

// onclick function handler : 
function inquireClick ( artworkID ) {
    console.log("inquire");
    // inquiry form request
    formRequestXMLDataHttp=GetXmlHttpObject();
    // if any one of the objs failed to be created we need to bail
    if (formRequestXMLDataHttp==null)
    {
        alert ("Your browser does not support AJAX!!!");
        return;
    }
    var dataUrl = "v02/asp/aspFormInquiryFormData.php";
    
    
    //----------------------------------------------
    // make the main content updata request  
    dataUrl=dataUrl+"?q="+artworkID;
    dataUrl=dataUrl+"&sid="+Math.random();
    formRequestXMLDataHttp.onreadystatechange=formDataReadyCallBackF;
    // make the data request
    formRequestXMLDataHttp.open("GET",dataUrl,true);
    formRequestXMLDataHttp.send(null);
}

function formDataReadyCallBackF () {
    if (formRequestXMLDataHttp.readyState==4) {
        closeGalleryWindow();
        document.getElementById("galleryWindowFixed").innerHTML=formRequestXMLDataHttp.responseText;
    }     
}

// onclick event handler
function showBigImageClick(  imageInfoArray ) { 
    url         = imageInfoArray[0];
    imageHeight = imageInfoArray[1];
    imageWidth  = imageInfoArray[2];
    media       = imageInfoArray[3];
    name        = imageInfoArray[4];
    date        = imageInfoArray[5];
    workHeight  = imageInfoArray[6];
    workWidth   = imageInfoArray[7];
    workDepth   = imageInfoArray[8];
    copyright   = imageInfoArray[9];
    available   = imageInfoArray[10];
    artworkID   = imageInfoArray[11];
    windowHeight = parseInt(imageHeight)  + 110;
    windowWidth = parseInt(imageWidth)  + 40
    if (windowWidth < 700) {
        windowWidth = 700;
    }
    arg3 = 'height='+windowHeight+',width='+windowWidth+',resizable=0'; 
    // todo arg -> orientation == horz or vert
    hspace = (windowWidth - imageWidth)/2;
    pageWidth = 900;
    imageCellPos = (pageWidth - windowWidth) / 2;
    if (imageCellPos < 0) {
        imageCellPos = 0;
    }
    htmlData = '';
   
    htmlData += galleryModalBackPanel(windowWidth , imageCellPos);
    htmlData += galleryImagePanel(windowWidth , imageCellPos ,url , imageHeight ,imageWidth , hspace);
    // info panel
    htmlData += galleryImageDataPanel (windowWidth , windowHeight , imageCellPos ,0, workHeight , workWidth , workDepth , available , copyright , url  ,artworkID );
    htmlData += galleryImageDataPanel (windowWidth , windowHeight , imageCellPos ,windowHeight + 8, workHeight , workWidth , workDepth , available , copyright , url,artworkID );
   
    document.getElementById("galleryWindowFixed").innerHTML=htmlData;
    scroll(0,0);
}

// where is this called from??
function inquiry ( artworkID ) {
    closeGalleryWindow();
    inquireClick(artworkID);
}
// where is this called from??
function closeGalleryWindow () {
   document.getElementById("galleryWindowFixed").innerHTML="";
}

// obsolete ??
function artworkPageClick(pageID) {
    alert("page id = " + pageID);                        
    }

/**********************************************************************
 **/
/**********************************************************************
* this is the function that gets called when the user clicks on 
* any of "buttons" in any menus
* that includes the main menu, and the "tab" buttons and child tab buutons
* the function makes a request to fetch new content, menu state and contextual
* help data via AJAX
*/
function menuClick (menuItemID) {
    xmlPageContenObj=GetXmlHttpObject();
    xmlMenuStateObj=GetXmlHttpObject();
    xmlContexualHelpObj=GetXmlHttpObject();
    
    // if any one of the objs failed to be created we need to bail
    if (xmlPageContenObj==null)
    {
        alert ("Your browser does not support AJAX!!!");
        return;
    }
    
    // our menu and help data requests active server pages 
    var menuUrl = "v02/asp/menuDataUpdater.php";
    var helpUrl = "v02/asp/helpDataUpdater.php";
    
    // select the asp process assocaited with this menuItemID
    
    //alert(menuItemID); // TODO : Remove when debug is complete - alert(menuItemID)
    pauseHomePage();
    switch (menuItemID)
    {
        case "home_0":
            playHomePage();
            var url = "v02/asp/aspHomePage.php";
            break;
        case "about_0":
        case "about_0_1":
        case "about_0_2":
        case "about_0_3":
        case "about_2":
        case "about_2_1":
        case "about_2_2":
        case "about_2_3":
        case "about_3":
        case "about_3_1":
        case "about_3_2":
        case "about_3_3":
        case "about_4":
            var url="v02/asp/aspAboutPageData.php";
            break;
        case "art_work_0": // feature work page 1
        case "art_work_0_1":
        case "art_work_0_2":
        case "art_work_0_3":
        case "art_work_1": // large work page 1
        case "art_work_1_1":
        case "art_work_1_2":
        case "art_work_2": // small work page 2  
        case "art_work_2_1":
        case "art_work_2_2":
        case "art_work_2_3":
            var url = "v02/asp/aspGalleryData.php";
            break;
        case "art_work_3": // prints work page 1
            var url = "v02/asp/aspGalleryData.php";
            break;
        case "resume_0":
            var url="v02/asp/aspResumeData.php";
            break;
        case "resume_2":
        case "resume_3":
        case "resume_3_1":
        case "resume_3_2":
            var url="v02/asp/aspExhibitionData.php";
            break;
        case "resume_4":
        case "resume_4_2":
        case "resume_4_3":
            var url ="v02/asp/aspPressMedia.php";
            break;
        case "contact_0":
            var url = "v02/asp/aspContactForm.php";
            break;
        case "special":
            menuItemID = "home";
            playHomePage();
            var url = "v02/asp/aspHomePage.php";
            break;
        default:
            alert("error" + menuItemID);
            var url="v02/asp/aspHomePage.php";
            break;
    
    }
    
    if (menuItemID == null )
    {
        menuItemID = "home";
    }
    
    //----------------------------------------------
    // make the main content updata request  
    url=url+"?q="+menuItemID;
    url=url+"&sid="+Math.random();
    xmlPageContenObj.onreadystatechange=stateChangedCallBackF;
    // make the data request
    xmlPageContenObj.open("GET",url,true);
    xmlPageContenObj.send(null);
    //----------------------------------------------
    // make the menu updater request
    menuUrl=menuUrl+"?q="+menuItemID;
    menuUrl=menuUrl+"&sid="+Math.random();
    xmlMenuStateObj.onreadystatechange=menuStateChangedCallBackF;
    // make the data request
    xmlMenuStateObj.open("GET",menuUrl,true);
    xmlMenuStateObj.send(null);
    //---------------------------------------------
    // make the help data request
    //alert(" help url = " + helpUrl);
    helpUrl=helpUrl+"?q="+menuItemID;
    helpUrl=helpUrl+"&sid="+Math.random();
    xmlContexualHelpObj.onreadystatechange=helpStateChangedCallBackF;
    // make the data request
    xmlContexualHelpObj.open("GET",helpUrl,true);
    xmlContexualHelpObj.send(null);
    }


function helpStateChangedCallBackF () {
    if (xmlContexualHelpObj.readyState==4) {
        document.getElementById("sideDomPanelContainer").innerHTML=xmlContexualHelpObj.responseText;
    }
    
}

function menuStateChangedCallBackF () {
    if (xmlMenuStateObj.readyState==4) {
        document.getElementById("menuContainer").innerHTML=xmlMenuStateObj.responseText;
    }
    
}

function stateChangedCallBackF () {
    if (xmlPageContenObj.readyState==4) {
        document.getElementById("pageContentContainer").innerHTML=xmlPageContenObj.responseText;
        }
    }


// generic function to retrieve the correct xml request object as a function of our browser type type
function GetXmlHttpObject() {
    var xmlHttpObj=null;
    try
      {
      // Firefox, Opera 8.0+, Safari
      xmlHttpObj=new XMLHttpRequest();
      }
    catch (e)
      {
      // Internet Explorer
      try
        {
        xmlHttpObj=new ActiveXObject("Msxml2.XMLHTTP");
        }
      catch (e)
        {
        xmlHttpObj=new ActiveXObject("Microsoft.XMLHTTP");
        }
      }
    return xmlHttpObj;
}

function showAnouncementDetail () {
    atext = "<div id='anouncementDetailContainer'><div id='anouncementDetailPane'>";
    atext += "<p id='aTitleP'>Meet Leslie Safarik!!</p>";
    atext += "<p id='bTitleP'>Open Studio</p><p id='bTitleP'>4pm to 8pm</p>";
    atext += "<BR>";
    atext += "Please join us for an afternoon of holiday fun at this unquiqe event on December 13th. I will be showing some exciting new work! So please come and have some holiday cheer and help me celebrate my new work and studio. ";
    atext += "<BR>";
    //atext += "The show will include new large scale and smaller ceramic sculptures.<BR>Leslie will be attending the opening, December 2, 2010, to meet with visitors. The opening is on the night of Palm Desert Art Walk.";
    atext += "<BR><BR>";
    atext += "To attend please call or email to receive your personal invitation.";
    //atext += "<BR>";
    //atext += "<BR>";
    atext += "";
    atext += "<p id='cTitleP'>leslie@safarikstudios.com<br>(213) 819-0558</p></div></div>";
    document.getElementById("anouncementDetailLayer").innerHTML= atext;
}

function closeAnouncementDetail () {
    document.getElementById("anouncementDetailLayer").innerHTML= "";  
}

function setShareImageUrl (shareImageUrl) {
    currentURL = document.getElementById("shareImageUrl").attributes.getNamedItem("href").value;
    document.getElementById("shareImageUrl").attributes.getNamedItem("href").value = shareImageUrl;
}

function submitInquire() {
    alert("submitInquire");
}
