//birbUtils.js
//Version: 12Nov2007 - (C) George Birbilis / Zoomicon (http://www.zoomicon.com)

function $(id) { 
 if (document.getElementById) {
  return document.getElementById(id);
 } else if (document.all) {
  return document.all[id];
 } else {
  return null;
 }
}

function center(child,parent) {
 centerHorizontal(child,parent);
 centerVertical(child,parent);
}

function centerHorizontal(child,parent) {
 var childWidth=child.offsetWidth;
 var parentWidth=parent.offsetWidth;
 child.style.left=(parentWidth-childWidth)/2;
}

function centerVertical(child,parent) {
 var childHeight=child.offsetHeight;
 var parentHeight=parent.offsetHeight;
 child.style.top=(parentHeight-childHeight)/2;
}

function centerHorizontalById(childId, parentId) {
 centerHorizontal($(childId),$(parentId));
}

function NS4_captureMouseMove(obj) { //for Netscape 4
/*
 if (document.captureEvents) { 
  document.captureEvents(Event.MOUSEMOVE);
  document.onmousemove=NS4_event_router;
 }
*/
 if (obj.captureEvents) { obj.captureEvents(Event.MOUSEMOVE); }
}

function NS4_event_router(e) //at Netscape 4, called by document
{
 if (self.routeEvent) { routeEvent(e); }
}

function scrollLeftRight(x, max_step, obj, parent)
{
 var xmax=parent.clientWidth;

 var step=Math.round(max_step*(xmax/2.0-x)/(xmax/2.0)); //max_step at left, 0 speed at center, -max_step at right
 var newLeft=obj.offsetLeft+step;
   
 var omax=obj.offsetWidth;
 if (omax>xmax) {
  var xmin=-(omax-xmax);
  if (newLeft<xmin) { //too much to the left
   newLeft=xmin;
  } else if (newLeft>0) { //too much to the right
   newLeft=0;
  }
  //alert(newLeft);
  obj.style.left=newLeft;
 }
}

function fitContentWidth(obj){
 var finished=false;
 var pos=obj.childNodes.length-1;
 while(!finished && pos>=0){
  var last=obj.childNodes[pos]; //last item is a CRLF if items are listed in one line each
  if (last.offsetLeft && last.offsetWidth) {
   obj.style.width=last.offsetLeft+last.offsetWidth;
   finished=true;
  } else {
   pos-=1;
  }
 }
}

function focusIt(obj) {
 if (obj && obj.focus) { 
  focus();
 }
}

function clickIt(obj) {
 if (obj && obj.click) { 
  obj.click();
 }
}

function clickMiddleChild(obj) {
 var finished=false;
 var pos=Math.round(obj.childNodes.length/2.0);
 while(!finished && pos>=0){
  var last=obj.childNodes[pos]; //last item is a CRLF if items are listed in one line each
  if (last.click) {
   last.click();
   finished=true;
  } else {
   pos-=1;
  }
 }
}

function singleQuote(str) {
 return "'" + str + "'";
}

function getFileExtension(str) {
 //alert(str);
 var pos=str.lastIndexOf('.');
 if (pos<0) { return ""; }
 return str.substring(pos,str.length);
}

var imageWidth=0;
var imageHeight=0;

function fitImageKeepRatio(obj,objParent) {
 if (obj==null) { return; }
 var parentWidth=objParent.clientWidth;
 var parentHeight=objParent.clientHeight;
 if ((imageWidth<=0) || (imageHeight<=0)) {
  imageWidth=obj.width;
  imageHeight=obj.height;
 }
 var width=imageWidth;
 var height=imageHeight;
 var ratio=width/height;

 var newWidth=parentHeight*ratio;
 var newHeight=parentHeight; //must define the newHeight out of the "if" block, so we can give it a default value here anyway (don't define it in an "else" block below cause it will be visible only inside there)
 if (newWidth>parentWidth) {
  newWidth=parentWidth;
  newHeight=parentWidth/ratio;
 }
 obj.style.width=newWidth;
 obj.style.height=newHeight;
 center(obj,objParent);
}

