// assumes jquery 1.5 framework, Class.js

var PPGContext = {
	g_hash : "",
	g_mainDiv : "mainDiv",
	scriptFragment : '<script[^>]*>([\\S\\s]*?)<\/script>',
	pageTracker : null,
	// this will be overwritten by evalScripts to get current script context after ajax call
	current : {
	
	},
	evalScripts : function(data){
		var matchAll = new RegExp(PPGContext.scriptFragment, 'img'), matchOne = new RegExp(PPGContext.scriptFragment, 'im');
		var matches = data.match(matchAll);
		if(matches){
			//PPGContext.debug(typeof(matches) + '-' + matches.length);
			//if(typeof(matches) == "Array"){
				for(var i = 0; i < matches.length; i++){
					//PPGContext.debug("Evaluating " + i);
					var actualMatch = matches[i].match(matchOne);
					//PPGContext.debug('evaluating : ' + actualMatch[1]);
					eval(actualMatch[1]);
				}
			/*} else {
				PPGContext.debug("Evaluating " + matches[0] + " :: " + matches[0].match(matchOne)[1]);
				//alert(typeof(matches) + ':' + matches);
				eval(matches[0].match(matchOne)[1]);
			}*/
		}
	},
	ajaxLoad : function(cont, url){
		PPGContext.debug('ajaxLoad ' + cont + ' ; ' + url);
		var hash = '!';
		if(cont != PPGContext.g_mainDiv){
			hash += cont + ',';
		}
		hash += escape(url);
		top.location.hash = hash;
		PPGContext.g_hash = hash;
		
		jQuery.ajax({
		  url: url,
		  success: function(data) {
			  
			
			jQuery('#' + cont).html(data.replace(new RegExp(PPGContext.scriptFragment, 'img'), ''));
			PPGContext.evalScripts(data);
			// after we are done...
			if(PPGContext.pageTracker){
				if(url.substr(0, 1) != '/'){
					url = '/' + url;
				}
				PPGContext.pageTracker._trackPageview(url);
			}
		  }
		});	
	},
	
	ajaxUpdater : function(cont, url) {
	
		
	},
	
	ajaxGetter : function(url) {
		
	},
	
	ajaxPoster : function(url, vars){
	
	
	},

	getHash : function(){
		var i, href;
		href = top.location.href;
		i = href.indexOf("#");
		return i >= 0 ? href.substr(i + 1) : "";
	},
	
	checkHash : function(){
		var hash = PPGContext.getHash();
		if(PPGContext.g_hash != hash){
			var cont = PPGContext.g_mainDiv;
			
			if(hash.indexOf('!') == 0)
				hash = hash.substr(1);
			
			var url = hash;
			
			if(hash.indexOf(',') > 0){
				cont = hash.split(',')[0];
				url = hash.split(',')[1];
			} 
			PPGContext.ajaxLoad(cont, url);
		}
		//
	},
	
	debug : function(s){
		if(typeof(console) !== 'undefined'){
			console.log(s);
		}
	},
	
	resetBackgrounds : function(){
		Backgrounds.timeToNextTransition = 4000;
		Backgrounds.images = PPGContext.arrBackgrounds;
	},
	
	setMainMenu : function(link){
		
		PPGContext.resetBackgrounds();
		
		$('#header').show();
		$('#mainMenu .main li').removeClass('selected');
		$.each($('#mainMenu .main li a'), function(e, elt){
			//console.log($(elt).html());
			if($(elt).attr('href') == '#!' + link){
				$(elt).parent().addClass('selected');
			}
		});
	},
	
	resetSubMenu : function(){
		var $subMenu = $('#mainMenu .menuContainer .sub');
		$subMenu.children().remove();
	},
	
	addSubmenu : function(link, title, selected){
		var $subMenu = $('#mainMenu .menuContainer .sub');
		$subMenu.append("<li" + (selected ? " class='selected'" : "") + "><a href='#!" + link + "'>" + title + "</a></li>");
	},
	
	checkEmail : function(s)
	{
		if(s.length == 0)
			return true;
		if(s.indexOf('@') != s.lastIndexOf('@'))
			return false;
		var regex = /[A-Za-z0-9_-]+([.]{1}[A-Za-z0-9_-]+)*@[A-Za-z0-9-]+([.]{1}[A-Za-z0-9-]+)+/;
		PPGContext.debug(s + '::' + regex.test(s));
		return regex.test(s);
	}

};

$(window).load(function () {
	PPGContext.debug('== START. window:loaded');
	setInterval(PPGContext.checkHash, 200);

	if(PPGContext.getHash() == ''){
		PPGContext.ajaxLoad("mainDiv", "home/");
	}
	
	// $("#mainMenu .menuContainer .main a[href='#!projects/{{ discipline.slug }}']").parent().addClass('selected');
/*	
	$('#mainMenu .menuContainer .main li').click(function(e){
		var target = $(this);
		target = target.closest('li');
		PPGContext.ajaxLoad(PPGContext.g_mainDiv, target.attr('href'));
	});
*/
});


