//Zfse.loadModule('Zfse_FoldingMedia_Image');

Zfse_FoldingMedia = function(id, media, title){

	this.id = id;
	this.media = media;
	this.title = title;
	
	this._container = null;
	this._folded = true;
	this._effect = 'fold';
	this._currentEffect = null;
	
	this.fold = function(effect){
		if(this._folded) return false;
		
		if(!effect) effect = this._effect;
		
		$(this._container).toggle(effect, this.updateTitleWidth());
		
		this._folded = true;
		this._currentEffect = effect;
		return true;
	}
	
	this.unFold = function(){
		if(!this._folded) return false;
		
		var effect = this._currentEffect || this._effect;
		
		$(this._container).toggle(effect);
		this._folded = false;
		
		return true;
	}
	
	this.setPosition = function(left, top){
		this._container.style.left = /^[0-9]*$/.exec(left) ? left+'px' : left;
		this._container.style.top = /^[0-9]*$/.exec(top) ? top+'px' : top;
	}
	
	this.isFolded = function(){
		return this._folded;
	}
	
	this.getMediaWidth = function(){
		var img = $('img',this._container).get(0);
		return img ? img.width : null;
	}
	
	this.getMediaHeight = function(){
		var img = $('img',this._container).get(0);
		return img ? img.height : null;
	}
	
	this.getOuterWidth = function(){
		return this._outerWidth;
	}
	
	this.getOuterHeight = function(){
		return this._outerHeight;
	}
	
	this.reset = function(){
		var c = this._container;
		
		this._outerWidth = $(c).width();
		this._outerHeight = $(c).height();

		this.unFold();
	}
	
	this.updateTitleWidth = function(){
		if(this.title){
			$('.zfse_foldingMediaPadder h6').css('width', this.getMediaWidth());
		}
	}
	
	this._construct = function(){
		var content;
		var c = document.createElement('div');
		c.style.position = 'absolute';
		c.style.zIndex = 1000;
		c.style.top = '-2000px';
		c.style.left = '-2000px';
		c.className = 'zfse_foldingMedia';
		c.id = this.id;
		
		var p = document.createElement('div');
		p.className = 'zfse_foldingMediaPadder';
		c.appendChild(p);

		content = this.media.render();
		if(typeof content == 'object'){
			p.appendChild(content);
		}
		else{
			p.innerHTML = content;
		}
		
		if(this.title){
			var t = document.createElement('h6');
			t.innerHTML = this.title;
			p.appendChild(t);
		}
		
		document.body.appendChild(c);
		this._container = c;
		
		var obj = this;
		var f = function(){obj.reset()};
		this.media.setOnLoad(f);
	}
	
	this._construct();
}

Zfse_FoldingMedia.OPT_EVENT_TYPE = 'eventType';

Zfse_FoldingMedia.EVENT_TYPE_CLICK = 'click';
Zfse_FoldingMedia.EVENT_TYPE_HOVER = 'hover';

Zfse_FoldingMedia._elements = new Object();

Zfse_FoldingMedia.init = function(options){
	
	var options = typeof options == 'undefined' ? new Object() : options;
	var elems = $('a.zfse_foldingMedia').get();
	var medias = new Array();
	
	var eventType = options[Zfse_FoldingMedia.OPT_EVENT_TYPE] || Zfse_FoldingMedia.EVENT_TYPE_CLICK;

	for(var id=0;id<elems.length;id++){
		var media = Zfse_FoldingMedia.createMedia(id, elems[id]);
		if(!media) continue;
		
		medias[id] = media;
		
		eval('var f = function(e){Zfse_FoldingMedia.toggleMedia(medias['+id+'], e.pageX, e.pageY);return false;}')
		
		if(eventType == 'hover'){
			$(elems[id]).bind('mouseover', f);
			$(elems[id]).bind('mouseout', f);
		}
		else{
			$(elems[id]).bind(eventType, f);
		}
	}
}

Zfse_FoldingMedia.createMedia = function(id, element){
	var title = element.title || element.name || null;
	var url = element.href || element.alt;
	var media, type;
	
	type = Zfse_FoldingMedia.getMediaTypeByUrl(url, id+'_content', title);
	if(type){
		media = new Zfse_FoldingMedia(id, type, title);
		Zfse_FoldingMedia._elements['t'+id] = media;
	}
	
	return media;
}

Zfse_FoldingMedia.getMediaTypeByUrl = function(url, id, title){
	var match = /\.([a-z0-9]+)(\?|$)/i.exec(url);
	var ext;
	
	if(match[1]){
		ext = match[1];

		switch(ext){
			case 'jpg':
			case 'jpeg':
			case 'png':
			case 'gif':
			case 'bmp':
				return new Zfse_FoldingMedia_Image(id, url, title);
			break;
			default:
				return null;
			break;
		}
		
	}
	else return null;
}

Zfse_FoldingMedia.toggleMedia = function(media, mouseX, mouseY){
	
	if(media){
		if(media.isFolded()){
			Zfse_FoldingMedia.unFold(media);
		}
		else{
			Zfse_FoldingMedia.fold(media, mouseX, mouseY);
		}
	}
}

Zfse_FoldingMedia.fold = function(media, mouseX, mouseY){
	var x = 0, y = 0;
	var offsetX = 10;
	var offsetY = 10;
	
	var marginX = 10;
	var marginY = 10;
	
	$(window).scrollTop()
	
	var dw = $(document.body).width()+$(window).scrollLeft();
	var dh = $(document.body).height()+$(window).scrollTop();
	
	var w = media.getOuterWidth();
	var h = media.getOuterHeight();
	
	if(mouseX+offsetX+w+marginX > dw){
		x = mouseX-offsetX-w-marginX;
		if(x < 0) x = marginX;
	}
	else{
		x = mouseX+offsetX;
	}
	
	if(mouseY+offsetY+h+marginY > dh){
		y = mouseY-offsetY-h-marginY;
		if(y < 0) y = marginY;
	}
	else{
		y = mouseY+offsetY;
	}
	
	media.setPosition(x, y);
	media.fold();
	
	$(document.body).bind('click.zfse_foldingMedia', function(){
		Zfse_FoldingMedia.unFoldAll();
	});
}

Zfse_FoldingMedia.unFold = function(media){
	media.unFold();
	$(document.body).unbind('click.zfse_foldingMedia');
}

Zfse_FoldingMedia.unFoldAll = function(){
	for(var key in Zfse_FoldingMedia._elements){
		Zfse_FoldingMedia.unFold(Zfse_FoldingMedia._elements[key]);
	}
}
