/**
  *  Habi JS scripts.
  *  (c) 2010 mLED
  **/ 

Position.getSize = function() {
    var width, h;
    var D = document;
    height = Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
    width = Math.max(
        Math.max(D.body.scrollWidth, D.documentElement.scrollWidth),
        Math.max(D.body.offsetWidth, D.documentElement.offsetWidth),
        Math.max(D.body.clientWidth, D.documentElement.clientWidth)
    );
    return { w:width-8, h:height-8 };
}
document.observe("dom:loaded", function() {
    $('header').setStyle("opacity: 1");
    $('header2').setStyle("opacity: 1");

    new Ajax.Request('shop.php?header=true', {
        method: 'get',
        onSuccess: function(transport) {
                var c=new Image();
                c.onload=function(){
                    fader(this.src,true);
                }
                c.src = transport.responseText;
        }
    });
    new PeriodicalExecuter(function(pe) {
        new Ajax.Request('shop.php?header=true', {
            method: 'get',
            onSuccess: function(transport) {
                var c=new Image();
                c.onload=function(){
                    fader(this.src,false);
                }
                c.src = transport.responseText;
            }
        });
    }, 10);
});

function fader(image_src, init) {
        if ($('header2').getStyle('opacity') == 1) {
            new Effect.Opacity('header2', { 
                from: 1, 
                to: 0, 
                duration: 0.3,
                delay: 0.2,
                afterFinish: function() { 
                    $('header2').setStyle({backgroundImage: 'url('+image_src+')'});
                    if (init) {
                        new Ajax.Request('shop.php?header=true', {
                            method: 'get',
                            onSuccess: function(transport) {
                                var c=new Image();
                                c.onload=function(){
                                    fader(this.src,false);
                                }
                                c.src = transport.responseText;
                            }
                        });
                    }
                    
                }
                
            });
        } else {
            new Effect.Opacity('header2', { 
                from: 0, 
                to: 1, 
                duration: 0.3,
                delay: 0.2,
                afterFinish: function() { 
                    $('header').setStyle({backgroundImage: 'url('+image_src+')'});
                }
                
            });
        }
}

function openThis(that){
    MsgBoxDisplay(371);
    
    $("modalBoxContent").innerHTML = '<img style="border:1px solid #ccc;" id="prodImg" src="images/blank.gif" alt="" />';
    $("modalBox").style.visibility="visible";
    
    var img = new Image();
    img.src = 'images/products/'+that;
    
    $('prodImg').style.margin = '19px';
    $('prodImg').src = img.src;
    $('prodImg').height = 327;
    $('prodImg').width = 400;

}

function modalboxHide() {
    if ($("modalwindow").style.visibility != 'hidden') {
        $("modalwindow").style.visibility="hidden";
        $("modalwindow").style.width="1px";
        $("modalwindow").style.height="1px";
        $("modalBox").style.visibility="hidden";
        $("modalBoxContent").innerHTML = "";
    }
}

function centerWindow(element) {
    if($(element) != null) {
        $(element).style.top = 
            Math.round(document.viewport.getScrollOffsets().top + 
            ((document.viewport.getHeight() - $(element).getHeight()))/2)+"px";
        $(element).style.left = 
                Math.round(document.viewport.getScrollOffsets().left + 
                ((document.viewport.getWidth() - $(element).getWidth()))/2)+"px";
    }
}
function MsgBoxDisplay(boxHeight) {

    var psize = Position.getSize();

    if (Object.isElement($("modalwindow")) != true) {
        var Overlay = new Element("div",{id: "modalwindow" });
        document.body.appendChild(Overlay);
        
        var modalBox = new Element("div",{id: "modalBox" });
        modalBox.innerHTML = '<div id="modalBoxContent" style="border:4px solid #990000;background-color:#fff;" ><img style="border:1px solid #333;" id="prodImg" src="images/blank.gif" alt="" /><\/div>';
        $(document.body).insert(modalBox);
        modalBox.setStyle({position:"absolute", backgroundColor: "#ffffff", zIndex: 1000, width: "448px" });

        var modalBoxC = new Element("div",{id: "modalBoxC",title:"[ESC]-Taste zum Schliessen" });
        $("modalBox").insert(modalBoxC);
        modalBoxC.setStyle({position:"absolute", cursor:"pointer",backgroundImage:"url(images/close.gif)", backgroundColor: "transparent", top:"4px",right:"4px", zIndex: 1001, width:"17px", height:"17px" });

        
        Overlay.setStyle({backgroundImage: "url(images/gray.gif)", position: "absolute", top: "4px", left: "4px", zIndex: 999, width: psize['w']+"px", height: psize['h']+"px" });
        Overlay.setStyle({opacity: 0.5 });
        Overlay.onclick = function(){ modalboxHide() }; 
        modalBoxC.onclick = function(){ modalboxHide() }; 
        Event.observe(document, "keypress", function(event){ if(event.keyCode == 27) modalboxHide(); });
        
        Overlay.show();
    } else {
        $("modalwindow").setStyle({visibility: "visible", backgroundImage: "url(images/gray.gif)", position: "absolute", top: 4, left: 4, zIndex: 999, width: psize['w']+"px", height: psize['h']+"px" });
    }
    $("modalBox").setStyle({visibility:"visible",height:boxHeight+"px"});
    centerWindow($("modalBox"));

}
function procCart() {

    new Ajax.Request('shop.php?submit=true&art='+$("art").value+'&menge='+$("qty").value, {
        method: 'get',
        onSuccess: function(transport) {
            modalboxHide();
        }
    });


    
}
function shop(sArt) {
    
    MsgBoxDisplay(240);

    new Ajax.Request('shop.php?art='+sArt, {
        method: 'get',
        onSuccess: function(transport) {
            $("modalBoxContent").innerHTML = transport.responseText;
            $("modalBox").setStyle({visibility:"visible"});
        }
    });

}

function fIsNum(myObj) {
    var myField = myObj;
    if (isNaN(myField.value)) {
        alert('Bitte nur Zahlen eingeben.');
        myField.focus();
        myField.select();
    } else {
        myField.value =parseInt(myField.value);
    }
}
function rotM (string) {
  var aCode = 'a'.charCodeAt();
  var zCode = 'z'.charCodeAt();
  var ACode = 'A'.charCodeAt();
  var ZCode = 'Z'.charCodeAt();
  var AtCode = '@'.charCodeAt();
  var ExCode = '!'.charCodeAt();
  var result = '';
  for (var c = 0; c < string.length; c++) {
    var charCode = string.charCodeAt(c);
    if (charCode >= aCode && charCode <= zCode)
      charCode = aCode + (charCode - aCode + 13) % 26;
    else if (charCode >= ACode && charCode <= ZCode)
      charCode = ACode + (charCode - ACode + 13) % 26;
    else if (charCode == ExCode)
      charCode = AtCode;
    result += String.fromCharCode(charCode);
  }
  return result;
}

