//Pushing the onLoad and onResize functions
var oInit = {
		oBroadcast 	: {}, // broadcaster
		aResizers 	: [], // resize statements
		aLoaders 	: [], // onload statements
		
		// initialization
		mPush		: function () { arguments [ 0 ][ arguments [ 0 ].length ] = arguments [ 1 ]},
		mOnload 	: function () { var i = 0; while ( i < arguments.length ) oInit.mPush ( oInit.aLoaders,  arguments [ i++ ])},
		mOnresize 	: function () { var i = 0; while ( i < arguments.length ) oInit.mPush ( oInit.aResizers, arguments [ i++ ])},
		mExecute 	: function () { var i = 0; while ( i < arguments [ 0 ].length ) arguments[ 0 ][ i++ ] (); },
		mResize 	: function () { oInit.mExecute ( oInit.aResizers ); },
		mLoad 		: function () { oInit.mExecute ( oInit.aLoaders ); oInit.mBroadcast ( "onload statements evaluated" );},
		
		// central broadcasting service
		mSubscribe : function ( sBroadcast, oFunction ) {
			if ( !oInit.oBroadcast [ sBroadcast ]) oInit.oBroadcast [ sBroadcast ] = [ oFunction ];
			else oInit.mPush ( oInit.oBroadcast [ sBroadcast ], oFunction );
		},
		mBroadcast : function ( sBroadcast ) {
			if ( oInit.oBroadcast [ sBroadcast ]) oInit.mExecute ( oInit.oBroadcast [ sBroadcast ] );
		}
}
//Client sniffer
var oClient = {

		mEval : function ( bStatement ) { return bStatement ? true : false; },
		mPass : function () {
			oClient.bMozilla	= oClient.mEval ( document.evaluate );
			oClient.bOpera		= oClient.mEval ( window.opera && document.addEventListener );
			oClient.bIE			= oClient.mEval ( document.all && document.createElement && !oClient.bOpera );
			oClient.bIEwin 		= oClient.mEval ( oClient.bIE && navigator.platform.indexOf ( "Win" ) !=-1 );
			oClient.bIEwinOld	= oClient.mEval ( oClient.bIEwin && !window.XMLHttpRequest); // IE6 & older
			oClient.bIE7plus	= oClient.mEval ( oClient.bIEwin && window.XMLHttpRequest); // IE7+
			oClient.bIEmac 		= oClient.mEval ( oClient.bIE && !oClient.bIEwin );
			oClient.bSafari 	= oClient.mEval ( navigator.appVersion.toLowerCase ().indexOf ( "safari" ) !=-1 );
			oClient.bPass		= oClient.bMozilla || oClient.bOpera || oClient.bIE || oClient.bSafari;
			return oClient.bPass;
		}
}
var oLayout = {
	mInit : function () {		
		if( ( oClient.bIE || oClient.bIEwin ) && oClient.bIEwinOld ) {
			var mContent = document.getElementById('content');
			if(mContent.offsetHeight<320)
				mContent.style.height = '320px';
		}
		oInit.mSubscribe ( "onload statements evaluated", oLayout.mShow );
	},
	mHide : function () {
		oLayout.mDeclare ( "#application { display:none; }" );
	},
	mShow : function () {
		if ( document.getElementById('application') ) document.getElementById('application').style.display = "block";
		oInit.mBroadcast ( "core functions evaluated" );
	},
	mDeclare : function ( sClasses ) {
		if ( oClient.bMozilla ) {
		
			if(document.styleSheets.length > 0) { // avoid if length is zero or less, it would have caused error.
			
				var oSheet = document.styleSheets [ document.styleSheets.length - 1 ];
				
				if ( oSheet ) {
					oSheet.insertRule ( sClasses, oSheet.cssRules.length );
				}
			}
		}
		else {
			document.write ( "<style type=\"text/css\">" + sClasses + "</style>" );
		}
	}
}
var oLinks = {
	mInit : function () {
		var oLink, i = 0, sHref = document.location.href.toString ();
			while (( oLink = document.links [ i++ ]) != null ) {
				if(oLink.className=='pdf' || oLink.className=='external')
					oLink.target = "_blank";
			}
	}

}
var oValidateForm = {
	mInit : function () {
		if ( document.forms.length > 0 ) {
			var oForm, a = 0;
			while (( oForm = document.forms [ a++ ]) != null ) {
				if ( oForm.className == "form" ) {
					oForm.mValidate	= oValidateForm.mBegin;
					oForm.onsubmit 	= oForm.mValidate;
				}
			}
		}
	},
	mBegin : function () {
		var x = document.forms[0].elements;
		var submit = true;
		for (var i=0;i<x.length;i++) {
			if (x[i].getAttribute('required') && !x[i].value || x[i].className == "warning") {
				x[i].value = "Feltet skal udfyldes";
				x[i].className = "warning";
				x[i].onclick = oValidateForm.mRemove;
				submit = false;
			}
		}
		if(submit) return true;
		else return false;
	},
	mRemove : function ( ) {
		if( this.className == "warning" ){
			this.className="";
			this.value="";
		}
	}
}

