$(function(){
    $("body").append("<div id='editor_popup' title='Editar Texto'><textarea style='width: 99%; height: 330px;'><textarea></div>");
    var popup = $("#editor_popup");
    popup.dialog({
		modal: true,
		resizable: false,
		draggable: false,
		autoOpen: false,
		closeOnEscape: false,
		height: 450,
		width: 600
	});

	$("div.editable").each(function(){
		var elemento = $(this).find("div.content");
		$(this).find("a").click(function(){
			var code = elemento.attr("id");
			var content = $.trim(elemento.html());
			popup.dialog("option", "buttons", [
			    {
			        text: "Guardar",
			        click: function() {
						var new_content = popup.find("textarea").val();
				        $.ajax({
				            url : 'content/edit',
				            dataType:'json',
				  	        data : {
					            code : code,
					            content : new_content
				        	},
				            success:function(data)
				            {
				        		elemento.html(data.content);
				        		popup.dialog("close");
				            }
				        });
                    }
			    },
			    {
			        text: "Cancelar",
			        click: function() {
			    		popup.dialog( "close" );
                    }
			    }
            ]);
			popup.find("textarea").val(content);
			popup.dialog("open");
		});
	});
});


// Namespace for Thrupons
if(!thrupons) var thrupons = {};

//Namespace for "template" in "thrupons" namespace
thrupons.template = {};

thrupons.template.init = function(){
	
    removeBg();
    
    $(function(){
		thrupons.template.buger.init();
		thrupons.template.loader.init();
		thrupons.template.ajax({});

		$("body").ajaxError(function(event, xhr){
			thrupons.template.buger.show(xhr.responseText);
		});
	});
};

thrupons.template.init();

thrupons.template.ajax = function(data){
	var now = new Date();
	var timezone = -now.getTimezoneOffset() / 60;
	var default_data = $.extend({
			XMLHttpRequest : true,
			fb_sig_cookie_sig : thrupons.config.session.sid,
			timezone : timezone
		}, data);

	$.ajaxSetup({
		type: "POST",
		data : default_data,
		beforeSend : function(xhr, setting)
		{
			if(setting.overlay)
				thrupons.template.loader.show();
		},
		complete : function()
		{
			thrupons.template.loader.hide();
		}
	});
};

thrupons.template.buger = {
	popup : null,
	init : function()
	{
	    $("body").append("<div id='buger_popup' title='Error'></div>");
	    thrupons.template.buger.popup = $("#buger_popup");
	    thrupons.template.buger.popup.dialog({
			modal: true,
			resizable: false,
			draggable: false,
			autoOpen: false,
			closeOnEscape: false
		});
	},

	show : function(text)
	{
		thrupons.template.buger.popup.html(text);
		thrupons.template.buger.popup.dialog("open");
	}
};

thrupons.template.loader = {
	overlay : null,
	init : function()
	{
	    $("body").append("<div id='global_overlay'></div>");
	    thrupons.template.loader.overlay = $("#global_overlay");
	    thrupons.template.loader.overlay.css('opacity', 0.15);
	},

	show : function()
	{
		thrupons.template.loader.overlay.show();
	},

	hide : function()
	{
		thrupons.template.loader.overlay.hide();
	}
};

function removeBg(){
    if (top.location != self.location){
        $("body").css({"background": "none"});
        //alert('remove background...');
    }
}

function setColsHeight(){
    
    var heightDiv1 = $('.body_cont div.left_col').height();
    var heightDiv2 = $('.body_cont div.right_col').height();
    
    //alert('heightDiv1: '+heightDiv1+' - heightDiv2: '+heightDiv2);
    
    
    if(heightDiv1>heightDiv2){
        if (navigator.appName == "Microsoft Internet Explorer"){
            $('.body_cont div.right_col .container').css('height',heightDiv1-1);
        }else{
            $('.body_cont div.right_col .container').css('height',heightDiv1-18);
        }
    }else if(heightDiv1<heightDiv2){
        $('.body_cont div.left_col .container').css('height',heightDiv2);
    }
}

function redirectPage(urlStr){
    top.location.href = urlStr;
    /*alert('redirect to: '+urlStr);*/
}
