$(function() {

	if ($('#bigcarousel').length) $('#bigcarousel').lsd({
		width: 1024,
		height: 464,
		controlsheight: 11,
		controlswidth: 30,
		autoscroll: true,
		easing: "easeOutSine",
		animtime: "slow",
		speed: 3000
	});
	
	if ($('#minicarousel').length) $('#minicarousel').lsd({
		width: 432,
		height: 404,
		controlsheight: 11,
		controlswidth: 30,
		autoscroll: true,
		easing: "easeOutSine",
		animtime: "slow",
		speed: 3000
	});
	
	if ($('#prodcarousel').length) $('#prodcarousel').lsd({
		width: 545,
		height: 391,
		controlsheight: 11,
		controlswidth: 30,
		autoscroll: true,
		easing: "easeOutSine",
		animtime: "slow",
		speed: 3000
	});
	
	if ($('#vidcarousel').length) $('#vidcarousel').lsd({
		width: 545,
		height: 300,
		controlsheight: 11,
		controlswidth: 30,
		easing: "easeOutSine",
		animtime: "slow"
	});
	
	if ($('#map').length) {
		var script = document.createElement("script");
		script.type = "text/javascript";
		script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=Map.init";
		document.body.appendChild(script)
	}

	if ($('#ajaxsubmit').length) $('#ajaxsubmit').submit(function() {
			contactSubmit($(this));
			return false;
		});

	if ($('.months').length) News.init();

	fixHeight();
	$(window).resize(function() { fixHeight(); });

});

function contactSubmit(theform) {
	$.ajax({
		type: "POST",
		url: "/do_contact",
		dataType: 'json',
		data: theform.serialize()+'&ajax=1',
		success: function(data) {
			var block = theform.parent();
			if (data.worked) {
				var formelements = block.children(); //seperate this from the fadeout ness below for better performance
				formelements.fadeOut("fast", function() {
					$(this).remove();
					if (block.find("p").length < 1) block.append('<div class="enquiry-thanks">'+data.message+'</div>');
				});
				if (block.find('.enquiry-error').length) block.find('.enquiry-error').fadeOut('fast', function() { $(this).remove(); });
			}
			else {
				if (!block.find('.enquiry-error').length) block.prepend('<span class="enquiry-error">Please check the <u>red</u> fields and try again.</span>');
				theform.find("input, textarea, select").attr("style", "").removeAttr("style");
				$.each(data.errors, function(i, val) {
					theform.find("input[name="+val+"], textarea[name="+val+"], select[name="+val+"]").css({ borderColor: "#eb4633" });
				});
			}
		}
	});
	return false;
}

function fixHeight() {
	var elem = $('section > div');
	var target = $(window).height() - 266;
	elem.css({ minHeight: target + "px" });
}

var Map = {

	close: function() {
		$('#map .title').html("&nbsp;").width(0);
		$('#map div.line').width(0);
		$('#map .themap').attr('class', 'themap');
	},

	info: function(d) {
		
		Snapper.dealer('<h5>'+d.name+'</h5><p>'+d.address+'</p><p>'+d.phone+'<br /><a href="mailto:'+d.email+'">'+d.email+'</a></p>');
		
		var damap = new google.maps.Map(document.getElementById("gmap"), {
			zoom: 14,
			center: new google.maps.LatLng(d.lat, d.lng),
			mapTypeId: google.maps.MapTypeId.ROADMAP
		});

		var mr = new google.maps.Marker({
			position: new google.maps.LatLng(d.lat, d.lng),
			map: damap,
			title: d.name,
			icon: new google.maps.MarkerImage("/media/images/map-pin.png", new google.maps.Size(138, 128))
		});
	
		return false;
	
	},

	init: function() {
		$('#map .themap a').each(function() {
			var c = $(this).attr('class');
			var t = $(this).attr('title');
			
			$(this).hover(function() {
				$('#map .themap').addClass("t" + c);
			}, function() {
				$('#map .themap').removeClass("t" + c);
			}).click(function(e) {
				e.preventDefault();
				Map.close();
				$('#map .themap').addClass("p" + c);
				$('#map div.line.' + c).animate({width: "656px"}, 800, "linear", function() {
				
					$('#map .title').show().animate({ width: "224px" }, 800, "linear", function() {
						$(this).html('<span style="display:none">' + t + '</span>')
						$(this).find('span').fadeIn("slow");
						$('#dealers').phil({ filterTo: "." + c });
						$('#map .clear').fadeIn();
						$('#dealercountry').val("");
					});
				
				});
			});
		});
		
		$('#dealercountry').change(function() {
			var v = $(this).val();

			Map.close();
			$('#dealers').phil({ filterTo: v });
			
			if (v == "") $('#map .clear').fadeOut();
			else $('#map .clear').fadeIn();
		});
	}

};

