/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Window Onload Manager (WOM) v1.0
 * Author : Justin Barlow - www.netlobo.com
 *
 * Description :
 * The WOM library of functions allows you to easily call
 * multiple javascript functions when your page loads.
 *
 * Usage :
 * Add functions to WOM using the womAdd() function. Pass the
 * name of your functions (with or without parameters) into
 * womAdd(). Then call womOn() like this :
 *     womAdd('hideDiv()');
 *     womAdd('changeBg("menuopts","#CCCCCC")');
 *     womOn();
 * WOM will now run when your page loads and run all of the
 * functions you have added using womAdd()
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * The womOn() function will set the window.onload function to
 * be womGo() which will run all of your window.onload
 * functions.
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function womOn()
{
   window.onload = womGo;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * The womGo() function loops through the woms array and
 * runs each function in the array.
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function womGo()
{
   for(var i = 0; i < woms.length; i ++ )
   eval(woms[i]);
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * The womAdd() function will add another function to the woms
 * array to be run when the page loads.
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function womAdd(func)
{
   woms[woms.length] = func;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * The woms array holds all of the functions you wish to run
 * when the page loads.
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
var woms = new Array();

function loadWoms()
{

   var d = document.title.toLowerCase().replace(String.fromCharCode(10), "");
   d = d.replace(String.fromCharCode(9), "")
   d = d.replace("\n", "")

   switch (d)
   {
      case  "brian gilbertson home" :
         {
            womAdd ('init()');
            womOn();
            break;
         }

      case "test slide sets", "manage photo gallery slides" :
         {
            womAdd ('initSlidesEdit()')
            womOn();
            break;
         }
      case "photo gallery" :
         {
            womAdd ('createGalleryViewer("flashcontent","divFlashContent",null)');
            womOn();
            break;
         }

   }

}


var selectedRow;
var myCss = false;
var gridOk = (document.getElementById && document.createElement && Array.prototype.push);

String.prototype.find = function(what)
{
   return(this.indexOf(what) >= 0 ? true : false);
}

//String.prototype.trim = function () {
////    return this.replace(/^\s*/, "").replace(/\s*$/, "");
//    return this.replace(/^(\s*)([\W\w]*)(\b\s*$), "").replace(/^(\s*)([\W\w]*)(\b\s*$), "");
//}


Array.prototype.max = array_max;
function array_max()
{
   var i, max = this[0];
   for (i = 1; i < this.length; i ++ )
   {
      if (max < this[i])
      max = this[i];
   }
   return max;
}



function initialize()
{

   // getById("lstFolders").attachEvent("onchange", syncBeGrid)


   if(gridOk == false) return;
   Nifty("div#container", "big")
   Nifty("div#folders,div#abbrvs,div.curved")

}


function AddMyCss()
{
   if (myCss == true) return
   myCss = true;
   var l = CreateEl("link");
   l.setAttribute("type", "text/css");
   l.setAttribute("rel", "stylesheet");
   l.setAttribute("href", "Jscript/MyDataGrid.css");
   l.setAttribute("media", "screen");
   document.getElementsByTagName("head")[0].appendChild(l);
}

function extractIndex(anyRow)
{
   var row = (anyRow ? anyRow : selectedRow)
   if ( ! selectedRow) return - 1
   ctlID = selectedRow.id.toLowerCase()
   ctlID = replaceString(ctlID, "datalist1__ctl", "")
   ctlID = replaceString(ctlID, "_row", "")
   return parseInt(ctlID)
}



function replaceString(str, oldstr, newstr)
{
   var r, re;
   re = new RegExp(oldstr);
   r = str.replace(re, newstr);
   return(r);
}

function fieldValue(rowIndex, field)
{
   var ctlID = "DataList1__ctl" + parseInt(rowIndex) + "_td" + field
   return document.all(ctlID).innerText
}


function getById(id)
{
   return document.getElementById(id)
}

function getByTag(tag)
{
   return document.getElementsByTagName(tag)
}

function getAll(obj, vIndex, sIndex)
{

   return obj.all(vIndex, sIndex)
}

function dataRow(index)
{
   if (index < 0) return;

   return getById("DataList1").rows(index).children(0).children(0)
}


function validateLogoFile(logoFile)
{
   var type = logoFile.substring(logoFile.lastIndexOf(".")).toLowerCase()
   if (type.match(".bmp")) return logoFile
   if (type.match(".jpg")) return logoFile
   if (type.match(".jpeg")) return logoFile
   if (type.match(".gif")) return logoFile
   if (type.match(".png")) return logoFile

   return "Error: Invalid image file!"
}



function focusText (id)
{
   // alert(id)
   var el = getById(id)
   if ( ! el)
   {
      el = this.event.srcElement
   }
   if (el)
   {
      //    alert(el.style.backgroundColor)
      el.style.backgroundColor = "white"
   }
}
function unfocusText (id)
{
   var el = getById(id)
   if (el)
   {
      el.style.backgroundColor = ""
   }
   var s = MakeLocString()
   getById("_ctl0_ContentCustRef_LocstringLabel").value = s
   return s
}

function testME()
{
   alert (window.event.srcElement.id)
   alert(getById("H1").value)
   var images = getByTag("IMG")
   var img
   for(i = 0; i < images.length; i ++ )
   {
      img = images[i];
      img.style.width = ""
      img.style.height = ""
      var  w = img.width;
      var  h = img.height;

      if(w > 200 || h > 200)
      {
         w = w * .5
         h = h * .5
         img.style.width = w + "px"
         img.style.height = h + "px"
      }
   }
}

function scaleImages()
{
   var images = document.images;
   // alert(images.length)
   for (i = 0 ; i < images.length; i ++ )
   {
      var el = images[i];
      scaleMe(el)
      // el.fireEvent("onclick");
   }
}

function scaleMe(el)
{

   var img = el
   var  w = img.clientWidth;
   var  h = img.clientHeight;
   if(w > 200 || h > 200)
   {
      w = w * .5
      h = h * .5
      el.style.width = w + "px"
      el.style.height = h + "px"
   }


}
function showPopUp(el)
{

   var divtext = document.getElementById(el.id.replace("img1", "divText"))

   var sw = el.width
   var sh = el.height
   el.style.visibility = "hidden"
   el.style.width = ""
   el.style.height = ""
   var w = el.width + 48;
   var h = el.height + 48;
   el.style.width = sw;
   el.style.height = sh;
   el.style.visibility = ""
   var dtext = ""
   if(divtext != null)
   {
      h = h + parseInt(divtext.style.height)
      dtext = divtext.innerHTML
   }
   var l = (screen.width - w) / 2;
   var t = (screen.height - h) / 2;
   var URLStr = 'PhotoPopUp.aspx?I=' + el.src + "&d=" + dtext
   // popUpWindow(URLStr, window.screenX + 10, window.screenY - h / 2, w, h)
   popUpWindow(URLStr, 10, t, w, h)
}

function fnStartInit()
{

   //   if (document.readyState == "complete")
   //   {
   //
   var el = getById("Panel1")
   return scaleImages();
   //   }
}
var popUpWin = 0;

function  popUpWindow(URLStr, left, top, width, height, scroll, resize)
{
   if(scroll == null)scroll = "no"
   else scroll = (scroll == true ? "yes" : "no")
   if(resize == null)resize = "no"
   else resize = (resize == true ? "yes" : "no")
   left = (left == null ? "" : left)
   top = (top == null ? "" : top)
   width = (width == null ? "" : width)
   height = (height == null ? "" : height)
   if(popUpWin)
   {
      if( ! popUpWin.closed) popUpWin.close();

   }
   popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,titlebar=no,menubar=no,scrollbars=' + scroll + ',resizable=' + resize + ',copyhistory=yes,width=' + width + ',height=' + height + ',left=' + left + ', top=' + top + ',screenX=' + left + ',screenY=' + top + '');

}

function fillContent()
{

   var oContent = getById("oContent")
   var oDiv = getById("oDiv")
   if (oContent)
   {
      oDiv.innerHTML = oContent.innerHTML
      skipTrans = false
   }

}
function shadeMe(el)
{
   alert("Make Me")
}

function showLarge(el, tblname, imglargename, maxwidth, maxheight, txtphotoid, txtfilename, selfolderlist, txtfilesize, txtdescription)
{
   // alert (el.width + "," + el.height)
   var tbl;
   var i;
   var imgLarge;
   var photoid;
   var filename;
   var photofolder;
   var filesize;
   var description;
   var select;
   var pdesc = getById(el.id.substring(0, el.id.lastIndexOf("_") + 1) + "pdesc");
   var pid = getById(el.id.substring(0, el.id.lastIndexOf("_") + 1) + "pid");
   var folder = getById(el.id.substring(0, el.id.lastIndexOf("_") + 1) + "folder");

   // var tbl = getById(tblname); // don't run this table at server

   var t = document.getElementsByTagName("table")
   if (t != null)
   for (i = 0; i < t.length; i ++ )
   if (t[i].id > "")
   if(t[i].id.find(tblname)) tbl = t[i]

   if (tbl != undefined)
   t = tbl.getElementsByTagName("img")
   if (t != null)
   for (i = 0; i < t.length; i ++ )
   if (t[i].id > "")
   if(t[i].id.find(imglargename)) imgLarge = t[i]

   t = tbl.getElementsByTagName("input")
   if (t != null)
   for (i = 0; i < t.length; i ++ )
   if (t[i].id > "")
   if(t[i].id.find(txtfilename)) filename = t[i]
   else if(t[i].id.find(txtfilesize)) filesize = t[i]
   else if(t[i].id.find(txtphotoid)) photoid = t[i]
   // else if(t[i].id.find(txtfolder)) photofolder = t[i]

   t = tbl.getElementsByTagName("textarea")
   if (t != null)
   for (i = 0; i < t.length; i ++ )
   if (t[i].id > "")
   if(t[i].id.find(txtdescription)) description = t[i]

   if(selfolderlist == null)var nolist = true
   else t = tbl.getElementsByTagName("Select")
   if (t != null)
   for (i = 0; i < t.length; i ++ )
   if (t[i].id > "")
   if(t[i].id.find(selfolderlist))select = t[i]


   if((imgLarge != undefined) && (filename != undefined) && (filesize != undefined))
   {
      imgLarge.src = ""
      imgLarge.style.width = ""
      imgLarge.style.height = ""
      tbl.style.display = ""
      imgLarge.src = el.src
      imgLarge.style.width = imgLarge.width + "px"
      imgLarge.style.height = imgLarge.height + "px"
      var fn = el.src.substring(el.src.lastIndexOf("/") + 1)
      filename.value = fn.substring(0, fn.indexOf("."))
      filesize.value = imgLarge.width + " x " + imgLarge.height
      if(imgLarge.width > maxwidth)imgLarge.style.width = maxwidth + "px"
      if(imgLarge.height > maxheight)imgLarge.style.height = maxheight + "px"
      if((pdesc != undefined) && (description != undefined))description.value = pdesc.value
      if((pid != undefined) && (photoid != undefined))photoid.value = pid.value
      // if((folder != undefined) && (photofolder != undefined))photofolder.value = folder.value
      if((folder != undefined) && (select != undefined))
      {
         for (i = 0; i < select.children.length; i ++ )
         if(select.children[i].innerText.toLowerCase() == folder.value.toLowerCase())
         select.children[i].selected = true
         else select.children[i].selected = false
      }

   }
   if(folder.value.toLowerCase()=="thumbnails")description.disabled="disabled";
    else
        description.disabled="";
}


function Pluralize(qty, desc)
{
   return qty + " " + PluralString(qty, desc)
}
function PluralString(qty, desc)
{
   if (qty != 1)
   return (desc + "s")
   else
   return (desc)
}
function testme(el, t)
{
   var i;
   // t.value = el.src.substring(el.src.lastIndexOf("/") + 1)
   t.src = el.src
}

function SetImage(el, img, phototxt, maxwidth, maxheight)
{
   var imglarge = getById(img)
   var txtphoto = getById(phototxt)
   imglarge.src = ""
   imglarge.style.width = ""
   imglarge.style.height = ""
   imglarge.src = el.src
   txtphoto.value = el.title
   if(imglarge.width > maxwidth)imglarge.style.width = maxwidth + "px"
   if(imglarge.height > maxheight)imglarge.style.height = maxheight + "px"

}

function createplayer(theFile, go)
{
   var s = new SWFObject("flash/player.swf", "mpl", "296", "20", "9");
   s.addParam("allowfullscreen", "false");
   s.addParam("allowscriptaccess", "always");
   s.addVariable("file", theFile);
   // 	        s.addVariable("displayheight", "225");
   s.addVariable("overstretch", "fit");
   s.addVariable("shuffle", "false");
   s.addVariable("thumbsinplaylist", "false");
   //            s.addVariable('skin', 'flash/metarby10.swf');
   if (go)
   {
      s.addVariable("autostart", "true");

   }
   s.write("player");
}
function DisplayNewImageInWindow(imgURL)
{

   html = "<HTML><HEAD><TITLE>Photo</TITLE>"
   +  "</HEAD><BODY LEFTMARGIN=0 "
   + "MARGINWIDTH=0 TOPMARGIN=0 MARGINHEIGHT=0><CENTER>"
   + "<IMG src='"
   + imgURL
   + "' BORDER=0 NAME=image "
   + "onload='window.resizeTo(document.image.width+20,document.image.height+20)'>"
   + "</CENTER>"
   + "</BODY></HTML>";
   popup = window.open  ('', 'image',  'toolbar=0,location=0,directories=0,menuBar=0,scrollbars=0,resizable=1,width=10,height=10');
   popup.document.open();
   popup.document.write(html);
   popup.document.focus();
   popup.document.close();

}
function previewGallery(startIndex)
{

   html = "<html><head><title>Photo Gallery Preview</title>"
   +      "<script src='JScript/swfobject.js' type='text/javascript'></script>"
   +      "<script src='JScript/Gilbertson.js' type='text/javascript'></script></head>"
   +      "<body onload='createGalleryViewer(\"flashcontent\",\"divFlashContent\"," + startIndex + ")'><div id='divFlashContent'>"
   +      "<div id='flashcontent'></div></div></body></html>";
   var X = (screen.width - 520) / 2;
   var Y = (screen.height - 670) / 2;
   popup = window.open  ('', 'image',  'toolbar=0,location=0,directories=0,menuBar=0,scrollbars=0,resizable=0,width=520,height=670, left=' + X + ',top=' + Y );
   //   popup.document.bgColor = "#000";
   popup.document.open();
   popup.document.write(html);
   popup.document.focus();
   popup.document.close();

}

function createGalleryViewer(flashDiv, holder, startIndex)
{
   var so = new SWFObject("flash/MyPhotoGallery.swf", "mymovie", "500", "640", "9", "#000000");
   so.addParam("allowfullscreen", "false");
//   so.addParam("width", "500");
//   so.addParam("height", "500");
   so.addParam("wmode", "transparent");
   so.addParam("allowscriptaccess", "always");
   so.addVariable("autostart", "true");
   if (startIndex != null) so.addVariable("startIndex", startIndex);
   document.getElementById(flashDiv).innerHTML = ""
   so.write(flashDiv);
   document.getElementById(holder).style.backgroundColor = "#000000";
}
