var Slides;
var Width;

function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=450,height=550,left = 458,top = 109');");
}

// In Array function
Array.prototype.in_array = function(val) {
	for(var z = 0, l = this.length; z < l; z++) {
		if(this[z] == val) {
			return z;
		}
	}
	return -1;
}

// Remove items from array
Array.prototype.removeItems = function(itemsToRemove) {

    if (!/Array/.test(itemsToRemove.constructor)) {
        itemsToRemove = [ itemsToRemove ];
    }

    var j;
    for (var i = 0; i < itemsToRemove.length; i++) {
        j = 0;
        while (j < this.length) {
            if (this[j] == itemsToRemove[i]) {
                this.splice(j, 1);
            } else {
                j++;
            }
        }
    }
}


jQuery.fn.ohopSlides = function(settings) {
	settings = jQuery.extend({
		nextId: 'nextSlide',
		prevId: 'prevSlide'
	}, settings);
	
	var Left;
	var Parent = $(this);
	
	
	// Let's set it all up
	$(this).find(".slide").each(function() {
		var Width = parseFloat( $(this).css("width") ) + parseFloat( $(this).css("paddingRight") );
		Left = ( isNaN( Left ) ) ? 0 : Left + Width;
		$(this).css( "left", Left + "px" );
		$(this).show();
	});
	
	Slides = $(this).find(".slide").length;
	Width = Left / ( Slides - 1 );
	
	// Active slide
	var Active = 1;
	var Found = false;
	$(this).find(".slide").each(function() {
		if( $(this).hasClass( 'active' ) ) {
			Found = true;
		} else if( Found == false ) Active++;
	});

	var fLeft = 0;
	for( var x = 1; x < Active; x++ ) {
		fLeft -= Width;
	}
	$(Parent).css("left", fLeft + 'px');

	// Next
	$( '#' + settings.nextId ).click(function() {
		return nextSlide(350);
	});
	
	function nextSlide(Duration) {
		if( permitSlide(1) == false ) return false;
		$(Parent).animate({
			left: '-=' + Width + 'px'
		}, Duration);
		slideButtons(1, settings);
		return false;
	}
	
	// Previous
	$( '#' + settings.prevId ).click(function() {
		if( permitSlide(0) == false ) return false;
		$(Parent).animate({
			left: '+=' + Width + 'px'
		});
		slideButtons(0, settings);
		return false;
	});
}

function slideButtons( Next, settings ) {
	return false;
	var tmpLeft = Math.ceil( Math.abs( parseFloat( $("#slides").css("left") ) ) );
	var SlideNo = Math.ceil( tmpLeft / Width ) + 1;
	
	// Show/hide next button
	if( Next == 1 && SlideNo == ( Slides - 1 ) ) $( '#' + settings.nextId ).hide();
	else if( $( '#' + settings.nextId ).css('display') == 'none' ) $( '#' + settings.nextId ).show();
	
	else if( Next == 0 && SlideNo == 1 ) return false;
	
	return false;
}

function permitSlide(Next) {
	var tmpLeft = Math.ceil( Math.abs( parseFloat( $("#slides").css("left") ) ) );
	var SlideNo = Math.ceil( tmpLeft / Width ) + 1;
	if( Next == 1 && SlideNo >= Slides ) return false; 
	else if( Next == 0 && SlideNo == 1 ) return false;
	
	return true;
}
	
// Promises
var Promises = new Array;

$(document).ready(function() {
	$("#slides").ohopSlides();
	
	$("#pc1,#pc2,#pc3").keyup(function() {
		if( $(this).val() == "" ) return;
		var Id = parseFloat( $(this).attr("id").substr( 2, 1 ) );
		$('#pc' + ( Id + 1 ) ).focus();
	});
	
	$(".cb").click(function() {
		if( $(this).hasClass("cbs") ) $(this).removeClass("cbs");
		else $(this).addClass("cbs");
		
		if( Promises.in_array( $(this).attr("id") ) == -1 && $(this).hasClass("cbs") ) Promises.push( $(this).attr("id") );
		else if( Promises.in_array( $(this).attr("id") ) >= 0 && $(this).hasClass("cbs") == false ) Promises.removeItems( $(this).attr("id") );
	});
	
	$("#pledgeForm").submit(function() {
		var Errors = new Array;
		
		if( $("#pName").val() == "" ) Errors.push("Name");
		if( $("#pEmail").val() == "" ) Errors.push("Email");
		if( $("#pc1").val() == "" || $("#pc2").val() == "" || $("#pc3").val() == "" || $("#pc4").val() == "" ) Errors.push("Postcode");
		if( Promises.length == 0 ) Errors.push("Promises");
		
		if( Errors.length > 0 ) {
			var Out = "You have left the following form fields empty:\n";
			for( var x = 0; x < Errors.length; x++ ) {
				Out += "   - " + Errors[x] + "\n";
			}
			Out += "Please correct these and re-submit the form.";
			alert( Out );			
			return false;
		}
		
		$("#promises").val( Promises.join(",") );

	});
	
	$("#sendForm").submit(function() {
		var Errors = new Array;
		
		if( $("#your_name").val() == "" ) Errors.push("Your name");
		if( $("#your_email").val() == "" ) Errors.push("Your email");
		if( $("#friends_name").val() == "" ) Errors.push("Friend's name");
		if( $("#friends_email").val() == "" ) Errors.push("Friend's email");
	
		if( Errors.length > 0 ) {
			var Out = "You have left the following form fields empty:\n";
			for( var x = 0; x < Errors.length; x++ ) {
				Out += "   - " + Errors[x] + "\n";
			}
			Out += "Please correct these and re-submit the form.";
			alert( Out );			
			return false;
		}
		
	});
	
	/*
	$(".readMore").parent().click( function() {

		
		var Url = $(this).attr('href');
		var Page = Url.substring( Url.lastIndexOf('/') + 1, Url.length );
		
		$.ajax({
			url: 'viewPage.php?page=' + escape( Page ),
			dataType: "html",
			success: function(data) {
				alert( data );
			},
			error: function() {
				alert( 'error' );
			}
		});
		
		return false;
		
	});
	*/
	
});
