/*
      ##### #     ##                                       ##           /##   	
   ######  /#    #### /                  #              /####         #/ ###  	
  /#   /  / ##    ###/                  ###     #      /  ###        ##   ### 	
 /    /  /  ##    # #                    #     ##         /##        ##       	
     /  /    ##   #                            ##        /  ##       ##       	
    ## ##    ##   #    /###     /###   ###   ########    /  ##       ######   	
    ## ##     ##  #   / ###  / / ###  / ### ########    /    ##      #####    	
    ## ##     ##  #  /   ###/ /   ###/   ##    ##       /    ##      ##       	
    ## ##      ## # ##    ## ##    ##    ##    ##      /      ##     ##       	
    ## ##      ## # ##    ## ##    ##    ##    ##      /########     ##       	
    #  ##       ### ##    ## ##    ##    ##    ##     /        ##    ##       	
       /        ### ##    ## ##    ##    ##    ##     #        ##    ##       	
   /##/          ## ##    ## ##    ##    ##    ##    /####      ##   ##       	
  /  #####           ######   ######     ### / ##   /   ####    ## / ##       	
 /     ##             ####     ####       ##/   ## /     ##      #/   ##.nl    	
 #                                                 #                          	
  ##                                                ##                        	
*/

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

	var bodySize = $(document.body).getSize();
	
	$('wrapper').setStyle('height',bodySize.y);
	
});


