/*
	its crossbrowser implemented and should work for
	IE5.5 and greater and
	Firefox1.5 and greater
*/

function NewColor(obj,bgcol) {
	if(obj.style) {
		obj.style.backgroundColor=bgcol; //Background-Color
	}
}

function NewBgImage(obj,img)
{
	if(obj.style)
	{
		var src = 'url("' + img + '")';
		obj.style.backgroundImage = src;
	}
}

function hoverButton(obj,img) {
	if (obj) {
		obj.src = '/fileadmin/package/images/btn/'+img;
	}
}
/*
	this script fixes the footer of the page
	positioned by the heighest row (left, center, right)
*/
function fixFooter()
{
	var height_left = document.getElementById('content_left').clientHeight;
	var height_center = document.getElementById('content_center').clientHeight;
	var height_right = document.getElementById('content_right').clientHeight;

	var max = 0;

	max = height_left > max?height_left:max;
	max = height_center > max?height_center:max;
	max = height_right>max?height_right:max;

	var footer_style = document.getElementById('main_footer').style;

	footer_style.position = "absolute";
	footer_style.top = (208 + max) + "px";
	footer_style.left = 6 + "px";
}

function fixFooterStartPage()
{
/*	var height_left = document.getElementById('start_left').clientHeight;
	var height_center = document.getElementById('start_center').clientHeight;
	var height_right = document.getElementById('start_right').clientHeight;
	
	var max = 0;
	
	max = height_left > max?height_left:max;
	max = height_center > max?height_center:max;
	max = height_right>max?height_right:max;

	var footer_style = document.getElementById('main_footer').style;
	
	footer_style.position = "absolute";
	footer_style.top = (208 + max) + "px";
	footer_style.left = 6 + "px"; */
}

/*
	init the hover menus if existent max menuitems 9
	mehr passd schlichtwech nich hin :)
*/
function initMenus()
{
	/*initial ofset of the menus*/
	
	var off_set=0;


	/* Set up hover ids */
	for(i = 51; i > 46; i--) {
		var strBlankHover = 'hover';

		if(!document.getElementById('hover')) continue;
		document.getElementById('hover').setAttribute("id", strBlankHover+i);
	}

	for(i = 51; i > 46; i--)
	{
		/*build current menu id and hover id*/
		var menu_id = 'menu'+i;
		var hover_id = 'hover'+i;
		/*if no such menu break end return*/
		if(!document.getElementById(menu_id))
			break;

		
		/*move hover*/
		var style = document.getElementById(hover_id).style;
		style.position = "absolute";
		style.left = off_set + "px";
		
		/*and now some sweet heart fix for the IE*/
		if(document.all)
		{
			style.display = ''; /*switching the hover for a short moment on to get height*/
			var iframe = document.createElement('iframe');
			var height = document.getElementById(hover_id).clientHeight - 10;
			iframe.style.position = "absolute"
			iframe.style.display = "none";
			iframe.style.width = "173px"
			iframe.style.height = height + "px";
			iframe.style.left = off_set + "px";
			iframe.frameborder = '0';
			iframe.id = "iebug" + i;
			iframe.style.top = 42;
			document.getElementById('mainnavi').appendChild(iframe);
			style.display = 'none'; /*switching the hover off again*/
		}

		/*register mousehandler*/
		document.getElementById(menu_id).onmouseover = mouseOver;
		document.getElementById(menu_id).onmouseout = mouseOut;
		document.getElementById(hover_id).onmouseout = mouseOut;

		/*calculate the offset for the next hovermenu*/
		off_set = off_set + document.getElementById(menu_id).clientWidth - 1;

	}
}

/*
	this saves the current visible hovermenu, for closing it later on
*/
var curr_hover_id = 0;

