//-------------------------------------------------------------------//
/* Image Handler */
//-------------------------------------------------------------------//
var is_IE = navigator.appName.indexOf('Microsoft') != -1;

var imagePlayer = Class.create();
imagePlayer.prototype = {

	//-------------------------------------------------------------------//

	initialize: function(name,cat_id) {
		this.options = Object.extend({
      user_id : 0
     ,article_id : 0
		 ,show_num : 6
		 ,images : new Array()
		 ,btn_scroll_prev : 'btn-scroll-prev'
		 ,btn_scroll_next : 'btn-scroll-next'
		 ,btn_prev : 'btn-prev'
		 ,btn_next : 'btn-next'
		 ,btn_scroll_first : 'btn-first'
		 ,btn_scroll_last : 'btn-last'
		 ,btn_orig : 'btn-orig'
		 ,first_run : true
		 ,img_container : 'image-small'
		 ,index_container : 'image-index'
		 ,image_width : 60
		 ,images_num : 0
		 ,pages : 0
		}, arguments[1] || {});

		if($(this.options.img_container)) Event.observe(this.options.img_container, 'click', this.nextImage.bindAsEventListener(this));
		if($(this.options.btn_prev)) Event.observe(this.options.btn_prev, 'click', this.prevImage.bindAsEventListener(this));
		if($(this.options.btn_next)) Event.observe(this.options.btn_next, 'click', this.nextImage.bindAsEventListener(this));
		if($(this.options.btn_scroll_prev)) Event.observe(this.options.btn_scroll_prev, 'click', this.prevScroll.bindAsEventListener(this));
		if($(this.options.btn_scroll_next)) Event.observe(this.options.btn_scroll_next, 'click', this.nextScroll.bindAsEventListener(this));
		if($(this.options.btn_scroll_first)) Event.observe(this.options.btn_scroll_first, 'click', this.scrollFirstPage.bindAsEventListener(this));
		if($(this.options.btn_scroll_last)) Event.observe(this.options.btn_scroll_last, 'click', this.scrollLastPage.bindAsEventListener(this));

		this.current_index = 0;

		if(this.options.user_id != 0) this.ajaxGetUserImages(this.current_index,this.options.show_num);
	},

	//-------------------------------------------------------------------//

	addCategory: function(cat_id) {
		this.options.cat_id = cat_id;
	},
	
	//-------------------------------------------------------------------//

	nextScroll: function() {
		var inc = this.options.show_num;

		if (this.current_page < this.options.pages) {
			this.fadeScroll(-inc);
			this.current_page++;
		}
	},
	
	//-------------------------------------------------------------------//

	prevScroll: function() {
		var inc = this.options.show_num;

		if (this.current_page > 1) {
			this.fadeScroll(inc);
			this.current_page--;;
		}
	},
	
	//-------------------------------------------------------------------//

	fadeScroll: function(num) {
		this.fadeBackground(num,'start');
	},
	
	//-------------------------------------------------------------------//

	doScroll: function(num) {
		new Effect.MoveBy($('image-thumbs'), 0, (num * this.options.image_width), { duration:'0.5',afterFinish : function(){ this.fadeBackground(0,'end'); }.bind(this) });
	},

	//-------------------------------------------------------------------//

	scrollLastPage: function() {
		if(this.current_page == this.options.pages) return;

		this.fadeScroll(-(this.options.show_num * ((this.options.pages - this.current_page))));
		this.current_page = this.options.pages;
	},

	//-------------------------------------------------------------------//

	scrollFirstPage: function() {
		if(this.current_page == 1) return;
		
		this.fadeScroll((this.options.show_num * (this.options.pages - 1)));
		this.current_page = 1;
	},

	//-------------------------------------------------------------------//

	fadeBackground: function(num,param) {

		switch(param) {
			case 'start' : Effect.Fade('image-thumbs', {to: 0.3, queue: { position:'end' , scope: "carousel" }, duration: 0.2, afterFinish : function(){ this.doScroll(num); }.bind(this)}); break;
			case 'end' : Effect.Fade('image-thumbs', {to: 1, queue: { position:'end' , scope: "carousel" }, duration: 0.2 }); break;
		}
	},

	//-------------------------------------------------------------------//

	prevImage: function() {
		if(typeof(this.options.images['SMALL'][this.current_index-1]) == 'undefined') {
			return;
		}

		this.current_index--;

		this.showSmallImage(this.current_index + 1
												,this.options.images['SMALL'][this.current_index]
												,this.options.images['ORIG'][this.current_index]['WIDTH']
												,this.options.images['ORIG'][this.current_index]['HEIGHT']);

		var page = Math.ceil((this.current_index + 1) / this.options.show_num);

		if(page < this.current_page) this.prevScroll();
	},
	
	//-------------------------------------------------------------------//

	nextImage: function() {
		if(typeof(this.options.images['SMALL'][this.current_index + 1]) == 'undefined') {
			return;
		}

		this.current_index++;

		this.showSmallImage(this.current_index + 1
											 ,this.options.images['SMALL'][this.current_index]
											 ,this.options.images['ORIG'][this.current_index]['WIDTH']
											 ,this.options.images['ORIG'][this.current_index]['HEIGHT']);

		var page = Math.ceil((this.current_index + 1) / this.options.show_num);

		if(page > this.current_page) this.nextScroll();
	},
	
	//-------------------------------------------------------------------//

	ajaxGetUserImages: function(from,to) {
		var ajax = new Ajax.Request(
											'/index.php?action=article',
											{
												method: 'post',
												parameters: '&id='+this.options.article_id+'&from='+from+'&to='+to+'&uid='+this.options.user_id+'&aid='+this.options.article_id+'&get_images=1',
												onComplete: this.ajaxGetUserImagesComplete.bindAsEventListener(this)
											}
											);
	},

	//-------------------------------------------------------------------//

	ajaxGetUserImagesComplete: function(o) {
		eval('this.options.images = ('+o.responseText+')');

		var data = {};
		var temp_images = [];

		this.options.images_num = this.options.images['THUMB'].length;
		this.options.pages = Math.ceil(this.options.images_num / this.options.show_num);

		if(this.options.first_run) {
			this.options.first_run = false;
			this.current_page = 1;

			for(i=0; i < this.options.images['THUMB'].length; i++) {
				temp_images.push({ image_thumb : this.options.images['THUMB'][i]
													,image_small : 	this.options.images['SMALL'][i]
													,image_orig_width : this.options.images['ORIG'][i]['WIDTH']
													,image_orig_height : this.options.images['ORIG'][i]['HEIGHT']
													,index : 	i+1
												});
			}

			var data = { uimages : temp_images };

			$('image-thumbs').setStyle({ width : (this.options.image_width * (this.options.images_num + 2))+'px' });
			$('image-thumbs').update(TrimPath.processDOMTemplate('t_image_player_thumbs', data));
		}
		else {
			
		}
	},

	//-------------------------------------------------------------------//

	showSmallImage: function(index,image,width,height) {
		var temp = $(this.options.index_container).innerHTML.split('/');
		var orig = image;
		orig = orig.replace('_small.jpg','_orig.jpg');

		this.current_index = index-1;

		if($(this.options.img_container).src != image) {
			$(this.options.img_container).src = image;
		}
		$(this.options.index_container).update(index+'/'+temp[1]);
		
		$(this.options.btn_orig).onclick = function(){ this.showOrigImage(orig,width,height); return false; }.bind(this);

		this.addPI();
	},

	//-------------------------------------------------------------------//

	showOrigImage: function(image,width,height) {
		if(is_IE) this.showImageInPopUpIE(image,width,height);
		else this.showImageInPopUp(image,width,height);
	},
	
	//-------------------------------------------------------------------//

	showImageInPopUpIE: function(image,width,height) {
		var popup = window.open(image,'_blank','resizable=yes, menubar=no, toolbar=no, scrollbars=yes, location=no, status=no, width='+(width+38)+',height='+(height+38)+'');
	},

	//-------------------------------------------------------------------//

	showImageInPopUp: function(image,width,height) {
		this.doWebaudit();

		var popup = window.open(
		image
		,"fnriporter-popup"
		,"innerWidth=" + (width+30) + ", innerHeight=" + (height+30) + ", resizable=yes, menubar=no, toolbar=no, scrollbars=yes, location=no, status=no"
		);
		
		return popup;
	},

	//-------------------------------------------------------------------//
	
	addPI: function() {
		this.ajaxAddPI();
	},

	//-------------------------------------------------------------------//

	ajaxAddPI: function() {
		var ajax = new Ajax.Request(
											'/riport/'+this.options.article_id,
											{
												method: 'post',
												parameters: '',
												onComplete: this.ajaxAddPIComplete.bindAsEventListener(this)
											}
											);
	},

	//-------------------------------------------------------------------//

	ajaxAddPIComplete: function(o) {
		return;
	}

}

//-------------------------------------------------------------------//
