var t;


$(function(){
	if($('body').hasClass('cp-gallery'))	{
		$("a.overlay").live('click', function (e) {
			e.preventDefault();
			
			$('#overlay .contentWrap').empty();
			//$("#overlay").css({'width': 800, 'height': 540, 'background-size': '', 'left': 76});
			
			var scrWidth = $('#content').width();
			$(this).overlay({
				mask: '#3B5872', 
				start: {
				  top: 0,
				  left: 0,
				  absolute: true
				},
				fixed: false,
				api: true,
				load: true,
				top: 50,
				left: 76,
				 onBeforeLoad: function () {
					var wrap = this.getOverlay().find(".contentWrap");
					$.get(this.getTrigger().attr("href"), function(data) {
					  $('#overlay .contentWrap').html(data);
						/* if we're displaying image */
						if($('.img-wrap img').length>0) {
							$('#overlay').css({'background': 'none'});
							var width = $('.img-wrap img').attr("width");
							var height = $('.img-wrap img').attr("height");
							
							$('#overlay').css({'width': width, 'height': height});
							
							var mLeft = (scrWidth-width)/2;
							$('#overlay').css('left', mLeft);
							$('.contentWrap').css({'left': 0, 'top': 0});
						}
						/* if we're displaying video */
						else {
							$('#overlay').css({'background': 'url(http://strangeanddawson.com/workspace/img/overlay/overlay-frame.png) top left no-repeat', 'width': 800, 'height': 540, 'left': 76});
							$('.contentWrap').css({'left': 64, 'top': 59});
							
						}
						wrap.load();
					});
					
				}
			});
			
			return false;
			
		});

		//scrollpane parts
		var scrollPane = $( ".scroll-pane" );
		var	scrollContent = $( ".scroll-content" );
		
		//build slider
		var scrollbar = $( ".scroll-bar" ).slider({
			animate: true,
			slide: function( event, 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() );
			var handleSize = 40;
			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" ), 10 );
				var gap = scrollPane.width() - showing;
				if ( gap > 0 ) {
					scrollContent.css( "margin-left", parseInt( scrollContent.css( "margin-left" ), 10 ) + gap );
				}
		}
		
		//change handle position on window resize
		$( window ).resize(function() {
			resetValue();
			sizeScrollbar();
			reflowContent();
		});
		//init scrollbar size
		setTimeout( sizeScrollbar, 10 );//safari wants a timeout
	}

	if($('body').hasClass('cp-contact'))	{
		
		$( "#locations" ).accordion({ 
			header: 'h3',
			//active: false,
			collapsible: true,
			autoHeight: false
		});
		
		doMaps();
	}	
	if($('body').hasClass('cp-home'))	{
		homeNews();
		
		t = setTimeout("climbAboardAnimate()",500);
		$('a#climb-aboard').mouseover(function()	{
			clearTimeout(t);
		});
		$('a#climb-aboard').mouseout(function()	{
			t = setTimeout("climbAboardAnimate()",500);
		});
	}
	
	// contact form validation
	if($("#contact-form").length){
	
		// validation rules, messages, &c.
		$("#contact-form").validate({
			debug: false,
			rules: {
				// simple rule, converted to {required:true}
				'name': "required",
				'subject': "required",
				'message': "required",
				// compound rule
				'email': {
					required: true,
					email: true
				}
			},
			messages: {
				'name': "Please enter your name",
				'subject': "Please enter a subject",
				'message': "Please enter your message",
				'email': {
					required: "We need your email address to contact you",
					email: "Your email address must be in the format of name@domain.com"
				}
			},
			errorClass: "error",
			validClass: "success",
			success: function(label) {
				// add class to parent div
				label.parent().addClass("valid");
	
			}

		});
	
	}

});


function doMaps()	{

	var counter = 1;
	$('div.map-container').each(function()	{

		var lat = $(this).find('span.lat').html();
		var lon = $(this).find('span.long').html();
		var latlong = $(this).find('span.latlong').html();
		var splitString = latlong.split(',');
		
		var map = new GMap2(document.getElementById("map_canvas_" + counter));
		
		map.setCenter(new GLatLng(lat, lon), 16);
								
		var point = new GLatLng(splitString[0], splitString[1]);
		map.addOverlay(new GMarker(point));
		
		counter++;
	
	});
}

function climbAboardAnimate()	{
	
	$('a#climb-aboard').toggleClass('climb-aboard-on');
	t=setTimeout("climbAboardAnimate()",200);
	
	
}

function homeNews()	{
	
	$('#home-news').orbit({
		 animation: 'fade',                  // fade, horizontal-slide, vertical-slide, horizontal-push
		 animationSpeed: 800,                // how fast animtions are
		 timer: true, 			 // true or false to have the timer
		 advanceSpeed: 8000, 		 // if timer is enabled, time between transitions 
		 pauseOnHover: true, 		 // if you hover pauses the slider
		 startClockOnMouseOut: true, 	 // if clock should start on MouseOut
		 startClockOnMouseOutAfter: 1000, 	 // how long after MouseOut should the timer start again
		 directionalNav: false, 		 // manual advancing directional navs
		 captions: false, 			 // do you want captions?
		 captionAnimation: 'fade', 		 // fade, slideOpen, none
		 captionAnimationSpeed: 800, 	 // if so how quickly should they animate in
		 bullets: true,			 // true or false to activate the bullet navigation
		 bulletThumbs: false,		 // thumbnails for the bullets
		 bulletThumbLocation: '',		 // location from this file where thumbs will be
		 afterSlideChange: function(){} 	 // empty function 
	});

}
	



