////////////////////// MMJS.SlideAlbum /////////////////////
// Version : 0.1.1
// Require : jQuery 1.4.2 , MMJS.PicSlide
// Author : Albert.Lee [at] Mediaman.com.cn
// Last Modify : 2010.09.13
//////////////////////////////////////////////////////////

var MMJS = MMJS || {};

MMJS.SlideAlbum =  function( args ){
	//init args
	args = args || {};

	var args_default = {
		//selectors
		ds_selector: '#slideAlbum_ds',  //data source selector
		img_con_selector: '#slideAlbum_img_con',  //image container selector
		btn_prev_selector: '#slideAlbum_prev',  //[PREV] button selector
		btn_next_selector: '#slideAlbum_next',  //[NEXT] button selector
		btn_play_selector: '#slideAlbum_play',  //[PLAY] button selector
		btn_stop_selector: '#slideAlbum_stop',  //[STOP] button selector
		btn_pause_selector: '#slideAlbum_pause',  //[PAUSE] button selector
		//data getter
		get_src : function(){ return this.find('a').first().attr('href') },
		get_alt : function(){ return this.find('img').first().attr('alt') },
		get_title : function(){ return this.find('a').first().attr('title') },
		get_trans : function(){ return this.find('a').first().attr('rel') },
		//data default
		default_src : '',
		default_alt : '',
		default_title : '',
		default_trans : 'fade',
		//init settings
		auto_start : false,			//the slide will be start after the page is loaded.
		btn_nav_fade : false,		//if true ,the [PREV] and [NEXT] will show and hide with fadeIn/fadeOut animation.
		btn_nav_perm_show : true,	//permanently show the [PREV] and [NEXT] btns
		//interval time
		interval: 3, //global interval in seconds
		//transaction flash var
		flash_t_p1: 50,	// flash parse1 in microseconds
		flash_t_p2: 400,	// flash parse2 in microseconds
		//transaction fade var
		fade_t_p1: 'slow',
		fade_t_p2: 'slow',
		//callbacks
		callback_goto:function(){},
		//nav
		nav_ul_selector:'',
		nav_active_item:null,
		nav_active_class_name:'active',
		// enable inArea ( like click left | right side to toggle the prev | next picture )
		enable_inArea_action:true
	};
	
	args = jQuery.extend( args_default,args );
	jQuery.extend( this,args );
	
	this.init();

}


