/*
 * 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.
 *
 */
var mouseX;
var mouseY;
var disable_events = 0;
var boxModel = false;
var drag_interval = null;
var mouseMoveAction = null;
var image_list = new Array();
var preloads   = new Array();
var onloads    = new Array();
var moveStart = 0;
var drag_object = 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 sb_onload() {
 var testNode = document.createElement('div');
 testNode.style.width = '100px';
 testNode.style.border = 'solid 2px black' ;
 testNode.style.visibility = 'hidden' ;
 testNode.style.position = 'absolute';
 document.body.appendChild(testNode);

 document.onmousemove = mouse_pos;
 
 if(testNode.offsetWidth == 104)
  boxModel = true;
 else
  boxModel = false;

 for(var i = 0; i < onloads.length; i++) {
  onloads[i]();
 }
}


function mouse_pos(evt) {
 if(evt) {
  mouseX = evt.pageX ;
  mouseY = evt.pageY ;
 }
 else {
  if(boxModel == true) {
   mouseX = event.clientX+document.documentElement.scrollLeft ;
   mouseY = event.clientY+document.documentElement.scrollTop ;
  }
  else {
   mouseX = event.clientX+document.body.scrollLeft ;
   mouseY = event.clientY+document.body.scrollTop ;
  }
 }
 if(mouseMoveAction != null) {
  mouseMoveAction();
 } 
 if(drag_object != null) {
  drag_object.onmousemove();
 }
}


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) {
 this.Node = Node;
 this.start = drag_start;
 this.onmousemove = drag_move; 
 this.onmouseup   = drag_stop;

 this.default_start = drag_start;
 this.default_onmousemove = drag_move; 
 this.default_onmouseup   = drag_stop;

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

}



function drag_start() {
 document.onmouseup = this.onmouseup;
 document.body.style.cursor = this.Node.style.cursor;
}

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

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

function sb_preload() {
 for(var i =0 ; i < preloads.length; i++) {
  tmp = new Image();
  tmp.src = preloads[i];
 }
}
onloads[onloads.length] = sb_preload;

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

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

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/images/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 escape_node;
var escape_mode = 0;
var sbEscapes = new Array();
var escapeDepth = 0;

function escapeNode(Node, mode, doc) {
 escape_node = Node;
 escape_mode = escape_mode;
 var o = new Object();
 o.node = Node;
 o.mode = mode;
/* 
 if(!sbEscapes[escapeDepth]) {
  sbEscapes[escapeDepth] = new Array();
 }
 sbEscapes[escapeDepth].push(o);
*/
 sbEscapes[escapeDepth] = o;

 if(!doc)
  document['onkeypress']=clearEscapeNode;
 else
  doc['onkeypress']=clearEscapeNode;
}
                                                                                                 
function clearEscapeNode(evt)
{
 if(evt) {
  if(evt.keyCode==27) 
   escapeClear();
 }
 else if(event) {
  if(event.keyCode==27)
   escapeClear();
 }
}

function escapeRemove(o) {
 if(o) {
  if(o.mode == true)
   o.node.parentNode.removeChild(o.node);
  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.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/images/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();
 }

 if(element.style.display == 'none') {
  thisId.className = "advanced_menu_on";
  element.style.display = 'block';
  status[status.length] = ID;
 }
 else {
  thisId.className = "advanced_menu";
  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;//status.join(',').replace(/^,/, "").replace(",,", ",");

}

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_load_edit_buttons(minus, id, title, link) {
 var node = document.getElementById(id);
 var img = document.createElement('img'); 
 if(minus == 0)
  img.src = "/smallbox4/images/plus.gif";
 else
  img.src = "/smallbox4/images/minus.gif";
 pos = getPos(node);
 img.style.position = 'absolute';
 img.style.zIndex = 1;
 img.style.left = (pos.left - 5)+"px";
 img.style.top  = pos.top+"px"
 img.style.cursor = 'pointer';
 
 setOpacity(img, 40);
 img.onclick = function() {window.location.href = link;}
 img.onmouseover = function() {sb_edit_button(node,img,  title)}

 document.body.appendChild(img);


}

function sb_edit_button(node, img, title) {
 setOpacity(img, 100);

 img.style.zIndex = 3;
 pos = getPos(node);
 var div = document.createElement("div");
 div.style.position = 'absolute';
 div.style.zIndex = '501';
 div.innerHTML = title;
 div.style.top  = pos.top+ 15 + "px";
 div.style.left = pos.left - 5  + "px";
 div.style.border = 'solid 1px #666666';
 div.style.backgroundColor = '#cccccc';
 div.style.padding = '2px 7px';
 div.style.font = 'normal 11px Verdana, sans-serif';
 document.body.appendChild(div);


// var cont = node.parentNode;
 var cont = node;
 if(cont.childNodes.length > 1) {
/*
  var clear = document.createElement('br');
  clear.className = 'clearer';
  cont.appendChild(clear);  
*/
  var contPos = getPos(cont);

  var overlay = document.createElement('div');
  overlay.style.position = 'absolute';
  overlay.style.zIndex = 2;
  overlay.style.backgroundColor = 'black';
  setOpacity(overlay, 60);

  overlay.style.top = 0;
  overlay.style.left = 0;
  overlay.style.height = contPos.top+"px";
  overlay.style.width  = '100%';
  document.body.appendChild(overlay);

  var overlay2 = overlay.cloneNode(true);
  overlay2.style.top = contPos.top+"px";;
  overlay2.style.left = 0;
  overlay2.style.height = cont.offsetHeight+"px";
  overlay2.style.width  = contPos.left+"px";
  document.body.appendChild(overlay2);

  var overlay3 = overlay.cloneNode(true);
  overlay3.style.top = contPos.top+"px";;
  overlay3.style.left = contPos.right+"px";
  overlay3.style.height = cont.offsetHeight+"px";
  overlay3.style.width  = document.body.offsetWidth - contPos.right + 'px';
  document.body.appendChild(overlay3);



  var overlay4 = overlay.cloneNode(true);
  overlay4.style.top = contPos.top + cont.offsetHeight+"px";
  overlay4.style.left = 0;
  overlay4.style.height = document.body.offsetHeight - contPos.top - cont.offsetHeight + "px";
  overlay4.style.width  = '100%';
  document.body.appendChild(overlay4);

 }

 img.onmouseout = function() {
  if(cont.childNodes.length > 1) {
   document.body.removeChild(overlay);
   document.body.removeChild(overlay2);
   document.body.removeChild(overlay3);
   document.body.removeChild(overlay4);
//   cont.removeChild(clear);
   
  }
  img.style.zIndex = 1;
  setOpacity(img, 40);
  document.body.removeChild(div);
  img.onmouseout = '';
 }
}

function sb_window(thisId, id) {
 escapeClear();
 var win = document.getElementById(id);
 pos = getPos(thisId);

 win.style.display = 'block';
 if(pos.left > 20)
  win.style.left = (pos.left - win.offsetWidth - 20)+"px";
 else
  win.style.left = "0px";
 var top = (pos.top - win.offsetHeight / 2);
 win.style.top = (top > 0 ? top : 0) + "px";


 escapeNode(win);
}



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); 
}

