/**
 * @author Rob van der Lugt
 */
var recentlyAdded;
function number_format(number, decimals, dec_point, thousands_sep){
    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
    var d = dec_point == undefined ? "," : dec_point;
    var t = thousands_sep == undefined ? "." : thousands_sep, s = n < 0 ? "-" : "";
    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}

bodysize = function(){
    var myHeight = 0;
    if (typeof(window.innerHeight) == 'number') {
        //Non-IE
        myHeight = window.innerHeight;
    }
    else 
        if (document.documentElement && document.documentElement.clientHeight) {
            //IE 6+ in 'standards compliant mode'
            myHeight = document.documentElement.clientHeight;
        }
        else 
            if (document.body && document.body.clientHeight) {
                //IE 4 compatible
                myHeight = document.body.clientHeight;
            }
    return myHeight;
}

initHeight = function(){
    if (typeof(standAlone) == 'undefined') {
        standAlone = false;
    }
    if (standAlone) {
        initHeightIndex();
        return false;
    }
    if (document.getElementById) {
        var bodyheight = bodysize();
        var divWebsite = document.getElementById('website');
        var divColumn1 = document.getElementById('column1');
        var divColumn2 = document.getElementById('column2');
        var divFooter = document.getElementById('footerdiv');
        var minheight = 650;
        if (bodyheight < minheight) {
            if (bodyheight < divColumn2.offsetHeight) {
                divFooter.style.position = 'static';
                divWebsite.style.height = parseInt(divColumn2.offsetHeight) + 23 + parseInt(divFooter.offsetHeight) + 'px';
            }
            else {
                divFooter.style.position = 'static';
                divWebsite.style.height = parseInt(minheight) + parseInt(divFooter.offsetHeight) + 'px';
                divColumn1.style.height = parseInt(minheight) + 'px';
                divColumn2.style.height = parseInt(minheight) + 'px';
            }
        }
        else 
            if (bodyheight < divColumn2.offsetHeight) {
                divFooter.style.position = 'static';
                divWebsite.style.height = parseInt(divColumn2.offsetHeight) + 23 + 2 + parseInt(divFooter.offsetHeight) + 'px';
            }
            else {
                divFooter.style.position = 'absolute';
                divWebsite.style.height = '100%';
            }
    }
}
initHeightIndex = function(){
    if (document.getElementById) {
        var bodyheight = bodysize();
        var divWebsite = document.getElementById('website');
        /*var divRow1 = document.getElementById('row1');
         var divRow2 = document.getElementById('row2');
         if(parseInt(divRow2.offsetHeight)==0){
         var divColumn2 = document.getElementById('column2');
         divRow2 = parseInt(divColumn2.offsetHeight);
         }else{
         divRow2 = parseInt(divRow2.offsetHeight);
         }
         */
        //var divRow3 = document.getElementById('row3');
        /*
         var minheight = parseInt(divRow1.offsetHeight) + parseInt(divRow2);
         var totalheight = parseInt(minheight) + parseInt(divRow3.offsetHeight) + 10;
         */
        var minheight = 750;
        if (bodyheight < minheight) {
            //divRow3.style.position = 'static';
            divWebsite.style.height = parseInt(minheight) + 'px';
        }
        else {
            //divRow3.style.position = 'absolute';
            divWebsite.style.height = '100%';
        }
    }
}


