// JavaScript Document

function initSelection(id) {	
					theURL = id;
					window.location = theURL;
	}
	
	
var createNavi = {
	init:function(args){
		if(args[0].responseText !== undefined){
			var root = args[0].responseXML.documentElement;
			createNavi.cats = root.getElementsByTagName('childCategory');
			createNavi.prodCats = document.getElementById('prodCats'); 
			for(i=0; i< createNavi.cats.length; i++){
				createNavi.tempCatText = createNavi.cats[i].getElementsByTagName('textvalue')[0].firstChild.nodeValue;
				createNavi.tempCcatID =  createNavi.cats[i].attributes[0].nodeValue;
				createNavi.hasChildren =  createNavi.cats[i].attributes[1].nodeValue;
				
				createNavi.tempLI = document.createElement("li");
				createNavi.tempA = document.createElement("a");
				createNavi.tempA.id = 'ccatID-'+createNavi.tempCcatID;
			
				createNavi.tempA.href = 'flashOCS.htm?cat='+createNavi.tempCcatID+'&hc='+createNavi.hasChildren;
				
				createNavi.tempA.onclick = new Function ("return false;");

				YAHOO.util.Event.addListener(createNavi.tempA, 'click', createNavi.callFlash);
				
				createNavi.tempAtext = document.createTextNode(createNavi.tempCatText);
				
				createNavi.tempA.appendChild(createNavi.tempAtext);
				createNavi.tempLI.appendChild(createNavi.tempA);
				createNavi.prodCats.appendChild(createNavi.tempLI);

			}
			
		temp = 	document.getElementById('prodCats').offsetHeight;
		if(YD.inDocument('ani')){
			var anim = new YAHOO.util.Anim('fader', {height: {from:0, to:temp}}, 2, YAHOO.util.Easing.easeBothStrong); 
			anim.animate();
		}else{
			YD.setStyle('fader', 'height', temp+'px');
			}
		}	
	},
	
	callCats: function(el){
		var CatName = el.innerHTML;		
		var vars = el.href.split('?')[1];
		var FlCat = parseFloat(vars.split('cat=')[1].split('&')[0]);
		var FlHC = vars.split('cat=')[1].split('&hc=')[1];

		setContent.remove();
		YD.setStyle('flashcontent2', 'display', 'block');
		createNavi.navigateToCategoryTimer(CatName,FlCat,FlHC);
	},
	
	navigateToProduct: function(productID,catID){
		setContent.remove();
		setHighlight(catID);
		YD.setStyle('flashcontent2', 'display', 'block');
		createNavi.navigateToProductTimer(productID);
	},	
	
	navigateToProductList: function(subCatID,catID){
		setContent.remove();	
		setHighlight(catID);
		YD.setStyle('flashcontent2', 'display', 'block');
		createNavi.navigateToProductListTimer(subCatID);
	},	
	

	navigateToCategoryTimer: function(CatName,FlCat,FlHC){
		if(typeof(document.getElementById("Main").navigateToCategory) == "function") {		
			 window.setTimeout("document.getElementById('Main').navigateToCategory("+FlCat+","+FlHC+",'"+CatName+"')", 1000);
		}else{
  			  window.setTimeout("createNavi.navigateToCategoryTimer('"+CatName+"','"+FlCat+"','"+FlHC+"')", 100);
		}	
	},
	navigateToProductTimer: function(productID){
		if(typeof(document.getElementById("Main").navigateToProduct) == "function") {		
			 window.setTimeout("document.getElementById('Main').navigateToProduct("+productID+")", 1000);
		}else{
  			  window.setTimeout("createNavi.navigateToProductTimer('"+productID+"')", 100);
		}	
	},
	navigateToProductListTimer: function(subCatID){
		if(typeof(document.getElementById("Main").navigateToProductList) == "function") {		
			 window.setTimeout("document.getElementById('Main').navigateToProductList("+subCatID+")", 1000);
		}else{
  			  window.setTimeout("createNavi.navigateToProductListTimer("+subCatID+")", 100);
		}	
	},	
		
	
	callFlash: function(){
		var tempCcatLink = this.id.split('ccatID-')[1];
		
		if(tempCcatLink == undefined){
			tempCcatLink = this;
		}

		setHighlight(tempCcatLink);
		
		if(YD.inDocument('flashcontent2') && this.href.indexOf('flashOCS') != -1){
			createNavi.callCats(this);
			setContent.remove();
		}else{
			YD.setStyle('flashcontent2', 'display', 'none');
			setContent.init(this);
		}
	}
}


function setHighlight(ccat) {
		var tempActive = YD.getElementsByClassName('active','a','navi');  
		YD.removeClass(tempActive,'active');

		if(typeof(ccat) =='object'){
			//wenn der ausgeführte Link nicht aus dem OCS kommt, also ein normaler Link ist
			YD.addClass(ccat,'active');	
		}else{
		//wenn der ausgeführte Link aus dem OCS kommt und nur die ccatID übergeben wurde
			YD.addClass('ccatID-'+ccat,'active');	
			}
	}

	
