/* $Id: scripts.js,v 1.15 2006/12/17 19:15:16 erkko Exp $ */

function chStyleNew (elem, uniqueId, toggleORactivate, classNamePrefix, setActiveLang) {
	var e_debug					= false;
	if (e_debug) var debugStr	= '';
	
	//got prefix?
	var classPre				= (classNamePrefix.length > 0) ? classNamePrefix + '_' : '';
	if (e_debug) debugStr		+= 'classPre = ' + classPre + '\n';//sample usage
	
	var tabClass				= Array();
	var conClass				= Array();
	tabClass[0]					= classPre + 'inactiveTab';
	tabClass[1]					= classPre + 'activeTab';
	conClass[0]					= classPre + 'inactiveCon';
	conClass[1]					= classPre + 'activeCon';
	
	//clicked tab id
	var elemId					= elem.getAttribute('id');
	//clicked tab idx
	var elemIdx					= elemId.split('_');
	elemIdx						= elemIdx[0];
	//clicked tab class
	var elemClass				= elem.className;
	//clicked tab class translated to state
	var elemState				= (elemClass == tabClass[0]) ? 0 : 1;
	var elemStateReversed		= (elemState == 1) ? 0 : 1;
	var newState				= (toggleORactivate == 'toggle') ? elemStateReversed : 1;
	var newTabClass				= tabClass[newState];
	var newConClass				= conClass[newState];
	
	//content element with same index as tab
	var elemConIdStr			= elemIdx + '_' + uniqueId + '_conId';
	var elemCon					= document.getElementById(elemConIdStr);
	//its class
	var elemConClass			= elemCon.className;

	//deactivate all AND (activate OR toggle) current
	for (i = 0; i < 35; i++) {//IE and its getElementsByName misimplementations...
		var xa					= document.getElementById('a' + i + '_' + uniqueId + '_tabId');
		if (xa && typeof(xa) == 'object') xa.className = tabClass[0];
		var xb					= document.getElementById('a' + i + '_' + uniqueId + '_conId');
		if (xb && typeof(xb) == 'object') xb.className = conClass[0];
	}
	elem.className				= newTabClass;
	elemCon.className			= newConClass;

	if (e_debug) alert(debugStr);
}


//courtesy of http://www.bbc.co.uk/topgear/
//Allow IE4 to emulate getElementById.
if (!document.getElementById && document.all)
	document.getElementById = function (id) {return document.all[id];}

if (!yesno || yesno == "undefined")			var yesno	= "yes";
if (!zeroone || zeroone == "undefined")	var zeroone	= "1";

function ho2(me) {if (me.className == '') { me.className = 'hoverBg' } else { me.className = '' };}

function hoverme (me, state, hoverbg) {
	//plan: kui state == 1, uurime, mis algne class oli ja lisame sinna 
	//sappa " hoverBg", kui state == 0, uurime kas klassinimes on hoverBg 
	//ja eemaldame
	var initClass	= me.className.split(' ');
	if (!hoverbg ) hoverbg = 'hoverBg';
	var targetClass	= '';
	if (state == 1) {
		for (var i in initClass) {
			if (initClass[i] != hoverbg) targetClass += initClass[i] + ' ';
		}
		targetClass += hoverbg;
	} else {
		for (var i in initClass) {
			if (initClass[i] != hoverbg) targetClass += initClass[i] + ' ';
		}
	}
	//alert( 'targetClass=' + targetClass + '\n' + 'initclass=' + initClass );
	me.className = targetClass;
}

function changeBg (what, toWhat) {what.bgColor = toWhat}
function lnk (whereTo) {window.location = whereTo}
function lnkNew (whereTo) {var peep = window.open(whereTo, 'uniqueTimeTag')}
function changeStatus (toWhat) {window.status = toWhat}
function formChanged () {}
/* TODO: universal, listingForm - seadku miski lipp 
püsti, et vormi muudeti vms see võiks öelda display 
block "suurele punasele nupule" ... */
function opensized (url, h, w) {
	var t = l = 10;
	var peep = window.open(url, 'peep', 'status=' + zeroone + ', toolbar=' + yesno + ', location=' + yesno + ', scrollbars=' + zeroone + ', resizable=' + yesno + ', top=' + t + ', left=' + l + ', height=' + h +' , width=' + w);
}

