
var TPage = Class.create({
	initialize: function(){
		this.ID=0;
	},
	
	currID: function(){ return 'ID'+this.ID; },

	nextID: function(){ return this.currID(++this.ID); },
	
	getCookie: function(c_name){
		if (document.cookie.length>0){
			c_start=document.cookie.indexOf(c_name + "=");
			if (c_start!=-1){
				c_start=c_start + c_name.length+1;
				c_end=document.cookie.indexOf(";",c_start);
				if (c_end==-1) c_end=document.cookie.length;
				return unescape(document.cookie.substring(c_start,c_end));
			}
		}
		return "";
	},

	setCookie: function(c_name,value,expiredays){
		var exdate=new Date();
		exdate.setDate(exdate.getDate()+expiredays);
		document.cookie=c_name+ "=" +escape(value)+
		((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
	},
	
	setLang: function(lang){
		//this.setCookie('lang', lang);
		//document.location='/'+lang;
	}
    	
}); 

var Page = new TPage();

var TSMGallery = Class.create({
		
	initialize: function(container, options){
		this.Options=Object.extend({pages:null, navigate:null, delay: 0, interval:0, effect:'fade', duration:1, sync:null, info_panel:null, info_format:'#{page}/#{pages}'}, options||{});	
		this.Container = container;
		this.CurrentPage = 0;
		this.inProgress = 0;
		this.Pages = this.Options.pages?this.Options.pages:container.childElements().inject([], function(pages, el){ if(el.tagName=='DIV') pages.push(el); return pages; });
		this.Navigate = this.Options.navigate?this.Options.navigate:container.childElements().inject(null, function(nav, el){ return nav?nav:(el.tagName=='UL')?el:null; });
		this.Information = this.Options.info_panel;
		if(this.Information) this.InfoFormat = this.Information.innerText?this.Information.innerText:this.Options.info_format;
		if(this.Navigate){ 
			if(this.Navigate.empty()){
				this.Navigate.insert(new Element('li').insert( new Element('a', {'class':'prev', href:'#', page:'prev'}).insert(new Element('span').update('&nbsp;'))));
				this.Pages.each(function(el, i){ this.Navigate.insert(new Element('li').insert(new Element('a', {href:'#', page:i}).insert(new Element('span').update(i+1))))}, this);
				this.Navigate.insert(new Element('li').insert( new Element('a', {'class':'next', href:'#', page:'next'}).insert(new Element('span').update('&nbsp;'))));
				this.Navigate.select('a').each( function(el, i){ el.observe('click', this.onClickEvent.bind(this)) }, this );
			};
			if(this.Pages.size()<2) this.Navigate.hide();	
		};
		if(this.Options.effect=='rail'){
			this.Container.setStyle( { width:this.Pages.inject(0, function(w, el){ return w+el.getWidth(); }) });
		};
		this.Sync = this.Options.sync;	
		this.go(0);
		this.auto.delay(this.Options.delay?this.Options.delay:this.Options.interval, this);
	},
	
	auto: function(obj){ 
		if(!obj.Options.interval) return;
		obj.next(); 
		obj.auto.delay(obj.Options.interval, obj);
	},
	 
	onClickEvent: function(event){
		this.Options.interval=0;
		var el = event.element();
		var page = el.readAttribute('page');
		if(!page){
			el = el.up();
			page = el.readAttribute('page');
		}
		el.blur(); event.stop(); 
		switch(page){
			case 'prev': this.prev(); break;
			case 'next': this.next(); break;
			default: this.go(page);
		};
	},
	
	rail: function(page){
		this.Pages.each(function(p, i){ i==page?p.addClassName('sel'):p.removeClassName('sel'); })
		var w = this.Container.up().getWidth();
		var p = this.Container.measure('left');
		var item = this.Pages[page]; iw=item.getWidth(); il=item.measure('left'); 
		var n = ((w-iw)/2 - il) - p;
		this.inProgress=1;
		new Effect.Move(this.Container, { 
			x: n, 
			duration: this.Options.duration,
			transition: Effect.Transitions.sinoidal,
			afterFinish: function(){ this.inProgress>0?this.inProgress--:0; }.bind(this)
		});
		
	},
	
	fade: function(page){
		var p1=this.Pages[this.CurrentPage];
		var p2=this.Pages[page];
		if(page != this.CurrentPage) {
			this.inProgress=2;
			p1.fade( {afterFinish: function(){ this.inProgress>0?this.inProgress--:0; }.bind(this)} );
			p2.appear( {afterFinish: function(){ this.inProgress>0?this.inProgress--:0; }.bind(this)} );
		} else
			p2.show();
		this.CurrentPage=page;
	},
	
	go: function(page){
		//if(this.inProgress) return false;
		page=Math.abs(page*1+this.Pages.size())%this.Pages.size();
		switch(this.Options.effect){
			case 'fade': this.fade(page); break;
			case 'rail': this.rail(page); break;
		};
		if(this.Navigate) this.Navigate.select('a').each( function(el){ el.readAttribute('page')==page?el.addClassName('sel'):el.removeClassName('sel'); }, this );
		if(this.Information) this.Information.update(this.InfoFormat.interpolate({page:this.CurrentPage+1, pages:this.Pages.size()}));
		if(this.Sync) this.Sync.go(page);
		return true;
	},
	
	next: function(){
		return this.go(this.CurrentPage+1);
	},
	
	prev: function(){
		return this.go(this.CurrentPage-1);
	}
	
	
});

function updatePlayer(container, file, image, options){
	var w=$(container).getAttribute('width'); w=w?w:640; // 658 
	var h=$(container).getAttribute('height'); h=h?h:w*9/16; //+35;
	var Opt = {
		'flashplayer': '/media/player/mediaplayer/player.swf',
		'skin': '/media/player/mediaplayer/skin/glow.zip',
		//'plugins': 'adtvideo',
		//'adtvideo.config': 'http://adserver.ebru.tv/adtvideo.php?loc=E-PLAYER',
		'image': image,
		'file': file.replace("&", "%26"),
		'height': h,
		'width': w,
		'autostart': true,
		'controlbar': 'bottom',
		'repeat': 'none',
		'stretching': 'uniform',
		'allowfullscreen': true,
		'allowscriptaccess': 'always',
		'wmode': 'opaque'
	};
	
	Object.extend(Opt, options||{});

	var file_part=file.split('|');
	if(file_part.size()>1)
		Object.extend(Opt, {
			'file':file_part[1].replace("&", "%26"), 
			'streamer':file_part[0]
		});

	jwplayer(container).setup(Opt);

}

function updateMediaContent(){
	Element.select(document, '[format="video"]').each(function(el){ 
		var video = el.readAttribute('video'); if(!video) return;
		var image = el.readAttribute('image');
		var options = { autostart: el.readAttribute('autostart')?true:false };
		if(!el.readAttribute('id')) el.writeAttribute({id:Page.nextID()});
		updatePlayer(el.readAttribute('id'), video, image?image+'@crop,640,360,c.jpg':'', options);
	});
}

function updateAuthorContent(){
	Element.select(document, 'ul.author').each(function(el){
		el.select('li').each(function(item){
			if(item.empty()) { item.remove(); return; }
			switch(item.readAttribute('f')){
				case 'author': item.insert({top: 'By '}); break;
				case 'copyright': item.insert({top: '&copy; '}); break;
			}
		})
	});
}

function popUp(el, win, pos){
	var p = el.cumulativeOffset();
	switch(pos){

		case 'c': 
			p.left-=parseInt(win.getStyle('width')); 	
			p.left+=el.getWidth()+10; 	
			p.top=p.top-10;
			break;
		case 'b': 
			p.top+=el.getHeight();
		default: 
	}
	//$(win).setStyle({left:p.left, top:p.top+el.getHeight()});
	$(win).setStyle({left:p.left+'px', top:p.top+'px'});
	$(win).show();
	//alert(p);
	return false;
}


Event.observe(window, 'load', updateAuthorContent);
Event.observe(window, 'load', updateMediaContent);


