SITE_URL = '/';


function myInit() {
	
		
	$("a[href^='http:']").not("[href*='jamiesfarm.org.uk']").attr('target','_blank');
	
	$('#navigation > ul > li:has(ul) > a').click( function() {											  
		return false;
		}); 
	$('#navigation ul li:has(li.selected)').addClass('selected');
	$('#siblingMenu ul li:has(li.selected)').addClass('selected');

	
	
	$("#navigation > ul > li").hover( function () {		
		$("#navigation > ul > li").removeClass('hover');
		$(this).addClass('hover');
		},function () {
		$(this).removeClass('hover');
		}
		);
	/*
	$("#navigation > li > ul").hover( function () {		
		},function () {
		$(this).parent().removeClass('hover');
		}
	);	
	*/
	
	$('#header #signupForm #MERGE1').val('Your Name');
	$('#header #signupForm #MERGE0').val('Your Email');	
	$('#header #signupForm #MERGE1').focus(function() {
		if ($(this).val() == 'Your Name') {
			$(this).val('');
			}
		});
	$('#header #signupForm #MERGE1').blur(function() {
		if ($(this).val() == '') {
			$(this).val('Your Name');
			}
		});
	$('#header #signupForm #MERGE0').focus(function() {
		if ($(this).val() == 'Your Email') {
			$(this).val('');
			}
		});
	$('#header #signupForm #MERGE0').blur(function() {
		if ($(this).val() == '') {
			$(this).val('Your Email');
			}
		});
	$('#header #signupForm form').submit( function() {
		var errors = '';
		if (!$('#header #signupForm #MERGE1').val() || $('#header #signupForm #MERGE1').val() == 'Your Name') {
			errors = "Your Name\n";
			}
		if (!$('#header #signupForm #MERGE0').val() || $('#header #signupForm #MERGE0').val() == 'Your Email') {
			errors += "Your Email\n";
			}
		if (errors) {
			alert("Please compelte the following fields:\n\n"+errors);
			return false;
			}
		});
	
	$('#archive > li > a').click( function() {		
		$(this).parent().find("ul:first").toggleClass("closed"); 
		return false;
		});
	$('#archive > li > ul > li > a').click( function() {		
		$(this).parent().find("ul:first").toggleClass("closed"); 
		return false;
		});
	
	

	/*
	$(".tabs").tabs("#carousel > li", {
		effect: 'fade',
		fadeOutSpeed: "fast",
		rotate: true
	}).slideshow({autoplay: true, interval: 12000});
	*/
	
	$('#carousel').cycle({ 
		fx:     'fade', 
		speed:  1000, 
		timeout: 12000, 
		pager:  '#tabs'
	});
	
	$('#galleryScrollLeft').click( function() {
		galleryScrollLeft();
		return false;
		});
	
	$('#galleryScrollRight').click( function() {
		galleryScrollRight();
		return false;
		});
	
	$('.thumbnail').click( function() {
		imageId = this.id;
		imageId = imageId.replace('thumb_','');
		loadImage(imageId);
		return false;
		});
	
	$('#imageLarge').click( function() {
		loadImage(nextId);
		return false;
		});
	
	$("#commentForm").submit(function() {		
		$('#commentForm').attr('method','post');
		errors = '';	
		if (!$('#cf_comment').val()) {
			errors += "Comment\n";
			}
		if (!$('#cf_name').val()) {
			errors += "Name\n";
			}
		if (!$('#cf_email').val()) {
			errors += "Email\n";
			}
		if (errors) {			
			alert("The following fields are required:\n\n"+errors);
			return false;
			} 	
		});
	

	$('#faq h3 a').click( function() {			
		$('#content div#faq div.faq').hide();
		$('#faq .on').removeClass('on');
								
		var theId = this.id;
		theDiv = theId.replace("q","a");		
		$('#'+theDiv).show();
		$(this).parent().addClass('on');
			
		return false;
		});
	
	$("#content div#faq div.faq").hide();
	
	
	
	$('#slideshow').cycle({
		});
	
	$('#testimonials > div').cycle({
		fx:     'scrollHorz',
   		next:   '#testimonials a.next',
   		prev:   '#testimonials a.prev',
		timeout: 0,
		speed:   200
		});
	
	}

function loadVideo(videoId) {
	if (!videoId) {
		return;
		}	
	$.ajax({
		type: "POST",
		url: SITE_URL+"loadVideo.php",
		data: "videoId="+videoId,
		success: function(returned){				
			$('#videoMain').html(returned);			
			}
		});			

	}

function loadImage(imageId) {	
	if (!imageId) {
		return;
		}		
	$('#imageLarge').fadeOut(200, function () {		
		$.ajax({
			type: "POST",
			url: SITE_URL+"loadImage.php",
			data: "imageId="+imageId,
			success: function(returned){	
				myData = returned.split("~");					
				$('#imageLarge').html(myData[0]);
				nextId = myData[1];
				$('#imageLarge').fadeIn(200, function () {				
					// scroll thumbnail into view
					//scrollThumbnails(myData[1]);					
					});				
				}
			});			
		});	
	}

function galleryScrollLeft() {	
	if (scrollToInt > 0) {
		scrollToInt = parseInt(scrollToInt) - parseInt(scrollIncrement);
		scrollThumbnails(scrollToInt);
		}
	}
function galleryScrollRight() {	
	if (scrollToInt < (maxWidth-paneWidth)) {
		scrollToInt = parseInt(scrollToInt) + parseInt(scrollIncrement);
		scrollThumbnails(scrollToInt);
		} 
	}

function scrollToImage(imageId) {	
	containerOffset = $('#thumbnails').offset();	
	thumbOffset = $('#thumb_'+imageId).offset();	
	scrollPixel = thumbOffset.left - containerOffset.left;	
	scrollThumbnails(scrollPixel);	
	}
	
function scrollThumbnails(scrollPixel) {		
	
	if (scrollPixel < 0) {
		scrollPixel = 0;
		}
		
	$('#thumbnails').scrollTo(  scrollPixel, { axis:'x', duration:1000 } );	
	scrollToInt = parseInt(scrollPixel);	
	if (scrollPixel >= (maxWidth-paneWidth)) {
		$('#galleryScrollRight').addClass('disabled');
		}
	if (scrollPixel > 0) {
		//$('#scrollUp').fadeIn('normal');
		$('#galleryScrollLeft').removeClass('disabled');
		}
	if (scrollPixel < (maxWidth-paneWidth)) {
		//$('#scrollDown').fadeIn('normal');
		$('#galleryScrollRight').removeClass('disabled');
		}
	if (scrollPixel == 0) {
		//$('#scrollUp').fadeOut('normal');
		$('#galleryScrollLeft').addClass('disabled');
		}
	}