function openconfigured (url, w, h, sta, tool, loc, scroll, resize, top, left) {
	//var t = l = 10;
	var peep = window.open(url, 'ssss', 'status=' + sta + ', toolbar=' + tool + ', location=' + loc + ', scrollbars=' + scroll + ', resizable=' + resize + ', top=' + top + ', left=' + left + ', height=' + h +' , width=' + w);
}

function chgImg (whichOne, toWhat) {if (document.images)document.images[whichOne].src = toWhat;}

function chgImgX (whichOne, toWhat) {
	var fadeSpeed = 1;
	var kes = document.images[whichOne];
	var was = toWhat;
	if (document.images) kes.src = was;
}

function chPicLnk (whichOne, toWhat, xx) {
	for (i=0; i <= 30; i++) {
		var picSrc = (xx == i) ? 'img/1pix_orange.gif' : 'img/spacer.gif';
		//if (document.getElementById('imgLinks_' + i))//not good for nn47
		if (document.images['imgLinks_' + i]) document.images['imgLinks_' + i].src = picSrc;
		if (document.images['imgLnk_' + i]) document.images['imgLnk_' + i].src = picSrc;
	}
	if (document.images) document.images[whichOne].src = toWhat;
}


function chgThisImg (hisNum, color) {
	var activeImgString = 'thumbImage_' + hisNum;
	//alert(activeImgString);
	var activeImage = document.images[activeImgString];
	if (activeImage.style) {
		var color = "#" + color;
		activeImage.style.borderColor = color;
		//activeImage.style.borderColor = color;
	}

	var fadeSpeed = 0.5;
	mis = hisNum;
	kes = document.images['fakt'];
	if (document.all && kes.filters) {
		kes.src = NewImg[mis];
		/*
		with (kes) {
			style.filter = "revealTrans(duration = fadeSpeed, transition = 12)";
			filters.revealTrans.Apply();
			src = NewImg[mis];
			filters.revealTrans.Play();
		}*/
	}	else {
		kes.src = NewImg[mis];
	}

	var activeImg = hisNum;
	document.forms['activeThumbForm'].elements['activeThumb'].value = hisNum;
	for (i = 0; i < document.images.length; i++) {
		if (document.images[i].name.indexOf("thumbImage_") != -1 && document.images[i].name != "thumbImage_" + hisNum) {
			if (document.images[i].style) document.images[i].style.borderColor = "#FFFFFF";
		}
	}
	//alert(document.forms['activeThumbForm'].elements['activeThumb'].value);
	//alert(activeImg);

}

function changeBorder (image, imgNum, color) {
	var color = "#" + color;
	if (image.style && imgNum != document.forms['activeThumbForm'].elements['activeThumb'].value) image.style.borderColor = color;
}

//rewrite changeText() AND changeText2(), add param called element('myCell')
function changeText (i) {
	var text = '<span>';//
	text += txt[i];
	text += '<\/span>';//
	if (document.getElementById) {
		document.getElementById('myCell').innerHTML = text;
	} else if (document.all) {
		document.all.myCell.innerHTML = text;
	} else if (document.layers) {
		document.layers['myILayer'].document.layers['myLayer'].document.open()
		document.layers['myILayer'].document.layers['myLayer'].document.write(text);
		document.layers['myILayer'].document.layers['myLayer'].document.close();
	}
	return true;
}

function changeText2 (i) {
	var text = '<span>';
	text += txt2[i];
	text += '<\/span>';
	if (document.getElementById) {
		document.getElementById('myCell2').innerHTML = text;
	} else if (document.all) {
		document.all.myCell2.innerHTML = text;
	} else if (document.layers) {
		document.layers['myILayer2'].document.layers['myLayer2'].document.open()
		document.layers['myILayer2'].document.layers['myLayer2'].document.write(text);
		document.layers['myILayer2'].document.layers['myLayer2'].document.close();
	}
	return true;
}

//TODO: rekursiivseks, idx => value, rekursioonil suurem whitespace...
function arrToText(arr) {
	if (arr && arr.length > 0) {
		var txt = "";
		//var txt = arr.toSource();//js v1.3 only, probably...
		for (var i in arr) {
			txt += '['+i+'] => '+arr[i] + ";\n";	
		}
		return txt;
	}
}

