/*******************************************************************************
 * utility 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
 *******************************************************************************
 *******************************************************************************/

// todo change the name of the var from generator to galleryWindow or just gallery
//var generator;
var gImageRequestXMLDataHttp;
var overMessageWindow = false;

function initHomePageFauxFlash() {
 
    // create an array to hold the url of the images used in the slide show animation
    slideShowArray = new Array(5);
    urlBase = "./v02/images/home/";
    urlSufix = ".jpg";

    slideShowArray[0] = urlBase + "homePhoto_BGarden" + urlSufix;
    slideShowArray[1] = urlBase + "SafarikBancroft_5720" + urlSufix;
    slideShowArray[2] = urlBase + "SafarikBancroft_5742" + urlSufix;
    slideShowArray[3] = urlBase + "SafarikBancroft_5753" + urlSufix;
    slideShowArray[4] = urlBase + "SafarikBancroft_5731" + urlSufix;

    // create our side show action : 
    //createSlideShowAction ( slideURLArray , elementID , delay ,waitForMeToComplete, loop  )
    // stage will call this action and then proceed to the next frame-action
    slideShow = createSlideShowAction (slideShowArray,"homeImageContainer", 10000, NO_WAIT_GO_TO_NEXT_FRAME, true);
    crossFadeB1B2   = createCrossFadeObj (10 , "homeBodyContainer" , "homeContactContainer", WAIT_TILL_ACTION_COMPLETES_BEFORE_GOING_TO_NEXT_FRAME);
    crossFadeB3ToB4 = createCrossFadeObj (10 , "homeContactContainer" , "homeBodyContainer", WAIT_TILL_ACTION_COMPLETES_BEFORE_GOING_TO_NEXT_FRAME);
   
   
    // create all of our pause action 
    pause2Sec = createPauseState(TWO_SECONDS);
    pause5Sec = createPauseState(FIVE_SECONDS);
    pause10Sec = createPauseState(TEN_SECONDS);
    
    showText = createShowElementObj("homeBodyContainer");
    hideText = createHideElementObj("homeContactContainer");
    
    frames = new Array(hideText,showText,slideShow,crossFadeB1B2,pause10Sec,pause5Sec,crossFadeB3ToB4,pause10Sec,pause5Sec);
    // loop stage frames from end back to frame 4
    initStage(frames,LOOP_FRAMES,4);
}

// interface to faux flash api
function pauseHomePage() {
    pauseStage();
}

function playHomePage(){   
    playStage();
}

/*
// is this used ??
function closeGallery () {
    alert(" close gallery " + generator );
    generator = null;
}
*/

function getInquireFormToolBarMenu (leftSpace) {
    leftP = 14;
    topP = 16;
    html = '<div id="inquireFormToolBarMenuContainer" style="left:'+ leftSpace +'; top: 100;"><img src="v02/images/inquireForm/inquireFormToolBar.png">' + closeBox( leftP , topP ) + '</div>';
   
    return(html);
}

// is this used ??
function getInquireFormArtWorkData ( url , name , workHeight , workWidth , workDepth ) {
    html = '<div id = "inquireFormArtworkDataContainer">';
    html +=  '<div id = "bandOneCell"> </div>';
    html +=  '<div id = "bandTwoCell"> </div>';
    html +=  '<div id = "bandThreeCell"> </div>';
    html +=  '<div id = "artworkInfoCell" ><img src = "./v02/images/imageDataPanel.jpg"  align="left">';
    html +=  '<div id = "statsCell">';
    html +=  '<div id = "titleCell"> ' + name + '</div>';
    html +=  '<div id = "heightCell"> ' + workHeight + ' </div>';
    html +=  '<div id = "widthCell"> ' + workWidth + ' </div>';
    html +=  '<div id = "depthCell"> ' + workDepth + ' </div>';
    html +=  '</div></div>';
    html +=  '<div id = "inquireFormPortraitContainer"><img src=' + url + '></div>';
    html +=  '</div>';
    return(html);
}
// is this used ??
function getInquireFormWelcomeMessage () {
   
    html = '<div id="inquireFormWelcomeMessageContainer"><p id="inquireFormMsgText">Send Leslie an Email Inquiring on the Availablity and Price of this Artwork</p></div>';
    return(html);
}
// is this used ??
function getInquireFormForm () {
    html = '';
    html +=   '<div id="inquireFormUserDataPanelContainer">';
    html +=   '<form action="v02/asp/sendInquireEmail.php" method="post">';
    html +=   '<p><label for="fname">First Name</label> <input type="text" id="fname"  size = "30"; /></p>';
    html +=   '<p><label for="lname">Last Name</label> <input type="text" id="lname"  size = "30"; /></p>';
    html +=   '<p><label for="email">E-mail</label> <input type="text" id="email"  size = "30"; /></p>';
    html +=   '<div id="inquireFormSubmitButtonContainer"><p class="submit"><input type="submit" value="Inquire" /></p></div>';
    html +=   '<fieldset>';
    html +=   '<legend>Optional Information</legend>';
    html +=   '<fieldset>';   
    html +=   '<legend>Comment / Notes</legend>';
    html +=   '<textarea rows="3" cols="50" wrap="virtual" name="comments" id="inquireFormComment_text">Please enter comments here</textarea>';
    html +=   '</fieldset>';
    html +=   '<fieldset>';   
    html +=   '<legend>Location</legend>';
    html +=   '<ol><li><label for="city">City</label><input id="city" name="city" class="text" type="text" /></li>';
    html +=   '<li><label for="state">State:</label><input id="state" name="state"   class="text textSmall" type="text" /></li>';
    html +=   '<li><label for="country">Country:</label><input id="country" name="country" class="text" type="text" /></li></ol>';
    html +=   '</fieldset>';
    html +=   '</fieldset>';
    html +=   '</form>';
    html +=   '</div>';
    return(html);
}

