/*************************************
*    Script : easyCMS_POP v2.0       *
*    Author : Kim Steinhaug          *
*    Url    : www.steinhaug.com      *
*                                    *
*  --   --  --  --  --  --  --  --   *
* Usage :                            *
* swPOP('link','x','y','scrollbar'); *
*                                    *
* link 	    = Dokument url           *
* x    	    = Popup window X value   *
* y    	    = Popup window Y value   *
* scrollbar = 0, no                  *
*             1, yes                 *
**************************************/
function easyCMS_POP(url,Xvalue,Yvalue,scroll) {
	var w = 480, h = 340;if (document.all || document.layers) { w = screen.availWidth;h = screen.availHeight; };
	var popW = Xvalue, popH = Yvalue;var leftPos = (w-popW)/2, topPos = (h-popH)/2;
	if (scroll) { i = open(url, "displayWindow","top="+topPos+",left="+leftPos+",width="+Xvalue+",height="+Yvalue+",status=no,toolbar=1,menubar=no,resize=no,dependent=yes,scrollbars=yes");} else { i = open(url, "displayWindow","top="+topPos+",left="+leftPos+",width="+Xvalue+",height="+Yvalue+",status=1,toolbar=1,menubar=0,resize=no,dependent=yes,scrollbars=no");};
};
function easyCMS_zoom(header,alt,url,Xvalue,Yvalue,scroll) {
	Xvalue = Number(Xvalue) + 20;
	Yvalue = Number(Yvalue) + 60;
	if (scroll) {
        i = open("", "EUindow","top=50,left=50,width="+Xvalue+",height="+Yvalue+",status=no,toolbar=no,menubar=no,resize=no,dependent=yes,scrollbars=yes");
	} else {
        i = open("", "EUindow","top=50,left=50,width="+Xvalue+",height="+Yvalue+",status=no,toolbar=no,menubar=no,resize=no,dependent=yes,scrollbars=no");
	};
    i.focus();
        i.document.open();
        i.document.write('<html>\n')
        i.document.write('<head>\n')
        i.document.write('<title>Easy CMS - ' + alt + '</title>\n')
        i.document.write('<style><!--\n')
        i.document.write('body { \n')
        i.document.write('margin : 0px 0px 0px 0px;\n')
        i.document.write('}\n')
        i.document.write('--></style>\n')
        i.document.write('<\head>\n')
        i.document.write('<body bgcolor="#ffffff" text="#000000">\n')
        i.document.write('<center>\n')
        i.document.write('<div style="font-size:14px; font-weight:bold; font-family:verdana,arial,sans-serif,helvetica;">',header,'</div><a href="javascript:window.close();"><img src="',url,'" border="2" style="border:2px solid white;" alt=""></a>\n')
        i.document.write('<div style="font-size:10px;font-family:verdana;color:#006;">' + alt + '</div>\n')
        i.document.write('<div style="font-size:10px;font-family:verdana;color:#999;">&copy; Easy CMS 2005</div></center>\n')
        i.document.write('</body>\n')
        i.document.write('</html>\n');
        i.document.close();
};
function $(o) {
  if (typeof(o) == "string")
    return document.getElementById(o)
    else
    return o;
};
function applyStyleString (obj,str) {
  if(document.all && !window.opera) {
    obj.style.setAttribute("cssText",str);
  } else {
    obj.setAttribute("style",str);
  }
};

/* Underliggende kode tilhører SwitchMenu */
/* Slett alt under om den ikke brukes     */
/* Fjern kommentarblokk for å aktivere    */
/*
if (document.getElementById){
  document.write('<style type="text/css">\n')
  document.write('.submenu{display: none;}\n')
  document.write('</style>\n')
};
function SwitchMenu(obj){
  if(document.getElementById){
  var el = document.getElementById(obj);
  if(el){ // For ikke å åpne ikke eksisterende menyer!
  var ar = document.getElementById("CMSmenu").getElementsByTagName("span");
    if(el.style.display != "block"){
      for (var i=0; i<ar.length; i++){
        if (ar[i].className=="submenu")
        ar[i].style.display = "none";
          };
        el.style.display = "block";
        } else {
        el.style.display = "none";
      };
    };
  };
};
*/


/* Function : getElementsByClassName Deluxe Edition
   Author    : http://muffinresearch.co.uk/archives/2006/04/29/getelementsbyclassname-deluxe-edition/ 
   */