initMenuIE6 = function(){
    if (document.all && document.getElementById) {
        if (document.getElementById('homesubnav')) {
            subNav = document.getElementById('homesubnav');
            navRoot = document.getElementById('homenavigation');
            divOpacity = document.getElementById('opacitysubnav');
            for (i = 0; i < navRoot.childNodes.length; i++) {
                node = navRoot.childNodes[i];
                if (node.nodeName == 'LI') {
                    node.onmouseover = function(){
                        this.className = "over";
                        divOpacity.style.display = 'block';
                        divOpacity.style.height = subNav.offsetHeight - 4;
                    }
                    node.onmouseout = function(){
                        this.className = this.className.replace("over", "");
                        divOpacity.style.display = 'none';
                    }
                }
            }
        }
    }
}
replaceHeaders = function(){
    if (document.getElementById) {
        var divContent = document.getElementById('content');
        var tags = new Array('h1', 'h2');
        for (var t = 0; t < tags.length; t++) {
            headers = divContent.getElementsByTagName(tags[t]);
            for (var i = 0; i < headers.length; i++) {
                var mode = '';
                var textvalue = headers[i].firstChild.nodeValue;
                if (headers[i].nodeName == 'H2') {
                    mode = 'LinkBlockTitle';
                }
                else {
                    mode = 'PageTitle';
                }
                textvalue = textvalue.replace("\n", "");
                var newImg = document.createElement('img');
                newImg.setAttribute('alt', textvalue);
                newImg.setAttribute('src', 'http://imp.porsche.com/netherlands/nl/ImageMachines/' + mode + '.ashx/rendered.gif?text=' + escape(textvalue.replace(" ", "+")))
                headers[i].replaceChild(newImg, headers[i].firstChild);
            }
        }
    }
}
getCartContents = function(){
    new Ajax(shop_relative + 'ajax/getcart.php', {
        method: 'get',
        onComplete: function(r, x){
            putCartContents(r, x);
        }
    }).request();
}
putCartContents = function(r, xml){
    var grandTotal 		= 0;
    itemsInCart 		= 0;
    xItem 				= xml.getElementsByTagName('item');
    if ($('pClone')) {
        $('pClone').remove();
    }
    var cartContent 	= $('cartPopupPreview');
    cartContent.empty();
    var obj 			= $('cartPreview');
    obj.empty();
    for (x = 0; x < xItem.length; x++) {
        var productid 	= xItem[x].getElementsByTagName('productid').item(0).firstChild.data;
        var variantid 	= xItem[x].getElementsByTagName('variantid').item(0).firstChild.data;
        var product 	= xItem[x].getElementsByTagName('product').item(0).firstChild.data;
        var amount 		= xItem[x].getElementsByTagName('amount').item(0).firstChild.data;
        var price 		= xItem[x].getElementsByTagName('price').item(0).firstChild.data;
        
        grandTotal 		= grandTotal + parseFloat((price * parseInt(amount)));
        itemsInCart 	= itemsInCart + parseInt(amount);
        
        var mainDiv 	= new Element('div').addClass('previewCartBlock').injectInside(cartContent);
        new Element('div').addClass('previewLeft').setHTML(amount).injectInside(mainDiv);
        new Element('div').addClass('previewRight').setHTML(product + '<br/>&euro; ' + number_format(parseFloat((price * parseInt(amount))), 2, '.', '')).injectInside(mainDiv);
        
    }
    
    grandTotal 			= number_format(grandTotal, 2, ',', '.');
    var mainDiv 		= new Element('div').addClass('previewCartBlock').injectInside(cartContent);
    new Element('div').addClass('previewLeft').setHTML('&nbsp;').injectInside(mainDiv);
    new Element('div').addClass('previewRight').setHTML('Totaalbedrag<br/>&euro; ' + grandTotal).injectInside(mainDiv);
    if (itemsInCart == 0) {
        imgSrc 			= shop_relative + 'img/gif/shoppingcart.gif';
        var htmlOutput 	= new Element('span').setHTML('0 Product(en)&nbsp;| &euro; 0,00');
        if ($('cartCheckout')) {
            $('cartCheckout').setStyles({
                display: 'none'
            });
        }
    }
    else {
        imgSrc = shop_relative + 'img/gif/shoppingcart_active.gif';
        var htmlOutput 	= new Element('a', {
            href: shop_relative + 'winkelwagen/'
        }).setHTML(itemsInCart + ' Product(en)');
        var extraOutput = new Element('span').setHTML('&nbsp;| &euro; ' + grandTotal);
        if ($('cartCheckout')) {
            $('cartCheckout').setStyles({
                display: 'block'
            });
        }
    }
    
    
    var p = new Element('span', {
        id: 'cartPreviewContent'
    });
    
    htmlOutput.injectInside(p);
    new Element('img', {
        src: imgSrc
    }).injectBefore(htmlOutput);
    if (typeof(extraOutput) != 'undefined') {
        extraOutput.injectAfter(htmlOutput);
    }
    
    p.injectInside(obj);
    pClone = p.clone();
    pClone.setProperty('id', 'pClone').injectBefore(cartContent);
    p.addEvent('mouseenter', function(){
        popupCartPreview();
    });
    
    /* added */
    var xAdded 			= xml.getElementsByTagName('added');
    if (xAdded.length > 0) {
        xAdded 			= xAdded[0];
        var obj 		= $('cartPopupAdded');
        obj.empty();
        var product 	= xAdded.getElementsByTagName('product').item(0).firstChild.data;
        var maat 		= xAdded.getElementsByTagName('maat').item(0).firstChild.data;
        var kleur 		= xAdded.getElementsByTagName('kleur').item(0).firstChild.data;
        var price 		= xAdded.getElementsByTagName('price').item(0).firstChild.data;
        var thumb 		= xAdded.getElementsByTagName('thumb').item(0).firstChild.data;
        var p 			= new Element('p').setText('Het volgende artikel is aan uw winkelwagen toegevoegd:');
        var arr = new Array();
        if (maat != '-') {
            arr.push(maat);
        }
        if (kleur != '-') {
            arr.push(kleur);
        }
        if (price != '-') {
            arr.push('&euro; ' + number_format(price, '2', ',', '.'));
        }
        var p2 = new Element('p').setHTML(product + '<br/>' + arr.join('&nbsp;|&nbsp;'));
        p.injectInside(obj);
        p2.injectInside(obj);
        if (thumb != '-') {
            var img = new Element('img', {
                src: thumb
            }).injectAfter(p2);
        }
        var img2 = new Element('img', {
            id: 'cartPopupCheckout',
            src: shop_relative + 'img/gif/cart-checkout.gif'
        });
        if (typeof(img) != 'undefined') {
            img2.injectAfter(img);
        }
        else {
            img2.injectAfter(p2);
            new Element('div').addClass('clear').injectBefore(img2);
        }
        
        addCartClickLink(img2);
        popupCartAdded();
        if (recentlyAdded) {
            $clear(recentlyAdded);
        }
        recentlyAdded = (function(){
            closeCartPopupContainer();
        }).delay(5000);
        obj.addEvent('mouseenter', function(){
            if (recentlyAdded) {
                $clear(recentlyAdded);
            }
            this.removeEvents('mouseenter');
        });
    }
    
}
addCartClickLink = function(obj){
    obj.addEvent('click', function(){
        document.location = shop_relative + 'winkelwagen/';
    });
    obj.setStyle('cursor', 'pointer');
}
window.addEvent('load', function(){
    getCartContents();
    if (window.ie6) {
        initMenuIE6();
    }
    initHeight();
    replaceHeaders();
});
window.addEvent('resize', function(){
    initHeight();
});

