$.fn.tinymce = function(options) {
    return this.each(function() {
        preString = "<";
	preString += "div class='jqHTML_frame' style='width:"+$(this).css("width")+"px;height:" + ($(this).css("height")+20) + "px;'>";
        postString = "<";
	postString += "/div>";
        $(this).wrap(preString + postString);
        tinyMCE.addMCEControl(document.getElementById(this.id), this.id);
    });
}

function initMCE()
{
	tinyMCE.init({
		mode : "none",
		theme : "advanced",
		plugins : "advlink,visualchars,table,style,advimage,paste,xhtmlxtras",
		theme_advanced_buttons1 : "cut,copy,pastetext,pasteword,selectall,separator,bold,italic,underline,styleprops,separator,justifyleft,justifycenter,justifyright,justifyfull,bullist,numlist,undo,redo",
		theme_advanced_buttons2 : "tablecontrols,separator,link,unlink,image,hr,charmap",
		theme_advanced_buttons3 : "formatselect,fontselect,fontsizeselect,separator,visualaid,visualchars,removeformat,cleanup,help,separator,code",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "center",
		theme_advanced_path_location : "bottom",
		extended_valid_elements : "b/strong,i/em,h1,h2,h3,a[name|href|target|title|onclick|class|style],img[style|class|src|border=0|alt=|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style],div[id|class|align|style],u",
		content_css : "/css/tinymce.css",
		save_enabledwhendirty : true,
		remove_script_host :  false,
		remove_linebreaks : false,
		apply_source_formatting : true,
		relative_urls : false
	});
}
initMCE();

$(document).ready(function(){
	$('#ContentContent').tinymce();
	$('.tinymce').tinymce();

	$('.sidemenu').hover(
		function() {
			var id = $(this).attr('id');
			var topid = id.replace('pLinkTR_', '').replace('_1', '');
			$('#sublink_table_' + topid).slideDown('fast');
		},
		function() {
			var id = $(this).attr('id');
			var topid = id.replace('pLinkTR_', '').replace('_1', '');
			$('#sublink_table_' + topid).slideUp('fast');
		}
	);

	$('.tablesorter').tablesorter({
		cssAsc: 'ts_headerSortUp',
		cssDesc: 'ts_headerSortDown',
		cssHeader: 'ts_header',
		widgets: ['zebra']
	});

	$("#EventPromocodeType").change(function(){ 
                var id = $(this).val();
                $(".promosection").each(function(){
                        $(this).hide();
                });
                $("#" + id).show();
        });
        $("#EventRegistrantTypeId").change(function(){
                var id = $(this).val();
                $(".event_session").each(function(){
                        $(this).remove();
                });

                $.getJSON("/conference/json_sessions/" + id, function(json){
                        html = "";
                        for(var id in json)
                        {
                                html += "<tr class=\"event_session\"><td colspan=\"2\">";
                                html += "<input id=\"event" + id + "\" class=\"attend required\" type=\"checkbox\" value=\"" + id + "\" name=\"data[EventRegistrant][attend][]\" />";
                                html += "<label id=\"event" + id + "_name\" for=\"event" + id + "\">" + json[id] + "</label>";
                                html += "</td></tr>";
                        }
                        $("#RegisterSubmit").before(html);
                });
        });

	/**
        * Add Field Row on Add Session Title
        */
        $("#add_session_field").click(function(){
                var html = '<tr>';
                html += '<td style="width: 150px;">';
                html += '<input type="text" size="30" name="data[EventSession][]" />';
                html += '</td>';
                html += '</tr>';

                $("#SessionAddField").append(html);
        });

        /**
        * Add Field Row on Add Type name
        */
        $("#add_type_field").click(function(){
                var count = new Date().getTime();
                var html = '<tr>';
                html += '<td style="width: 150px;">';
                html += '<input type="text" size="30" name="data[EventRegistrationType][]" />';
                html += '</td>';
                html += '</tr>';

                $("#TypeAddField").append(html);
        });
        /**
        * Add Promocode Assignment Row
        */
        $(".promocode_assignment").click(function(){
                var id = $(this).attr("id");
                id = id.replace(/pa/, '');

                var count = new Date().getTime();

                var html = '<tr>';
                html += '<td><input type="text" name="data[EventPromocodeAssignment][' + id + '][' + count + '][quantity]" size="3" /></td>';
                html += '<td>';
                html += '<select name="data[EventPromocodeAssignment][' + id + '][' + count + '][type]" onchange="promocode_type(this);" id="pat' + id + '">';
                html += '<option value=""></option>';
                html += '<option value="session">Session</option>';
                html += '<option value="discount_percent">Discount (Percent)</option>';
                html += '<option value="discount_amount">Discount (Amount)</option>';
                html += '</select>';
                html += '</td>';
                html += '<td id="pad' + id + '">&nbsp;</td>';
                html += '<td><input type="checkbox" name="data[EventPromocodeAssignment][' + id + '][' + count + '][unlimited]" /></td>';
                html += '</tr>';

                $("#parow" + id).after(html);
        });
});
