var moving = false;
var footer = false;

function initMenu() {
  $('#menu ul').hide();
  $('#menu li.selected ul').show();
  $('#menu li a').click(
    function() {
      var checkElement = $(this).next();
      if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
        return false;
        }
      if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
        $('#menu ul:visible').slideUp('normal');
        checkElement.slideDown('normal');
        return false;
        }
      }
    );
  }
  
function setFooterStyle() {	
	footer = true;
	$('#mid_col').css({ 'background-image':'none', 'width':'700px', 'left':'-680px', 'background': '#fff' });
	$('#mid_bg').css({ 'background-image':'url("assets/images/mid_shade_large.png")', 'width':'646px' });
	$('#scrollContainer').css({ 'width':'646px' });
	$('#scrollText').css({ 'width':'646px' });
}
  
function showPreview(img, alt) {
	$('#preview_holder').html( '<img src="assets/images/gallery/thumbs/preview/'+ img +'" width="230" />');
	if (alt != "") {
		$('#preview_holder').html( $('#preview_holder').html() + '<div class="gallery_title_preview">'+ alt +'</div>');
	}
	
	var newleft =  $('.thumb_col').size() * 37 + 5;
	$('#preview_holder').css({ left: newleft });
	$('#preview_holder').stop(true, true).fadeIn("fast");

}

function hidePreview() {
	$('#preview_holder').stop(true, true).fadeOut("fast");
}

function showGallery(img) {
	$('#gallery_holder').cycle(img); 
}

function playGallery() {
	var action = $('#pauseBTN').attr("class");
	if (action == 'play') {
		$('#gallery_holder').cycle('resume', true); 
		$('#pauseBTN').removeClass('play').addClass('pause');
		$('#pauseBTN').attr("src", "assets/images/gallery_pause.png");
		$('#pauseBTN').attr("alt", "Pause Slideshow");
		$('#pauseBTN').attr("title", "Pause Slideshow");
	} else {
		$('#gallery_holder').cycle('pause');
		$('#pauseBTN').removeClass('pause').addClass('play');
		$('#pauseBTN').attr("src", "assets/images/gallery_play.png");
		$('#pauseBTN').attr("alt", "Play Slideshow");
		$('#pauseBTN').attr("title", "Play Slideshow");
	}
	$('body').supersleight(); 
}

function expandEvent(num, image, thebg, gardens) {		
	if ($('#event_'+num).height() == 53) {
		if (image != "") {
			$('#bed_gallery').hide(0);
			$('#wrapper').css({ 'background-image': 'url(assets/images/eventsbig/'+ image + ')' });
		} else {
			if (thebg == "") {
				$('#wrapper').css({ 'background-image': 'none' });
			} else {				
				$('#wrapper').css({ 'background-image': 'url(assets/images/mainbg/'+ thebg + ')' });
			}
		}
		var container = 'event_height_'+num;
		
		$('.events_content').each(function(){

			//if ($(this).attr('id') != container)  {
				$(this).animate({
					  height: 53
					}, 400 );
			  //}
	
			// START - NRS
			$(this).find('a').each(function(){
				$(this).css('display','none');
			});
			// END - NRS

		});
		
		var pos = $("#eventnum"+num).position();
		var newPos = "-"+ pos.top +"px";
		
		// START - NRS
		var event_details = $("#eventnum"+num).attr('title').split('_');
		var newPos = "-"+(event_details[0] * 95)+"px";
		if (event_details[1] == "garden") {
			var newPos = event_details[0] * 107;
			newPos = newPos + 340;
			newPos = "-"+newPos+"px";
			newPos = newPos.replace('--','-');
		}
		//var newPos = "-"+($("#eventnum"+num).attr('title') * 110)+"px";
		// END - NRS
		
		$("#scrollText").animate({ top: newPos }, 400, function() {
																
			 $('#event_'+num).find('a').each(function(){													  
					$(this).fadeIn(700);					
			});
			 
			 var newH = $('#' +container).height();	
		
			$('#event_'+num).animate({
				 height: newH
			 }, 700, function() {			
				 
				if ($("#scrollText").height() > 520) {	
					$(".upArrow").show(0);
					$(".downArrow").show(0);	
					$(".scrollTextContainer").height(480);		
				}
			});
		});	
			
	} else {
		closeEvent(num, thebg, gardens);
	}

}

function closeEvent(num, thebg, gardens) {
	
	if (thebg == "") {
			$('#wrapper').css({ 'background-image': '' });
		} else {
			if (gardens == "yes") {
				$('#wrapper').css({ 'background-image': 'none' });
			} else {
				$('#wrapper').css({ 'background-image': 'url(assets/images/mainbg/'+ thebg + ')' });
			}
		}	
	$('#bed_gallery').show(0);
	$('#event_'+num).find('a').each(function(){
		if ($(this).position().top > 53) {
			$(this).fadeOut(400);	
		}
	});
	
	$('#event_'+num).animate({
    height: 53
  }, 400, function() {
	  
   	var position = $("#scrollText").position();	
	if (($("#scrollText").height() + position.top) < 520) {
		$("#scrollText").animate({ top: 0 }, 400);
	}	
	if ($("#scrollText").height() < 520) {	
		$(".upArrow").hide(0);
		$(".downArrow").hide(0);
		$(".scrollTextContainer").height(530);		
	}	
	
  });
	
		
}

function laodBedGallery(galID) {
	$('#bed_gallery').fadeOut('fast',function() {

		$.post("assets/includes/getgallery.php", { gallery_ID: galID }, function(data){		
		$('#bed_gallery').html(data);		
		$('#wrapper').css({ 'background-image': 'none'});		
		
		$('#bed_gallery').fadeIn('slow');		
		
		$('#bed_gallery').cycle({
			fx: 'fade'
		});	
				
		 });
      });	
}