window.addEvent('domready', function() {
	
	
	
	
	var content  = $('content');
	var menu     = $('menu');
	var zitate   = $('zitate');
	
	var zList = new Object();
	var mList = new Object();
	var bList = new Object();
	
	var romanNumbers = ['I','II','III','IV','V','VI','VII','VIII','IX','X','XI','XII','XIII','XIV','XV','XVI','XVII'];
	var randomPics = ['r1.jpg','r2.jpg','r3.jpg','r4.jpg','r5.jpg'];
	
	
	//////////////////////////////////////////////////////////////////////// Logo
	////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////


	$('logo').addEvents({
	    'click'     : function() {
	        initMenu();
	        initZitate();
	    },
	    'mouseover' : function(){
	        this.setStyle('text-decoration','underline');
	        this.setStyle('cursor','pointer');
	        
	    },
	    'mouseout'  : function(){
	        this.setStyle('text-decoration','none');
	        this.setStyle('cursor','none');
	    
	    }
    });

	
	
	//////////////////////////////////////////////////////////////////////// Zitate
	////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////

	
	var initZitate = function() {
	   	var request = new Request.JSON({
			url: 'data/zitate.php' ,
			onRequest: function() {
				zitate.empty();
			},
			onComplete: function(jsonObj) {
    			openZitate(jsonObj.liste);
    			
			}
		}).send();
    
	}
	
	
	var openZitate = function(zlist) {
	    zList = zlist;
        zList.each(function(zitat) {
            var z       = new Element('div', {
                    'class'	: 'zitat',
                    'events': {
                        'click' : function() {
                            openZitat(zitat.id);
                        },
                        'mouseover' : function() {
                            this.getElement('a').set('class','zitat_button_over');
                        },
                        'mouseleave' : function() {
                            this.getElement('a').set('class','zitat_button');
                        }
                    } 
            }).inject(zitate);						
            var z_link  = new Element('a', {
                'class'	: 'zitat_button',
                'html'  : '&nbsp;'  + romanNumbers[zitat.pos] + '&nbsp;<br/>' + zitat.link + '<br/>'
            }).inject(z);
        });
        
    	openZitat(zlist[1].id);
    };
	
	
	openZitat   = function(id) {
        content.fade('hide');
	    content.empty();
        zList.each(function(zitat) {
            if (zitat.id == id) {
            
                var blatt = new Element('div', {
                    'class'	: 'blatt',
                    'styles' : {
                        'background-color' : '#' + zitat.color
                    }
                });	
                blatt.inject(content);
                
                // if default image go random
                if (zitat.bg == 'default.jpg') {
                    var randNum = Math.floor(Math.random() * randomPics.length);
                    //zitat.bg = randomPics[randNum];
                }
                
                // set background
                $('wrapper').set('styles', {
                    'background': '#ffffff no-repeat url(img/bg/' + zitat.bg + '.jpg)'
                });
                
                
                var blatt_zitat = new Element('div', {
                    'class'	: 'blatt_zitat'
                }).inject(blatt);
                
                var blatt_zitat_text = new Element('div', {
                    'class'	: 'blatt_zitat_text',
                    'html'  : zitat.linklong
                }).inject(blatt_zitat);
                
                var blatt_titel = new Element('div', {
                    'class'	: 'blatt_titel',
                    'html'  : zitat.title
                }).inject(blatt);
                
                var blatt_text = new Element('div', {
                    'class'	: 'blatt_text',
                    'html'  : zitat.longtext
                }).inject(blatt);
                
                var blatt_symbol = new Element('div', {
                    'class'	: 'blatt_symbol'
                }).inject(blatt);
                
                
                
            };
        });
        content.fade('in');
    };
	
	//////////////////////////////////////////////////////////////////////// Menu Items
	////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////

	var initMenu = function() {
	   	var request = new Request.JSON({
			url: 'data/menu.php' ,
			onRequest: function() {
				menu.empty();
			},
			onComplete: function(jsonObj) {
    			createMenuButtons(jsonObj.liste);
    			
			}
		}).send();
    
	}
	
	

	var createMenuButtons = function(mlist) {
	    
	    
	    
        var filler  = new Element('div', {
            'class'	: 'filler'
        }).inject(menu);
	    
	    
	    
	    mList = mlist;
        mList.each(function(item) {
            var m       = new Element('div', {
                    'class'	: 'menu_wrapper',
                    'events': {
                        'click' : function() {
                            openMenu(item.id);
                        },
                        'mouseover' : function() {
                            this.getElement('a').set('class','menu_button_over');
                        },
                        'mouseleave' : function() {
                            this.getElement('a').set('class','menu_button');
                        }
                    } 
            }).inject(menu);						
            var m_link  = new Element('a', {
                'class'	: 'menu_button',
                'html'  : '&nbsp;'  + item.link + '&nbsp;'
            }).inject(m);
        });
        
    
    };
	
    openMenu = function(id) {
        content.fade('hide');
	    content.empty();
        mList.each(function(item) {
            if (item.id == id) {
                if (item.blog == 0) {
                    var blatt = new Element('div', {
                        'class'	: 'blatt',
                        'styles' : {
                            'background-color' : '#' + item.color
                        }
                    });	
                    blatt.inject(content);
                    
                    // if default image go random
                    if (item.bg == 'default.jpg') {
                        var randNum = Math.floor(Math.random() * randomPics.length);
                        //zitat.bg = randomPics[randNum];
                    }
                    
                    // set background
                    $('wrapper').set('styles', {
                        'background': '#ffffff no-repeat url(img/bg/' + item.bg + '.jpg)'
                    });
                    
                    
                    var blatt_zitat = new Element('div', {
                        'class'	: 'blatt_zitat',
                        'html'  : item.link
                    }).inject(blatt);
                    
                    var blatt_titel = new Element('div', {
                        'class'	: 'blatt_titel',
                        'html'  : item.title
                    }).inject(blatt);
                    
                    var blatt_text = new Element('div', {
                        'class'	: 'blatt_text',
                        'html'  : item.longtext
                    }).inject(blatt);
                    
                    var blatt_symbol = new Element('div', {
                        'class'	: 'blatt_symbol'
                    }).inject(blatt);
                } 
                
                if (item.blog == 1) {
                    // if default image go random
                    if (item.bg == 'default.jpg') {
                        var randNum = Math.floor(Math.random() * randomPics.length);
                        //zitat.bg = randomPics[randNum];
                    }
                    
                    // set background
                    $('wrapper').set('styles', {
                        'background': '#ffffff no-repeat url(img/bg/' + item.bg + '.jpg)'
                    });
                    
                    var blatt = new Element('div', {
                        'class'	: 'blog',
                        'styles' : {
                            'background-color' : 'transparent'
                        }
                    });	
                    blatt.inject(content);
                    openBlog(item.id, blatt, item.link, item.color);
                    
                    
                }
                
                
            };
        });
        content.fade('in');
    };
    
    
	//////////////////////////////////////////////////////////////////////// Blog
	////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////

	
	var openBlog = function(gid, blatt, link, color) {
	   	var request = new Request.JSON({
			url: 'data/blog.php?gid=' + gid,
			onRequest: function() {
				//content.empty();
			},
			onComplete: function(jsonObj) {
    			createBlog(jsonObj.liste, blatt, link, color);
    			
			}
		}).send();
	
	}
	
	var createBlog = function(blist, blatt, link, color) {
	    
	    bList = blist;
	    var pos = bList.length;
	    var textStyle = '';
	    var firstTime = true;
	    
	    bList.each(function(item) {
	    
	        //
            // row container
            //
            ////////////////////////////////////
            
            var blog_block = new Element('div', {
                'class'	: 'blog_container'
            }).inject(blatt);
            
            
            
            
            //
            // first, center, last textStyles
            //
            ////////////////////////////////////
            
            if (firstTime) {
	            textStyle = 'blog_blatt_text_first';
	        } else {
	            if (pos == 1) {
	                textStyle = 'blog_blatt_text_last';
	            } else {
	                textStyle = 'blog_blatt_text_center';
	            }
	        };
	        pos--;
	        
	        
	        //
            // image
            //
            ////////////////////////////////////
            
            if (item.imgshow == 1) {
            
                var blog_img = new Element('div', {
                    'class'	: 'blog_img',
                    'styles': {
                        'height': item.imgheight + 'px'
                    }
                }).inject(blog_block);
                
                var theimg = new Element('img', {
                    'src'   : 'img/uploads/med_' + item.img,
                    'width' : item.imgwidth + 'px',
                    'height': item.imgheight + 'px',
                    'styles': {
                        'display':'block'
                    }
                }).inject(blog_img);
                
            
            } else {
                var blog_img = new Element('div', {
                    'class'	: 'blog_img',
                    'html'  : '&nbsp;'
                }).inject(blog_block);
            
            }
            //
            // text
            //
            ////////////////////////////////////
            
            var blog_blatt = new Element('div', {
                'class'	: 'blog_blatt',
                'styles': {
                    'background' : '#' + color
                }
            }).inject(blog_block);
            
            
            if (firstTime) {
                var blog_titel = new Element('div', {
                    'class'	: 'blog_title',
                    'html'  : link
                }).inject(blog_blatt);
            }
            
            var blog_container = new Element('div', {
                'class' : textStyle,
                'html'  : ' '
            }).inject(blog_blatt);
            
            
            
            var blog_datum = new Element('div', {
                'class'	: 'blog_text_date',
                'html'  : item.date
            }).inject(blog_container);
            
            var blog_title = new Element('div', {
                'class'	: 'blog_text_title',
                'html'  : item.title
            }).inject(blog_container);
            
            var blog_text = new Element('div', {
                'class'	: 'blog_text_text',
                'html'  : item.longtext
            }).inject(blog_container);
            
            
            
            
            
            
            
            
            firstTime = false;
            if (textStyle == 'blog_blatt_text_last') {
                var blatt_symbol = new Element('div', {
                    'class' : 'blatt_symbol'
                }).inject(blog_blatt);
            };
            var contHeight = blog_block.getHeight();
            blog_blatt.setStyle('height',contHeight+'px');
            
        });
            
            
    };
	
	
	
	//////////////////////////////////////////////////////////////////////// Startups
	////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////


	initMenu();
	initZitate();
	
	
	
	
	
	
	
});