function debugWindow (debugData) {
    alert(debugData);
}

function galleryModalBackPanel(windowWidth , imageCellPos) {
    htmlData = '';
    htmlData += '<div id = "modalWindowFixed"><img src="./v02/images/blackSQ.gif"  style="opacity:0.5;filter:alpha(opacity=40); height: 900px; width: '+ pageWidth +';">';
    return(htmlData);
}

// this is the large view of an artwork photo from the gallery
function galleryImagePanel (windowWidth , imageCellPos , url , imageHeight ,imageWidth , hspace) {
    htmlData = '';
    h = parseInt(imageHeight)  + 11;
   
    htmlData += '<div id = "fullImageCell" style="width:'+ windowWidth +'; left:'+ imageCellPos +'; top: 120;"><div id = "bandOneCell"></div><img src= ' + url + ' height =' + imageHeight + ' width = ' + imageWidth +'  hspace=' + hspace + ' ></div>'; 
    return(htmlData);
}

function nextClick (artwrkid) {
    nextGalleryImageClick(artwrkid);
    
}

// onclick function handler : 
function _nextGalleryImageClick ( artworkID ) {
    
    console.log("next image "+artworkID);
    alert("next image "+artworkID);
    // image data request
    gImageRequestXMLDataHttp=GetXmlHttpObject();
    // if any one of the objs failed to be created we need to bail
    if (gImageRequestXMLDataHttp==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();
    gImageRequestXMLDataHttp.onreadystatechange=_galleryImageDataReadyCallBackF;
    // make the data request
    gImageRequestXMLDataHttp.open("GET",dataUrl,true);
    gImageRequestXMLDataHttp.send(null);
    
}

function _galleryImageDataReadyCallBackF () {
    if (gImageRequestXMLDataHttp.readyState==4) {
        //closeGalleryWindow();
        alert("galleryImageDataReadyCallBackF");
        document.getElementById("galleryWindowFixed").innerHTML=gImageRequestXMLDataHttp.responseText;
    }     
}

function galleryImageDataPanel (windowWidth , windowHeight , imageCellPos ,imageCellPosTop ,workHeight , workWidth , workDepth , available , copyright , imageUrl ,artworkID){
    
   // eventFunction = 'onClick="javaScipt:inquiry(  '  + artworkID +  '  );"';
    //htmlData += '<a href="#"' + eventFunction + 'class="ModalBoxMenuButtonType_G">';
    
    nextImageTag = ''; //'<a href="#" onClick="javaScript:_nextGalleryImageClick('+artworkID+');" >Next Image</a>';
    htmlData = '';
    htmlData += '<div id = "artworkDataBox" style="height : 120; width:'+ windowWidth +'; left:'+ imageCellPos +'; top :'+ imageCellPosTop + ';">';
    // make back ground stripes
    htmlData += '<div id = "bandOneCell"></div>';
    htmlData += '<div id = "bandTwoCell"></div>';

    // info cell
    htmlData += '<div id = "artworkInfoCell" ><img src = "./v02/images/imageDataPanel_long.jpg" align="left">'+nextImageTag;
    
    htmlData += '<div id = "statsCell"><div id = "titleCell">' + name + '</div>';
    if (workHeight == "") {
        htmlData += '<div id = "heightCell"> NA</div>';
        htmlData += '<div id = "widthCell"> NA</div>';
        htmlData += '<div id = "depthCell"> NA</div>';      
    }
    else {
        htmlData += '<div id = "heightCell">' + workHeight + ' in. </div>';
        htmlData += '<div id = "widthCell">' + workWidth + ' in. </div>';
        htmlData += '<div id = "depthCell">' + workDepth + ' in. </div>';     
    }
   
    htmlData += '</div>'; // close artworkInfoCell
    
    htmlData += '</div>'; // close artworkDataBox
    // logo box
    htmlData += '<div id="safarikLogoCell" ><img src = "./v02/images/logo.jpg" align="right">';
    
    htmlData += '<div id = "galleryCopyRightContainer"><p id="footer_copywrite_text" style="text-align: left;">&copy; ' + copyright +  ' Safarik Studios. All Rights Reserved</p></div>';
    
    // close safarikLogoCell
    htmlData += '</div>';
    // add the inquire link
    htmlData += inquireBox(imageUrl, artworkID);
    // add the close box
    htmlData += closeBox(12,96);
    // close data box
    htmlData += '</div>'; 

    return(htmlData);
}

function addBS (str) {
   parts = str.split("/");
   retStr = parts[0];
   for (i=1; i<parts.length; i++) {
        retStr = retStr + '//' + parts[i];
   }
   return(retStr);
}

function inquireBox(imageUrl , artworkID) {
    htmlData = '';
    htmlData += '<div id="galleryInquiryContainer">';
    // TODO : looks like image url is not used, remove please
    codedURL = imageUrl; 
    
    eventFunction = 'onClick="javaScipt:inquiry(  '  + artworkID +  '  );"';
   
    htmlData += '<a href="#"' + eventFunction + 'class="ModalBoxMenuButtonType_G">';
    htmlData += '<img src="./v02/images/inquiryBox.png" width="16px" height="16px" border= "0px"> Email Leslie About This Artwork';
    htmlData += '</a></div>';
    return(htmlData);
}
    
function closeBox( leftP , topP ) {
    htmlData = '';
    htmlData += '<div id="closeBoxContainer" style="left:'+ leftP +';top:' + topP + ';">';
    htmlData += '<a href="#"  onClick="javaScipt:closeGalleryWindow();" class = "ModalBoxMenuButtonType_G">';
    htmlData += '<img src="./v02/images/closeButton.png" width="16px" height="16px" border= "0px"> Close';
    htmlData += '</a></div>';
    return(htmlData);
}

/**********************************************************************
 **/
// todo: what is this???
function validateFile (fileInputID) {
    alert("a new file = " + document.getElementById(fileInputID).value);
}

// todo : appears to do nothing
function showImageFeatureBox(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];
    boxLeft     = imageInfoArray[11];
    boxTop      = imageInfoArray[12];

    boxUrl = './v02/images/gallery/rolloverInfoBox3.png';
    fbHTML = getFBShareButton(url);
    htmlData = '<div id="galleryOptionsWindowContextPos" style="left:'+ boxLeft +';top:' + boxTop + ';">'+ fbHTML + '</div>';
}

