// Thickbox

var tb = {

	resize: function(w){
	
		//$('#TB_ajaxContent').css('backgroundColor', 'transparent');
		$('#TB_window').animate({
			marginLeft: w * (-0.5) + 'px',
			width: w + 'px'
		}, 800);
		//$('#TB_ajaxContent').css('backgroundColor', '#CCC');
		$('#TB_ajaxContent').animate({
			width: w + 'px'
		}, 800, 0, function(){
			$('#widgetEditViewContent').fadeIn('slow');
			$('#TB_ajaxContent').css('backgroundColor', 'transparent');
			$('#TB_window').trigger('resizeComplete');
			TB_WIDTH = w;
			//$('#TB_window').css('top','20px');
		});
		
	},
	show: function(){
		if (document.getElementById("TB_overlay") === null) {
			$("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
			$("#TB_overlay").click(tb_remove);
		}
		$("#TB_overlay").css('opacity', '0');
		$("#TB_overlay").css('backgroundColor', '#000000');
		$("#TB_overlay").animate({
			opacity: '0.6'
		}, 1000);
		$("#TB_overlay").addClass("TB_overlayBG");
	}
	
}

var widget = {
	getContent : function(widgetId){
		if(typeof projectId == 'undefined'){
			return;
		}
		else varObj = {
			widgetId:widgetId
			,projectId:projectId
			,_fnc:'getWidgetContent'
			,callbackFunction : 'getContentComplete'
		}
		sendVars(varObj);
		return false;
	}

	,getContentComplete: function (data,success){
	
		if(typeof data.error != 'undefined'){
			alert(data.error);
			return;
		}
		$('#dynamicWidgetContent').html(data.content);
		
		$('#dynamicWidgetContent').hide();
		
		
		tb_show('Edit','#TB_inline?width=1&height=600&modal=true&inlineId=widgetContainer');
						
		if(typeof data.width != 'undefined'){
			width = data.width;
		}
		else width = 700;
		
		tb.resize(width);
		
		$('#TB_window').one('resizeComplete',function(){
				$('#dynamicWidgetContent').fadeIn('slow');
		})
		
		if(typeof data.js != 'undefined'){
			eval(data.js);
		}
	}

}



var climateprojects = {
	getLocalizedTextFields: function(projectId,language,linkObj){
		linkObj.blur();
		$('.activeFlag').removeClass('activeFlag');
		$(linkObj).addClass('activeFlag');
		sendVars({
			projectId: projectId
			,language:language
			,_fnc:'getLocalizedText'
			,callbackFunction : 'getLocalizedTextFieldsComplete'
		
		});
		return false;
	}
	
	,getLocalizedTextFieldsComplete : function(data,status){
		if (status == 'success') {
			
			if (typeof data.fields != 'undefined') {
				for (key in data.fields) {
					$('#project_' + key).html(data.fields[key]);
				}
			}
		}
	}
}


function sendVars(varObj,url){
	
		/**
		 * sends vars via ajax to the server 
		 * expects a _fnc property containing the function name of the serverside module or callback property 
		 * @param {Object} varObj
		 * @param {Object} url
		 */
		if(typeof url == 'undefined')url = defaultAjaxUrl;
		options = {
			url: url,
			processData: true,
			data: varObj,
			dataType: 'json',
			type: 'POST'
		};
		
		// the callback function should have th cp object accessible as this, so we have to apply the cp object
		
		if (typeof varObj.callbackFunction != 'undefined'){
			if(typeof widget[varObj.callbackFunction] == 'function') {
				options.success = function(data, status){
					widget[varObj.callbackFunction].apply(widget, arguments);
				}
			} 
			else if(typeof climateprojects[varObj.callbackFunction] == 'function') {
				options.success = function(data, status){
					climateprojects[varObj.callbackFunction].apply(climateprojects, arguments);
				}
			} 
			else options.success = varObj.callbackFunction;
		}
		else if (typeof varObj._fnc != 'undefined'){
			if (typeof widget[varObj._fnc + 'Complete'] == 'function') {
				options.success = function(data, status){
					// set the scope for the success function to the cp object
					widget[varObj._fnc + 'Complete'].apply(widget, arguments);
				}
			}
			else if(typeof climateprojects[varObj._fnc] == 'function') {
				options.success = function(data, status){
					climateprojects[varObj._fnc].apply(climateprojects, arguments);
				}
			} 

		}
		options.error = function(data, error, obj){
			alert('Sorry, an error occured!');
		};
		
		$.ajax(options);
		
}


