// Menu script
function registerRollover(img, rollover_url){
    if(Element.hasClassName(img,'image-loaded')){
        return;
    }
    
    var preload = new Image();
    preload.src = rollover_url;
    
    img.normal_url = img.src;
    
    img.onmouseout = function(){img.src=img.normal_url};
    img.onmouseover = function(){img.src=rollover_url};
    
    img.addClassName('image-loaded');
}

// Dreamweaver stuff to popup image
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_findObj(n, d) { //v4.01
  var p,i,x;  
  if(!d) 
      d=document; 
  if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
  }
  if(!(x=d[n])&&d.all) 
      x=d.all[n]; 
  for (i=0;!x&&i<d.forms.length;i++) {
      x=d.forms[i][n];
  }
  for(i=0;!x&&d.layers&&i<d.layers.length;i++){
      x=MM_findObj(n,d.layers[i].document);
  }
  if(!x && d.getElementById) {
      x=d.getElementById(n);
  } 
  return x;
}

function set_style(el, name, value){
    var style = get_style(el);
    if(style){
        style[name] = value;
        return true;
    }
    return false;
}

function get_style(el){
    var obj = ((typeof el == 'object')? el : $(el));
    if( obj ){
        if(obj.style){
            obj = obj.style;
        }
        return obj;
    }
    return false;
}

function get_style_val(el, name){
    var style = get_style(el);
    if(style){
        return style[name];
    }
    return false;
}

function show_hide_value(obj, value){
    // pass show or hide as value. (Macromedia way of doing things)
    var style = get_style(obj);
    if(style){
        if(obj.style){
            var attrs = {'show':'visible', 'hide': 'hidden'};
            return attrs[value];
        }
        return value
    }
    return false
}

function MM_showHideLayers(){

    var args = MM_showHideLayers.arguments;

    for( i=0; i<(args.length-2); i+=3){
        var obj = $(args[i]); // use prototype library here.
        if( obj ) {
            value = args[i+2];
            if(value == 'show'){
                // close the other images
                var elementlist = document.getElementsByClassName('vergrootfoto');
                for(el in elementlist){
                    var the_val = show_hide_value(elementlist[el], 'hide');
                    set_style(elementlist[el], 'visibility', the_val);
                }
            }
            set_style(obj, 'visibility', show_hide_value(obj, value));
        }
    }
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->

// ==================
// Front page scripts
// ==================

// Action tab stuff
var actiontabtimer = undefined;
var actiontabcountdown = 0;

function switchToNextActionTab() {
    if(actiontabcountdown>0){
        actiontabcountdown-=1;
        return;
    }

    var panels = $$('.front-page-panel');
    if(panels.length<2)
        return;
     
    var index = selectedActionIndex();
    index += 1;
    if(index>=panels.length)
        index = 0;
    switchToTab('front-page-actions', panels[index]);
}

function setupActionTabTimer(){
    var actiontabbuttons = $$('.front-page-panel');

    if(actiontabbuttons.length<2)
        return;

    actiontabtimer = new PeriodicalExecuter(switchToNextActionTab, 4);
}

Event.observe(window, 'load', setupActionTabTimer);

function selectedActionIndex(){
    var actiontabbuttons = $$('.front-page-panel');

    for(var i=0; i<actiontabbuttons.length; i++){
        if(actiontabbuttons[i].hasClassName('selected')){
            return i;
        }
    }
}

function focusActionTab(tabid){
    actiontabcountdown = 4; // Wait x times before starting to loop again
    switchToTab('front-page-actions', tabid);
}

function switchToTab(containerid, tabid){
    $A($(containerid).childNodes).each(function (element){
            if(element.nodeType!=3){
                element.hide();
                element.removeClassName('selected');
            }
        });
    $(tabid).addClassName('selected');
    $(tabid).show();
}

// ==================
// Table row coloring
// ==================

function colorTableRows(){
    var tables = document.getElementsByTagName('table');
    for(var i=0; i<tables.length; i++){
	var table = tables[i];
	if(table.className.indexOf('odd-even')!=-1){
	    var tBodies = table.tBodies;
	    var odd = false;
	    for(var j=0; j<tBodies.length; j++){
		var rows = tBodies[j].rows;
		for(var k=0; k<rows.length; k++){
		    var className = 'even';
		    if(odd){
			var className = 'odd';
		    }  
		    odd = !odd;
		    rows[k].className = className;
		}
	    }
	}
	
    } 
}

Event.observe(window, 'load', colorTableRows);

// ===================================
// Set browser homepage to a given URL
// works only for IE
// ===================================

function setHome(elem, homeUrl)
{       
    if(navigator.appName == "Microsoft Internet Explorer"){
        elem.style.behavior='url(#default#homepage)';
        elem.setHomePage(homeUrl);
    }  
}

// ===================================
// Modify the Google CSE to handle 
// suburl search
// ===================================
function subSearch(form) {
    var input = form.elements.q;
    input.name = 'tempq';
    var newinput = document.createElement('input');
    newinput.type = 'hidden';
    newinput.name = 'q';
    form.appendChild(newinput);
    newinput.value = input.value + form.elements.subsite.value;
    input.disabled = true;
    form.elements.subsite
    return true;
}
