/*
 * Smallbox CMS http://www.smallboxsoftware.com
 *
 * Copyright (C) 2000-2008 Smallbox Software Inc. 
 * Author: Kenneth Spencer
 *
 * This file is intended only for use within Smallbox CMS.
 * Unauthorized use is strictly prohibited.
 *
 */
/* search and fix all: 
 if(mouseMoveAction != null) {
  mouseMoveAction();
 } 
*/

var ie_version = 5;
var mouseX;
var mouseY;
var disable_events = 0;
var drag_interval = null;
var image_list = new Array();
var preloads   = new Array();
var onloads    = new Array();
var moveStart = 0;
var drag_object = null;
var mouseMoveAction = null;

function getStyle(oElm, strCssRule) {
 var strValue = "";
 if(document.defaultView && document.defaultView.getComputedStyle){
  strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
 }
 else if (oElm.currentStyle) {
  strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1) { return p1.toUpperCase();});
  strValue = oElm.currentStyle[strCssRule];
 }
 return strValue;
}

function mouse_pos(evt) {
 if(typeof(evt.pageX) != 'undefined') {
  mouseX = evt.pageX ;
  mouseY = evt.pageY ;
 }
 else {
  mouseX = evt.clientX + document.documentElement.scrollLeft ;
  mouseY = evt.clientY + document.documentElement.scrollTop ;
 }


 if(mouseMoveAction != null) {
  mouseMoveAction();
 }
}

function sb_onload() {
 addEvent(document, 'mousemove', mouse_pos);
 for(var i = 0; i < onloads.length; i++) {
  onloads[i]();
 }
}



function setOpacity(Obj, Level) {
 if(typeof(Obj.style.opacity) == "string")
  Obj.style.opacity=Level > 0 ? Level / 100 : 0;
 else if( typeof(Obj.style.filter) == "string") {
  Obj.style.filter = 'alpha(opacity='+Level+')';
 }
}


function getPos(thisId) {
 var pos  = new Object();
 pos.left = 0;
 pos.top  =  0;


 if(thisId.style.top) {
  pos.top  = parseInt(thisId.style.top);
  pos.left = parseInt(thisId.style.left);
  return pos;
 }

 for (var p = thisId; p.parentNode ; p =  p.parentNode) {
  if (getStyle(p, 'position') == 'fixed') {
   pos.top += typeof(window.scrollY) != 'undefined' ? window.scrollY : document.body.parentNode.scrollTop;
   pos.left += typeof(window.scrollX) != 'undefined' ? window.scrollX : document.body.parentNode.scrollLeft;
   break;
  }
 }

 for (var p = thisId; p ; p = p.offsetParent) {
  pos.top+= p.offsetTop;
  pos.left+= p.offsetLeft;
 }

 pos.bottom = pos.top + thisId.offsetHeight
 pos.right  = pos.left + thisId.offsetWidth;
 return pos;
}


function sb_drag_object(Node) {
 drag_object = this;
 this.horizontal = true;
 this.vertical   = true;
 this.Node = Node;
 this.start = drag_start;
 this.onmousemove = null; 
 this.onmouseup   = null;
 this.move = drag_move; 
 this.stop   = drag_stop;

 pos = getPos(Node);
 this.offsetY = mouseY - pos.top;
 this.offsetX = mouseX - pos.left;
}



function drag_start(evt) {
 var drag = this;
 document.onmouseup = function(evt) {
  var e = evt ? evt : event;
  drag.stop(e);
  if(drag.onmouseup) {
   drag.onmouseup(e);
  }
 }

 document.onmousemove = function(evt) {
  var e = evt ? evt : event;
  drag.move(e);
  if (drag.onmousemove) {
   drag.onmousemove(e);
  }
 }
 document.body.style.cursor = this.Node.style.cursor;
 document.onselectstart = function () { return false; };
 document.onmousedown = function () { return false; };
 document.body.focus();
}

function drag_move(e) {
 if (this.vertical == true) {
  this.Node.style.top  =  mouseY - this.offsetY +'px';
 }
 if (this.horizontal == true) {
  this.Node.style.left =  mouseX - this.offsetX +'px';
 }
}

function drag_stop() {
 drag_interval = null;
 document.onmousemove = null;
 document.onmouseup = null;
 document.body.style.cursor = null;
 document.onselectstart = null;
 document.onmousedown   = null;
 this.Nde = null;
 drag_object = null;
}

function sb_preload() {
 var id;
 for (id in preloads) {
  if (id.match(/^sb_/)) {
   var tmp = new Image();
   tmp.src = preloads[id];
  }
 }
}
onloads[onloads.length] = sb_preload;