function getCookie (name) {
	var arg		= name + "=";
	var alen	= arg.length;
	var clen	= document.cookie.length;
	var i		= 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) return "here";
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
}




/*
//TODO: vana tabimajandus KEELTE puhul lasi neid selecte manipuleerida
selle väljakutse oli järgmine: chStyle(this.id, 'e_i_:e_a_', '%s', '%s', 1);
viimane param on see setActiveLang

if (setActiveLang == 1 && document.getElementById('copyFromLang')) {
	//var copyFromLangSelect	= document.forms['newsForm'].elements['copyFromLang'];
	//var copyToLangSelect	= document.forms['newsForm'].elements['copyToLang'];
	var copyFromLangSelect	= document.getElementById('copyFromLang');
	var copyToLangSelect	= document.getElementById('copyToLang');
	var copyLangSubmit		= document.getElementById('copyLangSubmit');
	
	var copyFromLangOpt		= copyFromLangSelect.options;
	var copyToLangOpt		= copyToLangSelect.options;
	
	var debugStr = '';
	for (var idx = 0; idx < copyToLangOpt.length; idx++) {
		//var idx in activeLangOpt --huvitav miks SIIN on peale options massiivi 
		//veel muu pask ka hulgas ja PEAB pikkust loendama ja for in ei tööta?!
		//debugStr += idx + ', ' + activeLangOpt[idx].value + '\n';
		if (copyToLangOpt[idx].value == trShitNr)copyToLangSelect.selectedIndex	= idx;
	}
	//see vaja eraldi funci kirjutada, selecti iligutamisel peab ju taaskontrollima
	var subStat = (copyFromLangSelect.selectedIndex == copyToLangSelect.selectedIndex) ? false : true;
	//copyLangSubmit.disabled = subStat;
	
	var activeLangField		= document.getElementById('activeLang');
	activeLangField.value	= trShitNr;
	//alert(activeLangField.value + '\n\n' + debugStr);
}
*/
function changeTextx (target, srcArr, i, btnId) {
	var text = srcArr[i];
	if (document.getElementById) {
		document.getElementById(target).innerHTML = text;
	}	else if (document.all) {
		document.all.target.innerHTML = text;
	}	else if (document.layers) {
		/*document.layers['myILayer'].document.layers['myLayer'].document.open()
		document.layers['myILayer'].document.layers['myLayer'].document.write(text);
		document.layers['myILayer'].document.layers['myLayer'].document.close();*/
	}

	//alert(typeof(btnId) +' - '+ btnId.className +' - '+ typeof(btnId.className));
	//alert(typeof(btnId) +' - '+ btnId.id +' - '+ typeof(btnId.id));
	var styleOrigin			= btnId.className.split('_');
	var styleActiveArr		= new Array();
	var styleInactiveArr	= new Array();
	for (var x in styleOrigin) {
		if (styleOrigin[x] == 'i' || styleOrigin[x] == 'a') {
			styleActiveArr[x]		= 'a';
			styleInactiveArr[x]		= 'i';
			//alert('from i to a in ' + x);
		} else {
			styleActiveArr[x]		= styleOrigin[x];
			styleInactiveArr[x]		= styleOrigin[x];
			//alert('from i to a in ' + x);
		}
	}
	var styleActive		= styleActiveArr.join('_');
	var styleInactive	= styleInactiveArr.join('_');
	//alert('styleActive = '+styleActive+', styleInactive = '+styleInactive);
	btnId.className = styleActive;

	var idOrigin = btnId.id.split('_');
	var thisOnesIdNum = parseInt(idOrigin[1]);
	//alert('thisOnesIdNum = '+thisOnesIdNum);
	for (var c = 1; c < 50; c++) {
	//TODO: selle asemel võiks proovida igal aktiveerimisel salvestada aktiveeritu id numbrilise väärtuse muutujasse ja inaktiveerimisel teha seda ainult temaga, ilma igat võimalikku saasta taga otsides...
		if (c != thisOnesIdNum && document.getElementById(idOrigin[0] + '_' + c))
			document.getElementById(idOrigin[0] + '_' + c).className = styleInactive;
	}
	
	return true;
}


