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 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 ids 	= links.map(getid).get().join(',');
		var content = $(ids);

		var selectEl = function(el) {
			links.removeClass('active');
			el.addClass('active');
			content.hide();
			getel(0, el).show();
		}

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

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

	$('form.twitter').submit(function() {

		var $f = $(this);
		var $s = $f.find('.status');
		var $b = $f.find('.button');
		var hashTag = $f.attr('id');

		var status = $s.val();
		if (status.length < 5) {
			alert('Please enter a Twitter status message');
			return false;
		}

		status += ' #' + hashTag;

		$s.val(status);

		if (status.length > 140) {
			alert("Sorry, tweets are limited to 140 characters");
			return false;
		}

		$f.ajaxSubmit({
			success: function(data) {
				console.log(data);
			},
			iframe: true,
			dataType: 'xml'
		});

		$b.attr('disabled', 'disabled');

		setTimeout(function() {
			$s.attr('disabled', 'disabled');
		}, 500);

		setTimeout(function() {
			$b.attr('disabled', null);
			$s.attr('disabled', null);
			$s.val('#eurovision');
		}, 10000);

		return false;
	});

	// 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;

	});
});