/*
	it shows the corresponding hover menu to the tab on which the mouse
	is over.
*/
function mouseOver(e)
{
	/*firefox uses param e IE uses window.event :(*/
	if (!e) var e = window.event;
	/*firefox uses e.srcElement IE uses e.target :(*/
	var tg = (window.event) ? e.srcElement : e.target;
	/*firefox gives back the span tag IE gives directly the A tag back*/
	if(tg.nodeName != 'A')
		tg = tg.parentNode;
	/*mark the current hover div as shown*/
	curr_hover_id = tg.id.substr(4,2);
	/*show it*/
	showHover(curr_hover_id);
}

/**
	the hover div can't be close if one of the following
	things happens:

	1.we are at the link and proceed foreward to the div
	2.we are at the div and proceed foreward to an childNode
	inside of the div
	3.we are at the div and proceed foreward to the mannavi link
	belonging to the hovermenu div
*/
function mouseOut(e)
{
	/*firefox uses param e IE uses window.event :(*/
	if (!e) var e = window.event;
	/*firefox uses e.srcElement IE uses e.target :(*/
	var tg = (window.event) ? e.srcElement : e.target;
	/*same procedure for the to Element*/
	var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;

	if(!tg || !reltg)
	{
		hideHover(curr_hover_id);
		return;
	}

	/*went over to the div do nothin*/
	/*we could also walk straight into the mannavi span thus we also do nothing the only possibly child
	element of the mainnavi <a>*/

	/*the IE throws Exceptions if we try to get parent Nodes of html or body
	thus we ask for it and quit and done*/
	var hover_id = 'hover'+curr_hover_id;
	var menu_id = 'menu'+curr_hover_id;
	while(reltg.nodeName != 'HTML')
	{
		if(reltg.id == hover_id || reltg.id == menu_id)
		{
			return;
		}

		if(!reltg.parentNode)
		{
			hideHover(curr_hover_id);
		}

		reltg = reltg.parentNode;
	}

	hideHover(curr_hover_id);
}


/*
	show the hovermenu with id id
*/
function showHover(id)
{
	//alert('show hover: '+'hover'+id);
	var h_style = document.getElementById("hover"+id).style;
	h_style.display = '';
	if(document.all)
	{
		var iframe_style = document.getElementById("iebug"+id).style;
		iframe_style.display = '';
	}
}


/*
	hide the hovermenu with id id
*/
function hideHover(id)
{
	//alert('hide hover: '+'hover'+id);
	var hover_menu_style = document.getElementById("hover"+id).style;
	hover_menu_style.display = 'none';

	if(document.all)
	{
		var iframe_style = document.getElementById("iebug"+id).style;
		iframe_style.display = 'none';
	}
}

function switchLnkImg(link)
{
	var currImg = link.getElementsByTagName("IMG")[0];
	if(currImg.src.indexOf("_grey") != -1)
		currImg.src = "/fileadmin/package/images/link_pfeil.gif";
	else
		currImg.src = "/fileadmin/package/images/link_pfeil_grey.gif";
}

function highlightStars(child, nr)
{
	/*get the corresponding ul list*/
	while(child.nodeName != 'UL')
	{
		child = child.parentNode;
	}

	/*now get all images*/
	var lnk_list = child.getElementsByTagName("A");
	var i = 0;
	for(i = 0; i < nr; i++)
	{
		lnk_list[i].style.backgroundImage = "url('/fileadmin/package/images/star_on.gif')";
	}
}

function clearStars(child)
{
		/*get the corresponding ul list*/
	while(child.nodeName != 'UL')
	{
		child = child.parentNode;
	}

	/*now get all images*/
	var lnk_list = child.getElementsByTagName("A");
	var i = 0;
	for(i = 0; i < lnk_list.length; i++)
	{
		lnk_list[i].style.backgroundImage = "url('/fileadmin/package/images/star_off.gif')";
	}

}

