// ***********************************************************************************************
// *
// *  Main JGO-Module
//*

var JGOdebug = false;
if( document.location.href.indexOf("file:") != -1 )
	JGOdebug = false;

try {
	if( document.location.href == top.document.location.href )
		JGOdebug = false;
} catch (e){}


var currentlyShownNodes = 0;
var currentlyAllowed = new Array();

function jgoDebug( module, message, scream ){
	var sPrefix = "JGO-Debug-Modul ";
	if( JGOdebug === true ){
		if( scream === true ){
			prompt( sPrefix, message );
		} else {
			xGetElementById("nextOutput").innerHTML += sPrefix + "<font color='green'><b>" + module + ": </b></font>" + message + "<div id='nextOutput'></div>";
		}
	}
}



function JavaScriptGUUIDObject(id, bValue, type, objRef) {

	var visibility = false;
	var JGOChildren = new Array();
	var thisId = id;
	var thisType = type;
//	var obj = typeof objRef == 'object'?objRef:$('#'+id); // 2011-07-28/ml      var obj = objRef;
	var obj = objRef;
	var active = 1;
	var bitMask = bValue.split(";");
	var originalbitMask = bValue;

	if( FM ) FM.maxSelection(bitMask);

	// **********************************************************************
	// Method setVisibility

	
	this.setVisibility = function ( obj ){
		var JGOChildren = new Array();
		return visibility;
	}

	

	// **********************************************************************
	// Method setZebra

	
	this.setZebra = function ( num ){
		var zebra = 0;
		if( FM ){
			active = (bitMask.length == 0 ? false : FM.allowedMask(bitMask) == 1);
			if( thisType == "" && bitMask == "" ) active = true;
		}

//		if( !obj ) return (active === true ? 1 : zebra);
		if( active === false ) {
			if( obj )
				obj.className = obj.tagName + "Zebra_X";
			this.setZebras();
		} else {
			var a = this.setZebras();
			if( a == 0 ) {
//				if( obj )
					obj.className = obj.tagName + "Zebra_X";
			} else {
//				if( obj ){
					if( (thisType == "tree" || thisType == "" ) && JGOChildren.length != 0 ){
						obj.className = "JGOGroup";
					} else {
						obj.className = ( FilterMode == "ui.thorn_true" ? "CAT_Trunk" : obj.tagName + "Zebra_" + (IsFeatureList == 0 ? String(num) : "FeatureList" ));
					}
//				}
				if( FM ) FM.maxSelection(bitMask);
				currentlyShownNodes++;
				zebra = 1;
			}
		}
//		jgoDebug( "JGO", "set zebra to " + String(num ));
		return zebra;
	}



	// **********************************************************************
	// Method addJGO

	
	this.addJGO = function ( id, bValue, type, objRef ){
		JGOChildren[JGOChildren.length] = new JavaScriptGUUIDObject(id, bValue, type, objRef);
//		jgoDebug( "addJGO", " created with ID: " + id );
	}


	// **********************************************************************
	// Method setZebras

	
	this.setZebras = function (){
		if( JGOChildren.length == 0 ) {
//			jgoDebug( "setZebras", " no children for ID: " + thisId );
			return -1;
		}
		var j = 0;
		for( var i=0; i<JGOChildren.length; i++ ){
			j += JGOChildren[i].setZebra( j % (FilterMode == "ui.thorn_false" ? 2 : 4 ));
		}
		
//		obj.className = "JGOGroup";
//		jgoDebug( "setZebras", " done with ID: " + thisId );
		return j;
	}


	// **********************************************************************
	// Method resize

	
	this.resize = function(){
		if( JGOChildren.length == 0 ) return;
		var lo = null;
		var ro = null;
		for( var i=0; i<JGOChildren.length; i++){
			if( JGOChildren[i].active === false ) continue;
			if( lo == null ) {
				lo = JGOChildren[i];
				continue;
			}
			if( ro == null )
				ro = JGOChildren[i];
				
			if( lo.height( ro ) != 0 )
				ro.height( lo );
				
			lo = null;
			ro = null;			
		}
	}
	
	
	// **********************************************************************
	// Method height
	// 
	// this method will ajust the height to meet the reference object if the reference object is bigger

	
	this.height = function( jgoReference ){
		// no reference was set, thus return the hight of myself
		if( !jgoReference )
			return xHeight(obj);

		var newHeight = jgoReference.height();
		var myHeight = xHeight(obj);
		
		if( myHeight == newHeight ) return 0;

		if( myHeight < newHeight ){
			xHeight( obj, newHeight );
			return newHeight;
		}
		return newHeight;
	}
	


	// **********************************************************************
	// Method turnOf

	
	this.turnOf = function( id ){
		if( thisId == id ){
			active = 0;
			jgoDebug( "JGO", "TurnedOff " + id );
		} else {
			for( var i=0; i<JGOChildren.length; i++ ){
				JGOChildren[i].turnOf( id );
			}
		}
	}


	// **********************************************************************
	// Method turnOn

	
	this.turnOn = function( id ){
		if( thisId == id ){
			active = 1;
			jgoDebug( "JGO", "TurnedOn " + id );
		} else {
			for( var i=0; i<JGOChildren.length; i++ ){
				JGOChildren[i].turnOn( id );
			}
		}
	}

	

	// **********************************************************************
	// Method reloadValues

	
	this.reloadValues = function(p){
//		jgoDebug( "JGO", "relaodvalues " + thisId);
		var allowed = false;
		
		if( thisType == "tree" ){
			if( p == null || originalbitMask == "" ){
				bitMask = originalbitMask.split(";");
				allowed = true;
			} else if( allowed === false ){
				for( var i=0; i<p.length; i++){
					if( thisId == p[i][0] ){
//						jgoDebug( "JGOrelaod ", p[i][0] + " with " + thisId + " old " + bitMask.join(";") + " new " + p[i][1]);
		
						bitMask = p[i][1].split(";");
						allowed = true;
						break;
					}
				}
			}
		}

		if( thisType == "article" ){
			if( FM.allowedMask(bitMask) != 0 ) {
				if( FM ) FM.maxSelection(bitMask);
			}
		} else if( allowed === true ){
			if( FM ) FM.maxSelection(bitMask);
		} else { // it tree is not allowed
			bitMask = new Array();
		}
		for( var j=0; j<JGOChildren.length; j++ ){
			JGOChildren[j].reloadValues(p);
		}
	}
}

