/*
	Horodecki labs: BPlayer
	file: js/jquery.bplayer.js
	encoding: UTF-8
	author: Dawid Horodecki, http://horodecki.eu
	
	File under Creative Commons license, http://creativecommons.org/licenses/by-nc-sa/3.0/
	
	Table of contents:
		BPlayer:
			Settings
			Tab
			Interval
			Play or pause button
			Tabs switch
			Start
*/


/*	BPlayer
	==================================== */

(function($) {
	$.fn.BPlayer = function(opts) {
	
		/*	Settings
			------------------------------------ */
	
		opts = $.extend({
			tabsclass: '.bp-tabs', // tabs container class name
			tabclass: '.tab', // signle tab class name
			switchclass: '.bp-switch', // switch between tabs button class name
			buttonclass: '.bp-button', // play or pause button class name
			hasswitch: 1, // is there a switch?
			hasbutton: 1, // is there a button?
			interval: 2, // interval in seconds
			play: 1 // 1 is play, 0 is pause
		}, opts);
		
		var n = opts.interval * 1000; // interval in miliseconds
		var cur = 0; // index (starts from 0) of first tab
		var play = opts.play;
		var id = '#' + $(this).attr('id'); // BPlayer id
		var cbutton = id + ' ' + opts.buttonclass;
		var cswitch = id + ' ' + opts.switchclass + ' li a';
		var ctab = id + ' ' + opts.tabsclass + ' ' + opts.tabclass;
		var tsize = $(id+' '+opts.tabsclass+' '+opts.tabclass).size() - 1; // the number of tab elements - 1
		
		/*	Tab
			------------------------------------ */
		
		function BP_Tab() {
			$(ctab + '.selected').removeClass('selected'); // hide tab
			if(opts.hasswitch == 1) {
				$(cswitch).removeClass('selected'); // change link state
			}

			$(ctab).eq(cur).addClass('selected'); // show tab
			if(opts.hasswitch == 1) {
				$(cswitch).eq(cur).addClass('selected'); // change link state
			}

			return true;
		}
		
		/*	Interval
			------------------------------------ */
		
		function BP_Interval(state) {			
			if(state == 0) {
				clearTimeout(this.timeOut);
				
				return true;
			}
			if(state == 1) {
				this.timeOut = setInterval(function() {
					if(cur < tsize) {
						cur = cur + 1;
					}
					else {
						cur = 0;
					}
					BP_Tab(cur);
				}, n);
				
				return true;
			}
		}
		
		/*	Play or pause button
			------------------------------------ */
		
		function BP_Button() {
			if(play == 0) {
				BP_Interval(0); // stop player
				$(cbutton).removeClass('play'); // turn the play or pause button to off position
			}
			else {
				BP_Interval(1); // start player
				$(cbutton).addClass('play'); // turn the play or pasue button to on position
			}
		}
		
		if(opts.hasbutton == 1) {
			$(cbutton).bind('click', function() {
				if(play == 1) {
					play = 0;
					BP_Button(); // stop player
				}
				else {
					play = 1;
					BP_Button(); // start player
				}
				return false;
			});
		}
		
		/*	Tabs switch
			------------------------------------ */
		
		$(cswitch).bind('click', function() {
			play = 0; // 0 means pause
			cur = $(cswitch).index(this); // set current tab
			
			BP_Button(); // turn the play or pause button to off position
			BP_Tab(); // show current tab
			
			return false;
		});
		
		/*	Start
			------------------------------------ */

		if(tsize > 0) {
//		if(1) {
			if(opts.hasbutton == 1) {
				$(cbutton).show(); // show play/pause button
			}
			
			BP_Interval(play); // let's go
			
		}
	};
})(jQuery);
