/* Copyright 2009 Palm, Inc.  All rights reserved. */

//
// Web utility class
//
var YoutubeNet = Class.create({

	// Simple get request
	get : function(url, auth, callback, errCb) {
	    var data = AppAssistant.crypto.getDeviceData(auth.deviceKey, url);
		var headers = ['X-GData-Device', 'device-id="' + auth.deviceId + '" ,data="' + data + '"'];
		
	    new Ajax.Request(url, {
	        method: 'get',
	        requestHeaders: headers,
	        onSuccess: function(t){
	            callback(t.responseText);
	        },
	        onFailure: function(t){
				Mojo.Log.error("Youtube: net get ERROR: ", t.responseText)
	            //callback(null);
				if (errCb) {
					errCb(t.responseText);
				}
	        }
	    });
	},

	postFormUrlEncoded: function(url, auth, successCb, errCb) {
		var data = "developer=" + auth.develKey + "&serialNumber=" + auth.deviceSerialNum;
		data = Base64._utf8_encode(data);
		//data = escape(data); TODO how come when I url encode this request fails?
		
		Mojo.Log.info("Youtube: net postFormUrlEncoded: data: %s", data);
	    new Ajax.Request(url, {
			contentType: 'application/x-www-form-urlencoded',
			postBody: data,
	        onSuccess: function(t) {
				Mojo.Log.info("Youtube: net postFormUrlEncoded: success: %s", t.responseText);
				if (successCb) successCb(t);
	        },
			/*
	        onFailure: function(){
	        },
	        */
			on400: function(t) {
	        	Mojo.Log.info("Youtube: net postFormUrlEncoded: ERROR on400: %s", t.responseText);
				if (errCb) errCb(t);
	        },
	    });
	},

});

