
var if_drop_down;
var if_button;
var iftimeoutEvent;
var iftheme_id;
var http_request;
var if_current_image;

function if_get_style(x, styleProp)
{
	if (x.currentStyle)
		var y = x.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(x, null).getPropertyValue(styleProp);
	return y;
}

var TimeToFade = 1000.0;

function animateFade(lastTick, eid)
{  
  var curTick = new Date().getTime();
  var elapsedTicks = curTick - lastTick;
 
  var element = document.getElementById(eid);
 
  if(element.FadeTimeLeft <= elapsedTicks)
  {
    element.style.opacity = element.FadeState == 1 ? '1' : '0';
    element.style.filter = 'alpha(opacity = ' + (element.FadeState == 1 ? '100' : '0') + ')';
    element.FadeState = element.FadeState == 1 ? 2 : -2;
    return;
  }
 
  element.FadeTimeLeft -= elapsedTicks;
  var newOpVal = element.FadeTimeLeft/TimeToFade;
  if(element.FadeState == 1)
    newOpVal = 1 - newOpVal;

  element.style.opacity = newOpVal;
  element.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';
 
  setTimeout("animateFade(" + curTick + ",'" + eid + "')", 33);
}

function fade_in(eid)
{
  var element = document.getElementById(eid);
  if(element == null)
    return;
   
	element.FadeState = -2;
   
  if(element.FadeState == 1 || element.FadeState == -1)
  {
    element.FadeState = element.FadeState == 1 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade - element.FadeTimeLeft;
  }
  else
  {
    element.FadeState = element.FadeState == 2 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade;
    setTimeout("animateFade(" + new Date().getTime() + ",'" + eid + "')", 33);
  }  
}

function if_image_fade_in(x)
{
	x.style.opacity = "0";
	x.style.filter = "alpha(opacity=0)";
	x.style.display = "block";
	id = x.getAttribute("id")
	fade_in(id);
}

function if_next_image(node)
{
	var current_node = 0;
	var first_node = 0;
	node = node.parentNode;
	node = node.parentNode;
	node = node.parentNode;
	for(i=0; i<node.childNodes.length; i++) {
		if(node.childNodes[i].nodeName == 'DIV') {
			if(!first_node) {
				first_node = node.childNodes[i];
			}
			if(current_node) {
				current_node.style.display = "none";
				if_image_fade_in(node.childNodes[i]);
				return;
			}
			if(if_get_style(node.childNodes[i], "display") == "block") {
				current_node = node.childNodes[i];
			}
		}
	}
	current_node.style.display = "none";
	if_image_fade_in(first_node);
}

function if_previous_image(node)
{
	var last_node = 0;
	var current_node = 0;
	node = node.parentNode;
	node = node.parentNode;
	node = node.parentNode;
	for(i=0; i<node.childNodes.length; i++) {
		if(node.childNodes[i].nodeName == 'DIV') {
			if(if_get_style(node.childNodes[i], "display") == "block") {
				current_node = node.childNodes[i];
				if(last_node) {
					node.childNodes[i].style.display = "none";
					if_image_fade_in(last_node);
					return;
				}
			}
			last_node = node.childNodes[i];
		}
	}
	current_node.style.display = "none";
	if_image_fade_in(last_node);
}

function dam_video(image)
{
	var div_count = 0;

	image = image.parentNode;

	for(i=0; i<image.childNodes.length; i++) {
		div = image.childNodes[i];
		if(div.nodeName == 'IMG') {
			div.style.display = 'none';
		}
		if(div.nodeName == 'DIV' && div_count++ == 0) {
			div.style.display = 'inline';
		}
	}
}

function if_channel(e, s)
{
/*
	if(window.event) {
      var e = window.event;
   }
   else if(e.keyCode == 13) {
      e.returnValue=false; 
      e.preventDefault(); 
      e.stopPropagation(); 
   }
   if(e.keyCode != 13) {
      return(true);
   }
*/

   http_request = false;

   if (window.XMLHttpRequest) { // Mozilla, Safari,...
      http_request = new XMLHttpRequest();
   }
   else if (window.ActiveXObject) { // IE
      try {
         http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e) {}
      }
   }

   if (!http_request) {
      alert('Giving up :( Cannot create an XMLHTTP instance');
      return false; 
   }
  
   http_request.onreadystatechange = if_channel_get;
   http_request.open('GET', '/intranett/interne_sider/?factory=db&menu=channel_execute', true);
   http_request.send(null);


   return(false);
}

