$(document).ready(function() {

	// FIXES CMS BASED PARAGRAPHS AND ADDS CORRECT FOCUS SO CODE CHANGES NOT NEEDED IN CMS SOURCE VIEW
 	$(".slide p img").unwrap();
	$(".slide img").removeClass("focus");
	$(".slide img:first-child").addClass("focus");
 
	checkLowRes();
    
	// FADES
	$('#redback').stop().fadeTo(150, 0.6);
	
    // SLIDESHOWS
    var lock, direction, context;
    
    // BUTTON CLICK
    $('.galleryButtons a').click(function(ev) {
        ev.preventDefault();
        direction = $(this).attr('title');
        context = $('#slideGallery');
        slide(context, direction);
    });
    
    // LEFT,RIGHT ARROW KEY PRESS
    $(this).keyup( function(e) {
        
        if(e.keyCode == 39) {
            e.preventDefault();
            direction = 'next';
        } else if(e.keyCode == 37) {
            e.preventDefault();
            direction = 'previous';
        }
        
        context = $('#slideGallery');
        slide(context, direction);
    });
        
	// MOVES THE SLIDE
	function slide(con, direction) {
		var distance, change,
			theOld, theNew,
			imagePool = con.find('.slide img'),
			numOfImg = imagePool.size(),
			iOfFocus = 0,
			cnt = 0,
			limit = false;

			imagePool.each(function() {
				if($(this).hasClass('focus')) iOfFocus = cnt;
				cnt++;
			});
		
		if(direction == 'next') {               
			if(iOfFocus == (numOfImg - 1)) {
				limit = true;
			} else {
				theOld = $(con).children('.slide').children('img:eq('+iOfFocus+')');
				theNew = $(con).children('.slide').children('img:eq('+(iOfFocus+1)+')');
				distance = (parseInt($(theOld).width()) + parseInt($(theNew).css('marginRight')) + 2);
				change = parseInt($(con).children('.slide').css('left')) - distance; 
				
				if(iOfFocus == (numOfImg - 2)) {
					$('.galleryButtons a[title=next]').addClass('inactive');
				}
				$('.galleryButtons a[title=previous]').removeClass('inactive');
			}
		} else if(direction == 'previous') {
			if(iOfFocus == 0) {
				limit = true;
			} else {
				theOld = $(con).children('.slide').children('img:eq('+iOfFocus+')');
				theNew = $(con).children('.slide').children('img:eq('+(iOfFocus-1)+')');
				distance = parseInt($(theNew).width()) + parseInt($(theNew).css('marginRight'));
				change = parseInt($(con).children('.slide').css('left')) + distance + 2;
				
				if(iOfFocus == 1) {
					$('.galleryButtons a[title=previous]').addClass('inactive');
				}
				$('.galleryButtons a[title=next]').removeClass('inactive');
			}
		}
		
		// STOP SLIDE
		if (!lock && !limit) {
			lock = true;
				  
			$(con).children('.slide').animate({'left':change+'px'}, 600);
			$(theOld).animate({'opacity':'0.5'}, 300, function() {
				$(this).removeClass('focus');
				$(theNew).animate({'opacity':'1'}, 300, function() {
					$(this).addClass('focus');
					lock = false;
				});
			});
		}
	}
	
	//FADES
	$('#facebook').mouseenter(function(){
	$(this).stop().fadeTo(150, 0.7); 
	}); 
	$('#facebook').mouseleave(function(){
		$(this).stop().fadeTo(150, 1); 
	});
	$('#redback').mouseenter(function(){
	$(this).stop().fadeTo(150, 1); 
	}); 
	$('#redback').mouseleave(function(){
		$(this).stop().fadeTo(150, 0.6); 
	});
	
	//CONTENT SLIDES
	$(document).ready(function(){
		aboutExpanded = false;
		contactExpanded = false;
		delay = false;
	});
	
	function aboutExpand(){
		if (delay == true) {
			$('#about_wrapper').stop().delay(750).animate({height:'600px'},500, function(){ 
				$('#about').stop().fadeTo(300, 1);
				aboutExpanded = true;
				delay = false;
			});
		}
		else {
			$('#about_wrapper').stop().animate({height:'600px'},500, function(){ 
				$('#about').stop().fadeTo(300, 1);
				aboutExpanded = true;
			});
		}
	}
	function aboutCollapse(){
		$('#about').stop().fadeTo(300, 0, function(){
			$('#about').css("display", "none");
			$('#about_wrapper').stop().animate({height:'0px'},500);	
			aboutExpanded = false;
		});
	}
	function contactExpand(){
		if (delay == true) {
			$('#contact_wrapper').stop().delay(750).animate({height:'600px'},500, function(){ 
				$('#contact').stop().fadeTo(300, 1);
				contactExpanded = true;
				delay = false;
			});
		}
		else {
			$('#contact_wrapper').stop().animate({height:'600px'},500, function(){ 
				$('#contact').stop().fadeTo(300, 1);
				contactExpanded = true;
			});
		}
	}
	function contactCollapse(){
		$('#contact').stop().fadeTo(300, 0, function(){
			$('#contact').css("display", "none");
			$('#contact_wrapper').stop().animate({height:'0px'},500);	
			contactExpanded = false;
		});
	}
	$('#contactToggle').mouseenter(function(){
		$('#sub_menu').stop().fadeTo(150, 1); 
	}); 
	$('#navigation').mouseleave(function(){
		$('#sub_menu').css("display", "none");
	}); 
	$('#aboutToggle').mouseenter(function(){
		$('#sub_menu').css("display", "none");
	}); 
	$('#homeLink').mouseenter(function(){
		$('#sub_menu').css("display", "none");
	}); 
	$('#aboutToggle').click(
		function() {
			if (contactExpanded == true) {
				contactCollapse();
				delay = true; 
				aboutExpand();		
			}
			else if (aboutExpanded == true) {
				aboutCollapse();
			}
			else {
				aboutExpand();
			}
		}
	);
	$('#contactToggle').click(
		function() {
			if (aboutExpanded == true) {
				aboutCollapse();
				delay = true; 
				contactExpand();
			}
			else if (contactExpanded == true) {
				contactCollapse();
			}
			else {
				contactExpand();
			}
		}
	);
	
	//SET GALLERY WIDTH TO NUM IMAGES AND INTRO BOX
	$(window).load(function () {
		$('.slide').each(function() {
			var totalImageWidth = 0;
			$("img", this).each(function () {
			  totalImageWidth += $(this).width() + 15;
			});
			totalImageWidth = totalImageWidth + 232;
			$('.slide').css("width", totalImageWidth);
		});
	});
	
	//CHECK HEIGHT AND RESET STYLES TO FIT PAGE
	$(window).resize(function () {
		checkLowRes();					   						   
	});
	
	function checkLowRes(){
		winH = $(window).height();
		if (winH <= 732) {
			$('#gallery').css("margin-top", "15px");
			$('#gallery').css("height", "455px");
			$('.slide').css("height", "455px");
			$('.slide img').css("height", "455px");
			$('.introBox').css("height", "418px");
			$('.galleryButtons').css("top", "-121px");
			$('.content_wrapper').css("bottom", "25px");
		}
		else {
			$('#gallery').css("margin-top", "56px");	
			$('#gallery').css("height", "536px");
			$('.slide').css("height", "536px");
			$('.slide img').css("height", "536px");
			$('.introBox').css("height", "498px");
			$('.galleryButtons').css("top", "0px");
			$('.content_wrapper').css("bottom", "90px");
		}
	}
	
	function refreshPage() {
		location.reload();	
	}
	
});
