isOP = (navigator.userAgent.indexOf('Opera') > 0)?true:false;
isIE = (navigator.appName=='Microsoft Internet Explorer' && !isOP)?true:false;
isMOZ = (navigator.appName.indexOf=='Netscape')?true:false;

if (!document.getElementById){
	document.getElementById = function(){return null;}
	document.getElementsByTagName = function(){return null;}
}

function addListener(elm, evtName, callback){
	if(elm.addEventListener){
		elm.addEventListener(evtName,callback, false);
	}
	else if(elm.attachEvent){
		elm.attachEvent("on"+evtName, callback);
	}
	else{
		elm["on"+evtName] = callback;
	}
	return callback;
}

//@@ Add Events @@//////////////////////////////////////////////////////////////////////////
//addListener(window, "load", setFlaHeads);
addListener(window, "load", setDictionaryPopUps);



if(location.href.indexOf('WBCMODE=AuthoringReedit') != -1 || location.href.indexOf('WBCMODE=AuthoringNew') != -1 || location.href.indexOf('wbc_purpose=Authoring') != -1){
	addListener(window, "load", initToggle);
}

function setDictionaryPopUps(){
	spans = document.getElementById('content').getElementsByTagName('span');	
	
    for(var i = 0; i < spans.length; i++){
    	if(spans[i].className == 'dictionaryWord'){
    		spans[i].onmousemove = function(e){showDictionary(e,this)};
    		spans[i].onmouseout = function(e){hideDictionary()};
    	}
    }
}


//@@ Functions @@/////////////////////////////////////////////////////////////////////

function setFlaHeads(){
	
	var arrHead = $('content').getElementsByTagName('h1');
	
	for (var i=arrHead.length;i!=0;i--){
		var head = arrHead[i-1];
		//if (head.className != 'fla') continue;
		
		if(canPlayFlash()){
			vars = 'strTxt=' + head.innerHTML ;
			url = '/Resources/include/boligabc/header.swf';

			flHead = document.createElement('div');
			flHead.className = 'flHead';
			flHead.innerHTML = getFlashString(url,vars,700,23);
			
			head.parentNode.replaceChild(flHead,head);
		}else{
			head.className = '';
		}
	}
}
function objectEmbed(url,vars,w,h){
	if(canPlayFlash()){
		document.write(getFlashString(url,vars,w,h));
	}
}
function getFlashString(url,vars,w,h){
		return '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="'+w+'" height="'+h+'" id="kampagnefelt" align="" VIEWASTEXT><param name="FlashVars" value="'+ vars +'"><param name="movie" value="'+url+'"><param name="menu" value="false"><param name="quality" value="high"><param name="wmode" value="transparent"><embed src="'+url+'" menu="false" quality="high" wmode="transparent" width="'+w+'" height="'+h+'" FlashVars="'+ vars +'" name="" align="" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed></object>';
}

function showDictionary(e,obj){
	strDescription = "<div class=\"head\"></div><div class=\"section\"><div class=\"inner\"><h3>" + obj.innerHTML + "</h3>" + obj.nextSibling.innerHTML + "</div></div><div class=\"foot\"></div>";
	pos = findCursor(e);
	popup = $('dictionaryPopUp');
	popup.innerHTML = strDescription;
	setPosToCursor(popup,pos[0],pos[1]);
	popup.style.display = 'block';	
}
function hideDictionary(){
	$('dictionaryPopUp').style.display = 'none';
}

function findCursor(e) {
    var posX = 0;
	var posY = 0;
	if (!e) var e = window.event;

	if (e.pageX || e.pageY)
	{
		posX = e.pageX;
		posY = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		posX = e.clientX;
		posY = e.clientY;
		if (isIE)
		{
			posX += document.documentElement.scrollLeft;
			posY += document.documentElement.scrollTop;
		}
	}
	return [posX,posY];	
}

function setPosToCursor(elm,x,y,isPreview){
	elmW = elm.offsetWidth;
	elmH = elm.offsetHeight;
	
	x += 10;
	y += 10;
	
	if(x + elmW > (document.documentElement.clientWidth + document.documentElement.scrollLeft)){
		x -= elmW + 10;
	}
	if(y + elmH > (document.documentElement.clientHeight + document.documentElement.scrollTop)){
		y -= elmH + 10;
	}
	elm.style.left = x + "px";
	elm.style.top = y + "px";
}


//@@ FLASH CHECK @@//////////////////////////////////////////////////////////////////////////

minPlayer = 6;

function Flash_checkForPlugIn() {
  var plugin = (navigator.mimeTypes &&
  navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
  if (plugin) {
    var pluginversion = parseInt(plugin.description.substring(plugin.description.indexOf(".")-1))
    if(pluginversion >= minPlayer) {return true;}
  }
  return false;
}

// vbscript check for Flash ActiveX control in windows IE
if(isIE) {
  document.write(
    '<script language=VBScript>' + '\n' +
    'Function Flash_checkForActiveX()' + '\n' +
    'Dim hasPlayer, playerversion' + '\n' +
    'hasPlayer = false' + '\n' +
    'playerversion = 10' + '\n' +
    'Do While playerversion >= minPlayer' + '\n' +
    'On Error Resume Next' + '\n' +
    'hasPlayer = (IsObject(CreateObject(\"ShockwaveFlash.ShockwaveFlash.\" & playerversion & \"\")))' + '\n' +
    'If hasPlayer = true Then Exit Do' + '\n' +
    'playerversion = playerversion - 1' + '\n' +
    'Loop' + '\n' +
    'Flash_checkForActiveX = hasPlayer' + '\n' +
    'End Function' + '\n' +
    '<\/script>'
  );
}
function canPlayFlash(){
	if(isIE){
		return Flash_checkForActiveX();
	}else{
		return Flash_checkForPlugIn();
	}
}


//@@ CreunaMCMSLib functions @@//////////////////////////////////////////////////////////////////////////
function initToggle(){
	elems = getCheckBoxesByParentNodeClassName('mcmsLibToggle');
	for(i=0;i<elems.length;i++){
		
		//Show already active on onLoad
		if(elems[i].checked){
			elems[i].parentNode.parentNode.className = 'on';
		}
		
		//Attatch functions
		elems[i].onmousedown = setToggle;
		elems[i].onmouseout = maxCompat;
	}
}

function setToggle(){
	this.parentNode.parentNode.className = (this.checked)?'':'on';
}

function maxCompat(){
	this.checked = (this.parentNode.parentNode.className == 'on')?true:false;
}

function getCheckBoxesByParentNodeClassName(clName){
	checkBoxCollection = new Array();
	inputs = document.getElementsByTagName('input');
	for(i=0;i<inputs.length;i++){
		if(inputs[i].type == 'checkbox' && inputs[i].parentNode.className == clName){
			checkBoxCollection.push(inputs[i]);
		}
	}
	return checkBoxCollection;
}
