$(document).ready(function(){

	Nifty("div.box","normal");
	Nifty("#sidebarTestimonial","normal transparent");
	
	// Popup Window
	$('a.pop').click(function(){
		window.open(this.href,"_blank","width=970,height=751,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0");
		return false;
	});

	// Contact Form
	$("#sidebarContactUsForm").validate({ 
		rules: {
			name: "required",
			email: {
				required: true,
				email: true
			}
		},
		messages: {
			name: "",
			email: ""
		}
    })

	// Newsletter Form
	$("#newsletterSubmit").click( function(){

		result = $("#newsletterForm").validate({ 
			rules: {
				newsletterName: "required",
				newsletterEmail: {
					required: true,
					email: true
				}
			},
			messages: {
				newsletterName: "",
				newsletterEmail: ""
			}
	    }).form();
	    
	    
		if (result === true){


			var name = $('input[name=newsletterName]');
	        var email = $('input[name=newsletterEmail]');
	        var lastName = $('input[name=newsletterLastName]');
	        var comment = $('input[name=newsletterComment]');
		
	        var data = 'name=' + name.val() + '&lastName=' + lastName.val() + '&email=' + email.val() + '&comment=' + comment.val();
	         
	        //start the ajax
	        $.ajax({
	            url: "/ajax/newsletterSubscriptionProcess.php",
	            type: "POST",        
	            data: data,    
	            cache: false,
	            success: function (html) {             
	                //if process.php returned 1/true (send mail success)
	                if (html==1) {    
	                
	                	$("#newsletterForm").html('<h4>&nbsp;&nbsp;&nbsp;Thank You!</h4><p>You can also follow us on the following sites:</p>');             
	                	                     
	                //if process.php returned 0/false (send mail failed)
	                } else alert(html);              
	            }      
	        });
	    }
	    
		return false;
	});
	
	
	// Modal Window
	//When you click on a link with class of poplight and the href starts with a # 
	$('a.modallight[href^=#]').click(function() {
	    var popID = $(this).attr('title'); //Get Popup Name
	    var popURL = $(this).attr('href'); //Get Popup href to define size
	
	    //Pull Query & Variables from href URL
	    var query= popURL.split('?');
	    var dim= query[1].split('&');
	    var popWidth = dim[0].split('=')[1]; //Gets the first query string value
	
	    //Fade in the Popup and add close button
	    $('#' + popID).fadeIn().css({ 'width': Number( popWidth ) });
	
	    //Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and border width defined in the css
	    var popMargTop = ($('#' + popID).height() + 80) / 2;
	    var popMargLeft = ($('#' + popID).width() + 80) / 2;
	
	    //Apply Margin to Popup
	    $('#' + popID).css({
	        'margin-top' : -popMargTop,
	        'margin-left' : -popMargLeft
	    });
	
	    //Fade in Background
	    $('body').append('<div id="modalFade"></div>'); //Add the fade layer to bottom of the body tag.
	    $('#modalFade').css({'filter' : 'alpha(opacity=50)'}).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies 
	
	    return false;
	});

	//Close Popups and Fade Layer
	$('a.close, #modalFade').live('click', function() { //When clicking on the close or fade layer...
	    $('#modalFade , .modal_block').fadeOut(function() {
	        //$('#modalFade, a.close').remove();  //fade them both out
	    });
	    return false;
	});
	

});	