function if_channel_get()
{
   var j=0,len=0,n=0;

   if (http_request.readyState == 4) {
      if (http_request.status == 200) {
         var xmldoc = http_request.responseXML;
			if(!xmldoc) {
				alert("No responce!");
				return;
			}
         var root_node = xmldoc.getElementsByTagName('div').item(0);
         var content_node = document.getElementById('main');
			if(content_node) {
				if(root_node) {
				alert("Replace inner: " + root_node.nodeName);
				// content_node.innerHTML = root_node.childNodes[0].nodeValue;
				content_node.innerHTML = http_request.responseText;

				}
				else {
					alert("No root node");
				}
			}
			else {
				alert("Missing");
			}
      }
		else {
         alert('There was a problem with the request.');
      }
   }
}

function ifmenu_unfocus() 
{
	clearTimeout(iftimeoutEvent);
	if(if_drop_down) {
		if_drop_down.style.display = 'none';
	}
	if(if_button) {
		if_button.style.backgroundColor = '';
	}
	if(iftheme_id) {
		ifmenu_select_menu(iftheme_id);
	}
}

function ifmenu_focus(button) 
{
	clearTimeout(iftimeoutEvent);
	if(if_drop_down) {
		if_drop_down.style.display = 'none';
	}
	if(if_button) {
		if_button.style.backgroundColor = '';
	}
	// button.style.backgroundColor = '#b9d9dd';
	button.style.cursor = 'pointer';
	if_button = button;
	if_container = button;
	for(i=0; i<if_container.childNodes.length; i++) {
		if(if_container.childNodes[i].id == 'ifmenu') {
			if_container.childNodes[i].style.display = 'block';
			if_drop_down = if_container.childNodes[i];
		}
	}
}

function ifmenu_unfocus_item(menu) 
{
	menu.style.backgroundColor = '';
	menu.style.color = '';
}

function ifmenu_focus_item(menu) 
{
	clearTimeout(iftimeoutEvent);
	menu.style.backgroundColor = '#CCCCCC';
	menu.style.cursor = 'pointer';
	menu.style.color = '#000000';
}

function if_menu_open(from,id)
{
	obj = document.getElementById(id);
	if(!obj) {
		return;
	}
	none = "none";
// alert("style '" + obj.style.display + "'");
	if(obj.style.display == '' || obj.style.display == 'none') {
		none = "inline";
		from.style.backgroundImage = "url('/graphics/arrow-down.gif')";
	}
	else {
		from.style.backgroundImage = "url('/graphics/arrow-right.gif')";
	}
	obj.style.display = none;
}

function ifmenu_chapter_open(article)
{
	var i=0;
	object = document.getElementById(article);
	if(object) {
		if(object.childNodes[0].style) {
			// IE
			object.childNodes[0].style.color = '#000000';
		}
		else {
			// FF
			object.childNodes[1].style.color = '#000000';
		}
		ul = object.parentNode;
		if(ul) {
			div = ul.parentNode;
			if(div && (div.style.display == 'none' || div.style.display == '')) {
				div.style.display = 'inline';
			}
		}
	}
}

function ifmenu_channel(menu_item_id, channel_id, extra)
{
	var extra_s = '';
	if(extra.length) {
		extra_s = "&" + extra;
	}
	window.location = "?factory=db&menu=channel&channel=" + channel_id +"&menu_item=" + menu_item_id + extra_s;
}

function ifmenu_select_menu(theme_id)
{
	obj = document.getElementById('theme-' + theme_id);
	if(obj) {
		ul = obj.parentNode;
		if(ul) {
			ul.style.backgroundColor = '#333333';
			iftheme_id = theme_id;
		}
	}
}

function ifmenu_select_tab(channel_id)
{
	obj = document.getElementById("channel-" + channel_id);
	if(obj) {
		obj.style.borderLeftColor = '#BBBBBB';
		obj.style.borderRightColor = '#FFFFFF';
		obj.style.borderTopColor = '#BBBBBB';
		// obj.style.borderBottomColor = '#FFFFFF';
		obj.style.backgroundColor = '#FFFFFF';
	}
	obj = document.getElementById("channel-menu");
	if(obj) {
		obj.style.display = 'block';
	}
}

function ifmenu_tab_on()
{
	obj = document.getElementById("channel-menu");
	if(obj) {
		obj.style.display = 'block';
	}
}
