/*
      ##### #     ##                                       ##           /##   	
   ######  /#    #### /                  #              /####         #/ ###  	
  /#   /  / ##    ###/                  ###     #      /  ###        ##   ### 	
 /    /  /  ##    # #                    #     ##         /##        ##       	
     /  /    ##   #                            ##        /  ##       ##       	
    ## ##    ##   #    /###     /###   ###   ########    /  ##       ######   	
    ## ##     ##  #   / ###  / / ###  / ### ########    /    ##      #####    	
    ## ##     ##  #  /   ###/ /   ###/   ##    ##       /    ##      ##       	
    ## ##      ## # ##    ## ##    ##    ##    ##      /      ##     ##       	
    ## ##      ## # ##    ## ##    ##    ##    ##      /########     ##       	
    #  ##       ### ##    ## ##    ##    ##    ##     /        ##    ##       	
       /        ### ##    ## ##    ##    ##    ##     #        ##    ##       	
   /##/          ## ##    ## ##    ##    ##    ##    /####      ##   ##       	
  /  #####           ######   ######     ### / ##   /   ####    ## / ##       	
 /     ##             ####     ####       ##/   ## /     ##      #/   ##.nl    	
 #                                                 #                          	
  ##                                                ##                        	
*/


window.addEvent('domready', function() {
	
	var menus = $('wrapper').getElements('div[id^=menu]');

	var allSubsHide = function() {
        menus.each(function(item, index){
            var submenu = $('submenu' + (index + 1));
            submenu.fade('hide');
            item.fade('show');
        });
    };


	
    menus.each(function(item, index){
        var submenu = $('submenu' + (index + 1));
        item.addEvents({
            'mouseover' : function(e) {
                allSubsHide();
                submenu.fade('show');
                item.fade('hide');
            },
            'mouseout'  : function(e) {
                
            }
        });
        submenu.addEvents({
            'mouseover' : function(e){
                submenu.fade('show');
                item.fade('hide');
            },
            'mouseout'  : function(e){
                submenu.fade('hide');
                item.fade('show');
            }
        });
        var submenuItems = submenu.getElements('div[class^=submenu_button]');
        submenuItems.each(function(button, bindex){
            if (bindex != 0) {
                button.addEvents({
                    'mouseover' : function(e){
                        submenu.fade('show');
                        item.fade('hide');
                        this.setStyle('color','#865F7F');
                    },
                    'mouseout'  : function(e){
                        this.setStyle('color','#BB484A');
                        e.stop();
                    },
                    'click'     : function(e){
                    	if (button.get('cid') == '1') {
	                    	if (button.get('lang') != lang) {
								window.location = "http://verloskundigenvida.nl/?l=" + button.get('lang');
	                    	} else {
		                        openContent('con.php?id=' + button.get('cid') + '&lang=' + lang);
	                    	}
                    	} else {
                    		if (button.get('cid') == 'form') {
		                        openContent('form.php?lang=' + lang);
                    		} else if (button.get('cid') == 'date') {
		                        openContent('date.php?lang=' + lang);
                    		} else {
		                        openContent('con.php?id=' + button.get('cid') + '&lang=' + lang);
                    		}
	                    }
	                    pageTracker._trackPageview(button.get('html'));
	                    submenu.fade('hide');
                        item.fade('show');
                    }
                });
            } else {
                button.setStyle('color','#865F7F');
                button.addEvents({
                    'mouseover' : function(e){
                        submenu.fade('show');
                        item.fade('hide');
                    }
                });
            }
        });
        
    });
    
    
    openContent = function(link) {
        req.get(link);
    };
    
    var fixWrapperHeight = function() {
        if ($('cont').getStyle('height').toInt() > 450) {
            var targetHeight = $('cont').getStyle('height').toInt() +220;
            $('wrapper').setStyle('height',targetHeight + 'px');
            $('wrapper').setStyle('margin-bottom',250 + 'px');
            $('longImageFill').setStyle('height',targetHeight + 'px');
        } else {
            $('wrapper').setStyle('height','600px');
            $('wrapper').setStyle('margin-bottom',50 + 'px');
            $('longImageFill').setStyle('height',1 + 'px');
        }
    }
    
	var obj = new Swiff('quote.swf', {
			width: 170,
			height: 400,    
			params: {
				bgcolor: '#ffffff'
			},
			container: $('quote')
    });
	showQuote = function(quote){
        Swiff.remote(obj.toElement(),'echoText',quote);
    }
    
    
    if (Browser.Plugins.Flash.version == '0') {
		$('quote').empty();	    
		showQuote = function(quote){
			$('quote').set('html','<i>'+quote+'</i>');
			$('quote').setStyle('font-family','Times');
			$('quote').setStyle('line-height','20px');
			$('quote').setStyle('font-size','16px');
			
		}
    }


    switchLongImage = function(link){
        var old_img = $('longImage').getStyle('background-image');
        var new_img = "url(" + link + ")";
        if (new_img != old_img) {
       		$('longImage').setStyle('background-image','url(' + link + ')');
	    }
        
    }
    
    req = new Request.HTML({
            update:$('cont'),
            evalScripts:true,
            evalResponse:true,
            noCache:true,
            onRequest: function() {
                $('cont').set('html','<div id="ajax_load"></div>');
            },
            onComplete: function() {
                fixWrapperHeight();
                $('cont').fade('in');
            }
    });

	twitterUpdate = function(content) {
		$('twitter').set('html',$('twitter').get('html') + content);
	};
	
	twitterRequest = function() {
		treq = new Request.JSON({
			url: 'js/twitter.php',
			onComplete: function(responseJSON, responseText) {
				responseJSON.each(function(item,index){
					//alert('test:request send');
					if (index > 2) return;
					
					// date
					var today = new Date();
					var dateCreated = new Date();
					dateCreated.parse(item['created_at']);
					var difference = today.diff(dateCreated);
					var dateString = "";
					if (difference) {
						dateString = -today.diff(dateCreated) + ' days ago:';
					} else {
						dateString = 'Today:';
					}
					
					// text
					var textString = item['text'];
					textString = textString.replace(/(http?:\/\/[\w\-:;?&=+.%#\/]+)/gi, '<a href="$1">$1</a>');
	
					// output
					var output = '<b style=font-size:9px>' + dateString + '</b><br/>' + textString + '<br/>';
					twitterUpdate(output);
				});
				//$('twitter').inject($('cont'),'bottom');
				
			}
		}).get();
	};


    (function(){openContent('con.php?id=1&lang='+lang);}).delay(1000);
//    (function(){twitterRequest();}).delay(1100);
           
	
	
	
	
});