var News = {

	init: function() {
	
		$('.months > ul > li:last-child').addClass('iamlast');
	
		$('.months > ul > li > span').click(function() {
			$(this).parent().toggleClass("open");
		});
		
		//this is broken in firefox 6...
		//if ($('.months .sel').length) $('.months .sel').parent().parent().parent().find('span').click();
		//else $('.months > ul > li:first-child > span').click();
		
		if ($('.months .sel').length) $('.months .sel').parent().parent().parent().toggleClass("open");
		else $('.months > ul > li:first-child').toggleClass("open");
	
		$('.newstext').each(function() {
		
			var c = [];
			
			var p = $(this).find('p:first-child')
		
			p.nextAll().each(function() {
			
				c.push($(this));
			
			});
		
			if (c.length > 0) {
		
				p.after('<p><a class="readmore" href="#">Read more...</a></p><div class="more"></div>');
				
				var a = p.next();
				
				a.find('a').click(function(e) {
					e.preventDefault();
					$(this).parent().next().slideDown();
					$(this).fadeOut();
				});
				
				var d = a.next();
				
				$.each(c, function() {
					d.append($(this));
				});
			}
		});
		
		$('.photothumbs').each(function() {
		
			var menu = $(this);
			var photo = menu.prev();
			
			menu.find('a').click(function(e) {
				e.preventDefault();
				
				var clicked = $(this);
				
				menu.find('.sel').removeClass('sel');
				clicked.addClass('sel');
				
				photo.fadeOut("slow", function() {
					photo.attr('src', clicked.attr('href'));
					photo.fadeIn("slow");
				});
			});
		});
	
	}

};

var Snapper = {

	dealer: function(left) {
	
		$.snap({
			inputh: 466,
			inputw: 687,
			html: '<table><tr><td style="padding:0 25px;width:195px"><h4>Dealer Details</h4>'+left+'<span class="line"></span></td><td style="width:425px"><h4>Location</h4><div id="gmap">Loading map...</div></td></tr></table>'
		});
	
	},

	seeds: function(lis) {
	
		$.snap({
			inputh: 255,
			inputw: 710,
			html: '<table><tr><td style="padding:0 25px;width:660px"><h4>Compatible Seeds</h4><ul class="seeds">' + lis + '</ul></td></tr></table>'
		});
	
	}

};

//lucy sliding deck v2
(function(d){var e={init:function(c){var b=d.extend({width:300,height:200,autoscroll:!1,easing:"swing",speed:1E3,animtime:"fast",controls:".controls",controlsheight:10,controlswidth:10},c),a={};return this.each(function(){a.div=d(this);a.deck=d(this).find("ul");a.items=a.deck.children();a.count=a.items.length/a.deck.length;a.items.width(b.width).height(b.height).css({left:b.width});a.deck.width(2*b.width);d(this).find(".view").width(b.width).height(b.height);a.controls=d(this).find(b.controls);a.controls.height(b.controlsheight).width(b.controlswidth* a.count);a.controls.append('<div class="marker"></div>');a.items.each(function(){var b=d(this).index();d(this).css({zIndex:b+101});a.controls.append(function(){return'<a href="'+b+'"></a>'})});a.marker=a.controls.find(".marker");a.marker.width(b.controlswidth).height(b.controlsheight);a.controls.find("a").width(b.controlswidth).height(b.controlsheight).css({position:"relative",top:0-b.controlsheight}).each(function(){d(this).click(function(b){b.preventDefault();d(this).lsd("moveTo",a.div,d(this).attr("href"))})}); d(this).data("settings",b);d(this).data("constants",a);b.autoscroll&&(d(this).lsd("gogoScroller",a.div),d(this).hover(function(){clearInterval(a.timer)},function(){d(this).lsd("gogoScroller",a.div)}));a.items.eq(0).css({zIndex:100,left:0}).addClass("active")})},gogoScroller:function(c){var b=c.data("settings"),a=c.data("constants");return this.each(function(){a.timer=setInterval(function(){d(this).lsd("moveNext",a.div)},b.speed);d(this).data("constants",a)})},moveNext:function(c){c.data("settings"); var b=c.data("constants");return this.each(function(){var a=b.deck.find(".active").index(),a=a+2>b.count?0:a+1;d(this).lsd("moveTo",b.div,a)})},moveTo:function(c,b){var a=c.data("settings"),f=c.data("constants");return this.each(function(){var c=f.deck.find(".active");c.index()!=b&&f.items.eq(b).animate({left:0},a.animtime,a.easing,function(){c.css({left:a.width,zIndex:c.index()+101}).removeClass("active");if(c.find("iframe").length){var e=c.html();c.empty().html(e)}d(this).css({zIndex:100}).addClass("active"); f.marker.css({marginLeft:b*f.controls.find("a").width()})})})}};d.fn.lsd=function(c){if(e[c])return e[c].apply(this,Array.prototype.slice.call(arguments,1));else if(typeof c==="object"||!c)return e.init.apply(this,arguments);else d.error("Method "+c+" does not exist fool!")}})(jQuery);

