/**
 * Give AIR NRICH Editor access to bits and pieces it needs through the child sandbox bridge.
 */
/* For test purposes only...

$(document).ready(function() {
    // Disable all links. (Used in editor preview)
	$("a").addClass("disabledLink").click(function(event) {
	    event.preventDefault();
	    //alert("Oops - use the Site View to follow links");
	    //return false;
	});
});
*/

window.childSandboxBridge = {

    // These functions provides AIR access to the HTML problemview.
    getHTML: function() {
	return $("#problemview").html();
    },
    setHTML: function(s) {
	$("#problemview").html(s);
    },

    // Automated login from AIR
    login: function(username, password, referrer) {
	// 
	// We insert the hidden login parameter because login.php expects it, and it
	// is normally generated by pressing the login button on the page. We have
	// to simulate that bit...
	//
	$("input[name='username']").attr("value", username)
	.before('<input type="hidden" name="login" value="submit" />');
	$("input[name='password']").attr("value", password);
	$("input[name='referrer']").attr("value", referrer);
	$("#loginform > form").submit();
    },

    // Reload the current page
    refresh: function() {
	jsMath.ConvertTeX();
	jsMath.Process();
    },

    // Upload an edited part
    upload: function(objId, part, source, action) {
	//var action="https://nrich.local/admin/upload.php";//?sendXML=1&amp;obj_id="+objId+"&amp;part="+part;
	//alert("url = "+parent.location);
	//alert("uploading to:"+objId+ " part="+part + " source empty?" + (source==null || source ==""));
	$.post(action, 
	       {
		   sendXML : 1,
		   obj_id : objId,
		   part : part,
		   data : source
	       }, 
	       function(data) {
		   // We'll only receive this callback if the page upload was successful

		   //alert("objId="+objId+" part="+part);
		   url = '../public/viewer.php?obj_id='+objId+'&part='+part;
		   //alert("redirect to: " + url);
		   parent.location = url;
		   return data;
	       }
	      );
    },

    // Disable all links. (Used in editor preview)
    disableAllLinks: function() {
	$("a").addClass("disabledLink").click(function(event) {
	    event.preventDefault();
	    //alert("Oops - use the Site View to follow links");
	    //return false;
	});
    }
};

