/**
 * Azure Page Slider
 * 
 * @author	A.Surrey (Azure Design)
 */

// See scrollTo plugin for navigating to anchors! ******************************

(function($) {
	
	$.fn.slider = function(settings) {
		/**
		 * Defaults
		 */
		settings = $.extend({
			//easeFunc: 'jswing',
			//easeFunc: 'easeInExpo',
			//easeFunc: 'easeInBack',
			easeFunc: 'easeInOutExpo',
			//easeFunc: 'easeOutQuad',
			//easeFunc: 'easeOutBounce',
			//easeFunc: 'easeInSine',
			easeTime: 800,
			panelClass: '.panel',		// The panels we have
			linkClass: '.sliderLink',	// Buttons used to slide the window
			sliderId: '#slider',
			panelSpacing: 567			// Gap between the panels
		}, settings);
		
		return this.each(function() {
			var $container = $(this);
			var $containerWidth = $container.width();
			var $panels = $container.find(settings.panelClass)
			var $panelWidth = $panels.width();
			var $numPanels = $panels.length;
			var $sliderWidth = $container.find(settings.sliderId).width();
			var $buttons = $(settings.linkClass);
			var $numButtons = $buttons.length;
			
			var browserwidth = $(window).width();
			var browserheight = $(window).height();
		

			//AG:Begin -------------------------

			$(window).bind('resize', function () { setup_slides(); });
			
			setup_slides();

			/*
			Known screen sizes and bg positions against each other to a ratio
			1263 -410
							241 = 121			
											240 / 121 = 1.983471074...
			1022 -531
							61 = 30				
											61	/ 30 = 2.0333...
			961 -560		
							61 = 31			61 / 31 = 1.96774...
							
			AVG : (1.9834 + 2.0333 + 1.96774) / 3 = 1.994824333..
			*/

			function setup_slides()
			{
				browserwidth = $(window).width();
				if (browserwidth < 960)
					browserwidth = 960;
				$("#frame").css("width", browserwidth);
				first_margin = (browserwidth - 960) / 1.9948;
				$("#slider .homePanel").css("marginLeft", first_margin);				
				bgpos = 0 - ( ( (1263 - browserwidth) / 2) + 410 );
				$("#frame #slider").css("backgroundPosition", bgpos+"px 28px"); //-410
			}
			
			//AG:End -------------------------

			
			/** Debug test **/
			//azureDump(this);
			//var debug = new jQuery.debug();
			//debug.dump($test);
			//alert('screen width = ' + window.screen.width); // screen resolution
			//alert('screen width = ' + window.screen.availWidth); // screen resolution
			//var browserwidth = $(window).width();
			//var browserheight = $(window).height();

			/**
			 * Add events to each button/link
			 */
			$(settings.linkClass+':not(#usefulLink)').each(function() {
				//$(this).bind("myEvent", $data, function(evt) { //bla });
				$(this).click(function(evt){
					var $pos = 0;
					//evt.stopPropagation();
					$id = $(this).attr('rel');
					evt.preventDefault();	
					if($id == 'home') {
						$pos = 0;
					}
					else if($id == 'about') {
						$pos = -settings.panelSpacing -$panelWidth;
					}
					else if($id == 'work') {
						$pos =  2*(-settings.panelSpacing -$panelWidth);
					}
					else if($id == 'contact') {
						$pos = 3*(-settings.panelSpacing -$panelWidth);
					}
					/** Go with animation!! **/
					$container.find(settings.sliderId).animate({ left: $pos }, settings.easeTime, settings.easeFunc);
					//return false;
				});
			});							
		}); //each	
	}; // fn.slider
})(jQuery);