//  k3Gallery v0.1
//  by Nicolas Janik - http://www.k3media.com
//  2009-07-02
//
//  Licensed under the Creative Commons Attribution 3.0 License
//  http://creativecommons.org/licenses/by/3.0/
//



var k3Gallery = Class.create();  
k3Gallery.prototype = {
 
 	initialize: function(ul, options) {
	
		this.ul = $(ul);
		if(!this.ul){ return false; }
		
		if(typeof(options)=="undefined"){
			var options = {};
		}
		
		this.defaultOptions = $H({
			carousel:false,
			carouselOptions:{},
			showName:true,
 			showFullName:true,
			openInJsWin:true,
			jswinOptions:{modal:true,clearContentOnClose:true,zIndex:1300},
			jswinTemplate:"<div class='k3GalleryJsWinContainer'><div class='firstContainer'>#{media}<a href='javascript://' class='btn_gal previous_btn'>&lsaquo;</a><a href='javascript://' class='btn_gal next_btn'>&rsaquo;</a></div><div class='media_description'>#{description}</div><div class='permalinkContainer'><a href='#' class='permalink'>[ permalink ]</a></div></div>",
			videoWidth:425,
			videoHeight:344,
			videoAutoplay:true,
			videoYoutubeShowRelated:false,
			videoYoutubeEmbedTemplate:"<object width=\"#{width}\" height=\"#{height}\"><param name=\"movie\" value=\"http://www.youtube.com/v/#{youtubeid}&hl=en\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"http://www.youtube.com/v/#{youtubeid}&hl=en&autoplay=#{autoplay}&rel=#{related}\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"#{width}\" height=\"#{height}\"></embed></object>"
			
		});		
	
		this.options = this.defaultOptions.merge(options);
		
		
		this.lis = this.ul.select("li");
		if(!this.lis.length){ return false; }
		this.type = this.ul.down('li').className.split('_')[0];
		var img = this.lis[0].down('img');
		var w=img.getWidth();
		this.lis.each(function(li){
			var css = {width:w+"px"};
			if(this.options.get('carousel')){
				css.margin = 0;	
				css.border = 0;				
			}
			if(!this.options.get('showName')){
				li.down('span.spanname').hide();
			}
			li.setStyle(css);
			
		}.bind(this));
		
		
		if(this.options.get('carousel')){
			this.initCarousel();	
		}else{
		
			if(this.options.get('showFullName')){
				this.initFullName();	
			}
		}
		
		if(this.options.get('openInJsWin')){
			this.initJsWin();	
		}
		
		
		
		this.checkUrl();
		
	},
	
	
	checkUrl: function(){
		var url = document.location.href;
		if(url.include("#mediaGallery/")){
			var data = url.split("#mediaGallery/")[1].split("/");
			//check if it's the good object (because several k3Gallery can be on the same page)
			if(data[0] == this.ul.id){
				this.showJsWin(this.ul.select('li.' + data[1] + '_' + data[2])[0]);
			}
		}
	},
	
	
	
	initCarousel: function(){
		this.carousel = new k3Carousel(this.ul,$H(this.options.get('carouselOptions')).get('viewport'),this.options.get('carouselOptions'));
	},
	
	
	
	initFullName: function(){
		this.divFullName = new Element('div',{className:"k3GalleryDivFullName"});
		$$('body')[0].appendChild(this.divFullName);
		Element.observe(this.divFullName, 'click', function(e){this.showJsWin(e)}.bind(this));
		Element.observe(this.divFullName, 'mouseover', function(e){this.showFullname(e)}.bind(this));
		Element.observe(this.divFullName, 'mouseout', function(e){this.hideFullname(e)}.bind(this));
		
		this.divFullName.hide();
		
		this.lis.each(function(li){
			Element.observe(li, 'mouseover', function(e){this.showFullname(e)}.bind(this));
			Element.observe(li, 'mouseout', function(e){this.hideFullname(e)}.bind(this));
		}.bind(this));
	},
	
	
	showFullname: function(e){
		var el = Event.element(e);
		if(el == this.divFullName){
			el = this._li_showFullName_active;
		}else{
			var tag = el.tagName.toLowerCase();
			if(tag != 'li'){
				el = Event.element(e).up('li');
			}
		}	
		
		var spanName = el.down('span.spanname');
		this.divFullName.update(spanName.innerHTML);
		Element.clonePosition(this.divFullName, spanName, {setHeight:false});
		this.divFullName.show();
		this._li_showFullName_active = el;
	},
	
	hideFullname: function(e){
		this.divFullName.hide();
	},
	
	
	initJsWin: function(){
		this.jswin = new jsWin(this.options.get('jswinOptions'));
		this.template = new Template(this.options.get('jswinTemplate'));
		this.templateYoutube = new Template(this.options.get('videoYoutubeEmbedTemplate'));
		this.ul.select("li").each(function(li){
			Element.observe(li, 'click', function(e){this.showJsWin(e)}.bind(this));
			var a = li.down('a');
			a._href = a.href;
			a.href = 'javascript://';
		}.bind(this));
		
	},
	
	
	showJsWin: function(e){
		
		var flv = false;
		
		//e is an EVENT
		if(e.type){
		
			var el = Event.element(e);
			if(el == this.divFullName){
				el = this._li_showFullName_active;
			}else{
				var tag = el.tagName.toLowerCase();
				if(tag != 'li'){
					el = Event.element(e).up('li');
				}
			}	
		
		//e is an ELEMENT (li)
		}else{
			el = e;	
		}
		
		
		var mediaId = el.className.match(/[0-9]+/)[0];
		
		var data = {
			description:el.down('span.spandesc').innerHTML
		};
		

		var img = el.down('img.k3_img');

		
		if(this.type == 'image'){  //IMAGE
			var sizes = img.getAttribute("longdesc").split('#')[1].toQueryParams();
			data.media = "<img width='"+ sizes.w + "' height='" + sizes.h + "' src='" + el.down('a')._href + "' />";
			
			
		}else{ //VIDEO
			
			var videoData = {};
			
			videoData.width = this.options.get('videoWidth');
			videoData.height = this.options.get('videoHeight');
			
			//check video type
			if(el.hasClassName('VideoYoutube')){ //youtube

				videoData.autoplay = this.options.get('videoAutoplay') ? '1' : '0';
				videoData.related = this.options.get('videoYoutubeShowRelated') ? '1' : '0';
				videoData.youtubeid = img.getAttribute("longdesc").split('?v=')[1];
				data.media = this.templateYoutube.evaluate(videoData);
				
			}else{ //flv
				videoData.autoplay = this.options.get('videoAutoplay') ? 'true' : 'false';
				data.media = "<div style='height:"+ videoData.height +"px' id='flvplayer_" + this.ul.id + "'></div>";	
				flv = true;
			}
			
		}
		
		this._currentLi = el;
		
		this.jswin.setTitle(el.down('img').title);
		var html = this.template.evaluate(data);
		this.jswin.setContent(html);
		
		x=this.jswin.contentDomElement();
		
		var btnPrev = this.jswin.contentDomElement().select('a.previous_btn')[0];
		var btnNext = this.jswin.contentDomElement().select('a.next_btn')[0];
		
		if(this.prevLi()){
			btnPrev.observe('click', this.prev.bindAsEventListener(this));
		}else{
			btnPrev.hide();
		}
		
		if(this.nextLi()){
			btnNext.observe('click', this.next.bindAsEventListener(this));
		}else{
			btnNext.hide();	
		}
		
		
		var permalinkBtn = this.jswin.contentDomElement().select('a.permalink');
		if(permalinkBtn.length){
			permalinkBtn = permalinkBtn[0];
			var url = document.location.href;
			if(url.include("#")){
				url = url.split("#")[0];
			}
			permalinkBtn.href = url + "#mediaGallery/" + this.ul.id + "/" + this.type + "/" + mediaId + "/" + img.title;
		}
		
		
		if(!this.jswin.isVisible()){
			this.jswin.show();
		}
		
		if(flv){
			
			var swfPlayer = img.getAttribute("longdesc").split('/uploads/videos/')[0] + "/k3soft/swf/mpw_player.swf";
			
			var so = new SWFObject(swfPlayer, 'swfplayer', videoData.width, videoData.height, '9', '#000000');
			so.addVariable('flv', img.getAttribute("longdesc") );
			so.addVariable('autoplay', videoData.autoplay);
			so.addParam('allowFullScreen','true');
			so.write('flvplayer_' + this.ul.id);
		}
		
		
	},
	
	next: function(){
		this.showJsWin(this.nextLi());
	},
	
	prev: function(){
		this.showJsWin(this.prevLi());	
	},
	
	
	nextLi: function(liSrc){
		if(typeof(liSrc=='undefined')){
			liSrc = this._currentLi;	
		}
		return liSrc.next('li');
	},
	
	prevLi: function(liSrc){
		if(typeof(liSrc=='undefined')){
			liSrc = this._currentLi;	
		}		
		return liSrc.previous('li');	
	}
	
	
}



	

