/**
 * 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 cross domain communication for setting and getting third party cookie
 *
 * @param mystationsController : {Object} instance of MyStations
 *
 * @authors Cathy Bartlett <cathy.bartlett@bbc.co.uk>
 * @authors Sergejs Vaskevics <sergejs.vaskevics@ubcmedia.com>
 * 
 */

radioplayer.controllers.CrossDomain = function(mystationsController) {
	this._parentLocation = window.location.hostname.replace('www.', ''); 
	this._mystations = mystationsController;
	this._sendLoadRequest();
	this.bind();
};

radioplayer.controllers.CrossDomain.prototype = {	
	SERVER_ADDRESS: 'http://cookie.radioplayer.co.uk/cm/',
	
	load: function(cookienames) {
		this._sendMessage('load', cookienames, '');
	},
	
	save: function(cookiename, arr) {
		arr = arr.join(",");
		this._sendMessage('save', cookiename, arr);
	},
	saveVolume: function(cookieValue) {
		this._sendMessage('save', "volume", cookieValue);
	},
	_sendMessage: function(methodfn, cookiename, arr) {
		if(methodfn === "save"){
			this._sendSaveRequest(cookiename, arr);
		}else{
			this._sendLoadRequest();
		}	
	},
	_sendSaveRequest: function(cookiename, value){
		var ua = navigator.userAgent.toLowerCase();
		var isChrome = /\bchrome\b/.test(ua);
		var isSafari = !isChrome && /safari/.test(ua);
		var cookienames = ['lastplayed', 'presets', 'preferences','volume'];
		var parametres = ['lp', 'ms', 'pr','vl'];
		var saveParam; 
		var requestUrl;
		
		var opts = {};
		
		for(var i=0; i < cookienames.length; i++){ 
			if(cookienames[i] === cookiename){
				saveParam = parametres[i]; 	
			}
		} 
		requestUrl = this.SERVER_ADDRESS + saveParam + "/s";
		var tempVar = this._mystations;
		if(isSafari){
			var data = "{\""+saveParam+"\":\""+ value+"\"}";
			data = glow.data.decodeJson(data);
			glow.net.xDomainPost(requestUrl, data, opts);
		}else{
			requestUrl = requestUrl + "?"+saveParam+"="+value;
			glow.net.loadScript(requestUrl, opts );
		}
	},
	_sendLoadRequest: function(){
		requestUrl = this.SERVER_ADDRESS + "all";
		
		var opts = {
				onLoad: radioplayer.services.CrossDomain.prototype.receiveMessage,
				onError: function() {
				}
		};
		var tempVar = this._mystations;
		this._mystations.startErrorTimer();
		glow.net.loadScript(requestUrl, opts );
	}
};

