/**
 * @author Kasper
 */

function my_callback(data){
    if(data!=Dajaxice.EXCEPTION){
        alert(data.message);
    }
    else{
        alert('Error');
    }
}

 
// ensure that the debug console exists
if (window['loadFirebugConsole']) window.loadFirebugConsole(); 
else if (!window['console']) window.console = { info:function(s){}, log:function(s){}, warn:function(s){}, error:function(s){} }

function CoreClass(){
  	this.me = "core";
	this.rootURL = "";
	this.themeURL = "";
	this.currentLang = "NL";
}

CoreClass.prototype = {
  	init:function(){
		// initialize
		var _this = this;
		var language;
		$.ajax({ 
		    url: "http://ajaxhttpheaders.appspot.com", 
		    dataType: 'jsonp', 
		    success: function(headers) {
		        language = headers['Accept-Language'];
				// TODO IMPLEMENT PROPERLY!
				if(language.split(",")[0] == "nl"){
					_this.currentLang = "NL";	
				} else {
					_this.currentLang = "EN";
				}
		    }
		});
				
		this.getTranslations();
		
		
		
		//$.cookie('language', this.currentLang);
		
  	},
	initClass:function(className) {
		if (typeof fff[className].init == "function") {
			this[className].init();
		}
	},
	
	getTranslations:function(){
		// get translation xml
		var page = $("body").attr("id");		
		var _this = this;
		
		$.get('/public/translations/all.xml', function(data) {
  			_this.translate(data)
		});
		
		
	},
	translate:function(xml){
		
		
		
		var _this = this;
		
		$(".translate").each(function(){
			var classes = $(this).attr("class").split(" ");
			var stringId = "";
			var c = "";
			
			for(var i in classes){
				if(classes[i].indexOf("ts-") > -1){
					stringId = classes[i].replace("ts-", "");	
				}
			}
			
			var translations = $(xml).find("translation[id=" + stringId + "]");
			var node = $(this);
			var nodeParent = $(this).parent();
			
			translations.find("locale").each(function(){
				var clone = node.clone();
				clone.addClass("multi-lang") 
				clone.addClass($(this).attr("id"));
				clone.text($(this).text())
				
				node.after(clone); 
			});
			
			node.detach();
		});
		
		this.initCufon();
		this.initLangSelect();
		
	},
	
	initCufon:function() {
		Cufon.set('fontFamily', 'letter-gothic');
		Cufon.replace('h1');
		Cufon.replace('h2');
		Cufon.replace('.nav > ul > li');
		Cufon.replace('.large > p');
		Cufon.replace('.large > a');
		
		$("h1").css("visibility","visible");
		$("h2").css("visibility","visible");
		$(".nav > ul > li").css("visibility","visible");
	},
	
	initLangSelect:function(){
		//this.currentLang = $(".lang-select").find(".selected > h2").text();
		
		$(".lang-select").bind("mouseenter", {_this:this}, this.onLangOver);
		$(".lang-select").bind("mouseleave", {_this:this}, this.onLangOut);
		$(".lang-select > .lang").bind("mousedown", {_this:this}, this.onLangDown);
		
		this.selectLang();
		
		
		//this.switchCopy(0);
	},
	selectLang:function(){
		
		
		
		var _this = this;
		
		if($.cookie('language')){
			
			if (document.cookie.split("language=")[2]) {
				
				_this.currentLang = document.cookie.split("language=")[2].split(";")[0]
				//console.log("cookie?!", _this.currentLang)
			} else {
				_this.currentLang = document.cookie.split("language=")[1].split(";")[0]
				//console.log("cookie?!", _this.currentLang)
			}
		} else {
			_this.currentLang = "NL"
		}
		
		$(".lang").each(function(){
			if($(this).hasClass("lang-" + _this.currentLang)){
				$(this).trigger("mousedown");
			}
		});
	},
	onLangOver:function(event){
		var _this = event.data._this;
		var target = $(event.currentTarget);
		
		target.find(".lang").each(function(){
			if(!$(this).hasClass("selected")){
				
				$(this).delay(80).animate({
					marginLeft:-40
				}, {
					duration:150,
					queue:false
				});
			}
		});
	},
	onLangOut:function(event){
		var _this = event.data._this;
		var target = $(event.currentTarget);
		
		target.find(".lang").each(function(){
			if(!$(this).hasClass("selected")){
				$(this).delay(80).animate({
					marginLeft:-20
				},{
					duration:150,
					queue:false
				});
			}
		});
	},
	onLangDown:function(event){
		//console.log("down")
		
		var _this = event.data._this;
		var target = $(event.currentTarget);
		
		$(".lang").removeClass("selected");
		$(target).addClass("selected");
		
		$(target).delay(80).animate({
			marginLeft:-20,
			backgroundColor:'#ffffff'
		}, 150, function(){
			$(".lang").each(function(index,el){
   				el.style.backgroundColor='';
  			});
			
			_this.currentLang = $(".lang-select").find(".selected > h2").text();
			$.cookie('language', _this.currentLang, { expires: 7, path:'/' });
			_this.switchCopy(250);
		});
	},
	switchCopy:function(time){
		var _this = this;
		
		$(".multi-lang").each(function(){
			if($(this).hasClass(_this.currentLang)){
				if(!$(this).hasClass("hidden-copy")){
					$(this).fadeIn(time);	
				}
			} else {
				$(this).fadeOut(0);
			}
		});
	},
  	toString: function() { 
  		return this.me; 
	}
};

schwandt = new CoreClass();

$(document).ready(function(){
	schwandt.init();
});

