/*
______ _____ _____  _   _____________ _________________
| ___ \  _  /  __ \| | / /_   _| ___ \  ___|  ___|  ___|
| |_/ / | | | /  \/| |/ /  | | | |_/ / |__ | |_  | |_
|    /| | | | |    |    \  | | |    /|  __||  _| |  _|
| |\ \\ \_/ / \__/\| |\  \ | | | |\ \| |___| |   | |
\_| \_|\___/ \____/\_| \_/ \_/ \_| \_\____/\_|   \_|

*/
(function($) {
var html5 = (typeof $.browser.msie !== 'undefined' && parseInt($.browser.version, 10) >= 9) ||
		(typeof $.browser.mozilla !== 'undefined' && parseFloat($.browser.version, 10) >= 1.9) ||
		(typeof $.browser.webkit !== 'undefined') || (typeof $.browser.safari !== 'undefined'),
	ie = $.browser.msie,
LIVE = function() {
var	stills = {
		interval: 5 * 1000,
		maxAge: 60,
		cams: {},
		inViewport: {},
		init: function() {
			var that = this;
			var callback = function () {
				that.cams.forEach(function(cam) {
					var html = "";
					html += '<img id="c'+cam.id+'" src="'+cam.thumb+'?'+cam.modified+'" class="thumbs" alt="'+cam.name+'" />';
					html += '<p id="c'+cam.id+'d">'+cam.desc+'</p>';
					$("#preview").append(html);
					$("#preview").find('.thumbs').last().click(function() {
						that.inViewport = cam.id - 1;
						var ccam = that.cams[that.inViewport];
						var html = ""
						html += '<div id="webcam_caption">'+ccam.desc+'</div>';
						html += '<img class="webcam" width="640" height="480" src="'+ccam.src+'?'+ccam.modified+'" rel="webcam_caption" />';
						$("#viewport").html(html);
						$("#viewport img.webcam").captify({
							animation: 'always-on',
							opacity: '0.6',
						});
					});
				},that);
				$('img.thumbs').captify({
					animation: 'always-on',
					opacity: '0.7',
					className: 'caption-small',
				});
				var cam = that.cams[1];
				that.inViewport = 1;
				$("#viewport").append('<img src="'+cam.src+'?'+cam.modified+'" class="webcam" alt="'+cam.desc+'" />')
				$('#viewport img.webcam').captify({
					animation: 'always-on',
					opacity: '0.6',
				});
				that.render();
			}
			that.getData(callback);
		},
		registerEvents: function() {
			var that = this;
			setInterval(function () {that.getData(that.render);}, this.interval);
		},
		getData: function(callback) {
			var that = this;
			$.ajax({
				url: "http://live.rocktreff.de/data.json",
				dataType: "json",
				success: function(data) {
					that.parse(data);
					if (typeof callback === "function") {
						callback.call(that);
					}
				}
			});
		},
		parse: function(data) {
			var that = this;
			that.cams = data.cams;
			for(key in that.cams) {
				var cam = that.cams[key];
				cam.live =  ((data.timestamp - cam.modified) < that.maxAge) ? true : false;
			}
		},
		render: function () {
			var that = this;
			$("#preview img").each(function(index) {
				var cam = that.cams[index];
				var src = cam.thumb + '?' +cam.modified;
				$(this).attr("src", src);
				if (cam.live) {
					$(this).siblings().addClass("live");
				} else {
					$(this).siblings().removeClass("live");
				}
			});
			$("#viewport img.webcam").each(function() {
				if (typeof that.inViewport !== 'number') return;
				var cam = that.cams[that.inViewport];
				var src = cam.src + '?' +cam.modified;
				$(this).attr("src", src);
			});
		}
	},
	stream = {
		init: function() {
		},
		registerEvents: function() {
			var that = this;
		 	$("#livestream_teaser").click(function(){that.start()});
		},
		start: function () {
			/*
			var so = new SWFObject('/flash/player.swf','mpl','640','480','9');
			so.addParam('allowfullscreen','true');
			so.addParam('allowscriptaccess','always');
			so.addParam('wmode','opaque');
			so.addVariable('file','livestream.sdp');
			so.addVariable('streamer','rtmp://stream.rocktreff.de:1935/live');
			so.addVariable('provider','rtmp')
			so.addVariable('backcolor','000000');
			so.addVariable('frontcolor','999999');
			so.addVariable('lightcolor','EEEEEE');
			so.addVariable('screencolor','000000');
			so.addVariable('controlbar','over');
			so.addVariable('playlist', 'none');
			so.addVariable('autostart','true');
			so.addVariable('icons','false');
			so.write('viewport');
			*/
			var so = new SWFObject('http://www.justin.tv/widgets/live_embed_player.swf?start_volume=0.75&amp;backgroundImage=&amp;channel=rocktreff&amp;hostname=www.justin.tv&amp;auto_play=true','jtv_flash','640','480','9');
			so.addParam('allowfullscreen','true');
			so.addParam('allowscriptaccess','always');
			so.addParam('allowNetworking', 'all');
			so.addParam('movie', 'http://www.justin.tv/widgets/live_embed_player.swf');
			so.addParam('flashvars', 'start_volume=0.75&amp;backgroundImage=&amp;channel=rocktreff&amp;hostname=www.justin.tv&amp;auto_play=true');
			so.write('viewport');
		}
	},
	imprint = {
		registerEvents: function() {
			var that = this;
			$("#imprint_link").click(function() {that.handlers.click();})
		},
		handlers: {
			click: function() {
				$("#viewport").html($("#imprint").html());
			}
		}
	};
	(function __INIT__() {
		var init = function(object) {
			var key, element;
			for (key in object) {
				if (object.hasOwnProperty(key)) {
					element = object[key];
					if (typeof element === 'function') {
						if (key.search(/(init)|(registerEvents)/) !== -1) {
							element.call(object);
						}
					} else if (typeof element === 'object') {
						if (element.hasOwnProperty('init') || element.hasOwnProperty('registerEvents')) {
							init(element);
						}
					}
				}
			}
		};
		init(stills);
		init(stream);
		init(imprint);
	})();
};

// -------------------------------
//   BOOTSTRAPPING ...
// -------------------------------
	if (typeof jQuery !== 'function') {
		throw new Error('ROCKTREFF LIVE: Missing library (jQuery not found)');}
		
	// IE & andere Hacks
	if (typeof Array.prototype.forEach !== 'function') {
		Array.prototype.forEach = function(callback,scope) {
			var k;
			for (k = 0; k < this.length; k++) {
				callback.call(scope || this, this[k]);
			}
		}
	}
	
	// Yay-ho!
	jQuery(document).ready(function() {
		setTimeout(function() {
			window.LIVE = new LIVE();
		}, 1);
	});
})(jQuery);

