var L = LINGO = {
	detect: {},
	timestamps: {}
};

// ----------------------------------------------------------------------------
// append
// ----------------------------------------------------------------------------

var append_js = function (url, ignoreResponse) {	
	var script = document.createElement('script');
	script.setAttribute('language','javascript');
	script.setAttribute('src', url);
	
	var parentNode = null;
	if(typeof document != 'undefined' && document != null) {
		var d = document;
		if(typeof d.lastChild != 'undefined' && d.lastChild != null) {
			var lc = d.lastChild;
			if(typeof lc.firstChild != 'undefined' && typeof lc.firstChild != null) {
				parentNode = lc.firstChild;
			}
		}
		else if(typeof d.body != 'undefined' && d.body != null) {
			parentNode = d.body;
		}
		else if(typeof d.head != 'undefined' && d.head != null) {			
			parentNode = d.body;
		}
	}
	
	if(typeof parentNode == 'undefined' || parentNode == null) {
		return;
	}
	
	// added in case it causes crashes
	try {
		parentNode.appendChild(script);
	}
	catch(e) {}

	// short circuit
	if(L.REMOVE_NODE == '0') {
		return script;
	}

	if(ignoreResponse) {
		setTimeout(function () {
			try {
				if(typeof script != 'undefined' && script != null &&
				 	 typeof script.parentNode != 'undefined' && script.parentNode != null)
				{
					script.parentNode.removeChild(script);
				}
			}
			catch(e) {} // ok to fail here
		}, 100000); // 5000		
	}
	
	return script;
};

// ----------------------------------------------------------------------------
// add url prefixes
// ----------------------------------------------------------------------------

var TERM_SERVER = 'http://get.lingospot.com';
L.add_term_server = function (url) {
	var newUrl = TERM_SERVER;
	if(url.charAt(0) != '/')
		newUrl += '/';
	newUrl += url;
	return newUrl;	
};

var BLOGROLL_SERVER = 'http://image.lingospot.com/image';
L.add_blogroll_server = function (url) {
	var newUrl = BLOGROLL_SERVER;
	if(url.charAt(0) != '/')
		newUrl += '/';
	newUrl += url;
	return newUrl;	
};

var URL_SERVER = 'http://get.lingospot.com';
L.add_url_server = function (url) {
	var newUrl = URL_SERVER;
	if(url.charAt(0) != '/')
		newUrl += '/';
	newUrl += url;
	return newUrl;	
};

var noop = function () {};
L.add_code_server = noop;

L.IS_DEMO = false;
L.IS_BOOKMARK = false;
L.TRACK_EVENTS = false;
L.PARAMS = '';
L.LOCAL_OFF_NAME = '';
L.AUTHOR_REGION_CLASS = 'lingo_region';
L.css = '';
L.log_error = noop;

// ----------------------------------------------------------------------------
// log_error
// ---------------------------------------------------------------------------------

L.log_error = function (data, prefix) {	
	var pairs = [];
	for(var key in data)
		pairs.push( key + '=' + data[key] );
			
	var suffix = pairs.join('&');
	
	prefix = (prefix || 'error') + '/?';
	var url = L.add_url_server(prefix + suffix);
	append_js(url);
};

// ----------------------------------------------------------------------------
// swf
// ----------------------------------------------------------------------------

if(typeof deconcept == 'undefined') var deconcept = new Object();
if(typeof deconcept.util == 'undefined') deconcept.util = new Object();
if(typeof deconcept.SWFObjectUtil == 'undefined') deconcept.SWFObjectUtil = new Object();

deconcept.SWFObject = function(swf, id, w, h, ver, c, quality, xiRedirectUrl, redirectUrl, detectKey) {
	if (!document.getElementById) { return; }
	this.DETECT_KEY = detectKey ? detectKey : 'detectflash';
	this.skipDetect = deconcept.util.getRequestParameter(this.DETECT_KEY);
	this.params = new Object();
	this.variables = new Object();
	this.attributes = new Array();
	if(swf) { this.setAttribute('swf', swf); }
	if(id) { this.setAttribute('id', id); }
	if(w) { this.setAttribute('width', w); }
	if(h) { this.setAttribute('height', h); }
	if(ver) { this.setAttribute('version', new deconcept.PlayerVersion(ver.toString().split('.'))); }
	this.installedVer = deconcept.SWFObjectUtil.getPlayerVersion();
	if (!window.opera && document.all && this.installedVer.major > 7) {
		 
		deconcept.SWFObject.doPrepUnload = true;
	}
	if(c) { this.addParam('bgcolor', c); }
	var q = quality ? quality : 'high';
	this.addParam('quality', q);
	this.setAttribute('useExpressInstall', false);
	this.setAttribute('doExpressInstall', false);
	var xir = (xiRedirectUrl) ? xiRedirectUrl : window.location;
	this.setAttribute('xiRedirectUrl', xir);
	this.setAttribute('redirectUrl', '');
	if(redirectUrl) { this.setAttribute('redirectUrl', redirectUrl); }
};