function changeFlag(country) {		
	if (country == "British") {		
		$("#flags_img").attr('alt', "British");
		$("#flags_img").attr('src', "assets/images/british.jpg");	
		$("#flags_link").attr('href', "home");
	} else {
		$("#flags_img").attr('alt', "German");
		$("#flags_img").attr('src', "assets/images/german.jpg");	
		$("#flags_link").attr('href', "german");
	}
}

var anchorDone = false;

$(document).ready(function(){
	$('#boxList').hide().delay(2000).fadeIn('slow');

						   
	$("a.brown_box").css("opacity", 0.7);					   
						   
	initMenu();	
	$('body').supersleight(); 
	
	
	
	$('#gallery_holder').cycle({
		fx: 'fade',
		prev: '#prev',
		next: '#next'
	});	
	
	var newleft =  $('.thumb_col').size() * 37 + 5;
	$('#gallery_holder').css({ left: newleft + 'px' });
	
	
	$('.event_height').each(function(){		
		$(this).find('a').each(function(){		
		if ($(this).position().top > 53) {
			$(this).hide(0);	
		}
	});
	});
	
	if ($(".scrollTextContainer").height() != 250) {	
		if ($("#scrollText").height() < 520) {	
			$(".upArrow").hide(0);
			$(".downArrow").hide(0);
			$(".scrollTextContainer").height(530);
		}	
	}
	
	heightdif = 55;
	newpad = 19;
	$('body').css({'background-position' : '0px -'+ heightdif +'px', 'padding-top': newpad +'px'});
	/*
	if ($(window).height() < 825) {
		var heightdif = 825 - $(window).height();
		var newpad = 99-heightdif;
		if (heightdif > 99) {
			heightdif = 0;
			newpad = 0;
		}
		$('body').css({'background-position' : '0px -'+ heightdif +'px', 'padding-top': newpad +'px'});
	}
	/*
	$(window).resize(function() {
		if ($(window).height() < 825) {
		var heightdif = 825 - $(window).height();
		var newpad = 99-heightdif;
		if (heightdif > 99) {
			heightdif = 99;
			newpad = 0;
		}
		$('body').css({'background-position' : '0px -'+ heightdif +'px', 'padding-top': newpad +'px'});
	}
	});
	*/
	$('.events_content').each(function(){
		if ($(this).find('.event_height').height() <= 53)  {
			$(this).find('.event_height').find('.close_btn').hide(0);
	   }
	});
						   
	$("#left_col").animate({
		left:0
	}, 700, "easeOutQuart", 
		function(){
			
		$("#mid_col").css({'left' : '0'});
		$("#mid_col").animate({
		left:238 }, 900, "easeOutQuart", 
		function(){
			$("#wrapper").css({'background-position' : '-249px 0px'});
			if (footer ) {
				$("#wrapper").animate({
				backgroundPosition:"939px 0px" }, 1100, "easeOutQuart", 
				function(){		
				if (anchorDone == false) {
					var anchorName = window.location.hash;		
					anchorName = anchorName.replace("#", "");
					if (anchorName != "") {
						expandEvent(anchorName);	
						anchorDone = true;
					}
				}
				});				

			} else {
				$("#wrapper").animate({
				backgroundPosition:"619px 0px" }, 1100, "easeOutQuart", 
				function(){		
				if (anchorDone == false) {
					var anchorName = window.location.hash;		
					anchorName = anchorName.replace("#", "");
					if (anchorName != "") {
						expandEvent(anchorName);	
						anchorDone = true;
					}
				}
				});				
			}
		});	
		
		$("#gallery").animate({
		left:238 }, 1200, "easeOutQuart");	
		
		});
	
	$(".media_content").each(function(){
		$(this).find("p:last").css({'margin-bottom' : '0px'});
}); 
	
	$(".scrollText p:last").css({'margin-bottom' : '0px'});
	
	
	$("#move_up").css("display","none");
		
	$("#move_down").mousehold(function(){
		var top = $("#scrollText").css("top");	
		var display = $("#move_up").css("display");
		var height = $('#scrollText').innerHeight();	
		var containerHeight = $("#scrollContainer").css("height");
	
		top = top.replace("px","")
		containerHeight = containerHeight.replace("px","")
			
		top = parseInt(top)
		containerHeight = parseInt(containerHeight)
		
		var maxTop = height - containerHeight;
			
		maxTop = maxTop - (maxTop*2);
			
		var nextTop = top - 30;
	
		if (nextTop < maxTop) {			
			nextTop = maxTop;
		}
		
		if (display == "none") {		
			$("#move_up").css("display","inline");
		}
		
		if (moving == false) {	
			moving = true;
			$("#scrollText").animate({"top": nextTop}, 300, "linear", function(){
				 moving = false;
				 if (nextTop == maxTop) {
					$("#move_down").css("display","none");
				}				 
			});
		}
	});	
	
	$("#move_up").mousehold(function(){
		var top = $("#scrollText").css("top");	
		var display = $("#move_down").css("display");	
		top = top.replace("px","")	
		top = parseInt(top)
			
		maxTop = 0;
		
		var nextTop = top + 30;
		
		if (nextTop > maxTop) {		
			nextTop = maxTop;	
		}
		
		if (display == "none") {		
			$("#move_down").css("display","inline");
		}
		
		if ((top < 0) && (moving == false)) {
			moving = true;
			$("#scrollText").animate({"top": nextTop}, 300, "linear", function(){
				 moving = false;
				 if (nextTop == maxTop) {
					$("#move_up").css("display","none");
				}	
			});
		}
		
	
	});	
				   
});



