var ratedisplay = 0;
browsername=navigator.appName;
if (browsername.indexOf("Netscape")!=-1) {browsername="NS"} else {if (browsername.indexOf("Microsoft")!=-1) {browsername="MSIE"} else {browsername="N/A"}}; browserversion="0"; if (navigator.appVersion.indexOf("2.")!=-1) {browserversion="2"}; if (navigator.appVersion.indexOf("3.")!=-1) {browserversion="3"}; if (navigator.appVersion.indexOf("4.")!=-1) {browserversion="4"}; if (navigator.appVersion.indexOf("5.")!=-1) {browserversion="5"}; if (navigator.appVersion.indexOf("6.")!=-1) {browserversion="6"}; if (navigator.appVersion.indexOf("7.")!=-1) {browserversion="7"};

function ratecheck() {

    if ((browsername=="MSIE" && browserversion<6)) {
        return true;
    }

    if (ratedisplay == 0) {
        ratedisplay = 1;
        document.getElementById('action_hide').style.display = 'block';
        document.getElementById('action_display').style.display = 'none';
    } else {
        ratedisplay = 0;
        document.getElementById('action_hide').style.display = 'none';
        document.getElementById('action_display').style.display = 'block';
    }
}

function ratechecker() {

    if ((browsername=="MSIE" && browserversion<6)) {
        return true;
    }

    if (ratedisplay == 0) {
        ratedisplay = 1;
        document.getElementById('action_display').style.display = 'block';
        document.getElementById('action_hide').style.display = 'none';
    } else {
        ratedisplay = 0;
        document.getElementById('action_display').style.display = 'none';
        document.getElementById('action_hide').style.display = 'block';
    }
}

function popup(url,width,height,props){
    var props = props+',directories=no,status=no,menubar=no,copyhistory=yes,width='+width+',height='+height;
    var popup = window.open(url,'popup',props);
    if (popup) {
        popup.focus();
        return true;
    } else {
        alert('Bitte deaktivieren Sie Ihren Popup-Blocker fÃ¼r diese Seite.');
        return false;
    }
}

function check_length(messageId, outputId, maxchars) {
    var maxchars = maxchars ? maxchars : 255;

    var message = document.getElementById(outputId);
    var len = message.value.length;

    if (len > maxchars) {
        message.value = message.value.substr(0, maxchars);
        len = maxchars;
    }
    // document.getElementById(x).innerHTML = len;
    document.getElementById(messageId).innerHTML = maxchars - len;
}

function webmilesWindow(url) {
    infoWindow(url,680,604);
}

var infoWindowObject;
function infoWindow(url,width,height) {
    width = width ? width : 680;
    height = height ? height : 680;
    var options = 'dependent=yes, hotkeys=yes, left=100, top=50, location=no, menubar=no, resizable=yes, scrollbars=yes, status=yes, toolbar=no';
    if (navigator.product == 'Gecko') {
        options += ', innerWidth='+width+', innerHeight='+height;
    } else {
        options += ', width='+(width-3)+', height='+(height+36);
    }
    infoWindowObject = window.open(url,'webmilesWindow',options);
    infoWindowObject.focus();
}

var SlideWin;
function slide_window(url) {
    SlideWin = window.open(url,'zusatz','width=500,height=600');
    SlideWin.focus();
}

/*
startList = function() {
if (document.all&&document.getElementById) {
navRoot = document.getElementById("navi");
for (i=0; i<navRoot.childNodes.length; i++) {
node = navRoot.childNodes[i];
if (node.nodeName=="LI") {
node.onmouseover=function() {
this.className+=" over";
  }
  node.onmouseout=function() {
  this.className=this.className.replace(" over", "");
   }
   }
  }
 }
}
window.onload=startList;
*/ /* this script is needed in case of dropdown navigation, otherwise can be erased. It makes also trouble in ie5 */

/*
 * A function to add as many functions to the window.onload event as you like
 */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

/*
 * Methods used by product detail pages.
 *
 * NOTE: As this methods are called via addLoadEvent, it is not possible to
 * use "this" inside them.
 */
