(function(){
	var EYE = window.EYE = function() {
		var _registered = {
			init: []
		};
		return {
			init: function() {
				$.each(_registered.init, function(nr, fn){
					fn.call();
				});
			},
			extend: function(prop, prop2) {
				for (var i in prop) {
					if (prop[i] != undefined) {
						if (prop2) {
							prop2[i] = prop[i];
						} else {
							this[i] = prop[i];
						}
					}
				}
				if(prop2) {
					return prop2;
				}
			},
			register: function(fn, type) {
				if (!_registered[type]) {
					_registered[type] = [];
				}
				_registered[type].push(fn);
			},
			getFlashReference: function(obj) {
				return window[obj]||document[obj];
			},
			template: function(tpl, fn) {
				return tpl.replace(/\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g, fn);
			}
		};
	}();
	$(document).ready(EYE.init);
	
	
	EYE.content = function(){
		var currentFragment,
			matchFragment = /([^\/\.]+)\.ht/g,
			matchAnchor = /#(.+)$/g,
			replaceFragment = /\.ht/g,
			cachedContent = {},
			loading = false,
			flashAvailable = false,
			flashReady = false,
			flashTimer = null,
			modalVisible = false;
			
		var parseLinks = function(context) {
			var hrf, fragment;
			$('a', context)
				.not('#modalClose, #download, #SOMLink, #CSCcomLink, #CSCnlLink')
				.each(function(){
					if (!this.historyMarked && $(this).attr('target') != '_blank') {
						hrf = $(this).attr('href');
						fragment = $(this).attr('href').match(matchFragment);
						if (fragment) {
							fragment =  fragment[0].replace(replaceFragment, '');
							$(this).attr('href', '#' + fragment);
						}
						this.historyMarked = true;
						$(this).history(history);
					}
				});
		};
		var history = function(el) {
			var fragment =  el.hash.replace(/#/g, '');
			parseFragment(fragment, false);
		};
		var parseFragment = function(fragment, fromFlash, landingPage) {
			if (!EYE.CSC.pages.top[fragment] && !EYE.CSC.pages.bottom[fragment]) {
				if (fragment == 'login') {
					//window.location.href = EYE.CSC.myURI;
					window.open(EYE.CSC.myURI);
					return;
				} else {
					fragment = 'intro';
				}
			}
			if (modalVisible == true){
				closeModal();
			}
			if (fragment == 'headhunter-worden') {
				document.getElementById('tracking').src = "/track/register.html";
			} else if (fragment == 'home' || fragment == 'intro') {
				document.getElementById('tracking').src = "/track/home.html";
			}
			var firstFragment = landingPage || currentFragment == null;
			currentFragment = fragment;
			if (EYE.CSC.pages.top[fragment]) {
				loadContent(fragment, false);
				if (fromFlash != true) {
					navigateToFlash();
					window.scrollTo(0,0);
				}
			} else {
				if (firstFragment) {
					currentFragment = 'home';
					navigateToFlash('home');
					loadContent('home');
				}
				loadContent(fragment, true);
			}
		};
		var navigateToFlash = function() {
			if (flashReady == true) {
				EYE.getFlashReference('CSCFlash').setReferenceCode(EYE.CSC.referenceCode);
				EYE.getFlashReference('CSCFlash').navigateTo(currentFragment);
				if(flashTimer != null) {
					clearTimeout(flashTimer);
					flashTimer = null;
				}
			} else if(flashTimer != null) {
			} else {
				flashTimer = setInterval(navigateToFlash, 100);
			}
		};
		var getFlashAvailability = function() {
			var flashObj = EYE.getFlashReference('CSCFlash');
			if (flashObj && typeof flashObj.navigateTo == 'function') {
				flashAvailable = true;
				
			} else {
				setTimeout(getFlashAvailability, 100);
			}
		};
		var loadContent = function(fragment, modal) {
			if (cachedContent[fragment]) {
				displayContent(fragment, modal);
			} else {
				loading = true;
				$.ajax({
					complete: function() {
						loading = false;
					},
					data: $.param({
						request: 'getPage',
						fragment: fragment
					}),
					url: '/ajaxserver.php',
					dataType: 'xml',
					type: 'post',
					success: function(responseXML) {
						cachedContent[fragment] = {
							title: $('title', responseXML).text(),
							content: $('content', responseXML).text()
						};
						displayContent(fragment, modal);
					}
				});
			}
		};
		var displayContent = function(fragment, modal) {
			$('#deco').fadeIn(500);
			$('#download').fadeOut(500);
			if (modal) {
				showOverlay();
				$('#modalContent')
					.find('div:first')
						.html(cachedContent[fragment].content)
						.end()
					.find('h3')
						.html(cachedContent[fragment].title);
				var scrll = EYE.getScroll();
				modalVisible = true;
				showOverlay();
				$('#modal')
					//.css('top', scrll.t + 20 + 'px')
					.show();
			} else {
				if (EYE.CSC.pages.top[fragment] && EYE.CSC.pages.top[fragment].published == 1) {
					$('#title').css('backgroundImage', 'url(/images/' + fragment + '.gif)');
					if (fragment =='pers') {
						$('#deco').fadeOut(500);
						$('#download').fadeIn(500);
					}
				} else {
					$('#title').css('backgroundImage', 'url(/images/general_title.gif)');
				}
				parseLinks($('#content').html(cachedContent[fragment].content).get(0));
				document.title = cachedContent[fragment].title;
			}
			if (currentFragment == 'home') {
				$('#logos').fadeIn(500);
			} else {
				$('#logos').fadeOut(500);
			}
		};
		var showOverlay = function() {
			var scrll = EYE.getScroll();
			var clnt = EYE.getClient();
			$('#overlay')
				.css({
					width: Math.max(scrll.w, clnt.w) - 20 + 'px',
					height: Math.max(scrll.h, clnt.h) + 'px'
				})
				.show();
		};
		var hideOverlay = function() {
			$('#overlay').hide();
		};
		var closeModal = function() {
			modalVisible = false;
			hideOverlay();
			$('#modal').hide();
			return false;
		};
		/*var moveModal = function() {
			var scrll = EYE.getScroll();
			$('#modal').css('top', scrll.t + 20 + 'px');
		};*/
		return {
			init: function() {
				$('#overlay').css('opacity', 0.7);
				$('#modalClose').bind('click', closeModal);
				//getFlashAvailability();
				parseLinks(document.body);
				$.ajaxHistory.initialize();
				if (!window.location.hash) {
					var currentLocation = window.location.href;
					var fragment = currentLocation.match(matchFragment);
					if (fragment) {
						fragment =  fragment[0].replace(replaceFragment, '');
						$.ajaxHistory.update('#' + fragment);
						parseFragment(fragment, false, true);
					} else {
						$.ajaxHistory.update('#intro');
						parseFragment('intro', false);
					}
				}
				//$.historyInit(history);
				/*currentSection = 'intro';
				currentLocation = window.location.href;
				fragment = currentLocation.match(matchFragment);
				if (fragment) {
					currentSection =  fragment[0].replace(replaceFragment, '');
					$.historyLoad(currentSection);
				}*/
				//$(window).bind('scroll', moveModal);
			},
			navigateTo: function(fragment) {
				$.ajaxHistory.update('#' + fragment);
				parseFragment(fragment, true);
			},
			swfIsReady: function() {
				flashReady = true;
			}
		};
	}();
	//EYE.register(EYE.content.init, 'init');
	$(window).bind('load', EYE.content.init);
})();