/**
 * Code by cubeinteractive.co.uk
 *
 * Author: Aled Bartholomew
 * Copyright: (c) 2009 cubeinteractive.co.uk
 */



/*	Console bug prevention in IE
*/if (!window['console']) {
	window['console'] = {
		log: function() {
		}
	};
}



/*	Common library
*/
var Common = {
	DEBUG: false
};
Common.isUndefined = function(v) {
	return (typeof v === 'undefined');
};
Common.isDefined = function(v){
	return !this.isUndefined(v);
};
Common.isNull = function(v) {
	return (v === null);
};
Common.isSet = function(v) {
	return !(this.isUndefined(v) || this.isNull(v));
};
Common.isEmpty = function(v) {
	if (!this.isSet(v) || v === '' || v === 0 || v === false || v === '0') {
		return true;
	}
	if (typeof v === 'object') {
		for(i in v) {
			if (typeof v[i] !== 'function') {
				return false;
			}
		}
		return true;
	}
	return false;
};
Common.isNumeric = function(v) {
	return (typeof v === 'number');
};
Common.isInteger = function(v) {
	return (this.isNumeric(v) && parseFloat(v, 10) === parseInt(v, 10));
};
Common.isFloat = function(v) {
	return (this.isNumeric(v) && parseFloat(v * 1, 10) !== parseInt(v * 1, 10));
};
Common.isBool = function(v) {
	return (typeof v === 'boolean');
};
Common.isObject = function(v) {
	if (v instanceof Array) {
		return false;
	}
	return (!this.isNull(v) && typeof v === 'object');
};
Common.isArray = function(v) {
	if (v instanceof Array) {
		return true;
	}
	return false;
};
Common.isString = function(v) {
	return (typeof v === 'string');
};
Common.isTrue = function(v) {
	return (v === true || v === 1);
};
Common.isFalse = function(v) {
	return (v == false || v === 0);
};
Common.objectLength = function(obj) {
	var count = 0;
	for (item in obj) {
		++count;
	}
	return count;
};
Common.inArray = function(needle, haystack) {
	for (key in haystack) {
		if (haystack[key] == needle) {
			return true;
		}
	}
	return false;
}
Common.getValue = function(value, def) {
	if (this.isSet(value)) {
		return value;
	}
	if (this.isSet(def)) {
		return def;
	}
	return null;
};
Common.error = function(message) {
	if (!this.isSet(this._error_messages)) {
		this._error_messages = [];
	}
	if (!this.isSet(message)) {
		return this._error_messages;
	}
	this._error_messages.push(message);
	if (this.DEBUG === true) {
		console.log('ERROR: ' + message);
	}
}
Common.warning = function(message) {
	if (!this.isSet(this._warning_messages)) {
		this._error_messages = [];
	}
	if (!this.isSet(message)) {
		return this._error_messages;
	}
	this._error_messages.push(message);
	if (this.DEBUG === true) {
		console.log('ERROR: ' + message);
	}
}
Common.log = function(message) {
	if (!this.isSet(this._log_messages)) {
		this._log_messages = [];
	}
	if (!this.isSet(message)) {
		return this._log_messages;
	}
	this._log_messages.push(message);
	if (this.DEBUG === true) {
		console.log('INFO: ' + message);
	}
};



