var scriptTags = document.getElementsByTagName("script");
for(var i=0;i<scriptTags.length;i++) {
	if(scriptTags[i].src && scriptTags[i].src.match(/mentalfactory\.js(\?.*)?$/)) {
    	var path = scriptTags[i].src.replace(/mentalfactory\.js(\?.*)?$/,'');
	}
}
document.write('<script type="text/javascript" src="'+path+'config.php"></script>');

var busy = false;
var iframe;
var doc;
var foo;

function doWysiwyg(form) {
return;
    var elements = Form.getElements(form);
    for (var i = 0; i < elements.length; i++) {
      var element = elements[i];
      if (element.type == 'hidden') {
        element_id = element.id;
        element_div = document.getElementById(element_id+'_frame');
        if (element_div)
            setHTML(element_id, element_id+'_frame');
      }
    }
}


Element.showAll = function(array) {
  Element.show.apply(Element.show, array)
}

Element.hideAll = function(array) {
  Element.hide.apply(Element.hide, array)
}


function start_onload(leftover) {
	if (leftover)
		leftovers=leftover;
}

sorting = false;
var ajax;
var sorting_element;
var leftovers;

// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
	function setCookie(name, value, expires, path, domain, secure) {
		var curCookie = name + "=" + escape(value) +
			((expires) ? "; expires=" + expires.toGMTString() : "") +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			((secure) ? "; secure" : "");
		document.cookie = curCookie;
	}
// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
	function getCookie(name) {
		var dc = document.cookie;
		var prefix = name + "=";
		var begin = dc.indexOf("; " + prefix);
		if (begin == -1) {
			begin = dc.indexOf(prefix);
			if (begin != 0) return null;
		} else
		begin += 2;
		var end = document.cookie.indexOf(";", begin);
		if (end == -1)
			end = dc.length;
		return unescape(dc.substring(begin + prefix.length, end));
	}
// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds
	function deleteCookie(name, path, domain) {
		if (getCookie(name)) {
			document.cookie = name + "=" +
				((path) ? "; path=" + path : "") +
				((domain) ? "; domain=" + domain : "") +
				"; expires=Thu, 01-Jan-70 00:00:01 GMT";
		}
	}
	
	function toggleCookie(name) {
	   if (getCookie(name)) {
	       deleteCookie(name,'/');
	   } else {
	       setCookie(name,'1',null,'/');
	   }
	}
// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"
	function fixDate(date) {
		var base = new Date(0);
		var skew = base.getTime();
		if (skew > 0)
			date.setTime(date.getTime() - skew);
	}

function startclock()
{
	var thetime=new Date();
	
	var nday=thetime.getDay();
	var day=thetime.getDate();
	var nmonth=thetime.getMonth()+1;
	var nyear=thetime.getYear();
	var nhours=thetime.getHours();
	var nmins=thetime.getMinutes();
	var nsecn=thetime.getSeconds();
	
	if (nday==0)
	  nday="Domingo ";
	if (nday==1)
	  nday="2&#170; feira ";
	if (nday==2)
	  nday="3&#170; feira ";
	if (nday==3)
	  nday="4&#170; feira ";
	if (nday==4)
	  nday="5&#170; feira ";
	if (nday==5)
	  nday="6&#170; feira ";
	if (nday==6)
	  nday="S&aacute;bado ";
	
	if (nmonth<10)
		nmonth="0"+nmonth;
	
	if (nsecn<10)
	 	nsecn="0"+nsecn;
	
	if (nmins<10)
	 	nmins="0"+nmins;
    if (nyear<1000)
        if (nyear>100)
            nyear+=1900;

	document.getElementById("lblData").innerHTML=nday+day+"."+nmonth+"."+nyear;
	document.getElementById("lblHora").innerHTML=nhours+" h "+nmins+" m "+nsecn+" seg";
	
	setTimeout('startclock()',1000);

}

