/*
* Copyright 2002-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
 * This script, when included in a html file, can be used to make collapsible menus
 *
 * Typical usage:
 * <script type="text/javascript" language="JavaScript" src="menu.js"></script>
 */

if (document.getElementById){ 
  document.write('<style type="text/css">.menuitemgroup{display: none;}</style>')
}
/* AjaxTest 2006.12.08 */
function ajaxTest(){
	var req = (window.XMLHttpRequest)
		? new XMLHttpRequest()
		: ((window.ActiveXObject)
		? new ActiveXObject("Microsoft.XMLHTTP")
		: false);
	req.open("POST","dialogORbatch.html",true);
	req.onreadystatechange = function(){
		if (req.readyState == 4){
			if (req.status == 200){
//				var d = document.getElementById("ajaxTest");
//				d.innerHTML = req.responseText;
//				alert(req.responseText);
			}
		}
	}
	req.setRequestHeader("Content-Type","text/xml");
	req.send('<?xml version="1.0"?><dialogORbatch/>');
}

function SwitchMenu(obj, thePath){
var open = 'url("'+thePath + 'images/chapter_open.gif")';
var close = 'url("'+thePath + 'images/chapter.gif")';
  if(document.getElementById)  {
    var el = document.getElementById(obj);
    var title = document.getElementById(obj+'Title');

    if(el.style.display != "block"){ 
      title.style.backgroundImage = open;
      el.style.display = "block";
    }else{
      title.style.backgroundImage = close;
      el.style.display = "none";
    }
  }// end -  if(document.getElementById) 
}//end - function SwitchMenu(obj)

function warenh(WarenH){
	document.location = "shop.html?menues=SHOP&wareng="+WarenH;
}
function detailAnsicht(){
	var wind = window.open("","Detailansicht","resizable,width=620,height=620,top=0,left=0,status=no,dependent=no");
	wind.focus();
}
function num_sort(a,b){
	return a-b;
}
function correct_layout(){
	ajaxTest();
	var SEITE = "Seite";
	var FOOT = "foot-area";
	var MUSS = ["head-area",FOOT];
	var muss = maxDiv(MUSS);
	var SOLL = ["menu-area","main-area","info-area","main-area-art001"]; 
	var soll = maxDiv(SOLL);
	var KANN = ["home-area","homecol1","homecol2","homecol3"];
	var kann = maxDiv(KANN);
//	var hoehe = soll[0] + kann[0];
  var hoehe = 0;
  hoehe += Math.max.apply(null, soll);
  hoehe += Math.max.apply(null, kann);
	for (var x in muss){
		hoehe += muss[x];
	}
//	toggleTest("toggletext","toggler","togglext");
	document.getElementById(SEITE).style.height = hoehe  + "px";
	document.getElementById(FOOT).style.visibility = "visible";
	toggleTest("toggletext","toggler","togglext");
}
function maxDiv(namen){
	var namenLen = [0];
	for (var x in namen){
		var name = namen[x];
		var id = document.getElementById(name);
		if (id){
			namenLen.push(Math.max(id.offsetHeight, id.scrollHeight, id.clientHeight));
		}
	}
//	namenLen = namenLen.reverse(namenLen.sort(num_sort));
	return namenLen;
}
function correct_layoutOld(){
	ajaxTest();
	var mainarea;
	var namen = new Array("main-area","main-area-art001");
	for (var i = 0; i < namen.length; i++){
		var name = namen[i];
		if (document.getElementById(name)){
			if (document.getElementById(name).offsetHeight){
				mainarea = name;
				break;
			}
		}
	}
/*	alert("window.innerHeight=" + window.innerHeight
	+ " window.outerHeight=" + window.outerHeight
	+ " screen.height=" + screen.height
	+ " screen.availHeight=" + screen.availHeight
	+ " document.body.offsetHeight=" + document.body.offsetHeight
	); */ 
	if (document.getElementById('menu-area')){
		var hoehe = 320;	// Default Hoehe
		if (window.innerHeight){
			hoehe = window.innerHeight; // - 300;
		} else if (document.body.offsetHeight){	// fuer IE
			hoehe *= 2.5;
			hoehe = (document.body.offsetHeight > hoehe) ? document.body.offsetHeight : hoehe;
		}
		var div_heights = new Array(
			hoehe,
			document.getElementById('menu-area').offsetHeight,
			(document.getElementById(mainarea).offsetHeight | 0)
		);
		div_heights = div_heights.reverse(div_heights.sort(num_sort));
		var style_height = 100 + div_heights[0] + "px";
		document.getElementById('menu-area').style.height = style_height;
		document.getElementById(mainarea).style.height = style_height;
	}
}
// 
function toggleTest(toToggle, Toggler, TogglerExt){
	var totoggle = document.getElementById(toToggle);
	var toggler  = document.getElementById(Toggler);
	var togglext  = document.getElementById(TogglerExt);
	if (totoggle && toggler && togglext){
		totoggle.className = "toggletext";
		totoggle.style.visibility = "visible";
		if (totoggle.clientHeight >= totoggle.scrollHeight){
			toggler.style.visibility = "hidden";
		} else if ((totoggle.clientHeight * 3) < totoggle.scrollHeight){
			togglext.style.visibility = "visible";
			toggler.innerHTML = "";
		} else {
			toggler.style.visibility = "visible";
			togglext.innerHTML = "";
		}
	}
}