prodDetail = {
    zoomCover: {
        popup: false,
        image: new Image(),
        link: false,
        checkSize: function() {
            if (prodDetail.zoomCover.image.height <= 80 && prodDetail.zoomCover.image.width <= 56) {
                document.getElementById('zoom').style.visibility = 'hidden';
                document.getElementById('zoomlink').removeAttribute('href');
            }
        },
        openPopup: function() {
            prodDetail.zoomCover.popup = window.open(prodDetail.zoomCover.link,'Bild','width=' + (prodDetail.zoomCover.image.width) + ',height=' + (prodDetail.zoomCover.image.height) + ',toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,top=150');
            prodDetail.zoomCover.popup.focus();
        }
    },
    suggested: {
        covers: {
            suggested:  new Image(),
            product: new Image()
        },
        checkWidth: function() {
            for (i in prodDetail.suggested.covers) {
                prodDetail.suggested.covers[i].src = document.getElementById('suggested_'+i+'_cover').src;
                if (prodDetail.suggested.covers[i].height == 80 && prodDetail.suggested.covers[i].width == 56) {
                    document.getElementById('suggested_'+i+'_cover').style.width = 'auto';
                }
            }
        }
    }
}

function appendQueryToAction(form,queryId) {
    var query = document.getElementById(queryId);
    if (!query || !form.action) {
        return;
    }
    if (form.action.charAt(form.action.length-1) != '/') {
        form.action += '/';
    }
    form.action += 'search/'+escape(query.value);
    return true;
}

function generate_rand(min, max) {
    var rand = Math.floor(min+(max-min+1)*(Math.random()));
    return rand;
}

function banner_item_writer(position_nr,type,id,banner_ident) {

    eval('is_object = typeof(position_'+position_nr+')');
    if(is_object == 'object') {

        var position_raw = eval('position_'+position_nr);
        var position = new Array();

        // remove empty banners
        for(var i=0;i<position_raw.length;i++) {
            if (position_raw[i] && position_raw[i] != '') {
                position.push(position_raw[i]);
            }
        }

        var random_position;
        if(position.length == 0) {
            return;
        } else if(position.length > 1) {
            random_position = generate_rand(0,position.length-1);
        } else {
            random_position = 0;
        }

        var item = position[random_position];

        item = item.replace(/\[\[node_type\]\]/g,    type);
        item = item.replace(/\[\[id\]\]/g,   id);
        item = item.replace(/\[\[ident\]\]/g,    banner_ident);
        item = item.replace(/\[\[link_token\]\]/g,   link_token);
        item = item.replace(/\[\[flash_token\]\]/g,  flash_token);

        document.write( item );
    } else {
        document.write('<!-- banner position_'+position_nr+ ' not defined -->');
    }
    /*
    */
}

/* styleswitcher */

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function generate_rand(min, max) {
    var rand = Math.floor(min+(max-min+1)*(Math.random()));
    return rand;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);
/* end style switch*/

function resize_images() {
    if ((document.all) && (document.getElementById) && (navigator.userAgent.indexOf('Opera')== -1)) {

        resize_img('lists', 109);
        resize_img('teasers', 88);
    }
}

function resize_img(id, width) {
    if (document.getElementById(id)) {
        for (var i=0; i < document.getElementById(id).getElementsByTagName('img').length; i++) {
           img = document.getElementById(id).getElementsByTagName('img')[i];
           result = img.src.search(/.*?[0-9]+k\.jpg$/);

           if (result != -1) {

               new_img = new Image();
               new_img.src = img.src;

               var newimg = document.createElement('img');
               newimg.src = img.src;
               newimg.alt = img.alt;
               newimg.width = new_img.width;

               if (img.currentStyle['float']) {
                   newimg.style.marginTop = img.currentStyle['float'];
               }
               if (img.currentStyle['border']) {
                   newimg.style.marginLeft = img.currentStyle['border'];
               }
               if (img.currentStyle['marginLeft']) {
                   newimg.style.marginLeft = img.currentStyle['marginLeft'];
               }
               if (img.currentStyle['marginTop']) {
                   newimg.style.marginTop = img.currentStyle['marginTop'];
               }

               if (newimg.width > width) {
                   img.width = width;
               } else if (newimg.width > 1) {
                   img.parentNode.replaceChild(newimg, img);
               }
           }
        }
    }
}
function autosuggest_createObject() {
	var request_type;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
		request_type = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		request_type = new XMLHttpRequest();
	}
	return request_type;
}

