$(document).ready(function() {

   
   $("div.tableDetailsPanel").css("display","none");
   $("div.tablePricePanel").css("display","none");
   $("div.tableChangePanel").css("display","none");
   
   $(".tableDetailsButton").click(function(){
	$(this).parent().find(".tableDetailsPanel").slideToggle("fast");
   
   });
   
   $(".tablePriceButton").click(function(){
	$(this).parent().find(".tablePricePanel").slideToggle("fast");
   
   });
   
   $(".tableChange").click(function(){
	$(this).parent().find(".tableChangePanel").slideToggle("fast");
   });
   
   $(".tableDetailsEdit, .tablePriceEdit").click(function(){
   	
   	if ($(this).attr("class") == "tableDetailsEdit") {
   		var classContent = ".scheduleDetailsContent";
   		var formAction = "/doScheduleDetails.php";
   		var idContent = "#scheduleDetailsContent";
   		var formId = "detailsForm";
   		var textareaName = "details";
   		var editButton = "#tableDetailsEdit";
   	} else if ($(this).attr("class") == "tablePriceEdit") {
   		var classContent = ".schedulePriceContent";
   		var formAction = "/doSchedulePrice.php";
   		var idContent = "#schedulePriceContent";
   		var formId = "priceForm";
   		var textareaName = "price";
   		var editButton = "#tablePriceEdit";
   	}
   
	var content = $(this).parent().find(classContent).html();
	$(this).parent().find(classContent).empty();
	$(this).prev(".brRemove").hide();
	$(this).hide();
	
	var id = $(this).attr("id").split("-");
	id = id[1];
	
// 	$("body").append('<iframe style="display:none;" src="/data/snippets/form.tx" id="stupidIframe"></iframe>');
// 	var str = $("#stupidIframe").html();
// 	
// 	alert(str);
	
	content = content.replace(/<br \/>/ig,"\n");
	content = content.replace(/<BR>/ig,"\n")

	var formHtml = "<form action='"+formAction+"' method='post' id='"+formId+"-"+id+"' class='detailsForm'>"
			+	"<textarea name="+textareaName+" cols='50' rows='10'>"+content+"</textarea>"
			+	"<input type='hidden' name='id' value='"+id+"'  />"
			+	'<br><br><input type="submit" value="Въведи" class="sdb" />'
			+	"</form>";
	
	$(this).parent().find(classContent).append(formHtml);
	
	var options = { 
        //target:        scheduleDetailsContent,   // target element(s) to be updated with server response 
        //beforeSubmit:  showRequest,  // pre-submit callback 
        success:   processXml,   // post-submit callback 
 
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        dataType:  'xml'         // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    }; 
    
    function processXml(responseXML) {
    // 'responseXML' is the XML document returned by the server; we use 
    // jQuery to extract the content of the message node from the XML doc 
    var details = $('details', responseXML).text(); 
    
    details = details.replace(/\n/g,"<br />");
    details = details.replace(/\n/g,"<br>");
    
   $(idContent+"-"+id).html(details);
   $(idContent+"-"+id).show();
   $(editButton+"-"+id).show();
   $(editButton+"-"+id).prev(".brRemove").show();
   
}
 
    // bind form using 'ajaxForm' 
    $('#'+formId+'-'+id).ajaxForm(options); 
    
		function showRequest(formData, jqForm, options) { 
		// formData is an array; here we use $.param to convert it to a string to display it 
		// but the form plugin does this for you automatically when it submits the data 
		var queryString = $.param(formData); 
		
		// jqForm is a jQuery object encapsulating the form element.  To access the 
		// DOM element for the form do this: 
		// var formElement = jqForm[0]; 
		
		alert('About to submit: \n\n' + queryString); 
		
		// here we could return false to prevent the form from being submitted; 
		// returning anything other than false will allow the form submit to continue 
		return true; 
		} 
		
		// post-submit callback 
		function showResponse(responseText, statusText)  { 
		// for normal html responses, the first argument to the success callback 
		// is the XMLHttpRequest object's responseText property 
		
		// if the ajaxForm method was passed an Options Object with the dataType 
		// property set to 'xml' then the first argument to the success callback 
		// is the XMLHttpRequest object's responseXML property 
		
		// if the ajaxForm method was passed an Options Object with the dataType 
		// property set to 'json' then the first argument to the success callback 
		// is the json data object returned by the server 
		
		alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
			'\n\nThe output div should have already been updated with the responseText.'); 
		} 
	

   });
   

   
       $(".detailsForm").submit(function() {
        // inside event callbacks 'this' is the DOM element so we first 
        // wrap it in a jQuery object and then invoke ajaxSubmit 
        $(this).ajaxSubmit(options); 
 
        // !!! Important !!! 
        // always return false to prevent standard browser submit and page navigation 
        return false; 
    }); 
   
   
   
    $(".tableSwitchVisibility").click(function() {
    	var idsame = idnum = $(this).attr("id");
	var idnum = idsame.split("-");
	var id = idnum[1];
	var flag = idnum[2];
    	$.post("/doScheduleVisibility.php", {id: id, visible: flag}, function(xml) {
       		
       		if (flag == 1 && $("result", xml).text() == "OK") {
       			$("#"+idsame).html("направи видима");
       			$("#"+idsame).attr("id",idnum[0]+"-"+idnum[1]+"-0");
       		}
       		else if (flag == 0 && $("result", xml).text() == "OK") {
       			$("#"+idsame).html("направи невидима");
       			$("#"+idsame).attr("id",idnum[0]+"-"+idnum[1]+"-1");
       		}
       		else alert($("result", xml).text());
     });
    	
    });
 });
