


window.onload = init;





var lcId = new Date().getTime();
var flashProxy;


function init() {

	if( document.getElementById("body_mainmenu") || document.getElementById("body_othermenu") ) initMenu();

	if( document.getElementById("fullscreen_window") ) flashProxy = new FlashProxy(lcId, 'modules/m_bastina/portal/media/JavaScriptFlashGateway.swf');

	if( document.getElementById("search") ) initSearchTabControls();
	if( document.getElementById("search_results") || document.getElementById("personal_results") ) coloriseSearchResults();

	if( document.getElementById("biography_chunks") ) initEditControls();

	if( document.getElementById("person_alive") ) document.getElementById("person_alive").onclick = function() { disableDeathDate(this.checked); }
	if( document.getElementById("person_relationship_alive") ) document.getElementById("person_relationship_alive").onclick = function() { disableRelationshipDate(this.checked); }

	if( document.getElementById("same_data") ) {

		document.getElementById("same_data").onclick = function() { copyOtherData(this.checked); }

		for(var i=0; i < document.forms[0].elements.length; i++) {

			document.forms[0].elements[i].onchange = function() { submitNew(this); }

		}
	}

	if( ("undefined" != typeof(editControls)) && (editControls == 1) ) editControlsInit();


}










function initSearchTabControls() {

	children = document.getElementById("search").childNodes;
	for(var i=0; i < children.length; i++) {

		if( (children[i].nodeName == "A") ) {

			children[i].onmouseover = function() { showThisSearchTab(this, 1); return false; }
			children[i].onclick = function() { showThisSearchTab(this); return false; }
		}
	}

}









function initEditControls() {

	children = document.getElementById("biography_chunks").childNodes;
	for(var i=0; i < children.length; i++) {

		if( (children[i].nodeName == "A") ) {


			if ( children[i].className == "add_page" ) {
				children[i].onmouseover = function() { if ( this.className == "add_page" ) this.firstChild.style.display = "block"; }
				children[i].onmouseout = function() { if ( this.className == "add_page" ) this.firstChild.style.display = "none"; }

				children[i].onclick = function() { return false; }
			}
		}
	}

}










var timeoutID;
var activeSearchTabRef;
function showThisSearchTab(thisRef, repeatLoad) {

	if(thisRef != null) activeSearchTabRef = thisRef;

	clearInterval( timeoutID );

	activateThis = false;

	children = document.getElementById("search").childNodes;
	for(var i=0; i < children.length; i++) {


		if( (children[i].nodeName == "A") ) {

			if( (children[i] == activeSearchTabRef) ) {

				children[i].className = "active";
				activateThis = true;
			}
			else children[i].className = "";
		}


		if (children[i].nodeName == "DIV") {

			if( (activateThis == true) ) {

				children[i].style.display = "block";
				activateThis = false;
			}
			else children[i].style.display = 'none';
		}

	}


	if (repeatLoad == 1) {

		timeoutID = setInterval( showThisSearchTab, 10, activeSearchTabRef );
	}
}












function coloriseSearchResults() {

	n = 0;

	if( document.getElementById("search_results") )	children = document.getElementById("search_results").childNodes;
	if( document.getElementById("personal_results") ) children = document.getElementById("personal_results").childNodes;

	for(var i=0; i < children.length; i++) {

		if( (children[i].nodeName == "SPAN") ) {

			if ((n % 2) != 0 ) children[i].style.backgroundColor = "#f5f5f5";
			n++;

		}
	}


}






