///////////////////////////////////////////////////////////////////////////////////
// GENERIEKE FUNCTIES
///////////////////////////////////////////////////////////////////////////////////

String.prototype.getWords = function() {
	var string = this.replace(/\s+/g, ' ');
	var wordsArray = string.split(' ');
	return wordsArray;
}

String.prototype.countWords = function() {
	var string = this.replace(/\s+/g, ' ');
	var wordsArray = string.split(' ');
	return wordsArray.length;
}

var Cookie = {
    set: function(name, value) {
        document.cookie = name + "=" +escape(value)+"; path=/";
    },
    get: function(name) {
        var start = document.cookie.indexOf(name+"=");
        var len = start + name.length + 1;
        
        if ((!start) && (name != document.cookie.substring(0, name.length))) {
            return null;
        }
        
        if (start == -1) return null;
        
        var end = document.cookie.indexOf(";", len);
        
        if (end == -1) end = document.cookie.length;
        
        return unescape(document.cookie.substring(len, end));
    }
};
	
Object.extend(Event, {
    _domReady : function() {  
     if (arguments.callee.done) return;  
     arguments.callee.done = true;  
   
     if (this._timer)  clearInterval(this._timer);  
       
     this._readyCallbacks.each(function(f) { f() });  
     this._readyCallbacks = null;  
 },  
   onDOMReady : function(f) {  
     if (!this._readyCallbacks) {  
       var domReady = this._domReady.bind(this);  
         
       if (document.addEventListener)  
         document.addEventListener("DOMContentLoaded", domReady, false);  
           
        // for Internet Explorer (using conditional comments)
        /*@cc_on @*/
        /*@if (@_win32)
        document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
        var script = document.getElementById("__ie_onload");
        script.onreadystatechange = function() {
            if (this.readyState == "complete") {
                domReady(); // call the onload handler
            }
        };
        /*@end @*/

         if (/WebKit/i.test(navigator.userAgent)) {   
           this._timer = setInterval(function() {  
             if (/loaded|complete/.test(document.readyState)) domReady();   
           }, 10);  
         }  
           
         Event.observe(window, 'load', domReady);  
         Event._readyCallbacks =  [];  
     }  
     Event._readyCallbacks.push(f);  
   }  
}); 

///////////////////////////////////////////////////////////////////////////////////
// SPECIFIEKE FUNCTIES
///////////////////////////////////////////////////////////////////////////////////

/**
 * Titel over meerdere regels verspreiden
 */
function formatTitle(element, numberOfLines) {
	var t = $(element);
	if (t) {
		var wordsArray = t.innerHTML.getWords();
		var wc = t.innerHTML.countWords();
		var d = t.getDimensions();
		
		// woorden over x regels verspreiden
		var newHeader = document.createElement(t.tagName.toLowerCase());		
		newHeader.className = t.className.toString();
		
		var maxWordsPerLine = Math.ceil(wc / numberOfLines);

		var currentLine = 1;
		var wordIteration = 0;
				
		for (var i = 0; i < wc; i++) {
			
			if (wordIteration >= maxWordsPerLine || (i == wc-1 &&  currentLine == numberOfLines-1) ) {
				
				newHeader.innerHTML += '<br />';
				currentLine++;
				wordIteration = 0;
			}
			newHeader.innerHTML += ' '+wordsArray[i];
			wordIteration++;	
			
		}
		t.parentNode.insertBefore(newHeader, t);		
		Element.remove(t);
	}	
}

/**
 * Startpagina -> hoofdstelling -> prev / next functie
 */
function attachHoofdStellingSwitcher() {
	var h = $('hoofdStelling');
	if (h) { 
        Event.observe(h, 'mouseover', function(event) {
          h.addClassName('redBigBox');
          h.removeClassName('redBigBoxNoBG')
        }); 

        Event.observe(h, 'mouseout', function(event) {
            h.addClassName('redBigBoxNoBG');
            h.removeClassName('redBigBox'); 
        }); 

		var prev = $('prev');
		var next = $('next');
		Event.observe(prev, 'click', function(event) { 
			Event.stop(event);
			new Ajax.Request(prev.href, { 
				onLoading: function() { },
				onComplete: function(t) { 
					h.innerHTML = t.responseText;
					formatTitle('titel', 3);
					attachHoofdStellingSwitcher();
				}
			});
		});	
		
		Event.observe(next, 'click', function(event) { 
			Event.stop(event);
			new Ajax.Request(next.href, { 
				onLoading: function() { },
				onComplete: function(t) { 
					h.innerHTML = t.responseText;
					formatTitle('titel', 3);
					attachHoofdStellingSwitcher();
				}
			});
		});	
	}
}

///////////////////////////////////////////////////////////////////////////////////
// EVENT HANDLERS
///////////////////////////////////////////////////////////////////////////////////

Event.onDOMReady(function() { 

	//Position.center('wrapper');
	
	formatTitle('titel', 3);
	
	attachHoofdStellingSwitcher()
	
	var overzichtTitels = $A(document.getElementsByClassName('debatOverzichtTitel'));
	
	if (overzichtTitels && overzichtTitels.length > 0) {
		overzichtTitels.each(function(titel) { 
			formatTitle(titel, 3);
		} );
	}
	
	initMouseOver();
	
	
});


function initMouseOver(){
	$$('#rightColumn .redBox', '#rightColumn .redNoBGBox').each(function(el){
		if (!el.hasClassName('quote')) {
			el.observe('mouseover', function(e){
				el.addClassName('redBox');
				el.removeClassName('redNoBGBox');
			});
			el.observe('mouseout', function(e){
				el.addClassName('redNoBGBox');
				el.removeClassName('redBox');
			});
		}
	});
	
	$$('#rightColumn .blueBox', '#rightColumn .blueNoBGBox').each(function(el){
		el.observe('mouseover', function(e){
			el.addClassName('blueBox');
			el.removeClassName('blueNoBGBox');
		});
		el.observe('mouseout', function(e){
			el.addClassName('blueNoBGBox');
			el.removeClassName('blueBox');
		});
	});
}
