// Copyright 2007 by Patrick Mihm. ARCHITEXT Software GmbH http://www.architext.de
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

function linklabels ( styleId ) {
    var styleId = $(styleId);
    if ( styleId ) {
        var file = styleId.getElementsByTagName('a');
        if ( file ) {
            for ( var i = 0; i < file.length; i++ ) {
				if ( !file[i].href.match(location.host) && file[i].href !== "" && !Element.hasClassName( file[i], 'newwindow') ) {
                    file[i].setAttribute( 'title', 'Extern: ' + encodeURI(file[i].href) );
                }
			
                var images = file[i].getElementsByTagName('img');
                if ( images.length !== 0 ) { continue; }
                if ( file[i].href.match( /^javascript:/i ) || file[i].getAttribute( 'onclick' ) ) { continue; }
                if ( file[i].getAttribute( 'accesskey' ) ) {
                    file[i].setAttribute( 'title', 'accesskey: ' + file[i].getAttribute( 'accesskey' ) );
                }
                switch ( file[i].getAttribute('hreflang')) {
                    case 'en' :
                        AddiconsToLink.iconBottom(file[i],'en');
                    break;
                    case 'de' :
                        AddiconsToLink.iconBottom(file[i],'de');
                    break;
                }
                if ( file[i].href.match(/feedburner.com/) ) {
                        file[i].setAttribute('title', 'This feed is powered by FeedBurner.com');
                        AddiconsToLink.iconBottom(file[i],'feedburner');
                }
                else if ( file[i].href.match(/\.pdf$/) ) {
                    file[i].setAttribute( 'title', 'PDF: ' + file[i].firstChild.nodeValue );
                    file[i].setAttribute( 'target', '_blank' );
                    AddiconsToLink.iconBottom( file[i], 'pdf');
                }
                else if (file[i].href.match(/rss.xml/)) {
                    file[i].setAttribute('title', 'Kopieren Sie diesen Link in Ihr RSS-Programm');
                    AddiconsToLink.iconBottom(file[i],'rss');
                }
                else if (file[i].href.match(/mailto/) && !file[i].href.match(/(druckansicht)|[#]|[?]/)) {
                    AddiconsToLink.iconBottom(file[i],'mailto');
                }
                else if (file[i].href.match(/.zip$/) || file[i].href.match(/.doc$/) || file[i].href.match(/.exe$/) || file[i].href.match(/force-download.php/) || file[i].href.match(/.msi$/) ) {
                    file[i].setAttribute('title', 'Download: ' + file[i].firstChild.nodeValue);
                    AddiconsToLink.iconBottom(file[i],'disk');
                }
                else if (file[i].href.match(/.png$/) || file[i].href.match(/.jpg$/)) {
                    file[i].setAttribute('title', 'Screenshot: ' + file[i].firstChild.nodeValue);
                    AddiconsToLink.iconBottom(file[i],'preview');
                }
                else if ( !file[i].href.match(location.host) && file[i].href !== "" && !Element.hasClassName( file[i], 'newwindow') ) {
                    AddiconsToLink.iconLink(file[i],'newwindow');
                }
                else {
                    continue;
                }
            }
        }
    }
}
var AddiconsToLink = {
    iconBottom: function( ziel, typ ) {
        Element.insert( ziel, { bottom: '<span class="icons ' + typ + '">&nbsp;</span>' } );
    },
    iconLink: function( ziel, typ ) {
        Element.insert( ziel, { after: '<a class="icons ' + typ + '" href="' + ziel.getAttribute('href') + '" title="Neues Fenster" target="_blank" rel="nofollow">&nbsp;</a>' } );
    }
};
function architext_toolbar( styleId ) {
    var styleId = $(styleId);
    if ( styleId ) {
        var URL = encodeURIComponent(window.location);
        var Title = encodeURIComponent(document.title);
        var BookmarkDienste = {
            linkarena: "http://linkarena.com/bookmarks/addlink/?url="+URL+"&title="+Title+"&desc=&tags=",
            webnews: "http://www.webnews.de/einstellen?url="+URL+"&title="+Title,
            yigg: "http://yigg.de/neu?exturl="+URL+"&exttitle="+Title,
            mrwong: "http://www.mister-wong.de/index.php?action=addurl&bm_url="+URL+"&bm_description="+Title,
            google: "http://www.google.com/bookmarks/mark?op=add&hl=de&bkmk="+URL+"&title="+Title,
            delicious: "http://del.icio.us/post?url="+URL+"&title="+Title,
            alltagz: "http://www.alltagz.de/bookmarks/?action=add&address="+URL+"&title="+Title,
            oneview: "http://www.oneview.de/quickadd/neu/addBookmark.jsf?URL="+URL+"&title="+Title
        }
        Element.insert( styleId, { bottom: '<span id="scrolltotop" class="icons">&nbsp;</span>' } );
        for ( var Eigenschaft in BookmarkDienste ) {
            Element.insert( styleId, { bottom: '<span class="icons bookmarks" id="'+Eigenschaft+'" title="Lesezeichen setzen mit '+Eigenschaft+'">&nbsp;</span>' } );
        }
        var scrolltotop = $('scrolltotop');
        scrolltotop.onclick = function() {
            window.scrollTo(0,0);
        };
        var bookmarks = $$('.bookmarks');
        for(var i=0; i<bookmarks.length; i++) {
            bookmarks[i].onclick = function() {
                window.open(BookmarkDienste[this.id]);
            };
        }
    }
}
var search_by_input = {
    obj_inputfield:     'search_field',
    start: function() {
        try{
            new Element.insert( 'nav_top', { after: '<div id="search_by_input_results"></div>' } );
            new Ajax.Autocompleter(this.obj_inputfield, "search_by_input_results", "/suche/search_by_input.php", {
                    method: 'get',
                    paramName: "stichwort", minChars: 1, onException: function() {
                        Event.unloadCache();
                    }
                }
            );
        }
        catch(ex) {
            return false;
        }
    }
}
var navigate_through = {
	action: function(originalRequest) {
		items = originalRequest.responseXML.getElementsByTagName('item');
		Next = $('iconNext');
		Previous = $('iconPrevious');
		i = $('zufallszahl') ? $('zufallszahl').firstChild.nodeValue : 0;
		this.change_buttons();
        automatic = new PeriodicalExecuter(this.next_item.bind(this), 20);
        $('itemImage', 'iconNext', 'iconPrevious').each(function(element){
            element.observe('mouseover', function() { automatic.stop(); }, false);
        });
		Element.observe(Next, 'click', this.next_item.bind(this), false);
		Element.observe(Previous, 'click', this.previous_item.bind(this), false);
	},
	change_buttons: function() {
		Previous.style.cursor = 'pointer';
		Previous.firstChild.src = '/_images/icon_pfeil_previous_on.gif';
		Next.style.cursor = 'pointer';
		Next.firstChild.src = '/_images/icon_pfeil_next_on.gif';
	},
	next_item: function() {
		if ( i < items.length - 1 ) {
			i++;
			this.insert_item(items[i]);
		}
		else {
			i = 0;
			this.insert_item(items[i]);
		}
	},
	previous_item: function() {
		if ( i > 0 ) {
			--i;
			this.insert_item(items[i]);
		}
		else {
			i = items.length - 1;
			this.insert_item(items[i]);
		}
	},
	insert_item: function(items) {
		var itemTitleNew        = items.getElementsByTagName('title')[0] ? items.getElementsByTagName('title')[0].firstChild.nodeValue : false;
		var itemImageNew        = items.getElementsByTagName('image')[0] ? items.getElementsByTagName('image')[0].firstChild.nodeValue : false;
		var itemImageWidthNew   = items.getElementsByTagName('width')[0] ? items.getElementsByTagName('width')[0].firstChild.nodeValue : false;
		var itemImageHeightNew  = items.getElementsByTagName('height')[0] ? items.getElementsByTagName('height')[0].firstChild.nodeValue : false;
		var itemTextNew         = items.getElementsByTagName('beschreibung')[0] ? items.getElementsByTagName('beschreibung')[0].firstChild.nodeValue : false;
		var itemLinkNew         = items.getElementsByTagName('link')[0] ? items.getElementsByTagName('link')[0].firstChild.nodeValue : false;
        imgPreloader = new Image();
        imgPreloader.src = itemImageNew;
		Effect.Fade('itemImage', {
			beforeSetup: function() {
						$('itemLoading').style.backgroundImage = "url('/_images/icon_loading.gif')";
						$('itemLoading').style.backgroundPosition = "center";
						$('itemLoading').style.backgroundRepeat = "no-repeat";
			},
			afterFinishInternal: function() {
				(itemTitleNew && $('itemTitle')) ? Element.update($('itemTitle') , itemTitleNew ) : false;
				(itemTextNew && $('itemText')) ? Element.update($('itemText') , itemTextNew ) : false;
				if(itemLinkNew && $('itemImage')) {
					Element.update($('itemImage') , '<a href="' + itemLinkNew + '"><img alt="[Image: ' + itemTitleNew + ']" src="' + itemImageNew + '" width="' + itemImageWidthNew + '" height="' + itemImageHeightNew + '" title=""></a>' );
				}
				else if($('itemImage')) {
					Element.update($('itemImage') , '<img alt="[Image: ' + itemTitleNew + ']" src="' + itemImageNew + '" width="' + itemImageWidthNew + '" height="' + itemImageHeightNew + '" title="">' );
				}
                $('itemImage').style.display="none";
                Effect.Appear('itemImage', {
                    afterFinishInternal: function() {
                        $('itemLoading').style.background = "";
                    }
                });
            }
		});
	}
}
function init() {
    try{
        linklabels( "inhalt" );
        architext_toolbar( 'toolbar' );
        search_by_input.start(search_by_input);
		$$(".modul_box").each( function(element) { element.setStyle({opacity: 0.75}); } );
    }
    catch(ex) {
        return false;
    };
}
Event.observe(window, "load", init, false);
