$(document).ready(function(){

	$.fn.supersized.options = {  
		startwidth: 880,  
		startheight: 500,
		vertical_center: 1,
		slideshow: 0,
		navigation: 0,
		transition: 1, //0-None, 1-Fade, 2-slide top, 3-slide right, 4-slide bottom, 5-slide left
		pause_hover: 0,
		slide_counter: 1,
		slide_captions: 1,
		slide_interval: 6000  
	};

    $('#supersize').supersized(); 
	
	$('.fancyiframe').fancybox({
			'titleShow'		: false,
			'overlayOpacity' : .7,
			'overlayColor' : '#000',
			'padding' : 0,
			'transitionIn' : 'fade', 
			'transitionOut' : 'fade',
			'speedIn'	: 900, 
			'speedOut'	: 300,
			'scrolling'	: 'no',
			'height': 537,
			'width': 900,
			'autoScale': false,
			'autoDimensions': false,
			'hideOnContentClick' : false,
			'type':'iframe',
			'onStart': function(){
				// clearInterval(slideshow_interval);
				$.paused = true;
			},
			'onComplete': function(){

				// var fancyHeight = $('#fancybox-inner').height();
				// var fancyWidth = $('#fancybox-inner').width();
				// console.log('height = ' + fancyHeight);
				// console.log('width = ' + fancyWidth);
				// 
				// if ( fancyHeight < 537 ) {
				// 	$('body').css("overflow-y", "scroll");	
				// 	console.log('height less than number ');
				// 
				// } 
				// if ( fancyWidth < 900 ) {
				// 	$('body').css("overflow-x", "scroll");		
				// 	console.log('width less than number ');
				// }

			},
			'onClosed': function(){
				$.paused = false;
				//slideshow_interval = setInterval(nextslide, 6000);  
			}
	});	
	
	
	//iframe slider animation
	var slidecount = $('.separator').length;
	var slidewidth = 680;
	var slidestart = -9998 + slidecount*slidewidth;
	$('#contentslideinner').css('marginLeft', slidestart);
	$('.separator').each(function(i){
		mli = slidestart - i*slidewidth;
		$(this).bind('click',{ml: mli} ,function(event){
			$('#contentslideinner').slideDown(1000);
			$('#contentslideinner').animate({marginLeft: event.data.ml},1500,'easeOutExpo');
			return false
		});
	
	});

	//hover for maps from black and white to color
	var newMapS=new Image();
	newMapS.src='images/salem-location-map-clr.jpg';
	var oldMapS=$('#mapSalem').attr('src');
	$('#mapSalem').hover(
		function() {
			$(this).attr('src', newMapS.src);
		},
		function() {
			$(this).attr('src', oldMapS);
	
		});
	//hover for maps from black and white to color		
	var newMapK=new Image();
	newMapK.src='images/keizer-location-map-clr.jpg';
	var oldMapK=$('#mapKeizer').attr('src');
	$('#mapKeizer').hover(
		function() {
			$(this).attr('src', newMapK.src);
		},
		function() {
			$(this).attr('src', oldMapK);
	
		}
	);
	
	jQuery(".sprite-img_loc_salem_bw, .sprite-img_loc_keizer_bw, .sprite-img_loc_woodburn_bw").hover(function() {
		jQuery(this).fadeTo("slow", 0.00);
	},function() {
		jQuery(this).fadeTo("slow", 1.00);
	});

	
	
	//display random logo image
	var imgpreload = ['images/logo-new-blue.png','images/logo-new-brown.png','images/logo-new-gray.png','images/logo-new-ltblue.png','images/logo-new-rust.png'];
	var imgarray = [];
	for (var i=0; i<imgpreload.length;i++) {
		imgarray[i] = new Image();
		imgarray[i].src = imgpreload[i];
	}
	
	var x = Math.floor(Math.random() * imgarray.length);
	$('#logo').attr('src', imgpreload[x]);
	$('#logo').fadeTo(2000, 0.9);

		
	//slide up div with extra content in slider
	var cemargin = $('#contentextra').attr('marginTop');
	$('#celink').click(function(){
		//$('#contentextra').animate({marginTop: cemargin-573},700,'easeOutExpo');
		$('#contentslideinner').slideUp(1000);
		//$('#contentextra').show();
		return false
	});
	
	$('#ceclose').click(function(){	
		//$('#contentextra').animate({marginTop: cemargin},700,'easeOutExpo');
		$('#contentslideinner').slideDown(1000);
		return false
	});
	
	//get url for iframe pages and trigger click event on nav to display correct content
	var myAnchor = '#' + document.location.hash.split('#')[1];
	myAnchor = myAnchor.replace('_dis', '');

	$(window).load(function() {
		if (document.location.hash) {
			// click the navigation item corresponding to the anchor
			$(myAnchor).click();
			$('.sidenav').append('<p id="font_resize">Font Size: <a href="#" id="font_small">A</a> <a href="#" id="font_med">A</a> <a href="#" id="font_large">A</a></p>');
			return false;
		} 
	});
	
	
	//contact form ajax post
	$('form#contactForm').submit(function(){
		var formdata = $(this).serialize();
		var hasError = false;
		$('#contact_name').removeClass('required');
		$('#contact_email').removeClass('required');
		$('#contact_email').removeClass('bad_format');
		
		if(!$('#contact_name').val()) {
			$('#contact_name').addClass('required');
			hasError = true;
		}
		if(!$('#contact_email').val()) {
			$('#contact_email').addClass('required');
			hasError = true;
		}

		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var emailaddressVal = $("#contact_email").val();
  
		if(!emailReg.test(emailaddressVal)) {
			$('#contact_email').addClass('bad_format');
			hasError = true;
		}		
		
		if(!hasError) {
			$('#contactForm').html('One moment...');
			$.post('./php/sendemail.php', formdata, function(data) {
				$('#contactForm').html('<div id="thanks">Thank You! <br /> Your request for information has been submitted. <br />We will get back to you shortly.</div>');
			});
		}
		return false;
		
	});	
	

	

	function showlinks(){
		$('.bottomnav').show();
	}

	//when all the javascript is loaded and ready to go, display nav
	//window.setTimeout(showlinks, 6000);

	//the timeout isn't neccessary anymore because there isn't a slideshow
	$('.bottomnav').show();
	
	//font resizer
	$('.sidenav').append('<p id="font_resize">Font Size: <a href="#" id="font_small">A</a> <a href="#" id="font_med">A</a> <a href="#" id="font_large">A</a></p>');
	
	$('#font_small').click(function(){	
		$('.contentbody p, .contentbody li').css('fontSize', 12);
		$('#navlist a').css('fontSize', 14);
	});
	
	$('#font_med').click(function(){	
		$('.contentbody p, .contentbody li').css('fontSize', 14);
		$('#navlist a').css('fontSize', 16);
	});

	$('#font_large').click(function(){	
		$('.contentbody p, .contentbody li').css('fontSize', 16);
		$('#navlist a').css('fontSize', 18);
	});


	//display random background image
	var imgbackground = ['images/dr-rowell.jpg', 'images/dr-neahring.jpg', 'images/dr-paluska.jpg','images/dr-stice.jpg','images/dr-warner.jpg','images/dr-harrington.jpg','images/dr-mcdowell.jpg','images/precision.jpg','images/quality.jpg','images/friendly.jpg'];
	var imgtitle = ['Dr. David P. Rowell, M.D.','Dr. Richard Neahring, M.D.','Dr. Robert M. Paluska, O.D.','Dr. Scott E. Stice, M.D.','Dr. Russell B. Warner, M.D.','Dr. John R. Harrington, O.D.','Betsy D. McDowell, O.D.','precision eye care clinic','quality eye care doctor','salem eye doctor'];

	var x = Math.floor(Math.random() * imgbackground.length);
	$('#supersize').html('<a href="#"><img src="'+imgbackground[x]+'" title="'+imgtitle[x]+'"/></a>');
	$('#supersize').supersized(); 	
	
});