function sb_over(thisId) {
 if (typeof(thisId) == 'string') {
  thisId = document.getElementById(thisId);
 }
 var id = thisId.id;

 if(thisId.ie_png_src) {
  image_list[id] = thisId.ie_png_src;
  thisId.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+preloads[id]+"', sizingMethod='scale')";
 }
 else {
  image_list[id] = thisId.src;
  thisId.src = preloads[id];
 }
}

function sb_out(thisId) {
 if (typeof(thisId) == 'string') {
  thisId = document.getElementById(thisId);
 }
 var id = thisId.id;


 if(thisId.ie_png_src)
  thisId.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+image_list[id]+"', sizingMethod='scale')";
 else
  thisId.src = image_list[id];
}

function ie_png_loader() {
 var images = document.getElementsByTagName("img");
 for(var i = 0; i < images.length; i++) {
  var img = images.item(i);
  if(img.src.match(/\.png$/i)) {
   iePNG(img);
  }
 }
}

function iePNG(thisId) {
 var version = navigator.appVersion.match(/MSIE ([0-9])/);
 var pixel = "/smallbox4/images5/pixel.gif";
/*
 if(thisId.src.match(pixel)) {
  thisId.style.visibility = 'visible';
  return false;
 }
*/
 if(typeof(thisId.style.filter) == 'string' && parseInt(version[1]) < 7) {
  thisId.style.height = thisId.offsetHeight +"px";
  thisId.style.width  = thisId.offsetWidth +"px";
  var src = thisId.src;
  thisId.ie_png_src = src;
  thisId.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+src+"', sizingMethod='scale')";
  thisId.src = pixel;
//  thisId.style.visibility = 'visible';
 }
 else {
//  thisId.style.visibility = 'visible';
 }
}

var swfCount = 0;
function sbSWF(src, width, height) {
 this.src = src;
 this.width = width;
 this.height = height;
 this.params = new Array();
 this.params["quality"] = "high";
 this.append = swfAppend;
}

function swfAppend() {
 swfCount++;
 var str = "";
 str += "<object width=\""+this.width+"\" height=\""+this.height+"\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"&#10;http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\" >";
 str += "<param name=\"movie\" value=\""+this.src+"\" />";
 for(param in this.params) {
  str +="<param name=\""+param+"\" value=\""+this.params[param]+"\" />";
 }

 str += "<embed  src=\""+this.src+"\" width=\""+this.width+"\" height=\""+this.height+"\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" ";
 for(param in this.params) {
  str += " "+param+"=\""+this.params[param]+"\" ";
 }
 str += "></embed>";
 str += "</object>";
 document.write(str);
}

function ieSWF() {
 if(true){
  var theObjects = document.getElementsByTagName('object');
  var theObjectsLen = theObjects.length;


  for (var i = 0; i < theObjectsLen; i++) {
   node = theObjects[i];
   var params = node.getElementsByTagName('param');
   var fv = "";
   for(j =0; j < params.length; j++) {
    if(params[j].name == "FlashVars") {
     fv = "<param name='"+params[j].name+"' value='"+params[j].value+"'>\n";
    }
   }

   if(node.outerHTML){
    if(node.data){
     node.removeAttribute('data');
    }

    if(fv)
     node.outerHTML = node.outerHTML.replace(/<param name="FlashVars" value="">/ig, fv);
    else
     node.outerHTML = node.outerHTML;

     
    //node.parentNode.style.display='block';
   }
  }
 }
}

function sb_scroll() {
}

function sb_resize() {

}

//window.onscroll      = sb_scroll;
//window.onresize      = sb_resize;



var sbEscapes = new Array();
var escapeDepth = 0;

function escapeNode(Node, mode, depth, ref, doc) {
 var o = new Object();
 o.node = Node;
 o.mode = mode;
 o.ref  = ref;

 if (depth) {
  escapeDepth = depth - 1;
 }
 sbEscapes[escapeDepth] = o;

 if(!doc) {
  document.onkeydown = clearEscapeNode;
 }
 else {
  doc.onkeydown = clearEscapeNode;
 }
}
                                                                                                 
function clearEscapeNode(evt) {
 var e = evt ? evt : event;
 if (e) {
  if(e.keyCode == 27) {
   escapeClear();
/*
    document.onkeydown = '';
*/
   return false;
  }
 }
}

function escapeRemove(o) {
 if(o) {
  if (o.ref) {
   o.ref();
  }
  if(o.mode == true) {
   o.node.parentNode.removeChild(o.node);
  }
  if (o.mode == 2) {
   o.node.style.visibility = 'hidden';
  }
  else {
   o.node.style.display = 'none';
  }
 }
}

function escapeClear(depth) {
 depth = parseInt(depth) ? depth - 1 : escapeDepth;
 if(sbEscapes[depth]) {
  while(sbEscapes[depth]) {
   escapeRemove(sbEscapes.pop());
  }
  escapeDepth = sbEscapes.length > 0 ? sbEscapes.length - 1 : 0;
 }
}