function popupWDB(partnerID)
{
	var winFeatures="resizeable=no,status=no,menubar=no,locationbar=no,scrollbars=yes";
	var wdbWin;
	winX=550;
	winY=550;
	
	//winUrl='http://wissen.kochwelt.de/wissen/partner_wdb?partner='+partnerID;
	winUrl = "http://kochwelt.mellowmessage.de/?id=magazin_popup&partner=" + partnerID;
	winTop=(screen.height-winY)/2;
	winLeft=(screen.width-winX)/2;
	wdbWin=window.open(winUrl,"wdbWin",'width='+(winX+21)+',height='+(winY+29)+',top='+winTop+',left='+winLeft+','+winFeatures);
	
	if ( wdbWin )
	{
		wdbWin.focus();
	}
}

function deleteBuddy(dellink, dellink_alert){

  var delconfirm = '';
  delconfirm = confirm(dellink_alert);
  
  if ( delconfirm == true ) {
  
    self.location.href = dellink;
  
  }
  
}

function deleteItem(dellink, dellink_alert){

  var delconfirm = '';
  delconfirm = confirm(dellink_alert);
  
  if ( delconfirm == true ) {
  
    self.location.href = dellink;
  
  }
  
}

function showExtra(part){

  switch(part) {
      
    case 'industry':
      document.getElementById('industryExtra').style.display = '';
    break;
    
    case 'position':
      document.getElementById('positionExtra').style.display = '';
    break;
    
  }
  
}

function hideExtra(part){

  switch(part) {
      
    case 'industry':
      document.getElementById('industryExtra').style.display = 'none';
    break;
    
    case 'position':
      document.getElementById('positionExtra').style.display = 'none';
    break;
    
  }
  
}

function deleteMessage(dellink, dellink_alert){

  var delconfirm = '';
  delconfirm = confirm(dellink_alert);  
    
  if ( delconfirm == true ) {
  
    self.location.href = dellink;
  
  }
  
}

function deleteRecipe(dellink, dellink_alert){

  var delconfirm = '';
  delconfirm = confirm(dellink_alert);  
    
  if ( delconfirm == true ) {
  
    self.location.href = dellink;
  
  }
  
}

function addIngredientUnit(){
  
  //get the last unit in the view
  var motherUnit = document.getElementById('unitbox').lastChild;
  
  //clone this unit
  var copyUnit = motherUnit.cloneNode(true);
    
  //change the id of the unit ++1
  var unit_end = copyUnit.id.substr(4,copyUnit.id.length);  
  unit_end = parseInt(unit_end);
  copyUnit.id = 'unit'+(unit_end + 1);
  
  //change the name of the units first input
  var unit_input1 = copyUnit.firstChild.firstChild.name.substr(15,copyUnit.firstChild.firstChild.name.length);  
  unit_input1 = parseInt(unit_input1);
  copyUnit.firstChild.firstChild.name = 'input-quantity-'+(unit_input1 + 1);
  copyUnit.firstChild.firstChild.value = '';
  copyUnit.firstChild.firstChild.style.border = "1px solid #c6d9d5";
  
  //change the name of the units second input
  var unit_input2 = copyUnit.childNodes[1].firstChild.name.substr(12,copyUnit.childNodes[1].firstChild.name.length);  
  unit_input2 = parseInt(unit_input2);
  copyUnit.childNodes[1].firstChild.name = 'select-unit-'+(unit_input2 + 1);
  //deselect the dropdown
  for (i = 0; i < copyUnit.childNodes[1].firstChild.length; i++){
    
    if (copyUnit.childNodes[1].firstChild.options[i].defaultSelected == true) copyUnit.childNodes[1].firstChild.options[i].selected = false;
    
  }
  
  //change the name of the units last input
  var unit_input3 = copyUnit.lastChild.firstChild.name.substr(21,copyUnit.lastChild.firstChild.name.length);  
  unit_input3 = parseInt(unit_input3);
  copyUnit.lastChild.firstChild.name = 'input-ingredientname-'+(unit_input3 + 1);
  copyUnit.lastChild.firstChild.value = '';
  copyUnit.lastChild.firstChild.style.border = "1px solid #c6d9d5";
  
  //insert the new unit
  var unitbox = document.getElementById('unitbox');
  var insert = unitbox.appendChild(copyUnit);
  
  //remember the last id, to receive all data form with all inputs when sending the form
  var last_id = unitbox.lastChild.id.substr(4,unitbox.lastChild.id.length);
  last_id = parseInt(last_id);
  
  //set the hidden input with the lastunit_id
  document.getElementById('lastunit_id').value = last_id;

}

