jQuery(document).ready(function() {
	
	if($('.page-home').length > 0) {
		init_home_slideshow();
	}
	if($('.content-imgs').length > 0) {
		init_section_slideshow();
	}
	if($('#scroll').length > 0) {
		init_scroller();
	}
	if($('.page-register').length > 0) {
		init_register_form();
	}
	if($('.page-gallery').length > 0) {
		$('.fancyzoom').fancyZoom();
	}
});

/* ***************************************************************************************************************************
	News Scoller
*/
var containerHeight = 0;
var scrollCotent = '';

function getContainerHeight(o) {
   containerHeight = $(o).parent().height();
   containerHeight -= 10;
}
function getScrollContent(o) {
   sc = $(o).next();
   sch = sc.height();
}

function init_scroller() {
	
	$('#scroll').jScrollPane( { 
		showArrows : false,
		scrollbarWidth : 20,
		scrollbarMargin : 15 
	} );
	
	/*
	$('.scroll-handle').Draggable(
	{
		zIndex: 	1000,
		containment:     'parent',
		axis:    'vertically',
		opacity: 	0.7,
		onStart: function() { 
			getContainerHeight( this );
      getScrollContent( this )
		},
		onDrag : function(x,y)
		{
			var move = sch - containerHeight;

			if(y>0 && y<(containerHeight)) {
				sc.css('top', (- y/containerHeight*move + "px"));
			}
			return {x: x,y: y}
		}
	}
 );
	*/
}

/* ***************************************************************************************************************************
	Register Form Validation
*/
function init_register_form () {
	$('#register-submit').click( function () {
		
		if (validate_register()) {
			$('#register-submit').attr('disabled', 'disabled')
			ajaxCampaignMonitor();
		} 
		
		return false;
		
	});
}
function validate_register() {
	
	var valid = true;
	
	var ids = new Array('register-name', 'register-email', 'register-phone');
	
	jQuery.each(ids, function() {
		if ($("#" + this).attr('value') == '') {
			$("#" + this).css( 'background', '#f74646' );
			valid = false;
		} else {
			$("#" + this).css( 'background', '#ffffff' );
		}
  });

	if (!valid_email($('#register-email').val())) {
		$('#register-email').css( 'background', '#f74646' );
		valid = false;
	} else {
		$('#register-email').css( 'background', '#ffffff' );
	}
	
	return valid;
}

function ajaxCampaignMonitor() {
	
	formAction = $("form#cm-form").attr("action");
	
	//set the values from the ee form
	$('#cm-name').val($('#register-name').val());
	$('#hrhrb-hrhrb').val($('#register-email').val());
	$('#cm-phone').val($('#register-phone').val());

	// Serialize form values to be submitted with POST
	var str = $("form#cm-form").serialize();
	
	// Add form action to end of serialized data
	final = str + "&action=" + formAction;
	
	// Submit the form via ajax
	$('#contact_form').hide();
	$('#notification').show();
	
	$.ajax({
		url: "/campaign-monitor/proxy.php",
		type: "POST",
		data: final,
		success: function(html){
			submitExpressionEngineForm();
		}
	});
	
}

function submitExpressionEngineForm() {
	
	str = $('#message').val();
	str+= '\n\nName:\t' + $('#register-name').val();
	str+= '\nEmail:\t' + $('#register-email').val();
	str+= '\nPhone:\t' + $('#register-phone').val();
	
	$('#message').val(str);
	
	$('form#contact_form').submit();
}

function valid_email(value) {
    // contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
    return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value);
}

/* ***************************************************************************************************************************
	Home Slideshow
*/

function init_home_slideshow() {
	$('#home-slideshow').cycle({
		fx			: 'fade',
		speed		: 1000,
		timeout : 3000,
		autostop: true
	});	
}

/* ***************************************************************************************************************************
	Section Slideshows
*/

function init_section_slideshow() {
	$('.content-imgs').cycle({
		fx			: 'fade',
		speed		: 1000,
		timeout : 4000
	});	
}










