// Create business logic in a YUI sandbox using the 'io' and 'json' modules

YUI({combine: true, timeout: 10000}).use("node", "io", "dump", "anim", "json-parse",function (Y) {

    // Attach a click event listener to the button #demo_btn to send the request
    Y.on('click',function (e) {
        // Make the call to the server for JSON data
       var totem_id = e.target.getAttribute('totem_id');
       var url = e.target.getAttribute('url');
       var target_id = e.target.getAttribute('target_node');
       var cfg = { 
	       method: 'POST',
	       data: 'totem_id=' + totem_id,
	       timeout : 3000,
	       arguments: {
		        success: target_id,
		   },
	       on : {
		       success : function (x,o,args) {
                Y.log("RAW JSON DATA: " + o.responseText);

                var messages = [],
                    html = '', i, l;
                // Process the JSON data returned from the server
                try {
                    messages = Y.JSON.parse(o.responseText);
                }
                catch (e) {
                    alert("JSON Parse failed!");
                    return;
                }

                // Use the Node API to apply the new innerHTML to the target
               Y.one('#' + args.success).append('<img height=60 width=60 src=' + messages.qr_code_url + ' />');
               Y.one('.totem_link_holder').remove()
            },

            failure : function (x,o) {
                alert("Async call failed!");
            }
		   
	       },
		   
	       headers: { 
	        	'Content-Type': 'application/json', 
	        	'X-TRANSPORT-TYPE': 'Flash' 
	    	}
	   }
       transaction = Y.io(url, cfg);

    },'.totem_create_link');

  

    
	

});