function popup(){
	newPopUp = window.open(this.href, this.title, "width=850,height=650,scrollbars=yes,resizable=no");
	newPopUp.focus();
	}	
	

// Transaction samt Callback für das XML vom Webservice  BEGIN
var transactionObject = { 
	start:function(type, args){
		YD.setStyle('CatLoader', 'display', 'block');
	},
	complete:function(type, args){
		YD.setStyle('CatLoader', 'display', 'none');
	},	
	success:function(type, args){
		createNavi.init(args);
	},
	failure:function(type, args){
		alert('error: '+args[0].argument[0]);
	}
}


var callback = {
	customevents:{
		onStart:transactionObject.start,
		onComplete:transactionObject.complete,
		onSuccess:transactionObject.success,
		onFailure:transactionObject.failure
	}
};
// Transaction samt Callback für das XML vom Webservice  END

// Transaction samt Callback für das XML von den Contentseiten  BEGIN
var transactionSetContent = { 
	start:function(type, args){
		YD.setStyle('CatLoader', 'display', 'block');
	},
	complete:function(type, args){
		YD.setStyle('CatLoader', 'display', 'none');
	},	
	success:function(type, args){
		setContent.paste(args);
	},
	failure:function(type, args){
		alert('error: '+args[0].argument[0]);
	}
}

var SetContentCallback = {
	customevents:{
		onStart:transactionSetContent.start,
		onComplete:transactionSetContent.complete,
		onSuccess:transactionSetContent.success,
		onFailure:transactionSetContent.failure
	}
};
// Transaction samt Callback für das XML von den Contentseiten  END


var  setContent= {
	init:function(el){
		var tempContent = YAHOO.util.Connect.asyncRequest('GET', el.href, SetContentCallback);
		setContent.remove;
	},
	
	
	remove:function(){
		var OldChildren = YD.getChildren('flexContent');
		for(i = 0; i < OldChildren.length; i++){
			target.removeChild(OldChildren[i]);
		}	
	},
	
	paste:function(args){
		target.innerHTML = args[0].responseText;	
	}
}

// Transaction samt Callback für das XML von den Settings  BEGIN
var transactionSettings = { 
	start:function(type, args){
	},
	complete:function(type, args){
	},	
	success:function(type, args){
		//Hier werden die 2 Variablen anhand des "name" Attributes geholt
		var root = args[0].responseXML.documentElement;
		WS_HOST = YD.getElementsBy( function(el) {
			  return el.getAttributeNode("name").value == "WS_HOST";
			}, 'opt_key', root)[0].firstChild.nodeValue;

		WS_LOCATION = YD.getElementsBy( function(el) {
			  return el.getAttributeNode("name").value == "WS_LOCATION";
			}, 'opt_key', root)[0].firstChild.nodeValue;
	
		//Den Link zusammenbauen.	
		var webserviceRootRaw = WS_HOST+WS_LOCATION;
		
		//Die Anführungszeichen entfernen.
		var webserviceRoot = webserviceRootRaw.replace(/"/g, '');
		
		//lokale XML 
		//var request = YAHOO.util.Connect.asyncRequest('GET', '../../shared/GetChildProductCategories.xml', callback); 
		
		//Online XML 
		var request = YAHOO.util.Connect.asyncRequest('GET', webserviceRoot+'GetChildProductCategories.xml?cat_attr_ukey=CCAT_NAME&cl_id='+CL_ID+'&cat_id=150089&m_id='+M_ID, callback); 

	},
	failure:function(type, args){
		alert('error: '+args[0].argument[0]);
	}
}


var SettingsCallback = {
	customevents:{
		onStart:transactionSettings.start,
		onComplete:transactionSettings.complete,
		onSuccess:transactionSettings.success,
		onFailure:transactionSettings.failure
	}
};
// Transaction samt Callback für das XML von den Settings  END

function equalHeight(className){
	 var el = YD.getElementsByClassName(className);
	 if(el[0].offsetHeight > el[1].offsetHeight){
		 YD.setStyle(el, 'height', el[0].offsetHeight+'px');
	 }else{
		 YD.setStyle(el, 'height', el[1].offsetHeight+'px');
	 }
}


//Startfunction, wird aufgerufen, wenn alles geladen wurde.
function StartInit(){
	YD = YAHOO.util.Dom;	
	YE = YAHOO.util.Event;		
	
	//erstmal die Standard Variablen  holen:
	var request = YAHOO.util.Connect.asyncRequest('GET', '../../media/ocs/settings.xml', SettingsCallback); 	
	target = document.getElementById('flexContent');
	
	equalHeight('teaser');
	
	var standardLinks = YD.getElementsByClassName('standard','a','navi');  
	var popUps = YD.getElementsByClassName('popup','a');  
	YE.addListener(standardLinks, 'click',  createNavi.callFlash);	
	YE.addListener(popUps, 'click',  popup);	

}	

