/*
	Class:	nansen

	A framework with the most common functions used in our projects
*/
var nansen = nansen || {};

nansen = {
	/*
		Variable: version

		A variable stating which version of the framework we're using.
	*/
	version : "0.2",
	/*	Function: addLoadEvent();
	
		Description: Adds functions to a que which gets fired when the browser has finished loading the page.
		
		Dependencies: None
	*/
	addLoadEvent : function(func){
		var oldonload = window.onload;
		if(typeof window.onload != 'function'){
			window.onload = func;
		}else{
			window.onload = function(){
				if (oldonload){
					oldonload();
				}
				func();
			}
		}
	},
	
	/*	Function: externGoogleAnalyticsLink();
	
		Description: Loops through all a-tags with the class "nf_externLink" set, catching the onclick event and fires an urchinTracker(<title|href>);
		
		Dependencies: MooTools, Google Analytics
	*/
	externGoogleAnalyticsLink : function(event){
		if(pageTracker._trackPageview){
			$$('a.nf_externLink').each(function(obj, event){
				obj.addEvent('click', function(event){
					event = new Event(event).stop();
					var strUrchin = (obj.title)?obj.title:obj.href;
					strUrchin = nansen.changeSpecialChars(strUrchin);
					pageTracker._trackPageview(strUrchin);
					location.href = obj.href;
				});
			});
		}
	},

	/*	Function: changeSpecialChars();
	
		Description: Switches and strips out common special chars to clean usable characters. 
		
		Dependencies: None
	*/
	changeSpecialChars : function(strText){
		strText = strText.replace(/\&/gi, "och");
		strText = strText.replace(/[åä]/gi, "a");
		strText = strText.replace(/ö/gi, "o");
		strText = strText.replace(/[^a-z A-Z0-9.]/g, "");
		strText = strText.replace(/ /g, "_");

		return strText;
	},
	/*	Function: imageHover();
	
		Description: 	Makes all images with class "hoverImage" hoverable with an alternate image file ending with "Hover".
						Also skips hover effect on pictures ending with "Active" for navigations etc.
		
		Dependencies: MooTools
	*/
	imageHover : function(event){
		$$('.hoverImage').each(function(obj){
			var image = obj;
					if(!image.src.contains('Active')){
				image.addEvent('mouseover', function(){
					var strExt = image.src.match(".jpg|.gif|.png");
					imageSrc = image.src.split(strExt);
					image.src = imageSrc[0]+'Hover'+strExt;
				});
			}
			if(!image.src.contains('Active')){
				image.addEvent('mouseout', function(){
					imageSrc = image.src.split('Hover.');
					image.src = imageSrc[0]+'.'+imageSrc[1];
				});
			}
		});
	},
	/*	Function: searchBoxDefault();
	
		Description: 	Checks the default text in inputs with class nf_inputDefault, on focus clears it, on blur returns the default if no new text
		                has been entered.
		
		Dependencies: MooTools
	*/
    searchBoxDefault : function(){
        sBox = [];
        strDefault = [];
        i = 0;
        $$('input.nf_inputDefault').each(function(obj, i){
            sBox[i] = obj;
            strDefault[i] = sBox[i].value;
            sBox[i].addEvent('focus', function(){
                if(this.value == strDefault[i]){
                    this.value = '';
                }
            });
            sBox[i].addEvent('blur', function(){
                if(!this.value.match(/\w/)){
                    this.value = strDefault[i];
                }
            });
        });
    }
}