MMJS.SlideAlbum.prototype.init = function(){
	var class_obj = this;
		
	this.$ds = jQuery(this.ds_selector);
	this.$img_con = jQuery(this.img_con_selector);
	this.$btn_prev = jQuery(this.btn_prev_selector);
	this.$btn_next = jQuery(this.btn_next_selector);
	this.$btn_play = jQuery(this.btn_play_selector);
	this.$btn_stop = jQuery(this.btn_stop_selector);
	this.$btn_pause = jQuery(this.btn_pause_selector);
		
	this.list = [];
	
	this.$ds.children().each( function(index){
		var $this_obj = jQuery(this);
		$this_obj.click( function(){
			class_obj.goto(index);
			return false;
		} ).focus( function(){
			this.blur();
		} );
		$this_obj.find('a').focus( function(){
			this.blur();
		} );
		
		
		class_obj.list.push({
			'pic':class_obj.get_src.apply($this_obj)||class_obj.default_src,
			'alt':class_obj.get_alt.apply($this_obj)||class_obj.default_alt,
			'title':class_obj.get_title.apply($this_obj)||class_obj.default_title,
			'trans':class_obj.get_trans.apply($this_obj)||class_obj.default_trans
		});
	} );
	
	this.picSlide = new MMJS.PicSlide( {
		'container':this.img_con_selector,
		'list':this.list,
		'interval':this.interval,
		//nav
		'nav_ul_selector':this.nav_ul_selector,
		'nav_active_item':this.nav_active_item,
		'nav_active_class_name':this.nav_active_class_name
	});
	
	this.picSlide.stop();
	
	if ( this.enable_inArea_action ){
	
		this.$img_con.MMJS_inAreaHover( 
			function(){
				class_obj.mouse_in_area = true;
				class_obj.cur_dir = '';
				class_obj.stop();
			},
			function(){
				class_obj.mouse_in_area = false;
				class_obj.hideBtn( 'prev' );
				class_obj.hideBtn( 'next' );
				if ( class_obj.play_btn_checked ){
					class_obj.play();
				}
			}
		)
		
		this.$img_con.MMJS_inAreaMove( function(evt,coord){
			if ( class_obj.mouse_in_area ){
				class_obj.img_con_w = class_obj.$img_con.width();
				class_obj.img_con_offset = class_obj.$img_con.offset();
				class_obj.img_com_mid_line_x = class_obj.img_con_offset.left + ( class_obj.img_con_w/2 );
				
				if ( evt.pageX > class_obj.img_com_mid_line_x ){
					if ( class_obj.cur_dir != 'right' ){
						class_obj.showBtn( 'next' );
						class_obj.hideBtn( 'prev' );
					}
					class_obj.cur_dir = 'right';
				}else{
					if ( class_obj.cur_dir != 'left' ){
						class_obj.showBtn( 'prev' );
						class_obj.hideBtn( 'next' );			
					}
					class_obj.cur_dir = 'left';
				}
			}
		} );
	
		this.$img_con.MMJS_inAreaClick( function(evt){
			if ( class_obj.mouse_in_area ){
				if( class_obj.cur_dir == 'left' ){
					class_obj.prev();
				}else if( class_obj.cur_dir == 'right' ){
					class_obj.next();
				}
			}
		} );
	
	}
	
	this.$btn_prev.click( function(evt){
		class_obj.prev();
	} ).focus( function(){
		this.blur();
	} );;

	this.$btn_next.click( function(evt){
		class_obj.next();
	} ).focus( function(){
		this.blur();
	} );;

	this.$btn_play.click( function(evt){
		class_obj.play_btn_checked = true;
		class_obj.play();
	} ).focus( function(){
		this.blur();
	} );;

	this.$btn_pause.click( function(evt){
		class_obj.play_btn_checked = false;
		class_obj.stop();
	} ).focus( function(){
		this.blur();
	} );;

	this.$btn_stop.click( function(evt){
		class_obj.play_btn_checked = false;
		class_obj.stop();
	} ).focus( function(){
		this.blur();
	} );;
	
	
	if ( this.list.length <= 1 ){
		this.$btn_prev.hide();
		this.$btn_next.hide();
		this.$btn_play.hide();
		this.$btn_stop.hide();
		this.$btn_pause.hide();
		this.$img_con.unbind('mouseover');
		this.$img_con.css( {'cursor':'auto'} );
	}else{
		//autostart
		if ( this.auto_start ){
			class_obj.play_btn_checked = true;
			class_obj.play();		
		}else{
			class_obj.stop();	
		}
		//btn_nav_perm_show
		if ( this.btn_nav_perm_show ){
			class_obj.$btn_prev.show();
			class_obj.$btn_next.show();			
		}else{
			class_obj.$btn_prev.hide();
			class_obj.$btn_next.hide();		
		}
	}
	
}


MMJS.SlideAlbum.prototype.showBtn = function( btn_type ){
	switch( btn_type ){
		case 'prev':
			if ( !this.btn_nav_perm_show ){
				if (  this.btn_nav_fade ){
					this.$btn_prev.fadeIn( 'fast' );
				}else{
					this.$btn_prev.show();
				}
			}
			break;
		case 'next':
			if ( !this.btn_nav_perm_show ){
				if (  this.btn_nav_fade ){
					this.$btn_next.fadeIn( 'fast' );
				}else{
					this.$btn_next.show();
				}
			}
			break;
		case 'play':
			this.$btn_play.show();
			break;
		case 'stop':
			this.$btn_stop.show();
			break;
		case 'pause':
			this.$btn_pause.show();
			break;
	}
}

MMJS.SlideAlbum.prototype.hideBtn = function( btn_type ){
	switch( btn_type ){
		case 'prev':
			if ( !this.btn_nav_perm_show ){
				if (  this.btn_nav_fade ){
					this.$btn_prev.fadeOut( 'fast' );
				}else{
					this.$btn_prev.hide();
				}
			}
			break;
		case 'next':
			if ( !this.btn_nav_perm_show ){
				if (  this.btn_nav_fade ){
					this.$btn_next.fadeOut( 'fast' );
				}else{
					this.$btn_next.hide();
				}
			}
			break;
		case 'play':
			this.$btn_play.hide();
			break;
		case 'stop':
			this.$btn_stop.hide();
			break;
		case 'pause':
			this.$btn_pause.hide();
			break;
	}
} 


MMJS.SlideAlbum.prototype.play = function(){
	this.hideBtn( 'play' );
	this.showBtn( 'pause' );
	this.picSlide.play(this.callback_goto);
}

MMJS.SlideAlbum.prototype.stop = function(){
	this.hideBtn( 'pause' );
	this.showBtn( 'play' );
	this.picSlide.stop();	
}

MMJS.SlideAlbum.prototype.next = function(){
	this.picSlide.next(this.callback_goto);
}

MMJS.SlideAlbum.prototype.prev = function(){
	this.picSlide.prev(this.callback_goto);	
}

MMJS.SlideAlbum.prototype.goto = function( index ){
	this.picSlide.play();
	this.picSlide.goto(index,this.callback_goto);
}
