var Hyves = {
	'config':{
		'flash_dom_id':'Main',
		'api_url':'/campagne.php',
		'connect_url':'http://www.durftegenieten.nl/hyvesconnect.php?action=request',
		'debug_mode':true,
		'call_flash':true
	},
	'getFlash':function(){
		return document.getElementById( Hyves.config.flash_dom_id );	
	},
	'flashUpdate':function(envelope){
		Hyves.getFlash().r_update(envelope);
	},
	'initialize':function() {
	
		jQuery.ajaxSetup({
			"async":true,
			"cache":false,
			"contentType":"application/json; charset=UTF-8",
			"dataType":"json",
			"error":function(XMLHttpRequest, textStatus, errorThrown) {
	
				// AJAX error
				
			},
			"success":function(envelope, textStatus) {
				
				if (Hyves.config.debug_mode) {
					trace("api_response:");
					trace(envelope);
				}
				
				if (textStatus === "success") {			
					switch (envelope.method) {
						case "tip":
							if ( Hyves.config.call_flash ) {
								Hyves.flashUpdate(envelope);
							}
						break;
						case "www":
							if ( Hyves.config.call_flash ) {
								Hyves.flashUpdate(envelope);
							}
						break;
						case "gadget":
							if ( Hyves.config.call_flash ) {
								Hyves.flashUpdate(envelope);
							}
						break;
						case "taf_browse":
							if ( Hyves.config.call_flash ) {
								Hyves.flashUpdate(envelope);
							}
						break;
						case "taf_search":
							if ( Hyves.config.call_flash ) {
								Hyves.flashUpdate(envelope);
							}
						break;
						case "taf_sent":
							if ( Hyves.config.call_flash ) {
								Hyves.flashUpdate(envelope);
							}
						break;
					}
				}
			},
			"dataFilter":function (data, type) {
				if (type==="json") { return $.secureEvalJSON(data); }
			},
			"timeout":10000, // 10 seconds
			"type":"POST",
			"url":Hyves.config.api_url
		});
	
	},
	'flashClick':function(method,params){
		
		switch (method) {
		case "connect":
			if (!this.isConnected()) {
				nav_to( Hyves.config.connect_url ,false);
			}
			break;
		case "tip":
			if (this.isConnected()) {this.api(method);}
			break;
		case "www":
			if (this.isConnected()) {this.api(method,params);}
			break;
		case "gadget":
			if (this.isConnected()) {this.api(method);}
			break;
		case "taf_browse":
			if (this.isConnected()) {this.api(method,params);}
			break;
		case "taf_search":
			if (this.isConnected()) {this.api(method,params);}
			break;
		case "taf_sent":
			if (this.isConnected()) {this.api(method,params);}
			break;
		}
		
	},
	'api':function() {

		var envelope = {};
		
		if (arguments.length === 2) {
			envelope.method = arguments[0];
			envelope.params = arguments[1];
		} else if (arguments.length === 1) {
			envelope.method = arguments[0];
		}
		
		if (Hyves.config.debug_mode) {
			trace("api:");
			trace( envelope );
		}
		
		jQuery.ajax( {'data':$.toJSON(envelope)} );

	},
	'getAppExchange':function() {
		return AppExchange;
	},
	'isConnected':function(){
		var tmp = Hyves.getAppExchange().state.current_state;
		
		if ( tmp === "connected" || tmp === "request_success") {
			return true;
		}
		return false;
	},
	'getState':function(){
		
		var envelope = {};
		
		envelope.method = 'getState';
		envelope.r_status = Hyves.getAppExchange().state.current_state;
		
		Hyves.flashUpdate(envelope);
		return Hyves.getAppExchange().state;
		
	},
	'getProfile':function(){
		Hyves.flashUpdate( Hyves.getAppExchange().profile );
		return Hyves.getAppExchange().profile;
	}
};

function trace(mixed) {
	try { console.log(mixed); } catch (e) {}
}

function nav_to() {
	if ( arguments.length === 1 || arguments.length === 2) {
		if ( arguments[0].substr(0,7) !== "http://" ) {
			var views = gadgets.views.getSupportedViews();
			if (arguments.length === 2) {
				gadgets.views.requestNavigateTo( views[ arguments[0] ] , arguments[1] );
			} else {
				gadgets.views.requestNavigateTo( views[ arguments[0] ] );
			}
		} else {
			var newWindow = false;
			if (arguments.length === 2 && arguments[1]===true) {
				newWindow = true;
			}
			if (newWindow) {
				window.open( arguments[0] );
			} else {
				window.top.location.href = arguments[0]; 
			}
		}
	}
}

jQuery( Hyves.initialize );