﻿// Function position footer
(function($){  

$.fn.positionFooter = function(center){  
  
var element = this; 

var originalPosition = $(element).offset();
var originalTopPosition = originalPosition.top;  

positionTheFooter();  
  
$(window).bind("resize", function(){  
    positionTheFooter();  
});
 
function positionTheFooter(){ 
  
  var elementPosition = $(element).offset();
  var elementPaddingTop = $(element).css("padding-top");
  var elementPaddingBottom = $(element).css("padding-bottom");
  var elementPaddingleft = $(element).css("padding-left");
  var elementPaddingRight = $(element).css("padding-right");
  var elementHeight = $(element).height();
  var elementWidth = $(element).width();
  
  var windowHeight = $(window).height();
  var windowWidth = $(window).width();
  
  elementPaddingTop = elementPaddingTop.replace(/px/,"");
  elementPaddingBottom = elementPaddingBottom.replace(/px/,"");
  elementPaddingleft = elementPaddingleft.replace(/px/,"");
  elementPaddingRight = elementPaddingRight.replace(/px/,"");
  
  var newPosition = (parseInt(windowHeight) - (parseInt(elementHeight) + (parseInt(elementPaddingTop) + parseInt(elementPaddingBottom))));
  var currentPosition = elementPosition.top - (parseInt(elementHeight) + (parseInt(elementPaddingTop) + parseInt(elementPaddingBottom)));
    
  if(originalTopPosition < windowHeight){
    
    $(element).css({  
      "position" : "absolute",
      "top" : newPosition
    });
    
    if(center == true){
      $(element).css({  
        "left" : windowWidth / 2 - (((elementWidth + parseInt(elementPaddingleft) + parseInt(elementPaddingRight)) / 2))
      });
    }
  
  }
  
  if(newPosition <= originalTopPosition){

    $(element).css({  
      "position" : "absolute",
      "top" : originalTopPosition
    });
    
    if(center == true){
      $(element).css({  
        "left" : windowWidth / 2 - (((elementWidth + parseInt(elementPaddingleft) + parseInt(elementPaddingRight)) / 2))
      });
    }
  
  }
  
};
  
};  
  
})(jQuery); 

// Execute position footer function
$(function(){  
  $("#footer").positionFooter(true);  
}); 

// Pause plugin
$.fn.pause = function(milli,type) {
	milli = milli || 1000;
	type = type || "fx";
	return this.queue(type,function(){
		var self = this;
		setTimeout(function(){
			$.dequeue(self);
		},milli);
	});
};

$.fn.clearQueue = $.fn.unpause = function(type) {
	return this.each(function(){
		type = type || "fx";
		if(this.queue && this.queue[type]) {
			this.queue[type].length = 0;
		}
	});
};

$(document).ready(function() {

// Show #contact on #message button click
$(function(){
  $('#message').click(function() {
  $('#response-container').hide(0);
  $('#content').slideUp(500);
  $('#contact').pause(500).slideDown(500);
  });
});

// Show #contact on #enquire link click
$(function(){
  $('#enquire').click(function() {
  $('#content').slideUp(500);
  $('#contact').pause(500).slideDown(500);
  });
});

// Validate form on submit
$(function(){
    $("#contactForm").validate();
  });

// Asynchronos post message, append results to #response element and show
$(function() {
  $('#submit').click(function() {
    if ($("#contactForm").valid()) {
      // alert("Valid: " + $("#contactForm").valid());
      var name = $('#cname').val();
      var email = $('#cemail').val();
      var url = $('#curl').val();
      var message = $('#cmessage').val();
      var subscribe = $('#csubscribe').attr("checked");
    	
      $.ajax({
	      url: 'submit-enquiry.asp',
	      type: 'POST',
	      data: {name:name,email:email,url:url,message:message,subscribe:subscribe},

	      success: function(result) {
	        $('#response').empty();
		      $('#response').append(result);
		      $('#contact').slideUp(500);
          $('#response-container').pause(500).slideDown(500);
	      }
      });
    return false;
    }
  });
});

//Website slideshow
$(function() {
	$('#pics').show(0);
	$('#pics').cycle({ 
			fx:      'fadeZoom', 
			speed:    300, 
			timeout:  3000 
	});
});
});