// JavaScript Document
/**
* Function : dump()
* Arguments: The data - array,hash(associative array),object
*    The level - OPTIONAL
* Returns  : The textual representation of the array.
* This function was inspired by the print_r function of PHP.
* This will accept some data as the argument and return a
* text that will be a more readable version of the
* array/hash/object that is given.
*/
function dump(arr,level) {
    var dumped_text = "";
    if(!level) level = 0;
	
    //The padding given at the beginning of the line.
    var level_padding = "";
    for(var j=0;j<level+1;j++) level_padding += "    ";
	
    if(typeof(arr) == 'object') { //Array/Hashes/Objects
        for(var item in arr) {
            var value = arr[item];
	 
            if(typeof(value) == 'object') { //If it is an array,
                dumped_text += level_padding + "'" + item + "' ...\n";
                dumped_text += dump(value,level+1);
            } else {
                dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
            }
        }
    } else { //Stings/Chars/Numbers etc.
        dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
    }
    return dumped_text;
} 

function scrolling(init) 
{		
    if ( $(init) != null)
    {
        var scroller = new Fx.Scroll(window);
        var el = $(init).getElements('a');
        var index=0;for (var i=0;i<=el.length-1;i++)
        {
            var element = el[i];
            $(element).addEvent('click', function(){
                scroller.toElement(this.href.substring(this.href.lastIndexOf('#')+1,this.href.length));
            });
        }
    }
}
	
    
/*
 *	BROWSER BOOKMARK
 */

function addBookmark(title,url) {
		
    if( document.all ) {
        window.external.AddFavorite( url, title);
    }
    else if (window.sidebar) {
        window.sidebar.addPanel(title, url,"");
    }
    else if( window.opera && window.print ) {
        return true;
    }
}