function chStyle (elem, styles, elemGroup, uniqueId, setActiveLang) {
	var stylesExploded = styles.split(":");
	var elementsExploded = elemGroup.split(",");

	for (var xyz in elementsExploded) {
			//alert('searching for element ' + elementsExploded[xyz]);
		var trShitNr = elementsExploded[xyz];
		//var trShitNr = trShit[0];
		var newTdStyle = stylesExploded[0] + 'td';
		var newTrStyle = stylesExploded[0] + 'tr';
			//alert('INactive: tX' + uniqueId + '_' + trShitNr);
		var xy = document.getElementById('tr' + uniqueId + '_' + trShitNr);
		var xx = document.getElementById('td' + uniqueId + '_' + trShitNr);
		xy.className = newTrStyle;
		xx.className = newTdStyle;
		//e.blur();
	}
	var trShit = elem.split("_");
	var trShitNr = trShit[1];
		//alert('active: tX' + uniqueId + '_' + trShitNr);
		
	if (setActiveLang == 1 && document.getElementById('copyFromLang')) {
		//var copyFromLangSelect	= document.forms['newsForm'].elements['copyFromLang'];
		//var copyToLangSelect	= document.forms['newsForm'].elements['copyToLang'];
		var copyFromLangSelect	= document.getElementById('copyFromLang');
		var copyToLangSelect	= document.getElementById('copyToLang');
		var copyLangSubmit		= document.getElementById('copyLangSubmit');
		
		var copyFromLangOpt		= copyFromLangSelect.options;
		var copyToLangOpt		= copyToLangSelect.options;
		
		var debugStr = '';
		for (var idx = 0; idx < copyToLangOpt.length; idx++) {
			//var idx in activeLangOpt --huvitav miks SIIN on peale options massiivi 
			//debugStr += idx + ', ' + activeLangOpt[idx].value + '\n';
			if (copyToLangOpt[idx].value == trShitNr)
				copyToLangSelect.selectedIndex	= idx;
		}
		//see vaja eraldi funci kirjutada, selecti iligutamisel peab ju taaskontrollima
		var subStat = (copyFromLangSelect.selectedIndex == copyToLangSelect.selectedIndex) ? false : true;
		//copyLangSubmit.disabled = subStat;
		
		var activeLangField		= document.getElementById('activeLang');
		activeLangField.value	= trShitNr;
		//alert(activeLangField.value + '\n\n' + debugStr);
	}
	
	var newTdStyle = stylesExploded[1] + 'td';
	var newTrStyle = stylesExploded[1] + 'tr';
	var xy = document.getElementById('tr' + uniqueId + '_' + trShitNr);
	var xx = document.getElementById('td' + uniqueId + '_' + trShitNr);
	xy.className = newTrStyle;
	xx.className = newTdStyle;
	//f.blur();
}


//20061217, http://www.howtocreate.co.uk/tutorials/javascript/browserspecific
function findPosition(oLink) {
  if (oLink.offsetParent) {
    for (var posX = 0, posY = 0; oLink.offsetParent; oLink = oLink.offsetParent) {
      posX += oLink.offsetLeft;
      posY += oLink.offsetTop;
    }
    return [ posX, posY ];
  } else {
    return [ oLink.x, oLink.y ];
  }
}

//20061217, http://www.howtocreate.co.uk/tutorials/javascript/dhtml
function chbgcolor () {//TODO: t99lepanna
	if (myReference.style) { myReference = myReference.style; }
	if (myReference.background) {
		//supported by most browsers
		//like Gecko browsers and the IE series
		myReference.background = '#00ff00';
	} else if (myReference.backgroundColor) {
		//supported by most browsers
		myReference.backgroundColor = '#00ff00';
	} else if (myReference.bgColor) {
		//used by layers browsers
		myReference.bgColor = '#00ff00';
	} else {
		//FAILURE, there is no way to change the background colour
	}
}


//20061217, http://www.howtocreate.co.uk/tutorials/javascript/dhtml
function alertSize() {//TODO: töölepanna, alert on demo
  var myWidth = 0, myHeight = 0;
  if (typeof(window.innerWidth) == 'number') {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  window.alert('Width = ' + myWidth);
  window.alert('Height = ' + myHeight);
}


//20061217, http://www.howtocreate.co.uk/tutorials/javascript/dhtml
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if (typeof(window.pageYOffset) == 'number') {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

// vim: ts=2 sw=2 ff=unix
// vim600: fdm=marker