function toggleMore(toToggle, toggler, _more, _less){
	var textarea = document.getElementById(toToggle);
	if (textarea && toggler){
		var claxx = textarea.className;
		textarea.className = (claxx == "") ? "toggletext" : "";
		toggler.innerHTML  = (claxx == "") ? _more : _less;
	}
}
//-------------------------------------------------------------------
//   Eingabefeld mit einem Defaultwert fuellen
//-------------------------------------------------------------------
function populateElement(selector, defvalue) {
  if($.trim($(selector).val()) == "") {
    $(selector).val(defvalue);
  }
		
  $(selector).focus(function() {
    if($(selector).val() == defvalue) {
      $(selector).val("");
    }
  });
		
  $(selector).blur(function() {
    if($.trim($(selector).val()) == "") {
      $(selector).val(defvalue);
    }
  });
  $(selector).focus();
  $(selector).blur();
}
//-------------------------------------------------------------------
//   BilderCarousel in der Detailansicht
//-------------------------------------------------------------------
//var bildhome = "http://www.monochrom.com/cc/monoc/";
var bildhome ="../int/bild/";
var mycarousel_itemList = [];
function mycarousel_itemVisibleInCallback(carousel, item, i, state, evt)
{
    // The index() method calculates the index from a
    // given index who is out of the actual item range.
    var idx = carousel.index(i, mycarousel_itemList.length);
    carousel.add(i, mycarousel_getItemHTML(mycarousel_itemList[idx - 1]));
};

function mycarousel_itemVisibleOutCallback(carousel, item, i, state, evt)
{
    carousel.remove(i);
};

/**
 * Item html creation helper.
 */
function mycarousel_getItemHTML(item)
{
    return '<img onclick="mycarousel_click(this)" src="' + item.url + '" width="60" height="60" alt="' + item.title + '" />';
};
function mycarousel_click(bild){
	if (bild && (bild.alt != "")){
	  jQuery("#img212x212").attr('src',bildhome + "212x212/" + bild.alt + '.jpg');
    jQuery("#lnk600x600").attr('href',bildhome + "600x600/" + bild.alt + '.jpg');
	}
}
function makeCarousel(){
    jQuery('#mycarousel').jcarousel({
      auto: 0,
      scroll: 1, 
      initCallback: mycarousel_initCallback,
      itemVisibleInCallback: {onBeforeAnimation: mycarousel_itemVisibleInCallback},
      itemVisibleOutCallback: {onAfterAnimation: mycarousel_itemVisibleOutCallback}
    });
};
function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};
jQuery(document).ready(function() {
  var bildname = jQuery('#mycarousel').attr("bildname");
  if (bildname) { 
	  var bildzahl = 0;
	  var bildsize = "60x60/";
	  var bildUrl = bildhome + bildsize + bildname;
	  var bild = new Image();
	  bild.onabort = function(){ 
	  	// wenn nur 2 Bilder da sind wir ein 3. Dummybild (dot.gif) dazugestellt
	  	if (bildzahl > 1) {
	  		while (bildzahl <= 3){
          mycarousel_itemList[bildzahl] = {url: "../pic/dot.gif", title: ""};
          bildzahl++;
	  		}
	  		makeCarousel();
	  	}
	  };
	  bild.onerror = bild.onabort;
	  bild.onload  = function(text){
	    if (bildzahl >= 0) {
	      mycarousel_itemList[bildzahl] = {url: bild.src, title: bildname + ((bildzahl > 0) ? "_" + bildzahl : "")};
	    }
	    bildzahl++;
	    bild.src=bildUrl + "_" + bildzahl + ".jpg";
	  };
	  bild.src=bildUrl + ".jpg";
  }
  correct_layout();
  populateElement('#SearchText', '--- ??? ---');  
});
							