var ContentApi = function(baseurl) {
	this.baseurl = baseurl + "/api/content";
	
	this.saveContent = function(namespace, content, successHandler, errorHandler, authenticationToken) {	
		// Does not work in IE
		$.ajax({
			  url: this.baseurl + "/" + namespace + "/" + (authenticationToken ? authenticationToken : "") + "/",
			  type: "POST",
			  cache : false,
			  contentType: "application/json",
			  dataType : "json",			  
			  data : $.toJSON(content),
			  success:  function(response) {
			    successHandler(response);
			  },
			  error: function(error, ajaxOptions, thrownError){
				 errorHandler(error, ajaxOptions, thrownError);
			  }			
			});
			
	},
	
	this.deleteContent = function(namespace, contentId, successHandler, errorHandler) {		
		$.ajax({
			  url: this.baseurl + "/" + namespace + "/" + contentId,
			  type: "DELETE",
			  cache : false,
			  contentType: "application/json",
			  dataType : "json",
			  async : false,			  
			  success:  function(response) {
				  successHandler(response);
			  },
			  error: function(response){
				  errorHandler(response);
			  }			
			});
	}	
	
	this.getAccountInfo = function(namespace, successHandler) {
		$.getJSON(this.baseurl + "/account/" + namespace + "/?callback=?", successHandler);		
	},
	
	this.getContent = function(namespace, successHandler) {
		$.getJSON(this.baseurl + "/" + namespace, successHandler);
	},
	
	this.getContentText = function(namespace, path, locale, successHandler) {					
		$.getJSON(this.baseurl + "/text/" + namespace + "/"  + path + "/" + locale + "?callback=?", successHandler);		
	},
	
	this.getImages = function(namespace, successHandler) {					
		$.getJSON(this.baseurl + "/images/" + namespace + "?callback=?", successHandler);		
	},
	
	this.getContentTextById = function(namespace, contentId, locale, successHandler) {							
		$.getJSON(this.baseurl + "/text/id/" + contentId + "/" +namespace + "/" + locale + "?callback=?", successHandler);		
	},
		
	this.getContentTextByKeys = function(namespace, keys, locale, successHandler) {		
		if($.browser.msie) {
			this.getContentTextByKeysIEFix(namespace, keys, locale, successHandler);
			return;
		}
		
		var json = $.toJSON(keys); 
		var md5 = $.md5(json + "_" + locale + "_" + namespace);
		//console.log("md5 for locale " + locale + " : " + md5);
		var baseUrl = this.baseurl; 		
		$.ajax({
			  url: this.baseurl + "/checkCache/" + "?cacheKey=" + md5 + "&w=" + namespace,
			  type: "GET",
			  cache : true,
			  ifModified: true,
			  contentType: "application/json",
			  dataType : "json",
			  async : false,	
			  success: function(response, status, xhr) {
					// Content found in browser cache
				    if(200 == xhr.status) {
				    	successHandler(response);				    	
				    } else {
				    	// Get content from server and ...						
						var jqxhr = $.ajax({
							  url: baseUrl + "/text/" + namespace + "/" + locale + "?md5=" + md5,
							  type: "PUT",
							  cache : true,
							  ifModified: true,
							  contentType: "application/json",
							  dataType : "json",
							  data : json,
							  async : true,			  
							  success:  function(response) {
								  console.log("SUCCESS " + $.toJSON(response));
								  successHandler(response);
								  var cacheKey = jqxhr.getResponseHeader('cacheKey');
								  // .. put it in browser cache via etag
								  $.ajax({
									  url: baseUrl + "/checkCache/" + "?cacheKey=" + cacheKey + "&w=" + namespace,
									  type: "GET",
									  cache : true,
									  ifModified: true,
									  contentType: "application/json",
									  dataType : "json",
									  async : false									  		
									});
							  }	
							});
				    }
			  }			   
		});		
	},
	
	this.getContentTextByKeysIEFix = function(namespace, keys, locale, successHandler) {
		var keysQuery = decodeURIComponent($.param(keys)).replace(new RegExp("\\[\\]","g"), "");
		$.getJSON(this.baseurl + "/text/" + namespace + "/" + locale + "?callback=?&" + keysQuery, successHandler);
	},
	
	this.getCssUrl = function(namespace, key, locale) {
		return this.baseurl + "/css/" + namespace + "/" + key + "/" + locale;
	}
}

