

var Popup = function() {

	this.open = function(content) {

		this.close();

		if(content == '') {
			return;
		}

		var popup_id = 'popup_container';
		var popup_overlay = 'popup_overlay';

		// binding
		var _self = this;

		// overlay
		var height = $(document).height();

		var styles = {
			'width': $(window).width() + 'px',
			'height': height + 'px',
			'top': $('#header').height() + 'px',
			'z-index': '1000',
			'opacity': '0.8'
		};
		
		var overlay = $('<div>').attr({'id': popup_overlay}).css(styles).bind('click', function() { _self.close(); });
		$('body').append(overlay);

		// container
		var offset = (window.pageYOffset) ? window.pageYOffset : (document.body.scrollTop ? document.body.scrollTop : 0) ;
		var container = $('<div>').attr({'id': popup_id}).css({
			'z-index': '2000',
			'top': offset + 50 + 'px',
			'left': ($(document).width() / 2) - 275 + 'px'
		});

		container.html(content);
		
		$('body').append(container);
	};

	this.close = function() {
		$('#popup_overlay').remove();
		$('#popup_container').remove();
	};
};


var Player = function() {

	var options = arguments[0] || {} ;

	var defaults = {
		'media': [],
		'container': false,
		'next_text': 'next',
		'previous_text': 'previous',
		'current_text': 'number',
		'video': '',
		'current': 0
	};

	for(k in defaults) {
		this[k] = (options[k]) ? options[k] : defaults[k] ;
	}

	this.initilise = function() {

		if(!this.media.length) {
			return;
		}

		if(!this.container) {
			return;
		}

		this.insert_media();
		
		if(this.media.length > 1) {
			this.build_menu();
		}
		
	};

	/*
	 * Create media
	 */
	this.insert_media = function() {

		var data = this.media[this.current];
		var html = '', img = '', a = '';
		this.clear();

		// binding
		var _self = this;

		if(data.image != '') {

			html = $('<img>').attr('src', '/assets/' + data.image);

		} else if(data.video != '') {

			img = $(this.video);
			a = $('<a>').attr('href', '#').bind('click', function() {

				var video = '<object width="425" height="344">';
				video += '<param name="movie" value="/assets/'+_self.media[_self.current].video+'&fs=1&color1=0x000000&color2=0xffffff"></param>';
				video += '<param name="allowFullScreen" value="true"></param>';
				video += '<param name="allowscriptaccess" value="always"></param>';
				video += '<embed src="/assets/'+_self.media[_self.current].video+'&fs=1&color1=0x402061&color2=0x9461ca" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed>';
				video += '</object>';

				new Popup().open(video);
				return false;
			});
			a.append(img);
			html = a;

		} else if(data.code != '') {

			img = $(this.video);
			a = $('<a>').attr('href', '#').bind('click', function() {
				new Popup().open(_self.media[_self.current].code);
				return false;
			});
			a.append(img);
			html = a;
			
		}

		this.container.append(html);

	};

	/*
	 * Create menu
	 */
	this.build_menu = function() {
		var menu = $('<div>').css('opacity', '0.8').attr('class', 'player_menu');
		
		// bindings
		var _self = this;

		// prev
		var a = $('<a>').html(this.previous_text).attr('href', '#').bind('click', function() {
			_self.previous();
			return false;
		});
		menu.append(a);

		// media
		for(var i = 0; i < this.media.length; i++) {
			a = $('<a>').html((this.current_text != 'number') ? this.current_text : (i + 1)).attr({'href': '#', 'data-i': i}).bind('click', function() {
				_self.set($(this).attr('data-i'));
				return false;
			});
			menu.append(a);
		};

		// next
		a = $('<a>').html(this.next_text).attr('href', '#').bind('click', function() {
			_self.next();
			return false;
		});
		menu.append(a);

		this.container.append(menu);
	}

	/*
	 * Menu controls
	 */
	this.next = function() {
		this.current++;

		if(this.current > (this.media.length - 1)) {
			this.current = 0;
		}

		this.insert_media();
	};
	
	this.previous = function() {
		this.current--;

		if(this.current < 0) {
			this.current = (this.media.length - 1);
		}

		this.insert_media();
	};

	this.set = function(i) {
		this.current = i;

		if(!this.media[i]) {
			return;
		}
		
		this.insert_media();
	};

	this.clear = function() {
		this.container.children('a').remove();
		this.container.children('img').remove();
	};

};