﻿function SetupEditor(id, tag)
{
	var dest = document.getElementById( id);
	$(dest).before( "<div class='editor_host'><div class='editor_show'></div><div class='editor'>Text: <input type='text' class='text_color color_picker'/> Umbra: <input type='text' class='shadow_color color_picker'/> <span class='close'>(inchide)</span></div></div>");
	
	var editor_show = $(dest).prev(".editor_host").children(".editor_show");
	$(editor_show).click( function() {
		$(this).next(".editor").toggle();
	});
	
	var editor = $(editor_show).next(".editor");
	$(editor).children(".text_color").val("#333");
	$(editor).children(".shadow_color").val("#fff");
	
//	var inp = $(editor).next("input");
//	inp.attr("name", id+"_css");
	
	$(editor).children(".close").click( function() {
		$(this).parent().toggle();
	});
	
//	alert(tag);
	
	$(editor).children(".color_picker").colorPicker();
	$(editor).children(".text_color").bind( "change", function()
	{
		var text_color = $(this).val();
		$(dest).css("color", text_color);
		var css = "color:"+$(dest).css("color")+";text-shadow:"+$(dest).css("text-shadow")+";";
		PageMethods.SaveCSS( css, tag, function() {}, function(e) { alert(e) });
	});
	$(editor).children(".shadow_color").bind( "change", function()
	{
		var size_val = 10;//$(editor).children(".size_slider").slider( "option", "value");
		var shadow_color = $(editor).children(".shadow_color").val();
		$(dest).css("text-shadow", "0px 0px "+size_val+"px "+shadow_color);
		var css = "color:"+$(dest).css("color")+";text-shadow:"+$(dest).css("text-shadow")+";";
		PageMethods.SaveCSS( css, tag);
	});				 /*
	$(editor).children(".size_slider").slider( {
		min: 0, max: 10,
		change: function(event, ui)
		{
		//	alert( ui.value);
			var size_val = $(editor).children(".size_slider").slider( "option", "value");
			var shadow_color = $(editor).children(".shadow_color").val();
			$(dest).css("text-shadow", "0px 0px "+size_val+"px "+shadow_color);
		}
	} );			   */
}

var topx = 0;

$(document).ready(function() {
	var panel = $(".fixed_panel");
	if( panel.length)
	{
		topx = panel.offset().top;
		var off = Math.min(0, -topx + $(window).scrollTop() + $(window).height() - panel.innerHeight());
		panel.css("top", off);
		if (off < 0)
			panel.addClass("floated");
		else
			panel.removeClass("floated");

		$(window).scroll(function() {
			var off = Math.min(0, -topx + $(window).scrollTop() + $(window).height() - panel.innerHeight());
			panel.css("top", off);
			if (off < 0)
				panel.addClass("floated");
			else
				panel.removeClass("floated");
		});
		
		$(window).resize(function() {
			var off = Math.min(0, -topx + $(window).scrollTop() + $(window).height() - panel.innerHeight());
			panel.css("top", off);
			if (off < 0)
				panel.addClass("floated");
			else
				panel.removeClass("floated");
		});
	}
});

function SaveAnswer()
{
	var uid = $(".answer_edit *[id$=_UserID]").val();
	var text = $(".answer_edit *[id$=_AnswerText]").val();
	var file = $(".answer_edit *[id$=_FileUploadImage]").val();
	var embed = $(".answer_edit *[id$=_TextBoxEmbed]").val();
	if( file || embed)
	{
		eval($(".answer_edit *[id$=_ButtonUpdate]").attr("href").substring(11));
		return;
	}
	PageMethods.SaveAnswer( uid, text, qid, function() {
		$(".answer_edit .view_panel .text").text( text);
		$(".answer_edit .edit_panel").hide();
		$(".answer_edit .view_panel").show();
		$(".answer_edit").removeClass( "fixed_panel");
		if( $(".answer_edit").hasClass( "floated"))
		{
			$(".answer_edit").animate( { top: 0 }, 500, 'linear');
			$('html, body').animate({scrollTop:topx-($(window).height() - $(".answer_edit").innerHeight())+50}, 500, 'linear');
		}
	}, function(e) { alert(e) });
}
