// wurde aus dem Core entfernt und keine Alternative hinzugefügt

var $chk = function(obj){
    return !!(obj || obj === 0);
};

var Globals = Globals || {};

// Bildervergrößerung laden
//-------------------------------------------------------
Slimbox.scanPage = function() {
    
    Globals.popupImages = $$('div.image-and-caption');
    var temp = $$();
    
    Globals.popupImages.each(function(img){
        var captionEl1 = img.getChildren('div.caption');
        var captionEl = !!captionEl1 && captionEl1.length > 0 ? captionEl1[0].getChildren('div.template-verylow, div.template-low')[0]: false;
        var imgEl = img.getChildren('div.template-img')[0];
        if(!!captionEl && captionEl.get('text').trim() == '')
            captionEl.destroy();
        if(!!imgEl && !!imgEl.getChildren('img[data-popup]')[0]) {
            imgEl.addClass('link');
            // exclusion for press-photo download
            if(!!captionEl && !!captionEl.getElement && !!captionEl.getElement('a')) {
                imgEl.getChildren('img[data-popup]')[0].set('alt', captionEl.get('text'));
                temp.push(imgEl);
            }
            else if(!!captionEl && !!captionEl.addClass){
                captionEl.addClass('link');
                $$(imgEl, captionEl).addEvents({
                    mouseenter: function(){
                        captionEl.addClass('over');
                    },
                    mouseleave: function(){
                        captionEl.removeClass('over');
                    }
                });
                temp.push(img);
            }
            else
                temp.push(img);
            
            img.getElements('div').setStyle('cursor', 'inherit');
            
            if(Browser.firefox)
                img.setStyle('cursor', '-moz-zoom-in');
            else if(Browser.safari || Browser.chrome)
                img.setStyle('cursor', '-moz-webkit-in');
            else
                img.setStyle('cursor', 'url(\'../grafiken/lupe.cur\'), pointer');
        }
    });

    Globals.popupImages = temp;
    
    Globals.popupImages.slimbox({overlayOpacity: 0.55, counterText: 'Bild <strong>{x}</strong> von <strong>{y}</strong>'}, function(img) {
        var captionEl1 = img.getChildren('div.caption');
        var captionEl = !!captionEl1 && captionEl1.length > 0 ? captionEl1[0].getChildren('div.template-verylow, div.template-low')[0] : false;
        var caption = !!captionEl ? captionEl.get('text') : '&#160;';
        var imgEl = img.getChildren('div.template-img')[0];
        var imgSrc = '';
        if(!!imgEl && !!imgEl.getChildren('img[data-popup]')[0]) {
            imgSrc = imgEl.getChildren('img[data-popup]')[0].get('data-popup');
        }
        return [
            imgSrc,
            caption
        ];
    });
};

var Merkzettel = {
    bookmark: function(obj, type, id){
        if (type != null && type != '' && id != null && id != '') {
            var bookmarks = JSON.decode(Cookie.read('highway_bookmarks')) || [];

            var item = {
                type: type,
                id: Globals.url,
                link: id
            };

            var parent = obj.getParent('.editable');
            item.content_id = parent.get('id');

            var found = false;
            bookmarks.each(function(iItem) {
                if (iItem.content_id == item.content_id)
                    found = true;
            });
            if (found) {
                alert('Dieses Produkt befindet sich bereits auf Ihrem Merkzettel! Diesen finden Sie oben links auf der Internetseite.');
                return;
            }

            bookmarks.push(item);

            Cookie.write('highway_bookmarks', JSON.encode(bookmarks), {
                duration: (365 * 10),
                path: '/'
            });

            alert('Das Produkt wurde Ihrem Merkzettel hinzugefügt. Diesen finden Sie oben links auf der Internetseite.');
        }
    },
    isBookmarked: function(content_id){
        if (content_id != null && content_id != '') {
            var bookmarks = JSON.decode(Cookie.read('highway_bookmarks')) || [];

            var item = {
                content_id: content_id
            };

            var found = false;
            bookmarks.each(function(iItem){
                if (iItem.content_id == item.content_id)
                    found =  true;
            });
            return found;
        }
        return false;
    },
    refreshCounter: function() {
        var bookmarks = JSON.decode(Cookie.read('highway_bookmarks')) || [];
        
        if(bookmarks.length > 0){
            $$('.leaflet')[0].set('html', 'Merkzettel (' + bookmarks.length + ')');
        } else {
            $$('.leaflet')[0].set('html', 'Merkzettel');
        }
    }
};

window.addEvent('domready', function() {
    var globalSizeX = 0;

    var url = document.URL;
    url = url.replace(/^.*\/|\.[^.]*$/g, '') + '.php';
    Globals.url = url;
    Globals.barcodePath = Globals.url == 'index.php' || Globals.url == '.php' ? 'url(grafiken/startseite/barcode.png)' : 'url(grafiken/barcode.gif)';

    $$('.button_container').each(function(item, i)
    {
        var size = item.getSize();
        item.itemSize = globalSizeX;
        globalSizeX += size.x;

        var itemLink = item.getElement('a');
        itemLink.isActive = itemLink.hasClass('active') ? true : false;

        if (itemLink.isActive)
        {
            item.setStyle('background-image', Globals.barcodePath);
            item.setStyle('background-position', (-item.itemSize) + 'px 0');
            item.setStyle('background-repeat', 'repeat-x');
        }

        item.addEvents({
            'mouseenter': function(e) {
                item.setStyle('background-image', Globals.barcodePath);
                item.setStyle('background-position', (-item.itemSize) + 'px 0');
                item.setStyle('background-repeat', 'repeat-x');
                if (!itemLink.isActive) itemLink.set('class', 'active');
                e.stop();
            },
            'mouseleave': function(e) {
                if (!itemLink.isActive) item.setStyle('background', 'none');
                if (!itemLink.isActive) itemLink.removeClass('active');
                e.stop();
            },
            'click': function(e) {
                location.href = itemLink;
            }
        });
    });

    if (Globals.url != 'index.php' && Globals.url != '.php') Merkzettel.refreshCounter();
        // Bookmarks
    $$('.link_leaflet').each(function(item){
         var link = item.get('href');
         var type = link.match('.php') != null || link.match('.htm') != null || link.match('.html') != null ? 'site' : 'id';

         var content_id = item.getParent('.editable').get('id');

         if(Merkzettel.isBookmarked(content_id))
         {
            item.addClass('active');
         }

         item.addEvent('click', function(e){
            e.stop();
            if (!item.hasClass('active')) item.addClass('active');
            Merkzettel.bookmark(item, type, link);
            Merkzettel.refreshCounter();
        });
    });

    $$('a[href=#top]').addEvent('click', function(e){
        e.stop();
        new Fx.Scroll(window).toTop();
        this.blur();
    });

    if ($('search_field') != null) {
        $('search_field').addEvents({
            focus: function(){
                if (this.value == this.defaultValue)
                    this.value = '';
            },
            blur: function(){
                if (this.value == '')
                    this.value = this.defaultValue;
            }
        });
    }

    Slimbox.scanPage();
});
//-------------------------------------------------------