function disableDeathDate(actionBoolean) {


	if( actionBoolean == true ) {

		document.getElementById("person_death_date").disabled = true;
		document.getElementById("person_death_month").disabled = true;
		document.getElementById("person_death_year").disabled = true;
		document.getElementById("person_death_town").disabled = true;
		document.getElementById("person_death_country").disabled = true;

		document.getElementById("person_death_date").style.backgroundColor = '#f1f1f1';
		document.getElementById("person_death_month").style.backgroundColor = '#f1f1f1';
		document.getElementById("person_death_year").style.backgroundColor = '#f1f1f1';
		document.getElementById("person_death_town").style.backgroundColor = '#f1f1f1';
		document.getElementById("person_death_country").style.backgroundColor = '#f1f1f1';
	}
	else {

		document.getElementById("person_death_date").disabled = false;
		document.getElementById("person_death_month").disabled = false;
		document.getElementById("person_death_year").disabled = false;
		document.getElementById("person_death_town").disabled = false;
		document.getElementById("person_death_country").disabled = false;

		document.getElementById("person_death_date").style.backgroundColor = '#f8f8f8';
		document.getElementById("person_death_month").style.backgroundColor = '#f8f8f8';
		document.getElementById("person_death_year").style.backgroundColor = '#f8f8f8';
		document.getElementById("person_death_town").style.backgroundColor = '#f8f8f8';
		document.getElementById("person_death_country").style.backgroundColor = '#f8f8f8';
	}

}






function disableRelationshipDate(actionBoolean) {


	if( actionBoolean == true ) {

		document.getElementById("person_spouse_end_day").disabled = true;
		document.getElementById("person_spouse_end_month").disabled = true;
		document.getElementById("person_spouse_end_year").disabled = true;

		document.getElementById("person_spouse_end_day").style.backgroundColor = '#f1f1f1';
		document.getElementById("person_spouse_end_month").style.backgroundColor = '#f1f1f1';
		document.getElementById("person_spouse_end_year").style.backgroundColor = '#f1f1f1';
	}
	else {

		document.getElementById("person_spouse_end_day").disabled = false;
		document.getElementById("person_spouse_end_month").disabled = false;
		document.getElementById("person_spouse_end_year").disabled = false;

		document.getElementById("person_spouse_end_day").style.backgroundColor = '#f8f8f8';
		document.getElementById("person_spouse_end_month").style.backgroundColor = '#f8f8f8';
		document.getElementById("person_spouse_end_year").style.backgroundColor = '#f8f8f8';
	}

}







function copyOtherData(actionBoolean) {

	document.getElementById("recipient_name").value = document.getElementById("client_name").value;
	document.getElementById("recipient_address").value = document.getElementById("client_address").value;
	document.getElementById("recipient_town").value = document.getElementById("client_town").value;
	document.getElementById("recipient_zip").value = document.getElementById("client_zip").value;


}


function submitNew() {

	document.forms[0].submit();
}








var pwin = null;
function newWindow(mypage, myname, w, h, fs, scroll) {
	LeftPosition = (screen.width) ? (screen.width - w) / 2 : 0;
	TopPosition = (screen.height) ? (screen.height - h) / 2 : 0;

	settings = "";

	if (fs == 1) {
		scroll = 0;
		w = window.screen.availWidth;
		h = window.screen.availHeight;
		TopPosition = 0;
		LeftPosition = 0;
		settings = 'fullscreen=1, status=0, toolbar=0, menubar=0, titlebar=no, screenX=0, screenY=0,';
	}

	settings += 'height=' + h + ',width=' + w + ',top=' + TopPosition + ',left=' + LeftPosition + ',resizable=0' + ',scrollbars=' + scroll;


	pwin = window.open(mypage, myname, settings)
    if (pwin == null) {
        alert("Za uspjesno kreiranje rodoslovnog stabla u Mojoj Bastini potrebna je pop-up funkcionalnost. Molimo da dozvolite pop-upe za ove stranice u Vasoj aplikaciji za blokiranje pop-upa.");
    }
    else if(pwin.window.focus) {
        pwin.window.focus();
    }
}






