/**
 * Copyright: 
 * Filip Paczyński, Monad
 * filip.paczynski@monad.pl
 * All Rights Reserved
 */

if(window.Monad == undefined) {
	Monad = {
			globals: {}
	};
};



Monad.Filmbox = function(elem, cfg) {
	this.templates = {
		'smallVideo': {
			width: '599px',
			height: '525px',
			videoWidth: 560,
			videoHeight: 448
		},
		'fullVideo': {
			width: '934px',
			height: '474px',
			videoWidth: 920,
			videoHeight: 460
		}	
	};
	
	this.cfg = cfg;
	this.tpl = this.templates[cfg.cls];
	this.lbId = cfg.lbId || 'colorbox';
	
	this.init_elem = function(elem, cfg) {
		this.elem = elem;
		elem.filmbox = this;
		this.jelem = $j(elem);
		this.videoUrl = this.jelem.attr('href');
		this.jelem.attr('href', 'javascript:void(0)');
		this.jelem.click(this.on_elem_click);
	};
	
	this.on_elem_click = function () {
		var closeCallback = function(ctx) {
			return function() {
				ctx.lbDom.className = ctx.lbDom._prevClass;
				ctx.overlayDom.className = ctx.overlayDom._prevClass;
				ctx.player.destroy();
				$j.colorbox.init();
			};
		}(this.filmbox);
		
		var openCallback = function(ctx) {
			return function() {
				ctx.lb = $j('#colorbox');
				ctx.lbDom = ctx.lb.get(0);
				ctx.overlay = $j('#cboxOverlay');
				ctx.overlayDom = ctx.overlay.get(0);
				
				ctx.lbDom._prevClass = ctx.lbDom.className;
				ctx.lb.addClass(ctx.cfg.cls);
				
				ctx.overlayDom._prevClass = ctx.overlayDom.className;
				ctx.overlay.addClass(ctx.cfg.cls);
			};
		}(this.filmbox);
		
		var completeCallback = function(ctx) {
			return function() {
				ctx.videoContainer = document.createElement('div');
				ctx.videoContainer.id = "Monad-Filmbox-videoContainer";
				//ctx.lbDom.appendChild(ctx.videoContainer);
				document.getElementById('cboxLoadedContent').appendChild(ctx.videoContainer);
				ctx.videoContainer.style.position = 'absolute';
				
				ctx.player = new VideoLoad(ctx.videoContainer.id, ctx.videoUrl, null, ctx.videoUrl, 'poster.png', {
					hideControls: true,
					startMuted: false,
					width: ctx.tpl.videoWidth,
					height: ctx.tpl.videoHeight
				});
			};
		}(this.filmbox); 
		
		$j.colorbox({
			href: 'about:blank',
			slideshow: false, 
			rel: 'smallVideo',
			onClosed: closeCallback,
			onOpen: openCallback,
			onComplete: completeCallback,
			scalePhotos: false,
			width: this.filmbox.tpl.width,
			height: this.filmbox.tpl.height,
			initialWidth: this.filmbox.tpl.width,
			initialHeight: this.filmbox.tpl.height
		});
		
	};
	
	this.init_elem(elem, cfg);
	
};

/**
 * Monad.slideshow({
 * 	cls: 'images', # default: images, klasa ktora zostanie przypisana kontenerowi lightboxa 
 * 	elements: ['lista/urli.ext', ...],
 * 	lbId: 'id_lightboxa', # default: 'colorbox'
 * });
 */
Monad.slideshow = function(cfg) {
	if(! cfg) {
		console.error('Monad.slideshow: cfg not set');
	}
	
	if(! (cfg.elements && cfg.elements.length > 0)) {
		console.error('Monad.slideshow: cfg.elements not set or empty');
	}
	
	this.templates = {
			'images': {
					width: '741px',
					height: '551px'
			}
	}
	
	this.tpl = this.templates[cfg.cls];
	
	var $j = jQuery.noConflict();

	var cls = cfg.cls || 'images';
	var elements = cfg.elements;
	var lbId = cfg.lbId || 'colorbox';
	var lb = $j('#' + lbId);
	var lbDom = lb.get(0);
	this.lbDom = lbDom;
	
	lbDom._prevClass = lbDom.className;
	lbDom.className = cls;
	
	var ct = document.createElement('div');
	ct.style.display = 'none';
	ct.id = 'monadslideshow';
	this.ct = ct;
	
	document.body.appendChild(ct);
	lbDom._ct = ct;
	
	function buildAnchors(elems, ct) {
		var anchs = [];
		var a;
		for (var i=0; i< elems.length; i++) {
			a = document.createElement('a');
			a.setAttribute('href', elems[i]);
			ct.appendChild(a);
		}		
	}
	
	var closeCallback = function(ctx) {
		return function() {
			ctx.lbDom.className = ctx.lbDom._prevClass;
			document.body.removeChild(ctx.ct);
		};
	}(this);
	
	buildAnchors(elements, ct);
	
	$j('#'+ct.id + ' > a').colorbox({
		open: true,
		slideshow: false, 
		rel: 'group',
		onClosed: closeCallback,
		scalePhotos: false,
		width: this.tpl.width,
		height: this.tpl.height,
		initialWidth: this.tpl.width,
		initialHeight: this.tpl.height
	});
	
	lb.addClass(cls);	
};


Monad.HashManager = function() {
	this.separator = ',';
	this.assign = ':';
	this.links = [];
	
	this.parse = function() {
		var parsed = {};
		var name, value, l;
		var q = document.location.hash.replace('#', '');
		
		q = q.split(this.separator);
		for(var i in q) {
			if(q[i].search(this.assign) >= 0) {
				l = q[i].split(this.assign);
				name = l[0];
				value = l[1];
				if(name.toString().length > 0 && value.toString().length > 0) {
					parsed[name] = value;
				}
			}
		}
		
		return parsed;
	};
	
	this.serialize = function(params) {
		var serialized = "#";
		for(var name in params) {
			if(serialized.length > 1) {
				serialized += this.separator;
			}
			if(name.toString().length > 0 && params[name].toString().length > 0) {
				serialized += name + this.assign + params[name];
			}
		}
		
		document.location.hash = serialized;
	};
	
	this.updateLinks = function() {
		for(var i in this.links) {
			this.links[i].href = this.links[i].href.split('#')[0] + document.location.hash ;
		}
	};
	
	this.setParam = function (name, value) {
		var current = this.parse();
		current[name] = value;
		this.serialize(current);
		this.updateLinks();
	};
	
	this.getParam = function (name) {
		return this.parse()[name];
	};
	
	this.onload = [];
	this.evts = {'load': [], 'set': []}
	this.on = function(evt, call, ctx, extra) {
		if(!(evt in this.evts)) {
			return -1;
		}
		
		this.evts[evt].push({'call': call, 'ctx': ctx, 'extra': extra});
	};
	
	this.dispatch = function(evt) {
		for(var i in this.evts[evt]) {
			var l = this.evts[evt][i];
			l['call'].call(l['ctx'], this.parse(), l['extra']);
		}
	};
	
	this.initLinks = function() {
		this.links = $j('a[href^="http"]').get();
	};
	
	this.load = function() {
		this.initLinks();
		this.updateLinks();
		this.dispatch('load');
	};
}