deconcept.SWFObject.prototype = {
	 
	param: function (arg1, arg2) {
		if(typeof arg1 == 'string') 
			this.addParam(arg1, arg2);
		else
			for(var name in arg1)
				this.addParam(name, arg1[name]);
	},
	
	variable: function (arg1, arg2) {
		if(typeof arg1 == 'string') 
			this.addVariable(arg1, arg2);
		else
			for(var name in arg1)
				this.addVariable(name, arg1[name]);
	},
		
	useExpressInstall: function(path) {
		this.xiSWFPath = !path ? 'expressinstall.swf' : path;
		this.setAttribute('useExpressInstall', true);
	},
	setAttribute: function(name, value){
		this.attributes[name] = value;
	},
	getAttribute: function(name){
		return this.attributes[name];
	},
	addParam: function(name, value){
		this.params[name] = value;
	},
	getParams: function(){
		return this.params;
	},
	addVariable: function(name, value){
		this.variables[name] = value;
	},
	getVariable: function(name){
		return this.variables[name];
	},
	getVariables: function(){
		return this.variables;
	},
	getVariablePairs: function(){
		var variablePairs = new Array();
		var key;
		var variables = this.getVariables();
		for(key in variables){
			variablePairs.push(key +'='+ variables[key]);
		}
		return variablePairs;
	},
	getSWFHTML: function() {
		var swfNode = '';
		if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) {  
			if (this.getAttribute('doExpressInstall')) {
				this.addVariable('MMplayerType', 'PlugIn');
				this.setAttribute('swf', this.xiSWFPath);
			}
			swfNode = '<embed type=\'application/x-shockwave-flash\' src=\''+ this.getAttribute('swf') +'\' width=\''+ this.getAttribute('width') +'\' height=\''+ this.getAttribute('height') +'\'';
			swfNode += ' id=\''+ this.getAttribute('id') +'\' name=\''+ this.getAttribute('id') +'\' ';
			var params = this.getParams();
			 for(var key in params){ swfNode += [key] +'=\''+ params[key] +'\' '; }
			var pairs = this.getVariablePairs().join('&');
			 if (pairs.length > 0){ swfNode += 'flashvars=\''+ pairs +'\''; }
			swfNode += '/>';
		} else {  
			if (this.getAttribute('doExpressInstall')) {
				this.addVariable('MMplayerType', 'ActiveX');
				this.setAttribute('swf', this.xiSWFPath);
			}
			swfNode = '<object id=\''+ this.getAttribute('id') +'\' classid=\'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\' width=\''+ this.getAttribute('width') +'\' height=\''+ this.getAttribute('height') +'\'>';
			swfNode += '<param name=\'movie\' value=\''+ this.getAttribute('swf') +'\' />';
			var params = this.getParams();
			for(var key in params) {
			 swfNode += '<param name=\''+ key +'\' value=\''+ params[key] +'\' />';
			}
			var pairs = this.getVariablePairs().join('&');
			if(pairs.length > 0) {swfNode += '<param name=\'flashvars\' value=\''+ pairs +'\' />';}
			swfNode += '</object>';
		}
		return swfNode;
	},
	
	write: function(elementId){
		if(this.getAttribute('useExpressInstall')) {

			var expressInstallReqVer = new deconcept.PlayerVersion([6,0,65]);
			if (this.installedVer.versionIsValid(expressInstallReqVer) && !this.installedVer.versionIsValid(this.getAttribute('version'))) {
				this.setAttribute('doExpressInstall', true);
				this.addVariable('MMredirectURL', escape(this.getAttribute('xiRedirectUrl')));
				document.title = document.title.slice(0, 47) + ' - Flash Player Installation';
				this.addVariable('MMdoctitle', document.title);
			}
		}
		if(this.skipDetect || this.getAttribute('doExpressInstall') || this.installedVer.versionIsValid(this.getAttribute('version'))){
			var n = (typeof elementId == 'string') ? document.getElementById(elementId) : elementId;
			n.innerHTML = this.getSWFHTML();
			return true;
		}else{
			if(this.getAttribute('redirectUrl') != '') {
				document.location.replace(this.getAttribute('redirectUrl'));
			}
		}
		return false;
	}
};

L.deconcept = deconcept;

	