var sbShowThumb;
function sb_show_thumbnail(thisId, src, width, height) {
 sb_hide_thumbnail();
 pos = getPos(thisId);
 var div = sbShowThumb = sb_node(document.body, "div");
 div.style.visibility = 'hidden';
 div.style.position = 'absolute';
 div.style.zIndex = 2000;
 div.style.border = 'solid 2px #666666';
 div.style.top  = pos.top  - height / 2 + 8  +"px";
 div.style.left = pos.left - width - 10 + "px";
 div.style.width = width+'px';
 div.style.height = height+'px';
 img = sb_node(div, "img");
 img.src = src;
 div.style.background = 'url(/smallbox4/images5/bg_repeat.gif)';
/*
 div.style.backgroundImage = "url("+src+")";
 div.style.backgroundRepeat = 'no-repeat';
*/
 div.style.visibility = 'visible';
}

function sb_hide_thumbnail() {
 if(sbShowThumb) {
  document.body.removeChild(sbShowThumb);
  sbShowThumb = null;
 }
}

function sb_toggle(thisId, ID) {
 var element = document.getElementById(ID);

 var status;
 if(status = getCookie("toggles")) {
  status = status.split(',');
 }
 else {
  status = new Array();
 }

 var img = document.getElementById(ID+'_expand');

 if(element.style.display == 'none') {
  img.src = '/smallbox4/images5/icons/contract_block.png';
  thisId.parentNode.className = "expander on";
  element.style.display = 'block';
  status[status.length] = ID;
 }
 else {
  img.src = '/smallbox4/images5/icons/expand_block.png';
  thisId.parentNode.className = "expander";
  element.style.display = 'none';
  for(var i = 0; i < status.length; i++) {
   if(status[i] == ID) {
    delete status[i];
   }
  }

 }

 var retval = "";
 for(var i = 0; i < status.length; i++) {
  if(status[i]) {   
   retval += (retval.length ? "," : "") + status[i];
  }
 }
 document.cookie = "toggles="+retval;

}

var sb_reload_time;

function sb_set_reload() {
 if(sb_reload_time) {
  clearTimeout(sb_reload_time);
  sb_reload_time = null;
 }
 sb_reload_time = setTimeout(sb_reload, 3600 * 1000);
}

function sb_reload() {
 window.location.replace(window.location.href);
}

function sb_isTarget(e, node) {
 /** Detect if element is the element that originated the event */
 if (e.type != 'mouseout' && e.type != 'mouseover') return false;
 var reltg = e.relatedTarget ? e.relatedTarget :e.type == 'mouseout' ? e.toElement : e.fromElement;
 while (reltg && reltg != node) reltg = reltg.parentNode;
 return (reltg != node); 
}

function sb_rollover(img) {
 var match = img.src.match(/\.(png)|(gif)$/);
 if (match != null) {
  var out  = img.src;
  var over = img.src.replace(match[0], "_over"+match[0]);
  var pre = new Image();
  pre.src = over;
  img.onmouseover = function() {this.src = over}
  img.onmouseout  = function() {this.src = out}
 }
}


function sb_css(file) {

/*
 var style  = document.createElement('style');
 style.type = 'text/css';

 var textNode = document.createTextNode("@import url(\""+file+"\");");
 style.appendChild(textNode);
*/
 var link = document.createElement("link")
 link.setAttribute("href", file)
 link.setAttribute("rel", "stylesheet")
 link.setAttribute("type", "text/css")


 var head  = document.getElementsByTagName("head").item(0);
 head.appendChild(link);
}

var sbIncluded = new Array();
function sb_include(file, ref) {
 var scripts = document.getElementsByTagName('script');
 for (var i = 0; i < sbIncluded.length; i++) {
  if (sbIncluded[i] == file) {
   return;
  }
 }
 sbIncluded.push(file);

 var head = document.getElementsByTagName("head").item(0);

 var script = document.createElement('script');
 script.type = 'text/javascript';
 script.src  = file;
 if (ref) {
  if (typeof(script.onreadystatechange) == 'undefined') {
   script.onload = function() {
    ref();
   }
  }
  else {
   script.onreadystatechange = function() {
    if (this.readyState == 'loaded') { 
     ref();
    }
   } 
  }
 }
 head.appendChild(script);
}