// is this used ??
function hideImageFeatureBox() {
    alert("hideImageFeatureBox");
    if (overMessageWindow == false) {
        //alert("hideImageFeatureBox " + overMessageWindow);
        //document.getElementById("outerMessageLayer").innerHTML= "";
    }
    
}

function holdOpen () {
    //alert("holdOpen " + overMessageWindow);
    overMessageWindow = true;
}

function closeIt() {
    //alert("closeIt");
    overMessageWindow = false;
}

function getFBShareButton (shareURL) {
    fbShare_html = "<a name='fb_share' type='button_count' share_url='www.safarikstudios.com' href='http://www.facebook.com/sharer.php?u=www.safarikstudios.com&i="  + shareURL + "&src=sp'>Share</a>";
    js_html = "<script src='http://static.ak.fbcdn.net/connect.php/js/FB.Share' type='text/javascript'></script>";
    fbButton = "<br><br>" + fbShare_html + js_html;
    return(fbButton);
}

function setShareImageUrl (shareImageUrl) {
    currentURL = document.getElementById("shareImageUrl").attributes.getNamedItem("href").value;
    document.getElementById("shareImageUrl").attributes.getNamedItem("href").value = shareImageUrl;
}

function validateForm () {
    // var fname=document.forms["contactForm"]["fname"].value;
    //var lname=document.forms["contactForm"]["lname"].value;
    //var email=document.forms["contactForm"]["email"].value;
   alert(" are we here? ");
    /* if (fname==null || fname=="") {
        alert("First name must be filled out");
        return false;
    }
     if (lname==null || lname=="") {
        alert("Last name must be filled out");
        return false;
    }
        if (email==null || email=="") {
        alert("Email Address must be filled out");
        return false;
    }*/
    return true;
}