var geocoder = null; // global scope
var maps = null; // map object
var oGoogleMap = {
	mInit : function() {
		if( document.getElementById("map") ){
			var sMap = {"maps":[{"mapid":"map","title":"Interflux® ApS","lng":"9.02406","lat":"55.50175","address1":"Stenbro Alle 8","city":"6650 Brørup","zoom":"14","type":"normal","control":"small","typecontrol":"0","showbubble":"1","bubbletpl":"<h4>Interflux® ApS</h4><p>Stenbro Alle 8<br />6650 Brørup<br /><br /><br /></p>"},]};
			maps = sMap.maps;
	    	geocoder = new GClientGeocoder();
	    	oGoogleMap.mLoadMap(0);
    	}
	},
	mLoadMap : function(i) {
		maps[i].mapCanvas = new GMap2(document.getElementById(maps[i].mapid)); // local scope
		maps[i].found = 0; // default
		if(window.maps[i].lng !== undefined && window.maps[i].lat !== undefined) {
			point = new GLatLng(window.maps[i].lat, window.maps[i].lng);
			oGoogleMap.mDoPoint(point,i)
		} else {
			//geocode address
			oGoogleMap.mShowAddress(i);
		}
	},
	mDoPoint : function( point,i ) {
		if (!point) { //not found
			maps[i].found++;
			if (maps[i].found>0 && maps[i].zip !== undefined && maps[i].city !== undefined) { 
				if (maps[i].found==1) { // first fail
					if (maps[i].address2 !== undefined) { // try second address line
						maps[i].address = maps[i].address2+", "+maps[i].city+", "+maps[i].zip;
					} else {
						maps[i].address = maps[i].zip; // try zip only
						maps[i].found++; // ensure next fail is final
					}
					oGoogleMap.showAddress(i);		
				} else if(maps[i].found==2) { // second fail, try zip only
					maps[i].address = maps[i].zip;
					oGoogleMap.showAddress(i);
				} else { // fail
					alert(maps[i].address + " not found");
				}
			} else { // fail
				alert(maps[i].address + " not found");
			}	
		} else {
			// centre and zoom
			maps[i].mapCanvas.setCenter(point, parseInt(maps[i].zoom));
			// add zoom control
			if (maps[i].control == "large") {
				maps[i].mapCanvas.addControl(new GLargeMapControl());
			} else {
				maps[i].mapCanvas.addControl(new GSmallMapControl());
			}
			// add type control
			if (maps[i].typecontrol == "1") {
				maps[i].mapCanvas.addControl(new GMapTypeControl());
			}
			// set map type
			var type = G_NORMAL_MAP;
			if (maps[i].type=="satellite") type = G_SATELLITE_MAP;
			if (maps[i].type=="hybrid") type = G_HYBRID_MAP;
			maps[i].mapCanvas.setMapType(type);
			// create marker and bubble
			var marker = new GMarker(point);
			maps[i].mapCanvas.addOverlay(marker);
			var myMarkerContent = maps[i].bubbletpl;
			// show bubble on load?
			if (maps[i].showbubble == 1) {
				marker.openInfoWindowHtml(myMarkerContent);
			}
			// add onclick event handler for bubble
			GEvent.addListener(marker, "click", function() {
				marker.openInfoWindowHtml(myMarkerContent);
			});
		}
	},

	mShowAddress : function( i ) {
		if (geocoder) {	
			//format address
			if (maps[i].address === undefined) {
				if (maps[i].address1 !== undefined && maps[i].city !== undefined && maps[i].zip !== undefined) {
					maps[i].address = maps[i].address1 + ", " + maps[i].city + ", " + maps[i].zip;
				} else if(maps[i].address1 !== undefined && maps[i].zip) {
					maps[i].address = maps[i].address1 + ", " + maps[i].zip;
				} else {
					maps[i].address=null;
				}
			}
			if (maps[i].address !=null) {
				geocoder.getLatLng(
					maps[i].address, function(point) { 
						oGoogleMap.mDoPoint(point, i); 
					}
        		);
        	} else {
        		alert("Error: one or more of the following parameters are missing: address1, city or zip");
        	}
		}
	}
}

if ( oClient.mPass ()){
	oLayout.mHide();
	// listen
	window.onload = oInit.mLoad;
	window.onresize = oInit.mResize;
	
	// onload statements
	oInit.mOnload ( 
		oLayout.mInit,
		oLinks.mInit,
		oGoogleMap.mInit
		//oValidateForm.mInit
	);
	// resize statements
	oInit.mOnresize ( );
}