var msgIsMarked    = 'Dekor wurde vorgemerkt';
var msgIsNotMarked = 'Dieses Dekor vormerken';

function togglePreview(no)
{
  var obj = document.getElementById('m'+no);
  if (obj && obj.style.display != 'block') {
    if (this.lastFormat) { 
      this.lastFormat.style.display = 'none';
    }
    obj.style.display = 'block';
    this.lastFormat = obj;
  }
}

function openfull(img)
{
  window.open('/images/products/dekore/'+img, 'Vollbild', 'width=1200,height=1000,toolbar=no,status=no,menubar=no,resizable=yes');
  return true;
}

// ---------------------------------------------------------------------

// testing ground (alex)

/********************************
function Fensterweite () {
  if (window.innerWidth) {
    return window.innerWidth;
  } else if (document.body && document.body.offsetWidth) {
    return document.body.offsetWidth;
  } else {
    return 0;
  }
}

function CheckWeite() {
  W = Fensterweite();
  min = 800; // min width in pixel
  if (W < min) {
    alert('too small');
  }
}

Event.observe(window, 'resize', CheckWeite);
***********************************/


// ---------------------------------------------------------------------

/**
 * Calls serverside script and toggles the decor checkbox and HTML
 * depending on success
 *
 * @param bool isChecked   true if checkbox is checked, else false
 * @param int  decor_id
 */
function toggleDecorBasket(isChecked, decor_id)
{
  var handlerFunc = function(t) {
    // User marked decor and server script was successful
    if(isChecked && t.responseText == 1) {
      $('cbMsg').innerHTML = msgIsMarked;
    } else {
      $('cbMsg').innerHTML = msgIsNotMarked;
    }
  }
  new Ajax.Request('/cgi-bin/decor_basket.pl', {
    parameters: 'did=' + decor_id,
    onSuccess: handlerFunc
  });
}

/**
 * Toggle element and hide others if element is shown
 */
function toggle(id)
{
  // show or hide
  if($(id).style.display == 'block') {
    $(id).style.display = 'none';
    this.lastElement = false;
  }
  else {
    if(this.lastElement) { // hide if last element
      this.lastElement.style.display = 'none';
    }
    $(id).style.display = 'block';
    this.lastElement = $(id); // save last element
  }
}

//first a helper method
var $CE = function(tagName, attributes, styles){ //short for create element
      var el = document.createElement(tagName);
      if (attributes)
            $H(attributes).each(function(pair){
                  eval("el." + pair.key + "='" + pair.value + "'");
            });
      if (styles)
            $H(styles).each(function(pair){
                  el.style[pair.key] = pair.value;
            });

      return $(el);
};


/**
 * Applies onclick handler to all elements with id and class "toggle"
 * and binds a toggle function to it through the event observer.
 *
 * @example
 * element with id "toggle_4" or "desc_4"
 * toggles element with id "desc_4"
 */
function registerEventHandler()
{

  var ajaxpopups = document.getElementsByClassName("ajaxpopup");

  for(var i=0; i<ajaxpopups.length; i++) {
    //alert(i + " " + ajaxpopups[i].id);

    ajaxpopups[i].onclick=function() {
      var id = this.id || window.event;

      var url = $(id).href;
      
      var handlerFunc = function(t) {

        var msg = t.responseText;

        $('ajaxpop_' + id).innerHTML = msg;
        toggle('ajaxpop_' + id);
      }

      // User marked decor and server script was successful
      new Ajax.Request(url, {
       method: "GET",
       onSuccess: handlerFunc
       });


      //create_ajax_popup(id);
      return false;
    }
  }

  var toggles = document.getElementsByClassName("toggle");
  
  for(var i=0; i<toggles.length; i++) {
    // FIXME Possible bug in Prototype's Event.observe()
    //       The callback function (param 3) doesn't get the right
    //       "this" pointer in IE.
    /*
    Event.observe(toggles[i].id, 'click', function(){
      var id = new String(this.id);
      id = id.replace('toggle', 'desc');
      toggle(id);
    });
    */
    toggles[i].onclick=function() {
      var str = this.id || window.event;
      var id = str.replace('toggle', 'desc');
      toggle(id);
    }
  }

}


// function handlerFunc (t,id) {
//         alert("xxx " + $('ajaxpop_' + id).innerHTML);
//         var msg = "hallo welt"; //t.responseText;

//         if ($('ajaxpop_' + id)) {
//           alert("no problem");
//           $('ajaxpop_' + id).innerHTML = msg;
// 	  document.getElementById('ajaxpop_' + id).innerHTML = msg;
//         } else {
//           alert("problem");
//         }
// 	//$('ajaxpop_' + id).innerHTML = '<b>xxx</b>';
//        $('ajaxpop_' + id).show();
//       }


Event.observe(window, 'load', registerEventHandler, false);

// function create_ajax_popup(id)
// {
//   var url = $(id).href;
//   //alert(url);

//   //var pop = $CE('p', {className:'desc wide', id:'ajaxpop_' + id});
//       //pop.innerHTML = "msg";
//   //  $(id).parentNode.insertBefore(pop, $(id).nextSibling);


//   var handlerFunc = function(t) {
//     //alert(t.responseText);
//     //var 
//     alert("xxx " + $('ajaxpop_' + id).id);
//     var msg = "hallo welt"; //t.responseText;

//     //pop.innerHTML = msg;
//     if ($('ajaxpop_' + id)) {
//       alert("no problem");
//       $('ajaxpop_' + id).innerHTML = msg;
//     } else {
//       alert("problem");
//     }
//     $('ajaxpop_' + id).innerHTML = '<b>xxx</b>';
//     $('ajaxpop_' + id).show();
//     //var id = $(id).id;
//     //var pop = $CE('div',{id:'popup_' + id, className:'desc'});
//     //var pop = new Element('div', { class: 'desc' });
//     //alert(msg);
//     //var pop = $CE('div',{id:'popup_' + id, className:'desc'});
//     //Element.update(pop, 'alles klar' + $(id).id);
//     //var pop = document.createElement("div");
//     //pop.innerHTML="alles klar";
//     //alert('x' + pop);
//     //$(id).appendChild(pop);
//   }
//     // User marked decor and server script was successful
//     //if(t.responseText) {
//     //  alert(t.responseText);
//     //  //$('cbMsg').innerHTML = msgIsMarked;
//     //} 
//     //else {
//     //  $('cbMsg').innerHTML = msgIsNotMarked;
//     //}
//     //}
//   new Ajax.Request(url, {
//     method: "GET",
//     onSuccess: handlerFunc
//   });

// }
