/**
 * All intellectual property rights in this Software throughout the world belong to UK Radioplayer, 
 * rights in the Software are licensed (not sold) to subscriber stations, and subscriber stations 
 * have no rights in, or to, the Software other than the right to use it in accordance with the 
 * Terms and Conditions at www.radioplayer.co.uk/terms. You shall not produce any derivate works 
 * based on whole or part of the Software, including the source code, for any other purpose other 
 * than for usage associated with connecting to the UK Radioplayer Service in accordance with these 
 * Terms and Conditions, and you shall not convey nor sublicense the Software, including the source 
 * code, for any other purpose or to any third party, without the prior written consent of UK Radioplayer.
 *
 * @name CrossDomain
 * @description Handles the return data from third party cookie, requests station data from webservice, creates new datastore
 *
 *
 * @authors Cathy Bartlett <cathy.bartlett@bbc.co.uk>
 * @authors Sergejs Vaskevics <sergejs.vaskevics@ubcmedia.com>
 */

radioplayer.services.CrossDomain = function() { 
	this.bind();  
};

radioplayer.services.CrossDomain.prototype = {
	_dataRequestCallback : {},
		
	receiveMessage: function(data) {
		var i,
			response = data;
		var cookienames = ['lastplayed', 'presets', 'preferences','volume'];
		var	ids = [],
			presetsCookieData,
			lastplayedCookieData,
			volumeCookieData;
	for(var obj in response){
			switch (obj) {
				case 'presets':	
					if ((data.presets) && data.presets.length > 0) {
						var cookiedataArr = data.presets;
						ids.push(radioplayer.services.CrossDomain.prototype.extractIds(cookiedataArr));
						presetsCookieData = cookiedataArr;									
					} else {
						presetsCookieData = 'null';
					}					
				  break;
				case 'lastplayed':
					var id;
					if (data.lastplayed && (data.lastplayed !=="")) {
						id = data.lastplayed;
					} else {
						id = radioplayer.currentStationID;
					}
					ids.push(id);
					lastplayedCookieData = id;
				  break;
				case 'volume':
					volumeCookieData = data.volume;
					radioplayer.services.FlashEmp.prototype.createFlashEmp(volumeCookieData);
				  break;  
				case 'preferences':
					glow.events.fire(document, 'preferences:data', {d: data.preferences});
				  break;
				default:
				  //	  
			}	
		}	
		
		// Request station presets data
		var datastore = radioplayer.services.CrossDomain.prototype.getData(ids.join(','), {
				onLoad: function() {
					glow.events.fire(document, 'presets:data', {d: presetsCookieData});
					glow.events.fire(document, 'lastplayed:data', {d: lastplayedCookieData});
				},
				onError: function() {
					glow.events.fire(document, 'crossdomain:error', {
						displayMessage: "Cannot load your presets",
						logMessage: "Unable to get preset data from cookie"
						});
				}						
		});
	},
	
	getData: function(ids, callback) {
		this._dataRequestCallback = callback;
		if (typeof radioplayer.services.store === 'object') {
			// corner case scenario check vs performance on client computer(delete this block if client performance is critical ) 
			var idsArray = ids.split(",");
			for( var idIndex in idsArray){
				// if station is not in data store then break loop and make request to update data store.
				if(!radioplayer.services.store.isInDatastore(idsArray[idIndex])){
					this._requestPresetsDataFromStaticJSON(ids, callback);
					return;
				}				
			}
			// end of the block to delete
			if (this._dataRequestCallback.onLoad) {
				this._dataRequestCallback.onLoad();
			}
		}
		else{
			this._requestPresetsDataFromStaticJSON(ids, callback);
		}
			
	},
	_processData : function(data){
		//UKRP web site browse functionality function call
		//listStations(data);
		
		radioplayer.services.store = new radioplayer.services.Datastore(data);
		
		if (this._dataRequestCallback.onLoad) {
			this._dataRequestCallback.onLoad();
		}
	},
	_requestPresetsDataFromStaticJSON : function(ids, callback){
			var opts = {
					onLoad: this._processData,
					onError: function(response) {
					if (callback.onError) {
						callback.onError();
					}
				}
			};
			
			var requestDomain = radioplayer.stationListFileDomainName;
			if (typeof radioplayer.stationListType === 'undefined') {
				radioplayer.stationListType = ".jgz";
			}	
			var requestURL = "v1/json/UkrpWebSiteStationList"+radioplayer.stationListType;
			glow.net.loadScript(requestDomain + requestURL , opts );
	},
	
	extractIds: function(cookiedata) {
		var ids = new Array(); 
		for (var i in cookiedata) {
			var item = cookiedata[i].split(':');

			if (item[0] !== '') {
				ids.push(item[1]);
			}
		}
		return ids;
	}
};





