(function($){
	var defaults = {
		status:0, //0 is close, 1 is open
		leftsign:'http://static.mytuya.com/res/img/graffities/arrow_2.gif',
		rightsign:'http://static.mytuya.com/res/img/graffities/arrow_1.gif',
		openclass : '',
		titleHtml:null,
		imagenumber:0,
		images:null,
		type:1, //1:album 2:share
		showid:0 //albumid or tubaid
	};
	$.fn.tuyashow = function(o) {
		return new $.tuyashow(this, o);
	}
	$.tuyashow = function(e, o) {
		this.options = $.extend(defaults, o);
		this.titleHtml = this.options.titleHtml;
		this.images = this.options.images;
		this.imagenumber = this.options.imagenumber;
		this.imageId = this.options.imageId;
		this.size = this.options.images.length;
		this.first = 0;
		this.last = this.size > 2?1:this.size - 1;
		this.self = $(e);
		this.openHtml = null;
		this.closeHtml = null;
		this.capacity = 0;
		this.isScroll = false;
		var c = this;
		this.funcSetupOpen = function(){c.setupOpen();};
		this.funcSetupClose = function() {c.setupClose();};
		this.funcExpand = function() {c.expand();};
		this.funcCollapse = function() {c.collapse();};
		this.funcNext = function() {c.next();};
		this.funcPre = function() {c.pre();};
		this.type = this.options.type;//which type to use 1:album 2:share
		this.showid = this.options.showid;
		if(this.options.status == 1) {
			this.setupOpen();
		}
		else {
			this.setupClose();
		}
	}
	
	var $ts = $.tuyashow;
	$ts.fn = $ts.prototype = {
        tuyashow: '0.0.1'
    };
	$ts.fn.extend = $ts.extend = $.extend;
	$ts.fn.extend({
		setupOpen:function() {
			this.self.removeClass('graone_albumtop_close');
			this.self.empty();
			var title = $('<div class="graone_albumtop"></div>');
			title.append($('<span class="graone_album_name"></span>').html(this.titleHtml));
			title.bind('click', this.funcCollapse);
			this.larrow = $('<div class="graone_album_arrow1"><a href="javascript:void 0"><img/></a></div>');
			this.rarrow = $('<div class="graone_album_arrow2"><a href="javascript:void 0"><img/></a></div>');
		
			this.larrow.find('img').attr('src', this.options.leftsign);
			this.rarrow.find('img').attr('src', this.options.rightsign);
			this.larrow.find('a').click(this.funcPre);
			this.rarrow.find('a').click(this.funcNext);
			
			var album = $('<div class="graone_album"><div class="graone_album_mask"></div></div>');
			
			this.container = $('<ul><div class="clear"></div></ul>');
			
			this.clip = $('<div class="graone_album_tenpics"></div>');
			
			var viewLink;
			var bottom;
			switch(this.type) {
				case 0:
					viewLink = $(this.titleHtml).attr('href');
					bottom = $('<div class="graone_album_namelayout">' + this.size + '张 | <a href="'+ viewLink + '" class="edit">浏览</a></div>');
					break;
				case 1:
					viewLink = '/album/view/' + this.showid + '/small_p1/';
					bottom = $('<div class="graone_album_namelayout">' + this.imagenumber + '张 | <a href="'+ viewLink + '" class="edit">浏览</a> | <a href="/res/flash/MytuyaBook.swf?id='+ this.showid +'" target="_blank" class="edit">涂书</a></div>');
					break;
				case 2:
					 viewLink = '/tuba/sharelist_' + this.showid + '_1/';
					 bottom = $('<div class="graone_album_namelayout">' + this.imagenumber + '张 | <a href="'+ viewLink + '" class="edit">浏览</a></div>');
					 break;
			}
			album.children('.graone_album_mask').append(this.clip.append(this.container));
			//var bottom = $('<div class="graone_album_namelayout">' + this.imagenumber + '张 | <a href="'+ viewLink + '" class="edit">浏览</a> | <a href="/res/flash/MytuyaBook.swf?id='+ this.showid +'" target="_blank" class="edit">涂书</a></div>');
			this.self.append(title).append(album.prepend(this.larrow).append(this.rarrow).append('<div class="clear"></div>')).append(bottom);
			this.openHtml = 'on';
			
			/*
			 * Changed by liyun for showing images except current shown image
			 * Before changed, always shows the first two images
			 */
			//var l = this.size > 2?2:this.size;
			//for(var i=0; i < l; i++)
			//	this.add(i);
			var imageIndex = this.getImageIndex(this.imageId);
			for(var i=0; i<this.images.length; i++){
				if(i != imageIndex){
					this.add(i);
				}
			}
			for(var j=0; j<imageIndex-1; j++){
				this.isScroll = false;
				this.next();
			}
		},
		setupClose:function() {
			//this.self.addClass('graone_albumtop_close');
			/*this.closeHtml = '<span class="graone_album_name">' + this.options.titleHtml + '（分享）' +  this.options.imagenumber + '张 | <a href="1#" class="edit">编辑</a></span>'; 
			this.self.append($(this.closeHtml));
			
			this.self.bind('click', this.funcExpand);*/
			var title = $('<div class="graone_albumtop_close"></div>');
			title.append($('<span class="graone_album_name"></span>').html(this.titleHtml));
			title.bind('click', this.funcExpand);
			this.self.append(title);
			this.closeHtml = 'on';
		},
		setup:function() {
			
		}, 
		collapse:function() {
			this.self.children('.graone_albumtop').hide();
			this.self.children('.graone_album').hide();
			this.self.children('.graone_album_namelayout').hide();
			if(this.closeHtml == null) {this.funcSetupClose();}
			else {
				this.self.children('.graone_albumtop_close').show();
			}
		},
		expand:function() {
			this.self.children('.graone_albumtop_close').hide();
			if(this.openHtml == null) {this.funcSetupOpen();}
			else {
				this.self.children('.graone_albumtop').show();
				this.self.children('.graone_album').show();
				this.self.children('.graone_album_namelayout').show();
			 }
		},
		next:function() {
			/*
			 * changed from 'if(this.last == (this.size - 1) || this.isScroll) return;'
			 * changed by liyun for we only load images except the current showned image
			 */
			if(this.last == (this.capacity - 1) || this.isScroll) return;
			this.isScroll = true;
			var c = this;
			this.first++;
			this.last++;
			//this.add(this.last);
			this.clip.animate({left:'-=98'}, 'slow', '', function(){c.isScroll = false;});
		}, 
		pre:function() {
			if(this.first == 0 || this.isScroll) return;
			this.isScroll = true;
			var c = this;
			this.clip.animate({left:'+=98'}, 'slow', '', function(){c.isScroll = false;});
			this.first--;
			this.last--;
		},
		add:function(i) {
			var cIndex = this.capacity - 1;
			if(i > cIndex) {
				this.container.append('<li><a href="/tuya/' + this.getImageID(this.images[i]) + '/"><img src="' + this.images[i] + '"/></a></li>');
				this.capacity++;
			}
		},
		getImageID:function(url) {
			return url.substring(url.lastIndexOf('/') + 1, url.lastIndexOf('.'));
		},
		getImageIndex:function(imageId) {
			//console.log('[images size :=]' + this.images.length);
			for(var i=0; i<this.images.length; i++)
			{
				if(this.images[i].indexOf(imageId) >=0)
				{
					return i;
				}
			}
			return -1; 
		}
	});
})(jQuery);