/*	Cube library
*/
var Cube = {};
Cube.onready = function() {
	return function() {
		if (!window['jQuery']) {
			Common.error('Cube.onready() - jquery wasn\'t found');
			return;
		}
		for (var i = 0; i < arguments.length; ++i) {
			jQuery(document).ready(arguments[i]);
		}
	};
}();
Cube.elementExists = function(selector) {
	if (jQuery(selector).length) {
		if (arguments.length > 1) {
			for (var i = 1; i < arguments.length; ++i) {
				arguments[i]();
			}
		}
		return true;
	}
	return false;
};
Cube.elementNotExists = function(selector) {
	if (!jQuery(selector).length) {
		if (arguments.length > 1) {
			for (var i = 1; i < arguments.length; ++i) {
				arguments[i]();
			}
		}
		return true;
	}
	return false;
};
Cube.enableTrack = true;
Cube.track = function() {
	Cube._ga_tracker = null;
	if (Cube.enableTrack === true) {
		Cube.onready(function() {
			if (Common.isUndefined(Cube['GA_TRACK_CODE']) || Common.isEmpty(Cube['GA_TRACK_CODE']) || !Common.isString(Cube['GA_TRACK_CODE'])) {
				Common.error('Cube.track() - No track code defined');
				return;
			}
			jQuery.getScript('http://www.google-analytics.com/ga.js', function() {
				Cube._ga_tracker = _gat._getTracker(Cube.GA_TRACK_CODE);
				Cube._ga_tracker._initData();
				Cube._ga_tracker._trackPageview();
			});
		});
		return {
			event: function(page) {
				if (!Common.isNull(Cube._ga_tracker)) {
					Cube._ga_tracker._trackPageview(page);
				}
			}
		};
	} else {
		return {
			event: function() {
			}
		};
	}
}();
Cube.flash = function() {
	if (!window['swfobject']) {
		Common.error('Cube.flash() - swfobject wasn\'t found');
		return;
	}
	var _default_required_version = '9';

	return {
		setDefaultRequiredVersion: function(version) {
			Common.log('Cube.flash.setDefaultRequiredVersion');
			_default_required_version = version;
		},
		hasVersion: function(version) {
			Common.log('Cube.flash.hasVersion');
			return swfobject.hasFlashPlayerVersion(version);
		},
		canRunExpressInstall: function() {
			Common.log('Cube.flash.canRunExpressInstall');
			return this.hasVersion('6.0.65');
		},
		add: function(settings) {
			if (Common.isUndefined(settings.id)) {
				Common.error('Cube.flash.add() - settings.id is undefined');
				return;
			}
			if (Common.isUndefined(settings.src)) {
				Common.error('Cube.flash.add() - settings.src is undefined');
				return;
			}
			if (Common.isUndefined(settings.width) || !Common.isInteger(settings.width)) {
				Common.error('Cube.flash.add() - settings.width is undefined or not an integer');
				return;
			}
			if (Common.isUndefined(settings.height) || !Common.isInteger(settings.height)) {
				Common.error('Cube.flash.add() - settings.height is undefined or not an integer');
			}
			var version = Common.getValue(settings['version'], _default_required_version);
			var express_install = Common.getValue(settings['express_install'], false);
			if (!this.hasVersion(version)) {
				if (!express_install || !this.canRunExpressInstall()) {
					if (Common.isSet(settings['upgrade_message'])) {
						this.onready(function() {
							jQuery('#' + settings.id).html(settings.upgrade_message);
						});
					}
				}
				return;
			}
			var flash_vars = Common.getValue(settings['flash_vars'], {});
			var attributes = Common.getValue(settings['attributes'], {});
			attributes.id = Common.getValue(settings['id_prefix'], 'flash_') + settings.id;
			var parameters = Common.getValue(settings['parameters'], {});
			parameters.menu = Common.getValue(settings['menu'], 'false');
			parameters.allowscriptaccess = Common.getValue(parameters['allowscriptaccess'], 'always');
			parameters.allowfullscreen - Common.getValue(parameters['allowfullscreen'], 'true');
			if (Common.isTrue(Common.getValue(settings['transparent'], false))) {
				parameters.wmode = 'transparent';
			}
			Cube.onready(function() {
				swfobject.embedSWF(settings.src, settings.id, settings.width, settings.height, version, express_install, flash_vars, parameters, attributes);
			});
		}
	};
}();



/*	Site-specific functions
*/

Common.DEBUG = false;
Cube.GA_TRACK_CODE = 'UA-1894610-22';

var D6C = {};

D6C.REQUIRED_FLASH_VERSION = '9';

D6C.play = function(obj) {
	var rand = '';
	if (Common.DEBUG) {
		rand = '?' + (new Date()).getTime();
	}
	Cube.flash.add({
		id: 'player',
		src: '/static/swf/video_player.swf' + rand,
		width: 970,
		height: 680,
		flash_vars: {
			token: obj.token,
			track: obj.track
		},
		transparent: true
	});
};

D6C.track = function(code) {
	Cube.track.event(code);
};


D6C.realTimeTracking = function() {
	window['_sf_startpt'] = (new Date()).getTime();
	window['_sf_async_config'] = {uid: 1848, domain: "doublesixclub.tv"};
	Cube.onready(function() {
		jQuery.getScript('http://static.chartbeat.com/js/chartbeat.js');
	});
}();

D6C.ui = function() {
	jQuery('#content').removeClass('no-js');
	if (!Cube.flash.hasVersion(D6C.REQUIRED_FLASH_VERSION)) {
		jQuery('#content').addClass('no-flash');
		jQuery('#header').html('<a href="http://get.adobe.com/flashplayer" class="upgrade"><span class="hidden">Upgrade now</a>');
		return;
	}
	var title = jQuery('#header h1').html();
	jQuery('#content')
		.addClass('ui-ran')
		.html('<div id="player"></div>');
	Cube.flash.setDefaultRequiredVersion(D6C.REQUIRED_FLASH_VERSION);
	jQuery('#nav')
		.before('<div id="viewall"><p>View all sessions</p></div>')
		.prepend('<div id="title"><h1>' + title + '</h1></div>')
		.append('<div id="close"><img src="/static/img/close.gif" alt="close" style="cursor: pointer;" /></div>');
	jQuery('#viewall p').click(function() {
		jQuery('#nav').fadeIn('slow');
	});
	jQuery('#close img').click(function() {
		jQuery('#nav').hide();
	});
	jQuery('a.external').attr('target', '_blank');
};

Cube.onready(function() {

	D6C.ui();
});