function displayStr(contentClass,contentUrl) { //see http://alistapart.com/articles/byebyeembed
 var str=""; //better initialize to be safe
 var quotedUrl=singleQuote(contentUrl);
 var quotedClass=singleQuote(contentClass);
 //alert(getFileExtension(contentUrl));
 switch(getFileExtension(contentUrl).toUpperCase()){
  case ".JPG":
  case ".PNG":
  case ".GIF":
   imageWidth=0;
   imageHeight=0;
   str="<img id='viewImage' src=" + quotedUrl + " class=" + singleQuote(contentClass+"Image") + ">";
   break;
  case ".HTML":
  case ".HTM":
   window.open(contentUrl,"_blank", "status=yes,toolbar=yes,menubar=yes,location=yes"); //height=500,width=500
   str="";
   break;
  case ".SWF":
   str="<object type='application/x-shockwave-flash' data=" + quotedUrl + " class=" + quotedClass + ">";
   str+="<param name='movie' value=" + quotedUrl + " />";
   str+="<param name='allowScriptAccess' value='sameDomain' />";
   str+="<param name='quality' value='best' />";
   str+="<param name='bgcolor' value='#FFFFFF' />";
   str+="<param name='scale' value='noScale' />";
   str+="<param name='salign' value='TL' />";
   str+="<param name='FlashVars' value='playerMode=embedded' />";
   break;
  case ".AU":
  case ".AIFF":
  case ".AIF":
  case ".MOV":
  case ".MPG":
  case ".MPEG":
  case ".TIFF":
  case ".TIF":
/*
   //tried to use this one instead with "AC_QuickTime.js" from Apple, but didn't work OK
   str="<p class=" + quotedClass + ">";
   str+=QT_GenerateOBJECTText(quotedUrl, '100%', '100%', '', 'AUTOPLAY', 'True', 'SCALE', 'Aspect');
   str+="</p>";
*/
   str="<object classid='clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B' codebase='http://www.apple.com/qtactivex/qtplugin.cab' class=" + quotedClass + ">";
   str+="<param name='src' value=" + quotedUrl + " />";
   str+="<param name='Controller' value='true' />";
   str+="<param name='AutoPlay' value='true' />";
   str+="<param name='Scale' value='Aspect' />";
   str+="<!--[if !IE]>";
   str+="<object type='video/quicktime' data=" + quotedUrl +" class=" + quotedClass + ">";
   str+="<param name='src' value=" + quotedUrl + " />";
   str+="<param name='Controller' value='true' />";
   str+="<param name='AutoPlay' value='true' />";
   str+="<param name='Scale' value='Aspect' />";
   str+="<embed width='100%' height='100%' src=" + quotedUrl + " Controller='true' AutoPlay='true' Scale='Aspect' class=" + quotedClass + ">";
   str+="<noembed>Your browser doesn't support embedded files.</noembed>";
   str+="</embed>";
   str+="</object>";
   str+="<![endif]-->";
   str+="</object>";
   break;
/*
  case ".WAV":
  case ".MP3":
  case ".DIVX":
  case ".WMV":
   str="<object type='video/x-ms-wmv' data=" + quotedUrl + " class=" + quotedClass + "></object>"; //!!! FIX THIS TO NOT GET BLOCKED BY SOME IE SECURITY SETTINGS !!!
   break;
*/
  case ".PDF":
   str="<object classid='clsid:CA8A9780-280D-11CF-A24D-444553540000' class=" + quotedClass + ">";
   str+="<param name='src' value=" + quotedUrl + " />";
   str+="<embed width='100%' height='100%' src=" + quotedUrl + ">";
   str+="<noembed>Your browser doesn't support embedded files.</noembed>";
   str+="</embed>";
   str+="</object>";
   break;
  default:
   str="<object data=" + quotedUrl + " class=" + quotedClass + ">";
   str+="<param name='src' value=" + quotedUrl + " />";
   str+="<embed width='100%' height='100%' src=" + quotedUrl + ">";
   str+="<noembed>Your browser doesn't support embedded files.</noembed>";
   str+="</embed>";
   str+="</object>";
 }
 return str;
}

function displayById(view,contentClass,contentUrl,linkUrl,linkTarget) {
 var str="<div id='viewImageParent' style='width:100%; height:100%; overflow:hidden;' onresize='fitImageKeepRatio($(\"viewImage\"), $(\"view\"));'>";  //using grand-parent's size, since image parent isn't layed-out yet//must not use explicit width and height attributes here, but must specify them in style attribute instead for onresize event to fire
 if (linkUrl!="") { 
  str+="<a href='" + linkUrl + "' target='" + linkTarget + "'>";
 } else {
  str+="<a href='" + contentUrl + "' target='_blank'>"; //!!! doesn't work locally in IE7
 }
 str+=displayStr(contentClass,contentUrl);
 str+="</a></div>";
 //alert(str);
 $(view).innerHTML=str;
}

/*
 Parse the query string in your URL to grab certain values
 (from http://www.netlobo.com/url_query_string_javascript.html)
*/
function getUrlParam(name){
 name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
 var regexS = "[\\?&]"+name+"=([^&#]*)";
 var regex = new RegExp(regexS);
 var results = regex.exec(window.location.href);
 if (results==null) { return ""; } else { return results[1]; }
}