function checkRecipe(formid){

  var err = 0;
  var checkArr = new Array("input-recipename","input-portioncount","input-quantity-1","input-ingredientname-1","input-recipepreparation","input-preparationhour","input-preparationminute");
  
  //valdiation for the ingredients
  /*
  for (j=1; j<=document.getElementById('lastunit_id').value; j++){
  
    var push = checkArr.push("input-quantity-"+j, "input-ingredientname-"+j);
    
  }
  */ 
  
  //valid image-disclaimer
  if (document.getElementsByName('recipe-image')[0].value.length > 0){
  
    if(document.getElementById('recipe-image-disclaimer').checked == 1){
    
      var img_valid = 'true';
      document.getElementById('recipe-image-disclaimer-txt').style.color = "#545454"; 
      
    } else {
      
      var img_valid = 'false';
      document.getElementById('recipe-image-disclaimer-txt').style.color = "#fe0000"; 
      
      
    }
  
  } else {
  
    var img_valid = 'true';
    document.getElementById('recipe-image-disclaimer-txt').style.color = "#545454";
    
  }
        
  for (i=0; i<checkArr.length; i++){
  
    if (document.getElementsByName(checkArr[i])[0].value.length < 1){
    
      document.getElementsByName(checkArr[i])[0].style.border = "1px solid #fe0000";      
      err++;
    
    } else {
    
      document.getElementsByName(checkArr[i])[0].style.border = "1px solid #c6d9d5";
    
    }  
  
  } 
  
  if (err != 0 || img_valid == 'false'){
  
    document.getElementById('recipe_errtext').style.display = "";  
    window.scrollTo(0,0);
    
  } else {
  
    document.getElementById(formid).submit();
  
  }

}

function hoverLinkImg (link, img) {

    link.firstChild.src = '/fileadmin/package/images/btn/'+img;

}

function popupFlash(link)
{
	var winFeatures="resizeable=no,status=no,menubar=no,locationbar=no,scrollbars=no";
	var flashWin;
	
	winX=980;
	winY=680;
	
	winUrl = link;
	winTop=(screen.height-winY)/2;
	winLeft=(screen.width-winX)/2;
	
	flashWin=window.open(link,"flashWin",'width='+(winX+21)+',height='+(winY+29)+',top='+winTop+',left='+winLeft+','+winFeatures);
	
	if ( flashWin )
	{
		flashWin.focus();
	}
}

function hoverMainBtn (btn, status) {

    switch (status){
      
      case 'hover':    
              
        btn.previousSibling.previousSibling.style.backgroundImage = 'url(/fileadmin/img/spacer_mainnavi_h.png)';
        btn.previousSibling.style.backgroundImage = 'url(/fileadmin/img/btn_mainnavi_corner_h.png)';
        btn.nextSibling.style.backgroundImage = 'url(/fileadmin/img/btn_mainnavi_corner_h.png)';
        btn.nextSibling.nextSibling.style.backgroundImage = 'url(/fileadmin/img/spacer_mainnavi_h.png)';    
        
      break;
      
      case 'unhover':    
        
        btn.previousSibling.previousSibling.style.backgroundImage = 'url(/fileadmin/img/spacer_mainnavi.png)';
        btn.previousSibling.style.backgroundImage = 'url(/fileadmin/img/btn_mainnavi_corner.png)';   
        btn.nextSibling.style.backgroundImage = 'url(/fileadmin/img/btn_mainnavi_corner.png)';
        btn.nextSibling.nextSibling.style.backgroundImage = 'url(/fileadmin/img/spacer_mainnavi.png)';
        
      break;
      
    }      
}