window.addEvent('domready', function() {

    var scroller = new Fx.Scroll(window);
    var el	=	$('en_main').getElements('a[name=top]');
    for (var i=0;i<=el.length-1;i++) {
        var element	= el[i];
        $(element).addEvent('click', function() {
            scroller.toTop();
        });
    }

	if ($('topspeaker'))
		{
			//hole die Topspeaker

			var module	=	$('topspeaker').getProperty('title');
			var host	=	window.location.host;
				url		=	'http://'+host+'/konferenzen/ext_scripts/v2/php/topspeaker.php?module='+module;
		    var data	=	new Ajax(url, {	method: 'get', update: $('topspeaker') });
		    	data.request();
		    ( function () { data.request(); }).periodical(8000);


		}

		
    if ($('twitter')) {

        twitterContainer	=	$('twitter');
        twitterID			=	twitterContainer.getProperty('user');
        twitterName			=	twitterContainer.getProperty('name');
        twitterLimit		=	3;

        // http://twitter.com/statuses/user_timeline/ *twitterID*.json
        twitterRequestUrl		=	"/konferenzen/ext_scripts/v1/php/twitterJSON.php?twitterID=" + twitterID;
        twitterRequestUrl1                =        "/konferenzen/ext_scripts/v1/php/twitterJSON1.php?twitterID=" + twitterID;
        twitterRequestUrl2                =        "/konferenzen/ext_scripts/v1/php/twitterJSON2.php?twitterID=" + twitterID;

        var twitterJSON1 = new Json.Remote(twitterRequestUrl1, {
            onComplete: function(twitter){
                //alert(twitter);
                if(twitter != 0){
                    sortTwitterData(twitter);

                } else {
                    var twitterList =    new Element('ul').setProperty('id','twitterlists');
                    li = new Element('li').setStyle('visibility','hidden');
                    new Element('div').setProperty('class','content').setHTML("Twitter is currently not available.").injectInside(li);
                    li.injectInside(twitterList);
                    twitterList.injectInside(twitterContainer);

                    twitterContainerHeight = $('twitter').getStyle('height');
                    if (window.ie != true) {
                        twitterContainer.setStyle('height', 70);
                        new Fx.Style(twitterContainer, 'height', {
                            duration: 250,
                            transition: Fx.Transitions.linear
                            }).start(70,twitterContainerHeight);
                    } else {
                        col3Height	=	col3.getStyle('height').toInt();
                        col3.setStyle('height', col3Height+600);
                    }

                    twitterListings        =        $('twitterlists').getElements('li');
                    twitterListings.each(function(item,index) {
                        ( function(){
                            new Fx.Style(item, 'opacity', {
                                duration: 250,
                                transition: Fx.Transitions.linear
                                }).start(0.0,1.0);
                        }).delay(500*(index+1));
                    });

                }

                var twitterJSON2 = new Json.Remote(twitterRequestUrl2, {
                    onComplete: function(twitter){
                        if(twitter != 0){
                            var tw = document.getElementById('twitter');
                            var twl = document.getElementById('twitterlists');
                            twitterContainer.setStyle('height','');
                            tw.removeChild(twl);
                            sortTwitterData(twitter);
                        }
                    }
                    }).send();

            }
            }).send();

        function sortTwitterData(data) {
            //alert(dump(data[0]));

            // auslesen der elemente des twitterObjects
            twitterObjectLength	=	data.length;
            twitterObject		=	data;
            twitterResults		= 	new Array();


            var	twitterList	=	new Element('ul').setProperty('id','twitterlists');

            twitterObject.each(function(item,index) {
                if (index < twitterLimit)	{
                    twitterResults[index]			= 	new Array();
                    twitterResults[index]["name"]		=	twitterObject[index]["user"]["name"];
                    twitterResults[index]["location"]           =	twitterObject[index]["user"]["location"];
                    twitterResults[index]["url"]		=	"http://twitter.com/"+twitterObject[index]["user"]["name"];
                    twitterResults[index]["date"]		=	twitterObject[index]["created_at"];
                    twitterResults[index]["text"]		=	twitterObject[index]["text"];


                    // Datum transformieren
                    // Fri Jun 27 13:31:43 +0000 2008 => Freitag, 27. Juni, 13:31 Uhr

                    twitterResults[index]["date"]	=	twitterResults[index]["date"].substr(0, twitterResults[index]["date"].lastIndexOf("+"));


                    // Urls in texten finden und ggfls formatieren
                    if (twitterResults[index]["text"].contains('http')) {
                        twitterResults[index]["text"] = twitterResults[index]["text"].replace(/(http:\/\/\S*)/g, '<a href="$1">$1<\/a> ');
                    } else if (twitterResults[index]["text"].contains('www') ){
                        twitterResults[index]["text"] = twitterResults[index]["text"].replace(/(www.\S*)/g, '<a href="http://$1">$1<\/a> ');
                    }
                    if (twitterResults[index]["text"].contains('@') ){
                        twitterResults[index]["text"] = twitterResults[index]["text"].replace(/(@\S*)/g, '<a href="http://www.twitter.com/$1">$1<\/a> ').replace('@','');
                    }
                    if (twitterResults[index]["text"].contains('#') ){
                        twitterResults[index]["text"] = twitterResults[index]["text"].replace(/(#\S*)/g, '<a href="http://twitter.com/search?q=$1">$1<\/a> ').replace('q=#','q=%23');
                    }
                    // erstellen des HTML Quellcoed
                    li	=	new Element('li').setStyle('visibility','hidden');
                    div	=	new Element('div').setProperty('class','content');
                    // new Element('a').setProperty('href',twitterResults[index]["url"]).setText(twitterResults[index]["name"]+": ").injectInside(div);
                    new Element('span').setHTML(twitterResults[index]["text"]).injectInside(div);
                    new Element('div').setText(twitterResults[index]["date"]).setProperty('class','date').injectInside(div);
                    div.injectInside(li);
                    li.injectInside(twitterList);
                }
                if (index == twitterLimit)	{
                    li	=	new Element('li').setStyle('visibility','hidden');
                    new Element('div').setProperty('class','content').setHTML("Latest <b>"+ twitterLimit +"</b> Messages from Twitter.<br> More under <a href='http://twitter.com/"+twitterName+"'>http://twitter.com/"+twitterName+"</a> ").injectInside(li);

                    li.injectInside(twitterList);
                }
            });


            twitterList.injectInside(twitterContainer);
            // shcnell holen der höhe und sofort verstecken!
            twitterContainerHeight	=	$('twitter').getStyle('height');


            if (window.ie != true) {
                // alert(twitterContainerHeight);
                twitterContainer.setStyle('height', 70);

                new Fx.Style(twitterContainer, 'height', {
                    duration: 250,
                    transition: Fx.Transitions.linear
                    }).start(70,twitterContainerHeight);
            } else {
                col3Height	=	col3.getStyle('height').toInt();
                col3.setStyle('height', col3Height+600);
            }


            twitterListings	=	$('twitterlists').getElements('li');
            twitterListings.each(function(item,index) {
                ( function(){
                    new Fx.Style(item, 'opacity', {
                        duration: 250,
                        transition: Fx.Transitions.linear
                        }).start(0.0,1.0);
                }).delay(500*(index+1));
            });


        // einfaden und ausfahren der twitterbox
        }
    }
	

    if ($('generateSidebar'))
    {
        if ($('sessions-js'))
        {
            new Element('h2').setText('Sessions').injectInside('columncontent');
        }
			
        // hole alle h3s
        headlines	=	$('en_main').getElements('h3');
        output	=	new Element('div',{
            'id': 'col3_anchors',
            'class': 'box'
        });
        ul		=	new Element('ul');
        headlines.each(function(item, index)
        {
					
            if (item.getElement('span[class=company]'))
            {
                company	=	item.getElement('span[class=company]').getText();
                item.getElement('span[class=company]').setText('');
                name	=	item.getText('');
                item.getElement('span[class=company]').setText(company);
            }
            else if (item.getElement('span[class=info]'))
            {
                company	=	item.getElement('span[class=info]').getText();
                item.getElement('span[class=info]').setText('');
                name	=	item.getText('');
                item.getElement('span[class=info]').setText(company);
            }
            else
            {
                name	=	item.getText('');
            }
            li	=	new Element('li');
            a	=	new Element('a').setText(name).setStyle('cursor','pointer');
            a.addEvent('click', function()
            {
                scroller.toElement(item);
            });
            a.injectInside(li);
            li.injectInside(ul);
				
        });
				
        ul.injectInside(output);
        output.injectInside('columncontent');

    }
    else
    {
        scrolling('columncontent');
    }
		
	
	
    // Tool Tips
	
    var Tips1 = new Tips($$('.tooltip'));
    var Tips2 = new Tips($$('.tooltip_arrow'), {
        className: 'arrow'
    });

    if ( $('feed-icon') != null)
    {
        $('feed-icon').addEvent('mouseover', function()	{
            this.src='/konferenzen/img/layout/feed-active.png';
        });
        $('feed-icon').addEvent('mouseout', function()	{
            this.src='/konferenzen/img/layout/feed-inactive.png';
        });
        $('blog-icon').addEvent('mouseover', function()	{
            this.src='/konferenzen/img/layout/blog-active.png';
        });
        $('blog-icon').addEvent('mouseout', function()	{
            this.src='/konferenzen/img/layout/blog-inactive.png';
        });
        $('flickr-icon').addEvent('mouseover', function(){
            this.src='/konferenzen/img/layout/blog-active.png';
        });
        $('flickr-icon').addEvent('mouseout', function(){
            this.src='/konferenzen/img/layout/blog-inactive.png';
        });
        $('speaker-icon').addEvent('mouseover', function(){
            this.src='/konferenzen/img/layout/blog-active.png';
        });
        $('speaker-icon').addEvent('mouseout', function(){
            this.src='/konferenzen/img/layout/blog-inactive.png';
        });
    }
	
});


/* Googlemaps Implementierung */

var googleMaps	=	new Array();
var	googleAdressIndex	=	"";
var map			=	new Array();
var geocoder	=	new Array();
	
window.addEvent('load', function() {
    if (googleMaps.length > 0) {
        containerMaps	=	$('en_main').getElements('div[id=google]');
        googleMaps.each(function(item, index){
            /* Google API:begin --------------------------------------------------- */
            map[index] 			= null;
            geocoder[index] 	= null;
						
            containerMaps[index].setStyles({
                'height':375,
                'margin':'0px 0px 0px 0px',
                'overflow':'hidden'
            });

							
            if (GBrowserIsCompatible()) {
							  
                map[index] = new GMap2(containerMaps[index]);
								
                //map[index].setOnLoadCallback(function() { alert('fertig geladen'); });
								
                map[index].addControl(new GSmallMapControl());
                // map[index].addControl(new GScaleControl());
                geocoder[index] = new GClientGeocoder();
								
                eventTabOne	=	"<b>"+item['name']+"</b><br/>"+item['strasse']+"<br/>"+item['ort']+"<br/><a href='"+item['webseite']+"'>"+item['webseite']+"</a>";
                eventTabTwo	=	"";
								
                var tabsArray		=	new Array();
                tabsArray[0]	=	new GInfoWindowTab("Adresse", eventTabOne);
                address	=	item['adress'];
							
                if (geocoder[index]) {
                    geocoder[index].getLatLng(
                        address,
                        function(point) {
                            if (!point) {
                                alert(address + " not found");
                            }
									

                            map[index].setCenter(point, 13);
                            map[index].panDirection(0, 1);
									  
                            var marker = new GMarker(point);
                            map[index].addOverlay(marker);
                            marker.openInfoWindowTabs(tabsArray);
                            GEvent.addListener(marker, "click", function() {
                                marker.openInfoWindowTabs(tabsArray);
                            });
									
                        }
                        );
                }
            }
        /* Google API:en ----------------------------------------------------- */
        });
    }

    if ( $('toplink') != null)
    {
        $('toplink').addEvent('click', function() {
            scroller.toTop();
        });
    }


});
window.addEvent('load', function() {
	
	
    if ($('shortlinks'))
    {
        $('columncontent').empty();
        $('shortlinks').injectInside('columncontent');
			
    }
	
	
});

// PopUp Script //

var pop = null;

function popdown() {
//if (pop && !pop.closed) pop.close();
}

function popup(obj,w,h) {
    var url = (obj.getAttribute) ? obj.getAttribute('href') : obj.href;
    if (!url) return true;
    w = (w) ? w += 20 : 150;  // 150px*150px is the default size
    h = (h) ? h += 25 : 150;
    var args = 'width='+w+',height='+h+',resizable,'+'scrollbars=yes';
    popdown();
    pop = window.open(url,'',args);
    return (pop) ? false : true;
}

window.onunload = popdown;
window.onfocus = popdown;


/**
 * adds the title (second h1-tag) at the end of the url in the link-object
 */
function addTitle(linkObj) {
    linkObj.href += document.getElementsByTagName("h2")[0].innerHTML;
}



