var lcount = 0;
var contentID = '';
var contentArr = new Array();

function view(objID) {
	var divs;
	divs = document.getElementsByTagName("DIV");	
	for(var i=0; i<divs.length; i++){	
		if(divs[i].className=='ctab')divs[i].style.display = 'none';
	}	
	document.getElementById("ctab_"+objID).style.display = 'block';
}

function swTab(dir) {
	var selObj = document.getElementById('tabnav');
	var selIndex = selObj.selectedIndex;
	if(dir=='next')selIndex++;
	else if(dir=='prev')selIndex--;
	if (selIndex==-1 || selIndex==selObj.options.length) selIndex=0;
		selObj.selectedIndex = selIndex;	
		view(selIndex);
}

function printit(){
if (window.print) {
    window.print() ;
} else {
    var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
    WebBrowser1.ExecWB(6, 2);//Use a 1 vs. a 2 for a prompting dialog box    WebBrowser1.outerHTML = "";
}
}


sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

function windowSize() {  
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    winWidth = window.innerWidth;
    winHeight = window.innerHeight;
  } else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    winWidth = document.documentElement.clientWidth;
    winHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    winWidth = document.body.clientWidth;
    winHeight = document.body.clientHeight;
  }
}

function setLayout() {
  var hmargin = 110; // total margin
  var vmargin = 130; // total margin
  	if (document.getElementById) {
	
          windowSize();          
          if(winWidth>=400 && winHeight>=400){
            var topbarEl   = document.getElementById('topbar');
            var dropMenuEl = document.getElementById('dropmenu');
            var contentEl  = document.getElementById('content');
            var footerEl   = document.getElementById('footer');         

            contentEl.style.height = 'auto'; 
            var containerEl = document.getElementById('container');                                              
            if((winHeight - vmargin) >= containerEl.offsetHeight){
              var containerH = containerEl.offsetHeight;
              var topH = topbarEl.offsetHeight;
              var dropH = dropMenuEl.offsetHeight;
              var contentH = contentEl.offsetHeight;
              var footerH = footerEl.offsetHeight;      
                                  
              contentEl.style.height = (winHeight - (topH + dropH + footerH + vmargin)) + 'px';     

            }
            else {      
              contentEl.style.height = 'auto';         
            }
            
          } 
     if(window.parent.frames.length!=0){
		if(window.parent.frames['fnTree']!=null){
				window.parent.frames['fnTree'].checkCmdWin();
			
		}
	 }
     }  

}

function checkForEditMode(){
var editor = false;
	for (i=0; i<document.all.length; i++){
		if(document.all(i).className=='editblock') {
			document.all(i).contentEditable = 'true';
			contentArr[document.all(i).id] = document.all(i).innerHTML;
			editor = true;
		}	
	}
	if(editor==true && parent.fnTree!=null)parent.fnTree.checkCmdWin();
}
  
window.onload = function() {
  //setLayout();
  checkForEditMode();
}
  
window.onresize = function() {
  //setLayout();
}



function setToolPos() {	
	var parentEl = event.srcElement;	
	var toolbar = document.getElementById('editToolbar');
	
    if(toolbar!=null && parentEl.tagName!='HTML'){
		//contentID = '';
		while(parentEl.tagName != 'BODY'){
			if(parentEl.className=='editblock' || parentEl.className=='dirty') contentID = parentEl.id;
			if(parentEl==toolbar) return false;
		parentEl = parentEl.parentElement;
		}
		
		if(contentID!=''){	
			rePosToolBar();
			checkEditStatus();
		}
		else {
			toolbar.style.visibility = 'hidden';
		}
	}
}

function rePosToolBar(){
var toolbar = document.getElementById('editToolbar');
	if(contentID!=''){
		var doc = document.getElementById(contentID);				
		var top = doc.offsetTop;
		var left = doc.offsetLeft;
		var width = doc.offsetWidth;	
		var height = doc.offsetHeight;
		
		if (window.innerHeight)	{
			pos = window.pageYOffset
		}
		else if (document.documentElement && document.documentElement.scrollTop) {
			pos = document.documentElement.scrollTop
		}
		else if (document.body)	{
			pos = document.body.scrollTop
		}
		
		if(top < pos) {
			top = top + height;
		}
		else {
			top = top - toolbar.offsetHeight
		}		
		
		toolbar.style.visibility = 'visible';			
		toolbar.style.pixelTop = top;			
		toolbar.style.pixelLeft = (left + width) - toolbar.offsetWidth;	
	}
}

document.onmousedown = setToolPos;
window.onscroll = rePosToolBar;

function exCmd(cmd){	
	var doc = document.getElementById(contentID);
	if(cmd!='Save'){
	alert(cmd)
		document.execCommand(cmd);  
		checkEditStatus();		
	}
	else {
		//save content		
		document.forms['savecontent'].content.value = doc.innerHTML;		
		document.forms['savecontent'].contentid.value = contentID;
		document.forms['savecontent'].submit();
		contentArr[contentID] = doc.innerHTML;
		checkEditStatus();	
	}
}

function checkEditStatus(){	
	if(contentID!=''){
		var doc = document.getElementById(contentID);	
		if(doc.innerHTML != contentArr[contentID]) {
			document.getElementById('savebutton').src = '/system/edit/editor/skins/silver/toolbar/save.gif';
			doc.className = 'dirty';
		}
		else {
			document.getElementById('savebutton').src = '/system/edit/editor/skins/silver/toolbar/save.disabled.gif';
			doc.className = 'editblock';
		}
		
	}
}

function swMode(){
	if(window.parent.fnTree) window.parent.location = window.location;
	else window.location = '/system/?referer='+window.location;
}

function login(){   
if(event.keyCode==42){
 lcount++;
 setTimeout("lcount=0",1000);
}
if(lcount>2) window.location = '/system/login.php?referer=' + window.location;
}

document.onkeyup = function() {	
	window.event.cancelBubble = true;
	checkEditStatus();
}  

document.onkeypress = function() {	
	checkEditStatus();
	login();
}  
/*
window.onbeforeunload = function() {    

	var isDirty;
	var divs = document.getElementsByTagName('div');
	for(i=0;i<divs.length;i++){
		if(divs[i].className == 'dirty'){
			isDirty = true;
		}
	}
	if(isDirty){	
		event.returnValue = "You have unsaved changes on this page?";
		return false;
	}
}
*/
