// Start JQuery listening once everything is loaded
$(document).ready(function(){
	$.fn.slideBox = function(params){
	
		var content = $(this).html();
		var defaults = {
			width: "100%",
			height: "200px",
			position: "bottom"			// Possible values : "top", "bottom"
		}
		
		// extending the fuction
		if(params) $.extend(defaults, params);
		
		var divPanel = $("<div class='slide-panel'>");
		var divContent = $("<div class='feedbackContent'>");
	
		$(divContent).html(content);
		$(divPanel).addClass(defaults.position);
		$(divPanel).css("width", defaults.width);
		
		// centering the slide panel
		$(divPanel).css("left", (100 - parseInt(defaults.width))/2 + "%");
	
		// if position is top we're adding 
		if(defaults.position == "top")
			$(divPanel).append($(divContent));
		
		// adding buttons
		$(divPanel).append("<div class='slide-button'>Leave feedback</div>");
		$(divPanel).append("<div style='display: none' id='close-button' class='slide-button'>(Close box)</div>");
		
		if(defaults.position == "bottom")
			$(divPanel).append($(divContent));
		
		$(this).replaceWith($(divPanel));
		
		// Buttons action
		$(".slide-button").click(function(){
			if($(this).attr("id") == "close-button")
				$(divContent).animate({height: "0px"}, 1000);
			else
				$(divContent).animate({height: defaults.height}, 1000);
			
			$(".slide-button").toggle();
		});
		
	};
	
	// Instansiate the sidebox
	$("#feedbackContent").slideBox({width: "100%", height: "110px", position: "top"});

	// This basically replicates XMLHTTPRequest API
	// but without the XSS protection.
	function xss_ajax(url) 
	{
			var script_id = null;
			var script = document.createElement('script');
			script.setAttribute('type', 'text/javascript');
			script.setAttribute('src', url);
			script.setAttribute('id', 'script_id');
	
			script_id = document.getElementById('script_id');
			if(script_id)
			{
				document.getElementsByTagName('head')[0].removeChild(script_id);
			}
	
			// Insert <script> into DOM
			document.getElementsByTagName('head')[0].appendChild(script);
		}				

	// Set var for form (JS is on/off)
	$("#js").val(1);
	
	// Once the form is submitted, fade it out
	// and send AJAX
	$('#feedbackMsg').submit(function()
	{
		$('#feedbackMsg').fadeOut(500);
		
		setTimeout("$('#feedbackThanks').fadeIn(500)", 500);
		setTimeout('$(".feedbackContent").animate({height: "0px"}, 1000)', 3000);
		setTimeout('$(".slide-button").toggle()', 3000);
		
		setTimeout('$("#feedbackMsg").show()', 4000);
		setTimeout('$("#feedbackThanks").hide()', 4000);
				
		
		// localise the form vars
		var formJS = document.getElementById("feedbackMsg").js.value;
		var formMessage = document.getElementById("feedbackMsg").feedbackmessage.value;
		var formUseful = document.getElementById("feedbackMsg").useful.value;
		var formEase = document.getElementById("feedbackMsg").ease.value;
		
		
		var url = 'http://admin.betterbrief.co.uk/mlsfeedback.php';
		var poststr = 	"js=" + encodeURIComponent(formJS) + "&message=" + encodeURIComponent(formMessage) +
						"&useful="+encodeURIComponent(formUseful) + "&ease=" + encodeURIComponent(formEase);
				
		// Send the form with vars		
		xss_ajax(url + '?' + poststr);	
		return false;
	});	
});