function doZoom(url) {
window.open( "dinamic.php?ID=137&url="+url, "","resizable=1,HEIGHT=200,WIDTH=200,toolbar=0,directories=0,menubar=0,status=0,,location=0,scrollbars=0,copyhistory=0");
return false;
}

var sorting_blocks = false;

function blocks_sort(id) {
if (sorting_blocks) {
    $('list'+id).style.background='transparent';
var eles = document.getElementsByClassName('sorter');
for(i=0;i<eles.length;i++)
    Element.show(eles[i]);
Sortable.destroy(sorting_blocks);
sorting_blocks = false;
$('list-info').innerHTML='';
if (window.frames["preview_frame"])
    window.frames["preview_frame"].location=window.frames["preview_frame"].location;
    else
    window.parent.frames["contents"].location=window.parent.frames["contents"].location;
} else {
sorting_blocks = $('list'+id);
if (sorting_blocks) {
	    $('list'+id).style.background='#ffffcc';
	var eles = document.getElementsByClassName('sorter');
	for(i=0;i<eles.length;i++)
	    Element.hide(eles[i]);
	Element.show('sort'+id);
	Sortable.create('list'+id, {
	    onUpdate:function(){
	        new Ajax.Updater('list-info', 'ajax.php', {handle:'list'+id,
	            onComplete:function(request){
	                new Effect.Highlight('list'+id);
	            }, 
	            parameters:Sortable.serialize('list'+id), 
	            evalScripts:true, 
	            asynchronous:true
	        })
	    }
	});
	}	
}

}

///// Tree Functions

var droppables = [];
var draggables = [];

function tree_toggle(id) {
    return Element.visible($('treechilds'+id))?tree_colapse(id):tree_expand(id);
};

function tree_colapse(id) {
    deleteCookie("treeitem"+id,'/');
    var icon = $('icon'+id);
    var last_src='';
    if (icon) {
        last_src=icon.src;
        icon.src=$spinner;
    }
new Effect.BlindUp("treechilds"+id, {
    afterFinish:function(){
        icon.src = last_src;
        oicon = $('op_icon'+id);
        oicon.src=$plus;
    }
});
    return false;
}

function tree_expand(id) {
    setCookie("treeitem"+id,1,null,'/');
    var icon = $('icon'+id);
    var last_src='';
    if (icon) {
        last_src=icon.src;
        icon.src=$spinner;
    }
new Effect.BlindDown("treechilds"+id, {
    afterFinish:function(){
        icon.src = last_src;
        oicon = $('op_icon'+id);
        oicon.src=$minus;
    }
});
return false;
}

var prefix='';

function tree_load(id,el) {
    setCookie("treeitem"+id,1,null,'/');
    var icon = $('icon'+id);
    if (icon)
        icon.src=$spinner;
if (document.all)
    id = 0;
    tree_clean();
    if (el==null)
        el='treeitem'+id;
var myAjax = new Ajax.Updater(el, prefix+'tree2.php', {
    onComplete:function(request){
            new Effect.Highlight('treeitem'+id);
			correctPNG();
    },
    parameters:'ID='+id,
    evalScripts:true,
    asynchronous:true
    });
return false;
}

function tree_drag(id) {
    tree_clean();
        var elements = document.getElementsByClassName('treeitem');
        elements.reverse(true).each(function(value) {
            draggables.push(new Draggable(value,{
                handle:'treeicon',
                revert:true
            }));
            Droppables.add(value,{ 
                onHover:tree_hover, 
                onDrop:tree_drop 
            });
            droppables.push(value);
        })
        $('treeitem'+id).style.background='#CCFFCC';
}

