/*
 * BannerSlider script
 */

//alert('ok');

$(document).ready(function(){

	var slider = $('#banner_slider');
	
	slider.fadeIn('slow');
	//slider.slideDown('slow');

	$(document).everyTime("5s", function() {
		$('#banner_slider').getMovingBoxes().goForward();
	});

	$('#banner_slider').movingBoxes({
	
		panelWidth   : 0.5,       // current panel width adjusted to 50% of overall width
		reducedSize  : 0.7,       // non-current panel size: 80% of panel size
		fixedHeight  : false,     // if true, slider height set to max panel height; if false, slider height will auto adjust.

		// Behaviour
		speed        : 2000,       // animation time in milliseconds
		hashTags     : false,      // if true, hash tags are enabled
		//wrap         : false,     // if true, the panel will "wrap" (it really rewinds/fast forwards) at the ends
		//buildNav     : false,     // if true, navigation links will be added
		//navFormatter : null,      // function which returns the navigation text for each panel
		//easing       : 'swing',   // anything other than "linear" or "swing" requires the easing plugin
		
		startPanel   : 3,      // start with this panel
		width        : 500,    // overall width of movingBoxes (not including navigation arrows)
		wrap         : true,   // if true, the panel will "wrap" (it really rewinds/fast forwards) at the ends
		buildNav     : false,   // if true, navigation links will be added
		navFormatter : function(panelIndex){
			// function which returns the navigation text for each panel
			//return "&#9679;";
			
			var currentPanel = $('#banner_slider').data('movingBoxes').currentPanel();
			return $('#banner_slider')
				.find('a#link_wrapper_'+panelIndex)
				.attr('title');
			
		}
		
	});

	len = slider.getMovingBoxes().totalPanels;
	//alert(len);
	for (var i=1; i <= len; i++) {
		slider
		.find('a#link_wrapper_'+i)
		.click(function() {
		
			// attach Google Analytics page tracker
			_gaq.push(['_trackEvent', 'IndexSlider', 'Click', $(this).attr('tracker_id')]);
			//alert('clicked: '+$(this).attr('tracker_id'));
			
		});
	}

	// Examples of how to move the panel from outside the plugin.
	// get (all 3 examples do exactly the same thing):
	//  var currentPanel = $('#slider-one').data('movingBoxes').currentPanel(); // returns # of currently selected/enlarged panel
	//  var currentPanel = $('#slider-one').data('movingBoxes').curPanel; // get the current panel number directly
	//  var currentPanel = $('#slider-one').getMovingBoxes().curPanel; // use internal function to return the object (essentially the same as the line above)

	// set (all 4 examples do exactly the same thing):
	//  var currentPanel = $('#slider-one').data('movingBoxes').currentPanel(2, function(){ alert('done!'); }); // returns and scrolls to 2nd panel
	//  var currentPanel = $('#slider-one').getMovingBoxes().currentPanel(2, function(){ alert('done!'); }); // just like the line above
	//  var currentPanel = $('#slider-one').movingBoxes(2, function(){ alert('done!'); }); // scrolls to 2nd panel, runs callback & returns 2.
	//  var currentPanel = $('#slider-one').getMovingBoxes().change(2, function(){ alert('done!'); }); // internal change function is the main function

	// Set up demo external navigation links
	// could also set len = $('#slider-one').getMovingBoxes().totalPanels;
	
	/*
	var i, t = '', len = $('#banner_slider .mb-panel').length + 1;
	for ( i=1; i<len; i++ ){
		t += '<a href="#" rel="' + i + '">' + i + '</a> ';
	}
	$('.dlinks')
		.find('span').html(t).end()
		.find('a').click(function(){
			slider.movingBoxes( $(this).attr('rel') );
			return false;
		});
	*/

	// Report events to firebug console
	$('.mb-slider').bind('initialized.movingBoxes initChange.movingBoxes beforeAnimation.movingBoxes completed.movingBoxes',function(e, slider, tar){
		// show object ID + event in the firebug console
		// namespaced events: e.g. e.type = "completed", e.namespace = "movingBoxes"
		if (window.console && window.console.firebug){
			var txt = slider.$el[0].id + ': ' + e.type + ', now on panel #' + slider.curPanel + ', targeted panel is ' + tar;
			console.debug( txt );
		}
	});

});
