/* image preloading plugin */
( function( $ ){
  $.preLoadImages = function(){
    var tmp = [], i = arguments.length;
    // reverse loop run faster
    for( ; i-- ; ) tmp.push( $( '<img />' ).attr( 'src', arguments[ i ]));
  };
})( jQuery );

/* page init */
$(document).ready(function() {
	//preload images
	$.preLoadImages("/gfx/butbgallon.png","/gfx/butbgps3on.png","/gfx/butbgpsvon.png","/gfx/butbgpspon.png","/gfx/butbgps2on.png","/gfx/lbutbgon.png");
	
	//register topsearch handlers
	$('#topsearch #stxt').focus(function() {
		if ($(this).val() == "Suche nach...") {
			$(this).val("");
		}
	});
	$('#topsearch #stxt').blur(function() {
		if ($(this).val() == "") {
			$(this).val("Suche nach...");
		}
	});
});

/* resize avatar in box */
var avatar_w = 68;
var avatar_h = 92;
function resizeava() {
	var imgEle=document.getElementById('avatar');
	if(imgEle) {
	if (avatar_w == 0) { tmpuserpic_width = imgEle.width; } else {tmpuserpic_width = avatar_w;}
	if (avatar_h == 0) { tmpuserpic_height = imgEle.height; } else {tmpuserpic_height = avatar_h;}
	var ratio = imgEle.width / imgEle.height;
	for(z=0;z<2;z++) { 
		if (imgEle.width > tmpuserpic_width) { imgEle.width = tmpuserpic_width; imgEle.height = parseInt(imgEle.width / ratio); }    
		if (imgEle.height > tmpuserpic_height) { imgEle.height = tmpuserpic_height; imgEle.width = parseInt(imgEle.height * ratio); }
	}
	}
}

/* get review rating word */
function getrateword(val) {
if (0 <= val && 2.5 >= val) {return ('W&uuml;rg');}
if (2.6 <= val && 4.9 >= val) {return ('Schlecht');}
if (5 <= val && 6.5 >= val) {return ('Naja');}
if (6.6 <= val && 7.5 >= val) {return ('OK');}
if (7.6 <= val && 7.9 >= val) {return ('Gut');}
if (8.0 <= val && 8.9 >= val) {return ('Sehr Gut');}
if (9 <= val && 9.5 >= val) {return ('Genial');}
if (9.6 <= val && 10 >= val) {return ('Exzellent');}
}
/* get preview rating word */
function getprateword(val) {
if (val <= 4) {return ('Schlecht');}
if (val == 5) {return ('Naja');}
if (val == 6) {return ('Naja - OK');}
if (val == 7) {return ('OK - Gut');}
if (val == 8) {return ('Gut - Sehr gut');}
if (val == 9) {return ('Sehr gut - Genial');}
if (val == 10) {return ('Exzellent');}
}


/* reader rating slider */
function rrslider(trackId, sliderId, handleId, dispId, rwordid) {          
    this.track = document.getElementById(trackId);
    this.slider = document.getElementById(sliderId);
    this.handle = document.getElementById(handleId);
    this.display = document.getElementById(dispId);
    if (rwordid) {this.rword = document.getElementById(rwordid);}

    this.track.onmousedown = this.onmousedown;
    this.track.onmousewheel = this.onmousewheel;
    this.track.slider = this;
    
    this.track.onselectstart = this.slider.onselectstart =	
        this.handle.onselectstart = function() { return false };
    
    this.style = this.slider.style;
}

rrslider.prototype.startDrag = function(e) {
    rrslider.currentInstance = this;
    if (!e) e = window.event;
    if (!this.handleWidth) {
	    this.handleWidth = this.handle.offsetWidth;
	    this.initialWidth = this.slider.offsetWidth;
	    this.width = this.initialWidth;
	    this.maxWidth = this.track.offsetWidth;
    }
    this.screenX = e.screenX;
    var el = e.target || e.srcElement;
    if (el != this.handle) {
        var x = e.offsetX? e.offsetX : e.layerX;
        x += Math.round(this.handleWidth / 2);
        this.moveTo(x);
    }
    this.initialWidth = this.width;
}

