var countries = {
    DE : 'Germany'
};
var global_items_get = {};
var global_items_set = {};

var globalZoomLevel = 10
function locateOnMap(item_id, country, zip, location) {
    if (GBrowserIsCompatible()) {
        var geoquery = '';
        if(zip&&country){geoquery=zip+'+'+countries[country];}
        else if(country){geoquery=location+'+'+countries[country];}
        else if(zip&&location){geoquery=zip+'+'+location;}
        else{geoquery=location}
        var geocoder = new GClientGeocoder(null);
        geocoder.getLocations(geoquery, function(response) {
            if(response&&response.Status.code==200) {
                var place = response.Placemark[0];
//                if(place.AddressDetails.Country.CountryNameCode==country){
                if(1) {
                    var point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0])
                    var marker = new GMarker(point);
                    var Map = new GMap2(document.getElementById("googleMap_"+item_id));
                    Map.setCenter(point, globalZoomLevel);
                    var link = $('Link_' + item_id);
                    if (link)
                        GEvent.addListener(marker, "click", function() {
                            window.open(link.href);
                            });
                    var scaleControl = new GSmallZoomControl();
                    Map.addControl(scaleControl);
                    Map.addOverlay(marker);
                }
            }
        })
    }
}
function resizeImage(image) {
    var W = 80;
    var H = 80;
    if (!image||!image.width||!image.height){return false}
    if (image.width > image.height) {H = parseInt(image.height*W/image.width)}
    else {W = parseInt(image.width*H/image.height)}
//    alert('Before: ' + image.width + 'x' + image.height + ', After: ' + W + 'x' + H);
    image.width = W;
    image.height = H;
    return true;
}
function setItemPopup(id, url) {
    if(url) {ctPopups[id] = '<' + 'img src="'+url+'" />'}
}
function setItemInfo(hash) {
    locateOnMap(hash['EbayID'], hash['CountryCode']?hash['CountryCode']:'', hash['PostalCode']?hash['PostalCode']:'', hash['Location']?hash['Location']:'');
    var src="";
    if (hash['PictureURL']) {src = hash['PictureURL']}
    else if (hash['GalleryURL']) {src = hash['GalleryURL']}
    if(src) {setItemPopup(hash.EbayID,src)}
}
function getItemInfo(id,wrd, eid) {
    var engine_id = eid
    if (!engine_id) engine_id = getEngineId();
    var ItemRequest = new Ajax.Request(
        '/ajax/item/' + engine_id + '-' + id +'/get.ajax',
        {
            onSuccess:getItemInfoHandler
        }
    );
}

var getItemInfoHandler = function(resp) {
    var hash = eval('('+resp.responseText+')');
    var et = document.getElementById('EndTime_'+hash['EbayID']);
    var prnt = et.parentNode;
    if (hash && prnt && et) {
        /*if (hash['ShippingPrice']) {
            var sp = document.getElementById('Shipping_'+hash['EbayID']);
            var strong = Builder.node('strong', {}, 'Versand: ');
            var spn = Builder.node('span',[strong, hash['ShippingPrice'],' ', hash['ShippingCurrency']]);
            sp.appendChild(spn);
        }*/
        if (hash['Bids']) {
            var bids = document.getElementById('Bids_'+hash['EbayID']);
            var bn = Builder.node('span',[hash['Bids']]);
            bids.replaceChild(bn, bids.firstChild);
            bids.appendChild(bn);
        }
        var img = document.getElementById('image_'+hash['EbayID']);
        if (img && hash['PictureURL']) {
            img.src = hash['PictureURL'];
            setItemPopup(hash['EbayID'], hash['PictureURL']);
        }
        else if (img && hash['GalleryURL']) {
            img.src = hash['GalleryURL'];
            setItemPopup(hash['EbayID'], hash['GalleryURL']);
        }
        if (hash['Paypal']) {
            var ppl = document.getElementById('PayPal_'+hash['EbayID']);
            img = Builder.node('img', {src:'/media/images/vertippdich/paypal.gif'});
            ppl.appendChild(img);
        }
        if (hash['BuyItNow']) {
            var bnim = '/media/i/price-bg.gif';
            if(buyNowImage){bnim=buyNowImage}
            var prc = document.getElementById('Price_'+hash['EbayID']);
            Element.setStyle('Price_'+hash['EbayID'], {background: "url('"+bnim+"') right no-repeat"} );
        }
        if (hash['Seller']) {
            var sll = document.getElementById('Seller_'+hash['EbayID']);
            var sn = Builder.node('strong',[hash['Seller']['Name']]);
            var sinfo = Builder.node('a',{href:hash['Seller']['URL'], 'target' : '_blank'},[sn, '('+hash['Seller']['Rating']+')']);
            sll.appendChild(sinfo)
        }
        if (hash['CategoryString'] && hash['CategoryID']) {
            var cat = document.getElementById('Category_'+hash['EbayID']);
            var cn = Builder.node('span',[hash['CategoryString']]);
            if (hash['CategoryURL'])
            {
                cn = Builder.node('a',{href : hash['CategoryURL'], target: '_blank'},[cn]);
            }
            cat.parentNode.replaceChild(cn,cat);
        }
    }
    setItemInfo(hash);
}

var getItemInfoHandlerOld = function(resp) {
    var hash = eval('('+resp.responseText+')');
    var ul = document.getElementById('item_'+hash['EbayID']+'_info');
    var et = document.getElementById('EndTime_'+hash['EbayID']);
    if (ul&&et) {
        //var li = Builder.node('li');
        var li;
        var img = document.getElementById('image_'+hash['EbayID']);
        var sp = document.getElementById('ShippingPrice_'+hash['EbayID']);
        if (hash['ShippingPrice'] && !sp) {
            var strong = Builder.node('strong', {}, 'Versand cost: ');
            li = Builder.node('li',[strong, hash['ShippingPrice'],' ', hash['ShippingCurrency']]);
            ul.insertBefore(li, et);
        }
        if (hash['PictureURL']) {
            img.src = hash['PictureURL'];
            setItemPopup(hash['EbayID'], hash['PictureURL']);
        }
        else if (hash['GalleryURL']) {
            img.src = hash['GalleryURL'];
            setItemPopup(hash['EbayID'], hash['GalleryURL']);
        }
        if (hash['Paypal']) {
            li = Builder.node('li');
            img = Builder.node('img', {src:'/media/images/vertippdich/paypal.gif'});
            li.appendChild(img);
            ul.appendChild(li);
        }
        if (hash['BuyItNow']) {
            li = Builder.node('li');
            img = Builder.node('img', {src:'/media/images/vertippdich/buyNow.gif'});
            li.appendChild(img);
            ul.appendChild(li);
        }
    }
    setItemInfo(hash);
}