//jquery easing plugin v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
jQuery.easing.jswing=jQuery.easing.swing; jQuery.extend(jQuery.easing,{def:"easeOutQuad",swing:function(e,a,c,b,d){return jQuery.easing[jQuery.easing.def](e,a,c,b,d)},easeInQuad:function(e,a,c,b,d){return b*(a/=d)*a+c},easeOutQuad:function(e,a,c,b,d){return-b*(a/=d)*(a-2)+c},easeInOutQuad:function(e,a,c,b,d){if((a/=d/2)<1)return b/2*a*a+c;return-b/2*(--a*(a-2)-1)+c},easeInCubic:function(e,a,c,b,d){return b*(a/=d)*a*a+c},easeOutCubic:function(e,a,c,b,d){return b*((a=a/d-1)*a*a+1)+c},easeInOutCubic:function(e,a,c,b,d){if((a/=d/2)<1)return b/ 2*a*a*a+c;return b/2*((a-=2)*a*a+2)+c},easeInQuart:function(e,a,c,b,d){return b*(a/=d)*a*a*a+c},easeOutQuart:function(e,a,c,b,d){return-b*((a=a/d-1)*a*a*a-1)+c},easeInOutQuart:function(e,a,c,b,d){if((a/=d/2)<1)return b/2*a*a*a*a+c;return-b/2*((a-=2)*a*a*a-2)+c},easeInQuint:function(e,a,c,b,d){return b*(a/=d)*a*a*a*a+c},easeOutQuint:function(e,a,c,b,d){return b*((a=a/d-1)*a*a*a*a+1)+c},easeInOutQuint:function(e,a,c,b,d){if((a/=d/2)<1)return b/2*a*a*a*a*a+c;return b/2*((a-=2)*a*a*a*a+2)+c},easeInSine:function(e, a,c,b,d){return-b*Math.cos(a/d*(Math.PI/2))+b+c},easeOutSine:function(e,a,c,b,d){return b*Math.sin(a/d*(Math.PI/2))+c},easeInOutSine:function(e,a,c,b,d){return-b/2*(Math.cos(Math.PI*a/d)-1)+c},easeInExpo:function(e,a,c,b,d){return a==0?c:b*Math.pow(2,10*(a/d-1))+c},easeOutExpo:function(e,a,c,b,d){return a==d?c+b:b*(-Math.pow(2,-10*a/d)+1)+c},easeInOutExpo:function(e,a,c,b,d){if(a==0)return c;if(a==d)return c+b;if((a/=d/2)<1)return b/2*Math.pow(2,10*(a-1))+c;return b/2*(-Math.pow(2,-10*--a)+2)+c}, easeInCirc:function(e,a,c,b,d){return-b*(Math.sqrt(1-(a/=d)*a)-1)+c},easeOutCirc:function(e,a,c,b,d){return b*Math.sqrt(1-(a=a/d-1)*a)+c},easeInOutCirc:function(e,a,c,b,d){if((a/=d/2)<1)return-b/2*(Math.sqrt(1-a*a)-1)+c;return b/2*(Math.sqrt(1-(a-=2)*a)+1)+c},easeInElastic:function(e,a,c,b,d){var e=1.70158,f=0,g=b;if(a==0)return c;if((a/=d)==1)return c+b;f||(f=d*0.3);g<Math.abs(b)?(g=b,e=f/4):e=f/(2*Math.PI)*Math.asin(b/g);return-(g*Math.pow(2,10*(a-=1))*Math.sin((a*d-e)*2*Math.PI/f))+c},easeOutElastic:function(e, a,c,b,d){var e=1.70158,f=0,g=b;if(a==0)return c;if((a/=d)==1)return c+b;f||(f=d*0.3);g<Math.abs(b)?(g=b,e=f/4):e=f/(2*Math.PI)*Math.asin(b/g);return g*Math.pow(2,-10*a)*Math.sin((a*d-e)*2*Math.PI/f)+b+c},easeInOutElastic:function(e,a,c,b,d){var e=1.70158,f=0,g=b;if(a==0)return c;if((a/=d/2)==2)return c+b;f||(f=d*0.3*1.5);g<Math.abs(b)?(g=b,e=f/4):e=f/(2*Math.PI)*Math.asin(b/g);if(a<1)return-0.5*g*Math.pow(2,10*(a-=1))*Math.sin((a*d-e)*2*Math.PI/f)+c;return g*Math.pow(2,-10*(a-=1))*Math.sin((a*d-e)* 2*Math.PI/f)*0.5+b+c},easeInBack:function(e,a,c,b,d,f){f==void 0&&(f=1.70158);return b*(a/=d)*a*((f+1)*a-f)+c},easeOutBack:function(e,a,c,b,d,f){f==void 0&&(f=1.70158);return b*((a=a/d-1)*a*((f+1)*a+f)+1)+c},easeInOutBack:function(e,a,c,b,d,f){f==void 0&&(f=1.70158);if((a/=d/2)<1)return b/2*a*a*(((f*=1.525)+1)*a-f)+c;return b/2*((a-=2)*a*(((f*=1.525)+1)*a+f)+2)+c},easeInBounce:function(e,a,c,b,d){return b-jQuery.easing.easeOutBounce(e,d-a,0,b,d)+c},easeOutBounce:function(e,a,c,b,d){return(a/=d)<1/ 2.75?b*7.5625*a*a+c:a<2/2.75?b*(7.5625*(a-=1.5/2.75)*a+0.75)+c:a<2.5/2.75?b*(7.5625*(a-=2.25/2.75)*a+0.9375)+c:b*(7.5625*(a-=2.625/2.75)*a+0.984375)+c},easeInOutBounce:function(e,a,c,b,d){if(a<d/2)return jQuery.easing.easeInBounce(e,a*2,0,b,d)*0.5+c;return jQuery.easing.easeOutBounce(e,a*2-d,0,b,d)*0.5+b*0.5+c}});

