var headerimgchanger = {
	_path : 'media/files/header/',
	_img : null,
	_anz : 0,
	_num : 0,
	_nnum : 0,
	_allload : false,
	_cdiv : null,
	_imgParent : null,
	_imgs : [],
	_ie : false,
	_ff : false,
	_o : [],
	_delay : 50,



    init : function (counts, delay) {
    	var t = this;
    	t._anz = counts;
    	t._cdiv = t.di('headerimages');
    	t._delay = delay == null ? 50 : delay;
    	t._num = t._shuffle();
    	t._nnum = t._shuffle(t._num);
    	var ua = navigator.userAgent;
    	if(ua.indexOf('MSIE') != -1){this._ie = true;}
    	if(ua.indexOf('Firefox') != -1){this._ff = true;}
    	t._buildImages();
		window.setTimeout(function(){t._move()}, t._delay);

    },

	di : function(n){
		return document.getElementById(n);
	},

	setStyle : function(el, attr) {
		if(typeof(el) == "string"){el = this.di(el);}
		for(var k in attr) {
			el.style[k] = attr[k];
		}
		return el;
	},

	buildEl : function(el, attr, ch) {
		var nel = document.createElement(el);
		for(var k in attr) {
			nel.setAttribute(k, attr[k]);
		}

		this[nel.id] = nel;
		if(typeof(ch) != "undefined") {
			for(var i = 0; i < ch.length; i++) {
				nel.appendChild(ch[i]);
			}
		}
		return nel;
	},

	setOpacity : function (i)
	{
		if(this._o[i] > 1) {this._o[i] = 1;return this._imgs[i];}
		this._imgs[i].style.opacity = this._o[i];
		this._imgs[i].style.MozOpacity = this._o[i];
		this._imgs[i].style.KhtmlOpacity = this._o[i];
		if(this._ie == true){this._imgs[i].style.filter = "alpha(opacity=" + (this._o[i]*100) + ")";}
	},

	_buildImages : function(){
		var t = this;
		for(var i =0; i < t._anz;i++){
			t._imgs[i] = t.buildEl('img', {src:t._path+'header_'+(i+1)+'.jpg',alt:'Header Image '+i,id:'himg'+i});
			t._o[i] = 0;
			if(i == t._num){t._imgs[i].style.display = 'block';t._o[i] = 1;t.setOpacity(i);}
			t._cdiv.appendChild(t._imgs[i]);
		}
	},


	_move : function() {
		var t = this;
		var co = t._o[t._num];
		// var ni = t._imgs[t._num+1] != null ? t._num+1 : 0;
		var no = t._o[t._nnum];


		co -=.01;
		no +=.01;

		t._imgs[t._nnum].style.display = "block";
		t._o[t._num] = co;
		t._o[t._nnum] = no;

		t.setOpacity(t._num);
		t.setOpacity(t._nnum);

		if(co <= 0) {
			t._imgs[t._num].style.display = "none";
			t._num = t._nnum;
			t._nnum = t._shuffle(t._num);
			// t._num = ni;

		}
		setTimeout(function(){t._move()},t._delay);
	},

	_shuffle : function(e) {
		if(e == null) { e = -1;}
		var n = e;
		while(e == n){n = Math.floor(Math.random() * (this._anz - 1)) + 1;}
		return n;
	}
}
