/*
############################## bitCMS ###############################
### |-----------------------------------------------------------| ###
### |      COPYRIGHT 2005 by bitsolution.ch c/o Lukas Stalder   | ###
### |      IT IS NOT ALLOWED TO REDISTRIBUTE OR RESELL THIS     | ###
### |      SCRIPT IN ANY WAYS WITHOUT MY EXPLICIT PERMISSION!   | ###
### |      -----------------------------------------------      | ###
### |      Changing or removing logos or copyright no-          | ###
### |      tices is not allowed! Changing other code may        | ### 
### |      result in errors and not properly working of         | ### 
### |      the bitCMS. So be aware!                             | ### 
### |      Lukas Stalder, http://www.bitsolution.ch             | ### 
### |-----------------------------------------------------------| ###
#####################################################################
*/



// init
if (navigator.appName.indexOf("Microsoft")!=-1) muBrowser = "ie";
else muBrowser = "moz";

if (navigator.userAgent.toLowerCase().indexOf("macintosh")!=-1) muOs = "mac";
else muOs = "win";


muAddEvent(window, 'load', muSetStyles);
muAddEvent(window, 'load', muInitDragDrop);


function muChangeImgSrc(fromId, newSrc, maxW, maxH){
	var obj = muGetElement(fromId);
	var img = new Image();
	img.src = newSrc;
	if (newSrc != "" && img.width > 0){
		w = (img.width > maxW) ? maxW : img.width;
		h = (img.height > maxH) ? maxH : img.height;
	
		/*
		if (img.height > img.width || h >= maxH){
			height = h;
			width = img.width / img.height *h ;
		}else{
			width = w;
			height = img.height / img.width *w ;
		}
		*/
		if (w == maxW){
			width = w;
			height = img.height / img.width *w ;
		}else{
			height = h;
			width = img.width / img.height *h ;
		}
		
		obj.src = newSrc;
		obj.width = width;
		obj.height = height;
		obj.style.display = "inline";
	}else{
		obj.src = "/bitCMS/grfx/trans.gif";
		obj.width = 0;
		obj.height = 0;
	}
}

function muSetStyles(){
	var obj;
	winW = document.body.scrollWidth;
	winH = document.body.scrollHeight;
	
	// main abs layer for centerin editors
	if (muBrowser == "ie") {
		obj = muGetElement("muFullwidthLayer");	
		if (obj) obj.style.width = winW+"px";
	}
	
	// filebrowser icon ie fix
	if (muBrowser == "ie") {
		obj = muGetElements("muFilebrowser");
		for (var i=0; i < obj.length; i++){
			if (obj[i].style) obj[i].style.verticalAlign = "-1px";
		}
	}
}

function muInitDragDrop(){
	var theHandle = document.getElementById("muDragger");
	var theRoot   = document.getElementById("muLayer");
	if (theRoot) Drag.init(theHandle, theRoot);
}


function muAddEvent(obj, evType, fn, useCapture){
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be attached");
  }
}

function muGetElement(id){
	var obj;
	if (document.getElementById){
		obj = document.getElementById(id);	
	}else if (document.all){
		obj = document.all.id;	
	}
	return obj;
}

function muGetElements(name){
	var obj;
	obj = document.getElementsByName(name);	
	return obj;
}

function muCollapseExpand(id, button, ttcollapse, ttexpand){
	var obj = muGetElement(id);
	if (button.src.indexOf("collapse")!=-1){
		obj.style.display = "none";
		button.src = "/bitCMS/grfx/expand.gif";
		button.title = ttexpand;
	}else{
		obj.style.display = "block";
		button.src = "/bitCMS/grfx/collapse.gif";
		button.title = ttcollapse;
	}
	
}