rrslider.prototype.onmousedown = function(e) {
    var s = this.slider;
    s.startDrag(e);
    var d = document;
    if (d.addEventListener) {
        d.addEventListener("mousemove", s.onmousemove, true);
        d.addEventListener("mouseup", s.onmouseup, true);
    }
    else if (d.attachEvent) {
        d.attachEvent("onmousemove", s.onmousemove);
        d.attachEvent("onmouseup", s.onmouseup);
        d.attachEvent("onlosecapture", s.onmouseup);
    }
}

rrslider.prototype.onmouseup = function(e) {
    var s = rrslider.currentInstance;
    s.initialWidth = s.width;
    var d = document;
    if (d.removeEventListener) {
        d.removeEventListener("mousemove", s.onmousemove, true);
        d.removeEventListener("mouseup", s.onmouseup, true);
    }
    else if (d.detachEvent) {
        d.detachEvent("onmousemove", s.onmousemove);
        d.detachEvent("onmouseup", s.onmouseup);
        d.detachEvent("onlosecapture", s.onmouseup);
    }
}

rrslider.prototype.onmousemove = function(e) {
    if (!e) e = window.event;
    var s = rrslider.currentInstance;
    var x = s.initialWidth + (e.screenX - s.screenX);
    s.moveTo(x);
}

rrslider.prototype.onmousewheel =	function (e) {
    if (!e) e = window.event;
    var s = this.slider;
    s.moveBy(Math.round(e.wheelDelta / 10));
    return false;
}

rrslider.prototype.moveTo = function(x) {
    var s = rrslider.currentInstance;
    if (x < this.handleWidth) x = this.handleWidth;
    else if (x > this.maxWidth) x = this.maxWidth;
    this.width = x;
    this.style.width = '' + x + 'px';
    this.display.innerHTML = (s.getValue()).toFixed(1);
    if (this.rword) {this.rword.innerHTML = getrateword(this.display.innerHTML);}
}

rrslider.prototype.moveBy = function(x) {
    this.moveTo(this.width + x);
}

rrslider.prototype.getValue = function() {
    return Math.round((this.width-this.handleWidth) / 
    	(this.maxWidth-this.handleWidth) * 100)/10;
}

/* update side rating box routine */
function updaterr (xmlob) {
	var pobj = xmlob.getElementsByTagName('rrdata').item(0);
	if (pobj.getElementsByTagName('nr').item(0).firstChild.nodeValue != 0) {
	document.getElementById('rrvalue').innerHTML = pobj.getElementsByTagName('rating').item(0).firstChild.nodeValue;
	document.getElementById('rrword').innerHTML = getrateword(pobj.getElementsByTagName('rating').item(0).firstChild.nodeValue);
	} else {
	document.getElementById('rrbuttonbox').innerHTML = 'Keine Doppelwertungen!';
	}
	document.getElementById('rrbuttonbox').innerHTML = 'Bewertung registriert';
}






/* ajax request routine */
function ajaxrequest(func, url, mode, data, rettyp) {
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
	httpRequest = new XMLHttpRequest();
	if (httpRequest.overrideMimeType) {
	//set mimetype (ff 1.5 bug)
	httpRequest.overrideMimeType('text/xml');
	}
} 
else if (window.ActiveXObject) { // IE
	try {
		httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
	} 
	catch (e) {
	try {
		httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch (e) {} 
	}
}


//check for failed request
if (!httpRequest) {
            return false;
}
//call function
httpRequest.onreadystatechange = function() {
	if (httpRequest.readyState == 4) {
		if (httpRequest.status == 200) {
                if (rettyp == 'xml') {
                	func(httpRequest.responseXML);
                } else {
                	func(httpRequest.responseText);
                }
		} else {
		}
        }
};
httpRequest.open(mode, url, true);
if(mode == 'POST') {
	httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	httpRequest.send(data);
} else {
	httpRequest.send(null);
}
}