function page_init() {
    tree_load(0);
}
function scroll_init() {
    Event.observe($('statusbar'),"mousedown",function(e){
if (startX<0) {
    startX=Event.pointerX(e);
$('statusbar').style.background='#FFFFCC';
}
});
Event.observe(document.body,"mousemove",function(e){
if (startX>0) {
    var diff = Event.pointerX(e)-startX;
    if ((diff>2) || (diff<-2)) {
        cols=startCols+diff;
        cols=Math.min(500,cols);
        cols=Math.max(100,cols);
       top.document.getElementById('treeframe').cols=cols+",*";
    }
$('statusbar').style.background='#FFFFCC';
}
});
Event.observe(document.body,"mouseup",function(e){
startX=-1;
startCols = cols;
$('statusbar').style.background='#CCCCCC';
});
Event.observe(document.body,"mouseout",function(e){
startX=-1;
startCols = cols;
$('statusbar').style.background='#CCCCCC';
});
}

var childname;

function tree_hover(e1, e2, overlap) {
    if (childname)
        childname.style.background='transparent';
    childname = e2.childNodes[2];
    if (childname)
        childname.style.background='#FFFF99';
//    window.status=e2.id.substring(8,e2.id.length);
}

function tree_drop(e1,e2,overlap) {
    if (e1.id==e2.id)
        return;
    if (childname) {
        childname.style.background='transparent';
    }
    childname = e2.childNodes[2];
        var nome_destino='';
    if (childname)
        nome_destino = ' para '+childname.innerText;
        
    window.status=e2.id;
    var rootitem = $('treeitem0');
    if (rootitem)
       rootitem.style.background='transparent';

    if (confirm('Deseja realmente mover'+nome_destino+'?')) {
//        tree_load(e2.id.substring(8,e2.id.length));
//        alert("Moving from "+e1.id+" to "+e2.id);
new Ajax.Request('ajax2.php', {
            onComplete:function(request){
                if (request.responseText) {
                    alert(request.responseText);
                }
                window.location=window.location;
            }, 
            parameters:'drag='+e1.id.substring(8,e1.id.length)+'&drop='+e2.id.substring(8,e2.id.length), 
            evalScripts:true, 
            asynchronous:true
        })
    }
}


function tree_clean() {
        droppables.each(function(d){ Droppables.remove('treeitem'+d) });
        draggables.invoke('destroy');
        droppables=[];
        draggables=[];
}


function tree_toggle2(id) {
    if ($('list'+id))
        return Element.visible($('list'+id))?tree_colapse2(id):tree_expand2(id);
    else {
        oicon = $('op_icon'+id);
        oicon.src=$empty;
        return false;
    }
};

function tree_colapse2(id) {
    var icon = $('icon'+id);
    var last_src='';
    if (icon) {
        last_src=icon.src;
        icon.src=$spinner;
    }
new Effect.BlindUp("list"+id, {
    afterFinish:function(){
        icon.src = last_src;
        oicon = $('op_icon'+id);
        oicon.src=$plus;
    }
});
    return false;
}

function tree_expand2(id) {
    var icon = $('icon'+id);
    var last_src='';
    if (icon) {
        last_src=icon.src;
        icon.src=$spinner;
    }
new Effect.BlindDown("list"+id, {
    afterFinish:function(){
        icon.src = last_src;
        oicon = $('op_icon'+id);
        oicon.src=$minus;
    }
});
return false;
}

function correctPNG() 
   {
	if (document.all)
		for(var i=0; i<document.images.length; i++)
		{
			var img = document.images[i]
			var imgName = img.src.toUpperCase()
			if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
			{
				try {
					var imgID = (img.id) ? "id='" + img.id + "' " : ""
					var imgClass = (img.className) ? "class='" + img.className + "' " : ""
					var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
					var imgStyle = "display:inline-block;" + img.style.cssText 
					if (img.align == "left") imgStyle = "float:left;" + imgStyle
					if (img.align == "right") imgStyle = "float:right;" + imgStyle
					if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle		
					var strNewHTML = "<span " + imgID + imgClass + imgTitle
					+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
					+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
					+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
					img.outerHTML = strNewHTML
					i = i-1
				} catch (e) {
					
				}
			}
		}
   }
Event.observe(window,'load', correctPNG);