onloads[onloads.length] = function() {
 /** MSIE **/
 var ie = new Array();
 var state = new Object();
 if (ie = navigator.appVersion.match(/MSIE ([0-9]+\.[0-9]+)/)) {
  var version = parseFloat(ie[1]);

//  document.cookie = "ieWarning=";

  if (version < ie_version && !getCookie('ieWarning')) {
    var ref = function(obj) {
    if (obj.ref1 == true && obj.ref2 == true) {
     var kit = new sb_window_kit("Old version of Internet Explorer Detected.", 1);
     kit.underlay = true;
     kit.position = 'middle';
     kit.cont.style.width = '500px';

     var browsers = [
      { icon : 'chrome.gif', name : 'Google Chrome', link : 'http://www.google.com/chrome' },
      { icon : 'firefox.gif', name : 'Firefox', link : 'http://www.getfirefox.com' },
      { icon : 'ie.gif', name : 'Internet Explorer', link : 'http://www.getie.com' },
      { icon : 'safari.gif', name : 'Safari', link : 'http://www.apple.com/safari/download/' }
     ];

     kit.head.className += ' ie_message_head';
     var cont = sb_node(kit.body, 'div');
     cont.className = 'ie_message';
     
     var p, a, table, tbody, tr, td, img;
     p = sb_node(cont, 'p', "You are using an out of date version of Internet Explorer (IE) which is not supported by this website. Newer browsers are available with greatly improved support for web standards, security and load times. Please see alternatives below:");
     p = sb_node(cont, 'p', "If you would like to continue browsing this website using your current browser simply close this window.");
     sb_node(cont, 'br');
     table = sb_node(sb_node(cont, 'div'), 'table');
     tbody = sb_node(table, 'tbody');   
     for (var i = 0; i < browsers.length; i++) {
      var browser = browsers[i];
      tr    = sb_node(tbody, 'tr');
      td    = sb_node(tr, 'td');
      a     = sb_node(td, 'a');
      a.href = browser.link;
      a.target = '_new';
      img   = sb_node(a, 'img');
      img.src = '/smallbox4/images5/browsers/'+browser.icon;
      td    = sb_node(tr, 'td');
      td.style.paddingLeft = '15px';
      a     = sb_node(td, 'a', browser.name);
      a.href = browser.link;
      a.target = '_new';
     }   

     sb_node(cont, 'br');
     p = sb_node(cont, 'p', "For more information on this decision please ");
     a = sb_node(p, 'a', 'click here');
     a.href = 'http://www.smallboxsoftware.com/blog/post7';
     a.target = '_new';
     sb_text_node(p, '.');

 

     kit.append(document.body);
     var now = new Date();
     now.setTime(now.getTime() + 1000 * 60 * 60);
     document.cookie = "ieWarning=1;expires=" + now.toGMTString();
    }
   }
   sb_include('/smallbox4/js/xml.js', function() {state.ref1 = true;ref(state);});      
   sb_include('/smallbox4/js/windows.js', function() {state.ref2 = true;ref(state);});      
  }
 }
}

var toolTip;
function sb_tooltip(node, text) {
 sb_tooltip_clear();
 var pos = getPos(node);
 
 
 var div = toolTip = document.createElement('div');
 div.style.left = pos.left + 'px';
 div.className = 'sb_tooltip';
 textNode = document.createTextNode(text);
 div.appendChild(textNode);
 document.body.appendChild(div);

 if (pos.left + div.offsetWidth > document.body.offsetWidth - 5) {
  div.style.left = 'auto';
  div.style.right = '5px';
 }
 if (pos.top - 5 - div.offsetHeight > 5)
  div.style.top = pos.top - 5 - div.offsetHeight + 'px';
 else
  div.style.top = pos.bottom + 5 + 'px';

}

function sb_tooltip_clear() {
 if (toolTip) {
  document.body.removeChild(toolTip);
  toolTip = null;
 }
}


function sb_load_it(Node, Image) {
 var node = document.getElementById(Node);
 var image = document.getElementById(Image);
 if (node && image) { // && node.lastChild.nodeName == '#text') {
//  node.removeChild(node.lastChild);
  node.parentNode.removeChild(node);
  image.style.visibility = 'visible';
 }
}

function sb_setClass(node, name) {
 if (!sb_hasClass(node, name)) {
  node.className += (node.className ? ' ' : '')+name;
 }
}


function sb_toggleClass(node, name) {
 if (sb_hasClass(node, name)) {
  return;
 }
 else {
  for (var i = 2; i < sb_toggleClass.arguments.length; i++) {
   sb_deleteClass(node, sb_toggleClass.arguments[i]);
  }
  sb_setClass(node, name);
 }
}

function sb_hasClass(node, name) {
 return node.className.match("(^| )"+name+"( |$)");
}

function sb_deleteClass(node) {
 var split = node.className.split(' ');
 for (var i = 1; i < sb_deleteClass.arguments.length; i++) {
  var name  = sb_deleteClass.arguments[i];
  var index = split.indexOf(name);
  if (index > -1) {
   split.splice(index, 1);
   node.className = split.join(' ');
  }
 }
}