function getElementsByClassName(strClass, strTag, objContElm) {
  strTag = strTag || "*";
  objContElm = objContElm || document;
  var objColl = (strTag == '*' && document.all) ? document.all : objContElm.getElementsByTagName(strTag);
  var arr = new Array();
  var delim = strClass.indexOf('|') != -1  ? '|' : ' ';
  var arrClass = strClass.split(delim);
  for (i = 0, j = objColl.length; i < j; i++) {
    var arrObjClass = objColl[i].className.split(' ');
    if (delim == ' ' && arrClass.length > arrObjClass.length) continue;
    var c = 0;
    comparisonLoop:
    for (k = 0, l = arrObjClass.length; k < l; k++) {
      for (m = 0, n = arrClass.length; m < n; m++) {
        if (arrClass[m] == arrObjClass[k]) c++;
        if (( delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length)) {
          arr.push(objColl[i]);
          break comparisonLoop;
        }
      }
    }
  }
  return arr;
}
// To cover IE 5.0's lack of the push method
Array.prototype.push = function(value) {
  this[this.length] = value;
}

function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}
	
var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();
addEvent(window,'unload',EventCache.flush);

/* Steinhaug Webdesign JS library for Easy CMS */
var swlib = { 
  fixhr_id : 'content',
  fixhr_class : 'hr',
  fixhref: function() {
  var elements = document.getElementsByTagName('a');
  var i;
    for ( i=0;i<elements.length;i++ ) {
      var re = new RegExp("#$");
      if (elements[i].getAttribute('href').match(re)) {
        elements[i].setAttribute('href','javascript:;');
      }
    }
  },
  fixhr: function(){
    if (!document.getElementsByTagName) return;
    var hr = document.getElementById(swlib.fixhr_id).getElementsByTagName('hr');
    for (var i=0; i<hr.length; i++) { 
      var newhr = hr[i]; 
      var wrapdiv = document.createElement('div');
      wrapdiv.className = swlib.fixhr_class;  
      newhr.parentNode.replaceChild(wrapdiv, newhr);  
      wrapdiv.appendChild(newhr);  
    } 
  },

  hasClass: function (obj) {
     var result = false;
     if (obj.getAttributeNode("class") != null) {
         result = obj.getAttributeNode("class").value;
     }
     return result;
  },

  setstripe: function (){
    els = getElementsByClassName('stripe','table');
    for (var i=0; i<els.length; i++){
      swlib.stripe(els[i]);
    }
  },
  stripe: function (table) {
    var even = false;
    // if arguments are provided to specify the colours
    // of the even & odd rows, then use the them;
    // otherwise use the following defaults:
    var evenColor = arguments[1] ? arguments[1] : "#eff5fd";
    var oddColor = arguments[2] ? arguments[2] : "#dfecfd";
  
    if (! table) { return; }
    
    // by definition, tables can have more than one tbody
    // element, so we'll have to get the list of child
    // &lt;tbody&gt;s 
    var tbodies = table.getElementsByTagName("tbody");

    // and iterate through them...
    for (var h = 0; h < tbodies.length; h++) {
     // find all the &lt;tr&gt; elements... 
      var trs = tbodies[h].getElementsByTagName("tr");

      // ... and iterate through them
      for (var i = 0; i < trs.length; i++) {

        // avoid rows that have a class attribute
        // or backgroundColor style
//        if (! swlib.hasClass(trs[i]) &&
//            ! trs[i].style.backgroundColor) {
         if(! trs[i].style.backgroundColor) {		  

          // get all the cells in this row...
          var tds = trs[i].getElementsByTagName("td");
        
          // and iterate through them...
          for (var j = 0; j < tds.length; j++) {

            var mytd = tds[j];
            // avoid cells that have a class attribute
            // or backgroundColor style
            if (! swlib.hasClass(mytd) &&
                ! mytd.style.backgroundColor) {
        
              mytd.style.backgroundColor =
                even ? evenColor : oddColor;
            
            }
          }
        }
        // flip from odd to even, or vice-versa
        even =  ! even;
      }
    }
  }
};
addEvent(window,'load',swlib.fixhref,false);
addEvent(window,'load',swlib.setstripe,false);
// addEvent(window,'load',swlib.fixhr,false); // Do in document!

/* Easy CMS JS-Class for gallery template #03. 
   (c) Steinhaug AS - www.steinhaug.com
   Automatic slideshow with start and stop button */