//snap v2
(function(a){var f={close:function(b){a.support.opacity?a("#snap").fadeOut("fast",function(){a(this).remove();typeof b=="function"&&b()}):(a("#snap").remove(),typeof b=="function"&&b())},open:function(b){var d=a.extend({callback:null,html:"",inputh:0,inputw:0},b);a("#snap").length==0&&(a("body").append('<div id="snap"><div id="snap-back"></div><div id="snap-box"><div class="contents"><div class="controls"><a class="close" href="#"></a></div><div class="message"></div></div></div></div>'),a("#snap, #snap-back").width(a(window).width()).height(a(window).height()), a(window).resize(function(){a("#snap, #snap-back").width(a(window).width()).height(a(window).height())}),a("#snap .close").click(function(a){a.preventDefault();f.close(d.callback)}),a(document).keyup(function(a){a.keyCode==27&&f.close(d.callback)}));var b=a("#snap .message"),i=a("#snap .contents"),h=a("#snap");b.html(d.html).css({display:"inline-block"});h.css({position:"absolute",visibility:"hidden",display:"block"});d.inputw>0&&b.width(d.inputw);d.inputh>0&&b.height(d.inputh);var c=b.height(),e= b.width();if(e<190)e=190;else{var g=a(window).width()-40;e>=g&&(e=g)}fw=e+10;c<184?c=184:(g=a(window).height()-40,c>=g&&(c=g));c+=16;i.height(c).width(fw).css({marginTop:0-c/2});a("#snap .controls").width(e);b.attr("style","").removeAttr("style");h.attr("style","").removeAttr("style");b.width(fw).height(c-31);a.support.opacity?h.fadeIn():h.show()}};a.snap=function(b){if(f[b])return f[b].apply(this,Array.prototype.slice.call(arguments,1));else if(typeof b==="object"||!b)return f.open(b);else a.error("Method "+ b+" does not exist fool!");return a(this)}})(jQuery);

//phil filter plugin beta
(function(c){var b={init:function(a){var d=c.extend({items:".phil",filterTo:""},a);return this.each(function(){var a=c(this),b=a.find(d.items);d.filterTo==""?b.show("slow"):b.not(d.filterTo).hide("slow","linear",function(){a.find(d.filterTo).show("slow")})})}};c.fn.phil=function(a){if(b[a])return b[a].apply(this,Array.prototype.slice.call(arguments,1));else if(typeof a==="object"||!a)return b.init.apply(this,arguments);else c.error("Method "+a+" does not exist fool!")}})(jQuery);