var http;

var resultlist = null;
var current_selected_li = -1;
var last;
var to;

function autosuggest(e) {
	document.onmouseup = autosuggest_clear;
	http = autosuggest_createObject();

	last = document.quicksearchform.elements[1].value;
//	last = document.getElementById('qusearchfield').value; // ich meine so geht es wegen Referenz statt Kopie nicht 
	if (last.length == 0) {
		autosuggest_clear(e);
		return;
	}
	
	if (!e) e = window.event;
	var keyCode = e.keyCode;
	if (!keyCode) charCode = e.which;

	if (keyCode == 40 || keyCode == 38) return;
	else current_selected_li = -1;

	if(keyCode == 13 || keyCode == 27) {
		autosuggest_clear(e);
		return false;
	}
	if (to == null)	to = window.setTimeout(function () {autosuggest_complete(last)}, 400);
}

function autosuggest_complete(last) {
//	if (to) window.clearTimeout(to);
	to = null;
	var q = document.getElementById('qusearchfield').value;
	if (!q || last != q) return;
	last = encodeURIComponent(last);
	http.open('get', window.location.protocol + '//' + window.location.hostname + '/get_top_suchbegriffe.php?q='+last);
//	http.open('get', 'http://www.buecher.de/get_top_suchbegriffe.php?q='+last);
	http.onreadystatechange = autosuggest_reply;
	http.send(null);
}

function autosuggest_reply() {
	if(http.readyState == 4){
		var response = http.responseText;
		var el = document.getElementById('suggest_list');
		if(response!=""){
			el.innerHTML=response;
			el.style.display="block";
		} else {
			el.style.display="none";
		}
		resultlist = document.getElementById('resultlist');
	}
}

function autosuggest_clear(event) {
	if (!event) event = window.event;
	var targ;
	if (event.target) targ = event.target;
	else if (event.srcElement) targ = event.srcElement;
	if (targ && targ.id == 'aclink') {
		return false;
	}
	e = document.getElementById('suggest_list');
	e.innerHTML = '';
	e.style.display = "none";
	return true;
}

function autosuggest_updown(event) {
	if (!event) var e = window.event;
	var keyCode = event.keyCode;

	if (resultlist == null) return;
	var childs = resultlist.childNodes.length;
	
	if(keyCode == 38){ // keyUp
		if(current_selected_li > 0 || current_selected_li == -1){
			current_selected_li--;
		}
	} else if(keyCode == 40){ // keyDown
		if(current_selected_li < 9 && current_selected_li+1 < childs){
			current_selected_li++;
		}
	}
	else return;


	for ( i = 0; i < childs; i++) {
		if (i == current_selected_li) {
			resultlist.childNodes[i].className = 'selected';
			document.getElementById('qusearchfield').value = resultlist.childNodes[i].firstChild.firstChild.data;
		}
		else {
			resultlist.childNodes[i].className = '';
		}
	}
}

function initKlappNavi() {
	klapp_navi_menuediv=document.getElementById('klapp_navi_menue');
	if (klapp_navi_menuediv == null) return;
	divColl=klapp_navi_menuediv.getElementsByTagName("div");

	for(i=0;i<divColl.length;i++) {
		if(divColl[i].className == "klapp_navi_item") {
			if(!initKlappNavi["activeKlappNavi"]) { 
				initKlappNavi["activeKlappNavi"]=divColl[i]; 
			} 
			divColl[i].onmouseover=function() { 
				displayKlappNavi(this); 
			}
		}
	}
}

function displayKlappNavi(KlappNaviObj,ignoreOld) {
	if(!ignoreOld) {
		initKlappNavi["activeKlappNavi"].getElementsByTagName('div')[1].style.display='none';
		initKlappNavi["activeKlappNavi"].getElementsByTagName('div')[0].style.display='block';
		initKlappNavi["activeKlappNavi"]=KlappNaviObj;
	}
	initKlappNavi["activeKlappNavi"].getElementsByTagName('div')[0].style.display='none';
	initKlappNavi["activeKlappNavi"].getElementsByTagName('div')[1].style.display='block';
}

