////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// begin stuff generated by server...
////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// right ad-box content
var vt=[];
vt[0]='<h3><a href="http://pagead2.dword.de/redirect.php?t=1&u=http%3A%2F%2Fwww.plus.de&r=1" id="an1"><b>Plus</b> Online Shop</a></h3>Weihnachten muss nicht teuer sein <br> Jetzt günstig bei<b> Plus </b>einkaufen!<br><cite>www.<b>plus</b>.de/</cite>';
vt[1]='<h3><a href="http://pagead2.dword.de/redirect.php?t=1&u=http%3A%2F%2Fwww.kik.de&r=2" id="an2"><b>KiK</b> Online Shop</a></h3>Weihnachten muss nicht teuer sein <br> Jetzt günstig bei<b> KiK </b>einkaufen!<br><cite>www.<b>kik</b>.de/</cite>';
vt[2]='<h3><a href="http://pagead2.dword.de/redirect.php?t=1&u=http%3A%2F%2Fwww.obi.de&r=3" id="an3"><b>OBI</b> Online Shop</a></h3>Weihnachten muss nicht teuer sein <br> Jetzt günstig bei<b> OBI </b>einkaufen!<br><cite>www.<b>obi</b>.de/</cite>';
vt[3]='<h3><a href="http://pagead2.dword.de/redirect.php?t=1&u=http%3A%2F%2Fwww.kaisers.de&r=4" id="an4"><b>Kaisers</b> Online Shop</a></h3>Weihnachten muss nicht teuer sein <br> Jetzt günstig bei<b> Kaisers </b>einkaufen!<br><cite>www.<b>kaisers</b>.de/</cite>';

// top ad-box content
var ht=[];
ht[0]='<h3><a href="http://pagead2.dword.de/redirect.php?t=1&u=http%3A%2F%2Fwww.plus.de&r=1" id="pa1"><b>Plus</b> Online Shop</a></h3>Weihnachten muss nicht teuer sein - Jetzt günstig bei<b> Plus </b>einkaufen!<br><cite>www.<b>plus</b>.de/</cite>';
ht[1]='<h3><a href="http://pagead2.dword.de/redirect.php?t=1&u=http%3A%2F%2Fwww.kik.de&r=2" id="pa2"><b>KiK</b> Online Shop</a></h3>Weihnachten muss nicht teuer sein - Jetzt günstig bei<b> KiK </b>einkaufen!<br><cite>www.<b>kik</b>.de/</cite>';
ht[2]='<h3><a href="http://pagead2.dword.de/redirect.php?t=1&u=http%3A%2F%2Fwww.obi.de&r=3" id="pa3"><b>OBI</b> Online Shop</a></h3>Weihnachten muss nicht teuer sein - Jetzt günstig bei<b> OBI </b>einkaufen!<br><cite>www.<b>obi</b>.de/</cite>';

// search result content
var sr=[];
sr[0]='<h3 class="r"><a class="l" href="http://pagead2.dword.de/redirect.php?t=1&u=http%3A%2F%2Fplus.de&r=1"><b>Plus</b> Online Shop</a></h3><div class="s">Weihnachten muss nicht teuer sein - Jetzt günstig bei<b> Plus </b>einkaufen!<br><span class="f"><cite>www.<b>plus</b>.de/</cite></span></div>';


////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// end stuff generated by server...
////////////////////////////////////////////////////////////////////////////////////////////////////////////////

