// Date: 2010-08-10 17:46
// File: jquery.brthTv.js
// The jquery plugin for slide-show widget
// All rights reserved
//

;(function($) {
	$.extend($, {brthTv:{
		//Initialises the tv.
		initTv: function( evt ){
			if($(this).attr('slideCount')<1)return;
			
			initParam=$.brthTv.initParam[$(this).attr('brthTv')];
			$(this).attr('currentSlide', 0);
			$(this).trigger('timeout');
			if($(this).attr('navBarAutoHide')=='true') 
				$(this).find('.brthTv-navBar').hide('slide', {direction:'down'}, 'slow');
		}, 
		// The timeout event. It will show/re-show the current slide
		slideTimeout: function( evt ){
			currentSlideIndex=parseInt($(this).attr('currentSlide'));
			currentSlide=$(this).find('.brthTv-slide:eq('+currentSlideIndex+')')
			
			//Hide and show
			$(this).find('.brthTv-slide:visible').hide();
			if(initParam.direction=='fadeIn')
				$(currentSlide).fadeIn();
			else
				$(currentSlide).show('slide', {direction: initParam.direction}, 'slow');

			//Timeout
			t=window.setTimeout('$("#'+$(this).attr('id')+'").trigger("next")', $(currentSlide).attr('duration'));
			$(this).attr('timerId', t);
		},
		// The mouse-enter event. It will stop the timer
		mouseEnter: function( evt ){
			brthTv=$(this);
			timeout=$(brthTv).attr('timerId');
			jQuery.brthTv.clearTimeout( timeout );
		},
		// The mouse-out event. It will restart the timer
		mouseOut: function( evt ){
			t=window.setTimeout('$("#'+$(this).attr('id')+'").trigger("next")', $(this).find('.brthTv-slide:visible').attr('duration'));
			$(this).attr('timerId', t);
		},
		// The paging button clicked event. Handling the paging 
		slideBtnClick: function( evt ){
		}, 
		// The slide clicked event. Handling the click on the slide
		handleClick: function( evt ){
			brthTv=$(this).parents('.brthTv:first');
			currentSlideId=parseInt($(brthTv).attr('currentSlide'));
			currentSlide=$(this);
		},
		// The next event. It will calculate the next slide and trigger the timeout event.
		nextSlide: function( evt ){
			//Clear the timeout
			jQuery.brthTv.clearTimeout( $(this).attr('timerId') );
			
			//Get the current-slide-index
			currentSlideIndex=parseInt($(this).attr('currentSlide'));
			
			//Calculate the next-slide-id
			slideCount=parseInt($(this).attr('slideCount'));
			currentSlideIndex++;
			if(currentSlideIndex>slideCount)currentSlideIndex=0;
			$(this).attr('currentSlide', currentSlideIndex);
			$(this).trigger('timeout');
		},
		// The back event. It will calculate the back slide and trigger the timeout event.
		backSlide: function( evt ){
			//Clear the timeout
			jQuery.brthTv.clearTimeout( $(this).attr('timerId') );
			
			//Get next and current
			currentSlideIndex=parseInt($(this).attr('currentSlide'));
			
			//Calculate the back-slide-id
			currentSlideIndex--;
			if(currentSlideIndex<0)currentSlideIndex=parseInt($(this).attr('slideCount'))-1;
			$(this).attr('currentSlide', currentSlideIndex);
			$(this).trigger('timeout');
		}
		// Clear the Timeout with related timer-id
		,clearTimeout: function( timerId ){
			if(!(timerId==null || typeof(timerId)=='undefined')){
				timeout=parseInt(timerId);
				window.clearTimeout(timerId);
			}
		}
		,tvCounter: 0
		,initParam: []
		,initTvSlide: function(data){
			initParam=$.brthTv.initParam[$(this).attr('brthTv')];
			
			//Initialises according to init param
			if(initParam!=null){
				//Init the navBar
				if(initParam.navBar!=null){
					navBar=$('<div></div>').css({width:'100%', height:initParam.navBar, position:'relative',left:'0px','z-index':10}).addClass('brthTv-paging-panel');
				}
			}
			
			//Populate the slide
			slideCounter=0;
			$(data).find('slide').each(function(){
				slideId='brthTv'+initParam.brthTv+'_slide_'+(slideCounter++);
				slide=$('<div></div>').attr({id:slideId,title:$(this).attr('title'),duration:$(this).attr('duration'),slideCount:(slideCounter-1)}).css({width:'100%',height:'100%'});
				if($(this).attr('type')=='img'){
					content=$('<img/>').attr({src:$(this).find('src').text(),alt:$(this).attr('title'),title:$(this).attr('title')}).addClass('brthTv-slide-img');
					slide.addClass('brthTv-slide');
				}else if($(this).attr('type')=='html')
					slide
						.css({width:'100%',height:'100%'})
						.attr({alt:$(this).attr('title'),title:$(this).attr('title')})
						.addClass('brthTv-slide-html')
						.html($(this).find('src').text())
					;
				else{
					content=$('<p></p>').text('Unknow type: '+$(this).attr('type'));
					slide.append(content);
				}
				slide.appendTo('.brthTv[brthTv='+initParam.brthTv+']').addClass('brthTv-slide');
			});
			$(this).attr('slideCount', $(this).find('.brthTv-slide').size());
			
			//Binding event
			$(this)
				.bind('init', 		jQuery.brthTv.initTv)
				.bind('timeout', 	jQuery.brthTv.slideTimeout)
				.bind('next', 		jQuery.brthTv.nextSlide)
				.bind('back', 		jQuery.brthTv.backSlide)
				.bind('mouseenter', jQuery.brthTv.mouseEnter)
				.bind('mouseleave', jQuery.brthTv.mouseOut)
				.find('.brthTv-slide')
					.bind('click', jQuery.brthTv.handleClick)
					.end()
				.find('.brthTv-slideBtn')
					.bind('click', jQuery.brthTv.slideBtnClick)
			;
			
			//Fire the init
			$(this).trigger('init');
		}
		
	}});
	
	// jQuery plugin definition
	$.fn.brthTv = function(params) {
		// merge default and user parameters
		params = $.extend({src:null, navBar:null, init:null, navBarAutoHide: false, direction:'down'}, params);
		
		// traverse all nodes
		this.each(function() {
			// Assign the id
			$(this).attr('brthTv', ($.brthTv.tvCounter++));
			params.brthTv=$(this).attr('brthTv');
			// Init the tv widget
			if(params.src==null || typeof(params.src)=='underfined')
				$(this).html('SRC==null');
			else{
				loadingElement=$(this).html();
				$(this).html('<div class="brthTvLoading">'+loadingElement+"</div>").addClass('brthTv');
				$.brthTv.initParam[$(this).attr('brthTv')]=params;
				jQuery.ajax({type:'GET',url:params.src,dataType:'xml',success:$.brthTv.initTvSlide,context:this});
			}
		});
		// allow jQuery chaining
		return this;
	};
})(jQuery);