function JGO_CreateGroup( id, bValue, type, objRef ){
	JGOMainList[JGOMainList.length] = new JavaScriptGUUIDObject(id, bValue, type, objRef);
//	xGetElementById(id).className = "red";
	currentGroup = id;
}

function JGO_CreateChild( id, bValue, type, objRef ){
	JGOMother = JGOMainList[JGOMainList.length -1];
	if( !JGOMother ){
		JGOMainList[JGOMainList.length] = new JavaScriptGUUIDObject("", "", "tree", "");
		JGOMother = JGOMainList[JGOMainList.length -1];
	}
	
	JGOMother.addJGO(id, bValue, type, objRef);
//	xGetElementById(id).className = "green";
}

var JGOMainList = new Array();

jgoDebug( "JGO", "JGO Engine Loaded!" );



/////////////////////////////////////////////////////////////////////////
/// test functions

function mOver(id){
//	alert('mOver');
	lastState = window.status;
	var obj = xGetElementById(id);
	if( obj ) {
		obj.className += " " + obj.tagName + "Over";
	}
}

function mOut(id){
//	alert('mOut');
	var obj = xGetElementById(id);
	if( obj ) {
		obj.className = obj.className.replace(/ (TR|DIV)Over/, "" );
	}
}

/////////////////////////////////////////////////////////////////////////
/// test functions

function JGO_Redraw() {
	jgoDebug( "JGO_Redraw", "current Filter = " + FM.filtered() );
	var start = new Date();
	var j = 0;
	currentlyShownNodes = 0;
	for( var i=0; i<JGOMainList.length; i++ ){
		j += JGOMainList[i].setZebra(j % 2);
	}
	var stop = new Date();
//	alert("Redraw done in " + (stop - start));
	if( FM ) FM.redraw(FMNoOfProductsUnfiltered);
		jgoDebug( "JGO_Redraw", "currentlyShownNodes = " + currentlyShownNodes );

	$("td.bgcL0, td.bgcD0, td.bgcL, td.bgcD").each( function(){
		$(this).css("background-color", $(this).parent().css("background-color"));
	});
}

function JGO_Resize(){
	// change the hight of elements if in ZL Tree to solve the background-color problem
	if(( FilterMode == "ui.zumtobel.b_true" || FilterMode == "ui.zumtobel.g_true" ) && FMisProductList === false ) {
		jgoDebug( "JGO_Resize", "started " );
		for( var j=0; j<JGOMainList.length; j++ ){
			JGOMainList[j].resize();
		}
	} else if( FilterMode == "ui.thorn_true" && FMisProductList === false ) {
		if( typeof(calculateHeight) == 'function') {
			calculateHeight();
		}
	}
}

	// **********************************************************************
	// function JGO_Groups

	
function JGO_Groups(){
	return currentlyShownNodes;
}

	// **********************************************************************
	// function noOfProducts

	
function JGO_noOfProducts( p ){
	if( p != null ){
		currentlyAllowed = p;
		for( var i=100000; i<p.length; i++ ){ // turned off --> this code is only for debbuging purposes
			var a = p[i][1].split(";");
			var s = ";";
			for( var j=0; j<a.length; j++ ){
				s += a[j] + "=" + Number(a[j]).toString(2) + ";";
			}
			jgoDebug( "JGO # ", p[i][0] + s);
		}
	}
	
	FM.status();
	FM.init();
	for( var i=0; i<JGOMainList.length; i++ ){
		JGOMainList[i].reloadValues(p);
	}
	JGO_Redraw();
	FM.status();
}


function turnOfJGO(id){
	var start = new Date();
	for( var j=0; j<JGOMainList.length; j++ ){
		JGOMainList[j].turnOf(id);
	}
	var stop = new Date();
	//alert("turn off done in " + (stop - start));
	JGO_Redraw();
}


function JGO_TurnOn(id){
	for( var j=0; j<JGOMainList.length; j++ ){
		JGOMainList[j].turnOn(id);
	}
	JGO_Redraw();
}

