var octoEmbedded = false;
octoshapeVariant="ESCTV";
window.octopvDebugDiv="debug";

function thisMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}

function octoChangeChannel(value) {
	thisMovie("esctv-player").changechannel(value);
}

function showNoPluginWarning(streamName) {
	$('#center').addClass('noplugin');
	$('#instructions').show();

	var isLinux = navigator.userAgent.match(/Linux/);

	if (typeof(streamName) != "undefined") {
		if (isLinux) {
			$('#instructions').append();
		}
	} else {
		if (isLinux) {
			$('#instructions').append('<div class="linuxHelp">Plugin detection is not available on linux. Once the broadcast is about to start, you will see the streamname here which you can add to the octoshape command</div>'); 	
		}
	}
}

jQuery(document).ready(function() {
	var isLinux = navigator.userAgent.match(/Linux/);
	if (typeof(octoStream) != 'undefined' && octoStream.length > 2 && octoType == "wmv") {
		if (isLinux) {

			$('#player').html('<div class="linuxHelp">WMV streams through Octoshape do not work from inside the browser. If you have already installed the plugin, execute the following command at your command line: <pre>./OctoshapeClient -url:' + octoStream + '</pre> from the directory where you installed the Octoshape.<br /><br/>If you have not installed the Octoshape plugin, please <a href="http://www.octoshape.com/plugin/get.asp?variant=ESCTV">download and install it from octoshape.com</a></div>').addClass('help');
		} else {
			var player = octoGetEmbeddedPlayer('player', 512, 307);
			player.start(octoStream);
		}
	}

	if (isLinux) {
		$('#octo-note').append('<p>Make sure you start the plugin by executing <pre>./OctoshapeClient</pre> on the commandline (from the directory where you installed octoshape) before connecting to the stream. To play the stream outside your browser, execute the following command: <pre>./OctoshapeClient -url:' + octoStream + '</pre></div>');
	}

	if (typeof(octoChangeChannel) != 'undefined') {

		$('a.switch-stream-on').click(function() {
			octoChangeChannel('JESC.main');
			return false;
		});
		$('a.switch-stream-back').click(function() {
			octoChangeChannel('JESC.backstage');
			return false;
		});
	}

	$('#tabs').each(function() {

		var current;
		var loaded = {};
		var $tabs = $(this);
		var tabSelect = {
			
			'tab-facebook': function(is_first) {
			},
			'tab-twitter': function(is_first) {
				if (is_first) {

					// even though it loads hyper-slow, still wait until the tab is selected
					// it appears that the content needs to be visible at the moment of loading
					// kinda makes sense
					twttr.anywhere(function (T) {
							T("#tweet-box").tweetBox({
								conter: true,
								height: 65,
								width: 340,
								label: 'What\'s the buzz?',
								defaultContent: '#eurovision ',
								onTweet: function() {

								},
								data: {

								}
							});
						});
				}
			}
		};
		var tabUnselect = {
			
			'tab-facebook': function() {
			},
			'tab-twitter': function() {
				return false;
				twitter_search_widget.stop();
			}
		};

		var getid = function(num, el) {
			var h = $(el).attr('href').split('#')[1];
			return '#' + h;
		}

		var getel = function(num, el) {
			return $(getid(num, el));
		}

		var links = $(this).find('a');

		var selectEl = function(el) {
			var switchTo = getel(0, el).slideDown().attr('id');
			if (switchTo == current) {
				return true;
			}

			if (typeof(current) != 'undefined') {
				tabUnselect[current]();
			}

			$tabs.parent().find('div.tab').not('#' + switchTo).hide();
			current = switchTo;

			if (typeof(loaded[current]) == 'undefined') {
				tabSelect[current](true);
			} else {
				tabSelect[current](false);
			}

			loaded[current] = true;
		}

		links.click(function() {
			selectEl($(this));
			return false;
		});

		selectEl($(links.get(0)));
	});

	// makes 
	$('#channels li').click(function(target) {
		var link = $(this).find('a').attr('href');
		document.location = link;
		return false;
	});

	$('a.switch').click(function() {
		var s = $(this).attr('href').split(':')[1];
		octoChangeChannel(s);
		return false;
	});

	$('a.octoshape-flash').each(function() {
		if (octoEmbedded) return;
		if ($('#esctv-player').length > 0) reeturn;
		octoEmbedded = true;

	});
});