var swGallerySlideshow = {
  lang_start : 'START',
  lang_stop  : 'STOP',
  url_start  : '/index.php',
  url_next   : '',
  seconds    : 5,
  img_anim   : '<img src="/images/timer-01.gif" width="16" height="16" alt="">',
  img_none   : '<img src="/images/spacer.gif" width="16" height="16" alt="">',
  active     : false,
  getVersion : function() {
    return 'v1.0';
  },
  toggle : function (el){
    if(this.active){
      this.deactivate(el);
    } else {
      this.activate(el);
    }
  },
  timersequence : function () {
    this.seconds = this.seconds - 1;
    if(this.seconds==0){
      clearTimeout(this.timer01);
      document.location.href = this.url_next;
//      alert('Complete');
    } else {
      self.status = this.seconds;
      this.timer01 = setTimeout('swGallerySlideshow.timersequence()',1000);
    }
  },
  activate : function (el) {
    this.active = true;
//    document.getElementById('swGallerySlideshow-button').value = this.lang_stop;
    document.getElementById('swGallerySlideshow-button').innerHTML = this.lang_stop;
    document.getElementById('swGallerySlideshow-anim').innerHTML = this.img_anim;
    this.applyStyleString(document.getElementById('swGallerySlideshow-button'),'background-color: #990000;')
    this.timer01 = setTimeout('swGallerySlideshow.timersequence()',1000);
  },
  deactivate : function (el) {
    this.active = false;
    clearTimeout(this.timer01);
//    document.getElementById('swGallerySlideshow-button').value = this.lang_start;
    document.getElementById('swGallerySlideshow-button').innerHTML = this.lang_start;
    document.getElementById('swGallerySlideshow-anim').innerHTML = this.img_none;
    this.applyStyleString(document.getElementById('swGallerySlideshow-button'),'background-color: #009900;')
//    this.clearInnerHTML(document.getElementById('swGallerySlideshow-anim'));
  },
  clearInnerHTML : function (obj){
    while(obj.firstChild) obj.removeChild(obj.firstChild);
  },
  applyStyleString : function (obj,str) {
    if(document.all && !window.opera) {
      obj.style.setAttribute("cssText",str);
    } else {
      obj.setAttribute("style",str);
    }
  }
}

if (!SI) { var SI = new Object(); };
/******************************************************************************
 SI.Scroll module v1.0
 
 Based on and including code originally created by Travis Beckam of 
 http://www.squidfingers.com | http://www.podlob.com
 
 ******************************************************************************/
SI.Scroll = {
	yOffset			: 0,
	scrollLoop 		: false, 
	scrollInterval	: null,
    containername   : 'container',
	getWindowHeight	: function() {
		if (document.all) {  return (document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.clientHeight; }
		else { return window.innerHeight; }
		},
	getScrollLeft	: function() {
		if (document.all) { return (document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft; }
		else { return window.pageXOffset; }
		},
	getScrollTop	: function() {
		if (document.all) { return (document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop; }
		else { return window.pageYOffset; }
		},
	getElementYpos	: function(el) {
		var y = 0;
		while(el.offsetParent){
			y += el.offsetTop
			el = el.offsetParent;
			}
		return y;
		},
	to 				: function(id){
		if(this.scrollLoop){
			clearInterval(this.scrollInterval);
			this.scrollLoop = false;
			this.scrollInterval = null;
			}
		var container = document.getElementById(this.containername);
		var documentHeight = this.getElementYpos(container) + container.offsetHeight;
		var windowHeight = this.getWindowHeight()-this.yOffset;
		var ypos = this.getElementYpos(document.getElementById(id));
		if(ypos > documentHeight - windowHeight) ypos = documentHeight - windowHeight;
		this.scrollTo(0,ypos-this.yOffset);
		},
	scrollTo 		: function(x,y) {
		if(this.scrollLoop) {
			var left = this.getScrollLeft();
			var top = this.getScrollTop();
			if(Math.abs(left-x) <= 1 && Math.abs(top-y) <= 1) {
				window.scrollTo(x,y);
				clearInterval(this.scrollInterval);
				this.scrollLoop = false;
				this.scrollInterval = null;
				}
			else {
				window.scrollTo(left+(x-left)/2, top+(y-top)/2);
				}
			}
		else {
			this.scrollInterval = setInterval("SI.Scroll.scrollTo("+x+","+y+")",100);
			this.scrollLoop = true;
			}
		}
	};

/* Quick validation for the login form */
function chk_ecms_login(el){
  if(!el.usr.value.length){
    alert('Du har ikke oppgitt noen brukernavn!');
  }
  if(!el.pas.value.length){
    alert('Du har ikke oppgitt noen passord!');
  }
  return true;
}
