// Dropdown IE

sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);


// Campaign Monitor

var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
  document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
  function checkEmail(email) { 
    var pattern = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    var emailVal = $("#" + email).val();
    return pattern.test(emailVal);
  }
  $(function() {
    $("#subForm input:submit").click(function() { 
      
      // First, disable the form from submitting
      $('form#subForm').submit(function() { return false; });
      
      // Grab form action
      var formAction = $("form#subForm").attr("action");
      
      // Hacking together id for email field
      // Replace the xxxxx below:
      // If your form action were http://mysiteaddress.createsend.com/t/r/s/abcde/, then you'd enter "abcde" below
      var id = "uyydtj";
      var emailId = id + "-" + id;
      
      // Validate email address with regex
      if (!checkEmail(emailId)) {
        alert("Please enter a valid email address");
        return;
      }
      
      // Serialize form values to be submitted with POST
      var str = $("form#subForm").serialize();
      
      // Add form action to end of serialized data
      // CDATA is used to avoid validation errors
      //<![CDATA[
      var serialized = str + "&action=" + formAction;
      // ]]>
      
      // Submit the form via ajax
      $.ajax({
        url: "proxy.php",
        type: "POST",
        data: serialized,
        success: function(data){
          // Server-side validation
          if (data.search(/invalid/i) != -1) {
            alert('The email address you supplied is invalid and needs to be fixed before you can subscribe to this list.');
          }
          else
          {
            $("#theForm").hide(); // If successfully submitted hides the form
            $("#confirmation").slideDown("slow");  // Shows "Thanks for subscribing" div
            $("#confirmation").tabIndex = -1;
            $("#confirmation").focus(); // For screen reader accessibility
            // Fire off Google Analytics fake pageview
            var pageTracker = _gat._getTracker("UA-3694556-2");
            pageTracker._trackPageview("/newsletter_signup");
          }
        }
      });
    });
  });

/*
 * jQuery Timer Plugin
 * http://www.evanbot.com/article/jquery-timer-plugin/23
 *
 * @version      1.0
 * @copyright    2009 Evan Byrne (http://www.evanbot.com)
 */

jQuery.timer = function(time,func,callback){
	var a = {timer:setTimeout(func,time),callback:null}
	if(typeof(callback) == 'function'){a.callback = callback;}
	return a;
};

jQuery.clearTimer = function(a){
	clearTimeout(a.timer);
	if(typeof(a.callback) == 'function'){a.callback();};
	return this;
};

// jQuery EASING

jQuery.extend(jQuery.easing,{
	easeOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	},
	easeInOutExpo: function(x,t,b,c,d){
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	},
	easeInExpo: function (x, t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	}
});

//Gallery

$(function(){

	totalGallery = $('#gallery_slider div').size();
	xPos = 0;
	currentGallery = 1;
	moose = true;
	autoMoose = true;
	galleryDelay = 6000;
	easeIn = 'easeInExpo';
	easeOut = 'easeOutElastic';
	inSpeed = 400;
	outSpeed = 1000;
	
	$('#scroller .next a').click(function(){
		if(moose==true && totalGallery>currentGallery){
			xPos = xPos - 960;
			currentGallery = currentGallery + 1;
			slideTo(xPos, 'right');
		}else if(moose==true){			
			currentGallery = 1;
			infiniteLoop('right');
		}
		return false;
	});
	
	$('#scroller .prev a').click(function(){
		if(moose==true && currentGallery>1){
			xPos = xPos + 960;
			currentGallery = currentGallery - 1;
			slideTo(xPos, 'left');
		}else if(moose==true){
			currentGallery = totalGallery;
			infiniteLoop('left');
		}
		return false;
	});
	
	function infiniteLoop(dir){
		
		moose = false;
		
		totalX = 960 - (960 * totalGallery);
		
		if(dir == 'left'){
			$('#gallery_slider').prepend('<div class="gallery_content">'+$('#gallery_slider div.gallery_content:last').html()+'</div>').css({left:-960}).animate({left:480},inSpeed,easeIn,function(){
				$(this).animate({left:0},outSpeed,easeOut,function(){
					$('#gallery_slider').css({left:totalX});
					$('#gallery_slider div.gallery_content:first').remove();
					moose = true;
				})
			});
			xPos = totalX;
		}else if(dir == 'right'){
			$('#gallery_slider').append('<div class="gallery_content">'+$('#gallery_slider div.gallery_content:first').html()+'</div>').animate({left:totalX-480},inSpeed,easeIn,function(){
				$(this).animate({left:totalX-960},outSpeed,easeOut,function(){
					$('#gallery_slider').css({left:0});
					$('#gallery_slider div.gallery_content:last').remove();
					moose = true;
				});
			});
			xPos = 0;
		}
	}
	
	$('#gallery_panel').hover(function(){
		$.clearTimer(galleryTimer);
	},function(){
		autoMagical();
	});
	
	var galleryTimer = {};
	
	function autoMagical(){
		galleryTimer = $.timer(galleryDelay,function(){
			$.clearTimer(galleryTimer);
			$('#scroller .next a').click();
			autoMagical();
		});
	}
	
	autoMagical();
	
	function slideTo(newX, dir){
		moose = false;
		if(dir == 'left'){
			halfX = newX - 480;
		}else{
			halfX = newX + 480;
		}
		$('#gallery_slider').animate({left:halfX},inSpeed,easeIn,function(){
			$(this).animate({left:newX},outSpeed,easeOut,function(){
				moose = true;
			});
		})
	}
});