var pop_newWin = null; 
function newWinPopUp(strURL, strType, strHeight, strWidth) { 

	if (pop_newWin != null && !pop_newWin.closed) pop_newWin.close(); 
	var strOptions=""; 
	if (strType=="console") strOptions="resizable,height="+ strHeight+",width="+strWidth; 
	if (strType=="fixed") strOptions="status,height="+ strHeight+",width="+strWidth; 
	if (strType=="elastic") strOptions="toolbar,menubar,scrollbars,"+ "resizable,location,height="+ strHeight+",width="+strWidth; 
	if (strType=="fullscreen") strOptions = "fullscreen=1,scrollbars=0,status=0,toolbar=0,resizable=1";
	pop_newWin = window.open(strURL, 'pop_newWin', strOptions); 
	pop_newWin.focus(); 
}














function initMenu() {

	if( document.getElementById("body_mainmenu") ) menuRef = document.getElementById("body_mainmenu");
	else menuRef = document.getElementById("body_othermenu");

	menuChildren = menuRef.childNodes;
	pNode = menuChildren[0];
	for(var i=0; i < menuChildren.length; i++) {

		if (menuChildren[i].nodeName == "DIV") {

			active = 0;
			if ( pNode.className == "active" ) active = 1;

			submenuChildren = menuChildren[i].childNodes;
			for(var j=0; j < submenuChildren.length; j++) {

				if ( submenuChildren[j].className == "active" ) active = 1;
			}

			pNode.onclick = function() { switchMenu(this); if (this.href.substring(this.href.length-1) == "#") return false; }
			if (active == 0) menuChildren[i].style.display = "none";
			else menuChildren[i].style.display = "block";
		}

		if (menuChildren[i].nodeName == "A") pNode = menuChildren[i];
	}

}



function clearActive() {

	if( document.getElementById("body_mainmenu") ) menuRef = document.getElementById("body_mainmenu");
	else menuRef = document.getElementById("body_othermenu");

	menuChildren = menuRef.getElementsByTagName("A");
	for(var i=0; i < menuChildren.length; i++) {

		if ( menuChildren[i].className == "active" ) menuChildren[i].className = "";
	}
}



function switchMenu( eventObj ) {

	clearActive();
	eventObj.className = "active";

	found = 0;
	menuChildren = eventObj.parentNode.childNodes;
	for(var i=0; i < menuChildren.length; i++) {

		if (menuChildren[i].nodeName == "DIV") {

			menuRef = menuChildren[i];

			if (found == 1) {

				if( menuRef.style.display == "none" ) menuRef.style.display = "block";
				else menuRef.style.display = "none";
				found = 0;
			}
			else {
				menuRef.style.display = "none";
			}
		}

		if ( menuChildren[i] == eventObj ) found = 1;
	}

}


try {
	document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}




















// flash editor stuff ===========================================


function init_flash( stringUrl ) {
	var sample = new FlashTag(stringUrl, "100%", "100%", "7,0,14,0");
	sample.setFlashvars("lcId=" + lcId);
	sample.write(document);
}






var cancelCheckInterval = 666;
function launchFlashWindow( urlString ) {

	newWindow(urlString, 'edit_window', 590, 500, 0, 1);
//	newWinPopUp(this.href,'fixed',590,500);

	cancelCheckInterval = setInterval(cancelCheck, 200);
}






function cancelCheck() {
	try {
		if (pwin.closed && !pwin.location) {
			flashProxy.call('cancelEdit', 0);
			clearInterval( cancelCheckInterval );
		}
	} 
	catch (e) {}
}




function reloadNewData( parameterString ) {
	flashProxy.call('reloadChanges', parameterString);
	clearInterval( cancelCheckInterval );
	exitTest = setTimeout('exitFix()', 200);
}





function exitFix() {
	pwin.close();
	clearTimeout(exitTest);
}




function checkExitFlash( mapSaved ) {

	if( parseInt(mapSaved) == 0 ) {
		if( confirm(confirmExitLabel) ) flashProxy.call('saveAndExitTree');
		else exitFlashWindow();
	}
	else exitFlashWindow();
}




function exitFlashWindow() {

	self.close();
}




