// JavaScript Document

jQuery(document).ready(function(){
					   
			
			//set our accordion functionality
			jQuery(".div_toggle").click(function() {
												 
					var id = jQuery(this).attr("data-id");
					
					jQuery("#" + id + "_content").toggle("slow");
					jQuery("." + id + "_tog_but").toggle();
					jQuery("#" + id + "_ajax_holder").empty();
										
			}); //end div toggle
					   
					   
			//set iframe loader functionality
			jQuery(".best_stuff_link").click(function() {
													  
					jQuery(".loader").show();
					jQuery(".iframe_shadow").show();
					
			}); //end iframe loader
					   
			//set ajax functionality for content links
			jQuery(".content_link").click(function() {
				
				var id = jQuery(this).attr("data-id");
				var parent = jQuery(this).attr("data-parent");
				jQuery("#" + parent + "_ajax_holder").html("<img class=\"ajax_loader\" src=\"graphics/ajax-loader.gif\" />");
													
				jQuery.get("content.php", {
				id: id
				}, function (response) {
					
					jQuery("#" + parent + "_ajax_holder").html(response);
			
				}
				
				); //end jQuery call
												
													
		   }); //end ajax functionality
			
			
		//-------------------------------- begin slider js ---------------------------------------------------------------------------------------------	
			//scrollpane parts
		var scrollPane = $('.scroll-pane');
		var scrollContent = $('.scroll-content');
		
		//build slider
		var scrollbar = $(".scroll-bar").slider({
			slide:function(e, ui){
				if( scrollContent.width() > scrollPane.width() ){ scrollContent.css('margin-left', Math.round( ui.value / 100 * ( scrollPane.width() - scrollContent.width() )) + 'px'); }
				else { scrollContent.css('margin-left', 0); }
			}
		});
		
		//append icon to handle
		var handleHelper = scrollbar.find('.ui-slider-handle')
		.mousedown(function(){
			scrollbar.width( handleHelper.width() );
		})
		.mouseup(function(){
			scrollbar.width( '100%' );
		})
		.append('<span class="ui-icon ui-icon-grip-dotted-vertical"></span>')
		.wrap('<div class="ui-handle-helper-parent"></div>').parent();
		
		//change overflow to hidden now that slider handles the scrolling
		scrollPane.css('overflow','hidden');
		
		//size scrollbar and handle proportionally to scroll distance
		function sizeScrollbar(){
			var remainder = scrollContent.width() - scrollPane.width();
			var proportion = remainder / scrollContent.width();
			var handleSize = scrollPane.width() - (proportion * scrollPane.width());
			scrollbar.find('.ui-slider-handle').css({
				width: handleSize,
				'margin-left': -handleSize/2
			});
			handleHelper.width('').width( scrollbar.width() - handleSize);
		}
		
		//reset slider value based on scroll content position
		function resetValue(){
			var remainder = scrollPane.width() - scrollContent.width();
			var leftVal = scrollContent.css('margin-left') == 'auto' ? 0 : parseInt(scrollContent.css('margin-left'));
			var percentage = Math.round(leftVal / remainder * 100);
			scrollbar.slider("value", percentage);
		}
		//if the slider is 100% and window gets larger, reveal content
		function reflowContent(){
				var showing = scrollContent.width() + parseInt( scrollContent.css('margin-left') );
				var gap = scrollPane.width() - showing;
				if(gap > 0){
					scrollContent.css('margin-left', parseInt( scrollContent.css('margin-left') ) + gap);
				}
		}
		
		//change handle position on window resize
		$(window)
		.resize(function(){
				resetValue();
				sizeScrollbar();
				reflowContent();
		});
		//init scrollbar size
		setTimeout(sizeScrollbar,10);//safari wants a timeout
		
		
		$('#coda-slider').codaSlider({
     		autoSlide: true,
 	 		autoSlideInterval: 7000,
      		autoSlideStopWhenClicked: false,
			autoHeight: false,
			dynamicArrows: false,
			dynamicTabs: false,
			slideEaseDuration: 0
    });
			

}); //end document ready