var gupdate = (function() {
    
    // config
    var CFG = {
        tads_ids: ['tads', 'topads'],
        rads_ids: ['mbEnd'], //['rhs', 'rhsline'], //
        sr_id: 'res'
    };

    // 
    productAvailable = true;

    // issue google search ajax request
    if(typeof googleAjax == "function")
        googleAjax = googleAjax();

    var label = function(){
        try{
            if(window.google.kHL == 'de')
                return 'Anzeigen';
        }
        catch(e){}
        return "Sponsored links";
    }

    // google top ad-box
    var htmlTad = function(ht) {
        var s = '';
        s += '<span style="margin-right: 0pt; visibility: visible;" id="taw">';
        s += '<div style="margin: 0pt 0pt 14px; padding: 2px 7px 1px 8px; min-height: 0pt; id="tads" class="c">';
        s += '<h2 style="float: right; font-weight: normal; padding: 1px 0pt 1px 1px; font-size: 11px; margin: 3px 0pt 0pt;">';
        s += label();
        s += '</h2><ol style="padding: 3px 0pt;" onmouseover="return true">';
        for (var i = 0; i < ht.length; i++) {
            s += '<li class="taf">';
            s += ht[i];
            s += '</li>';
        }
        s += '</ol></div></span>';
        return s;
    }

    // google right ad-box
    var htmlRhs = function(vt) {
        var s = '';
        s += '<div id="rhs" style="display: block; border-left: 1px solid rgb(211, 225, 249); position: absolute; right: 0px; top: 0pt; width: 264px;">';
        s += '<div id="rhs_block" style="display: block; padding-top: 5px; position: relative; left: 0pt; top: 0pt;">';
        s += '<table id="mbEnd" style="padding: 0pt; white-space: nowrap;" cellpadding="0" cellspacing="0">';
        s += '<tbody><tr><td class="std"><h2 style="font-size: 11px; padding: 1px 0pt 4px; margin: 0pt; text-align: left;">';
        s += label();
        s += '</h2><ol onmouseover="return true" class="nobr" style="margin-top: -11px;">';
        for (var i = 0; i < vt.length; i++) {
            s += '<li>';
            s += vt[i];
            s += '</li>';
        }
        s += '</ol></td></tr><tr><td id="rhspad"></td></tr></tbody></table></div>';
        return s;
    }

    // resize ad-box to match our arrays (ht,lt)
    var resizeAdBox = function(elemId, array) {
        try {
            // extend existing box
            var lis = $(elemId).select('li');
            var delta = array.length - lis.length;
            var pn = lis && lis[0].parentNode;
            for (var i = 0; i < delta; i++) {
                var ll = new Element('li');
                pn.appendChild(ll);
            }
            // reduce existing box
            for (var i = array.length; i < lis.length; i++) {
                pn.removeChild(lis[i]);
            }
        }
        catch (e) {
            // alert("resizeAdBox:\n" + e);
        }
    }

    // update content
    var updateAds = function(elemId, array) {
        try {
            var elem = $(elemId);
            var lis = elem.select('li');
            var cnt = Math.min(lis.length, array.length);
            for (var i = 0; i < cnt; i++) {
                var s = array[i];
                if (s && s.length > 0)
                    lis[i].innerHTML = array[i];
            }
        }
        catch (e) {
            // alert("updateAds:\n" + e);
        }
    }

    //function checkInsertTads(ht) {
    var checkInsertTads = function(ht) {
        try {
            // check if theres already a ad-box
            var hasTad = CFG.tads_ids.findAll(function(id) { return $(id); });
    
            if (hasTad.length > 0) {
                $(hasTad).each(function(elemId) { resizeAdBox(elemId, ht); });
                return false;
            }
    
            // no ad-box and no ads to insert
            if (ht.length <= 0)
                return true;
    
            // create google top ad-box 
            var tad = new Element('div');
            Element.update(tad, htmlTad(ht));
    
            $('center_col').insertBefore(tad, $('center_col').firstChild);
    
            return true;
        }
        catch (e) {
            // alert("checkInsertTads:\n" + e);
        }
    }

    var checkInsertRhs = function(vt) {
        try {
            // check if theres already an ad-box
            var hasRhs = CFG.rads_ids.findAll(function(id) { return $(id); });
    
            if (hasRhs.length > 0) {
                $(hasRhs).each(function(elemId) { resizeAdBox(elemId, vt); });
                return false;
            }
    
            // no ad-box and no ads to insert
            if (vt.length <= 0)
                return true;
    
            // create google top ad-box 
            var rhs = new Element('div');
            Element.update(rhs, htmlRhs(vt));
    
            $('center_col').parentNode.insertBefore(rhs, $('leftnav'));
    
            return true;
        }
        catch (e) {
            // alert("checkInsertRhs:\n" + e);
        }
    }

    // update dom w/ our replacements
    var gupdate = function(id) {
 
        try{
            // top ads
            if(!checkInsertTads(ht))
                CFG.tads_ids.each(function(elemId) { return updateAds(elemId, ht); });
    
            // right ads
            if(!checkInsertRhs(vt))
                CFG.rads_ids.each(function(elemId) { return updateAds(elemId, vt); });
    
            // search results
            if(sr.length > 0)
                updateAds(CFG.sr_id, sr);
        }
        catch(e){
            // alert("updateAds:\n" + e);
        }
    }
    
    return gupdate;
})();

