function equalHeight(group) {
    tallest = 0;
    group.each(function() {
        thisHeight = $(this).height();
        if(thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
    group.height(tallest);
}
window.onload = function() {
	//$("#background").dropShadow({left: -1, top: -1, blur: 1, opacity: 1});
	
	$(".shadow").dropShadow({left: 0, top: 0, blur: 1, opacity: 0.4});
			
	
};

$(document).ready(function(){	
	
	// check if the email is valid and exists -----------
	$('#email').blur(function() { 
		var email = document.getElementById('email').value;
		var emptyerror = "You must enter a valid email address";
		
		AtPos = email.indexOf("@")
		StopPos = email.lastIndexOf(".")
		
		if(email == "" || AtPos == -1 || StopPos == -1 || StopPos < AtPos || StopPos - AtPos == 1) {
			$("#email").addClass("bad");
			$("#email").removeClass("good");
			$(".emailerror").html(emptyerror);
			$(".emailerror").slideDown(500);
		}
		
		else {
			$.ajax({
				method: "GET",url: "/check-user.php",data: "email="+email,
				beforeSend: function(){ $('.loading-checkmail').fadeIn(500); }, //show loading just when link is clicked
				complete: function(){ $('.loading-checkmail').fadeOut(500); }, //stop showing loading when the process is complete
				success: function(html){ //so, if data is retrieved, store it in html
					var result = html;
					if(result == "1") {
						$("#email").addClass("good");
						$("#email").removeClass("bad");
						$(".emailerror").slideUp(200);
						validemail = 1;						
						}
						else {
						$("#email").addClass("bad");
						$("#email").removeClass("good");
						$(".emailerror").html(result);
						$(".emailerror").slideDown(500);
						validemail = 0;
						}
					}
			}); //finish ajax
		}
		
	}); // End check email -----------------------------
	
	$('#regsubmit').click(function() {
	
		var name = document.getElementById('name').value;
		var password = document.getElementById('password').value;
		var email = document.getElementById('email').value;
		var confirmCode = document.getElementById('confirm').value;
		
		HasSpace = name.indexOf(" ")
		
		if(name == "" || HasSpace == -1){
			$("#name").addClass("bad");
			$("#name").removeClass("good");
			$(".nameerror").html("You must enter your first and last name");
			$(".nameerror").slideDown(500);
			validname = 0;
		}
		else {
			$(".nameerror").slideUp(500);
			$("#name").addClass("good");
			$("#name").removeClass("bad");
			validname = 1;
		}
		if(password == "") {
			$("#password").addClass("bad");
			$("#password").removeClass("good");
			$(".passerror").html("You must enter a password");
			$(".passerror").slideDown(500);
			validpass = 0;
		}
		else {
			$(".passerror").slideUp(500);
			$("#password").addClass("good");
			$("#password").removeClass("bad");
			validpass = 1;
		}
		if( confirmCode == "YbcdDx234Afg" || confirmCode == "rewQQfa332" || confirmCode == "LObbdsp1442" || confirmCode == "GHHDSssd124" ) {
			$(".confirmerror").slideUp(500);
			$("#confirm").addClass("good");
			$("#confirm").removeClass("bad");
			validconfirm = 1;
		}
		else {
			$("#confirm").addClass("bad");
			$("#confirm").removeClass("good");
			$(".confirmerror").html("This confirmation code is invalid");
			$(".confirmerror").slideDown(500);
			validconfirm = 0;
		}
		if(email == "") {
			$("#email").addClass("bad");
			$("#email").removeClass("good");
			$(".emailerror").html("You must enter a valid email address");
			$(".emailerror").slideDown(500);
			validemail = 0;
		}
		
		if(validemail == 1 && validname == 1 && validpass == 1 && validconfirm == 1) {
			
			$.ajax({
				method: "GET",url: "/add-user.php",data: "email="+email+"&name="+name+"&password="+password,
				beforeSend: function(){ 
				$('#create-loading').html('<img src="/templates/secret/images/loading-1.gif" /><p>Please Wait.. Creating Account</p>');
				$('#create-loading').fadeIn(500); 
				}, //show loading just when link is clicked
				complete: function(){ }, //stop showing loading when the process is complete
				success: function(html){ //so, if data is retrieved, store it in html
					var result = html;
					if(result == "1") {
						$('#create-loading').html('<p>Congratulations! Your account has been created. You can now login using the form to the right.</p>');
						}
						else {
						$('#create-loading').html('<p>There was a problem creating your account</p>');
						}
					}
			}); //finish ajax
			
		}
	
	});
	
	$('#loginsubmit').click(function() {
									 
		var password = document.getElementById('qpassword').value;
		var email = document.getElementById('qemail').value;
		
		$.ajax({
				method: "GET",url: "/login.php",data: "email="+email+"&password="+password,
				beforeSend: function(){ $('#logging-in').fadeIn(500); }, //show loading just when link is clicked
				complete: function(){ $('#logging-in').slideUp(500); }, //stop showing loading when the process is complete
				success: function(html){ //so, if data is retrieved, store it in html
					var result = html;
					if(result == "") {
							$('#logged-in').html('<p>Invalid Username or Password</p>');
							//$('#logged-in').slideDown(500);
							
							setTimeout
                				(
                        			function()
                        			{
                                		$('#logged-in').slideDown(500);
                        			},
                        			500
                				);
							
						}
						else {
							$('#logged-in').html('<p>Welcome back, <strong>' + result + '</strong></p><p>&nbsp;</p><p>What do you want to do?</p><p>&nbsp;&nbsp;<a href="/members-area/">&raquo; Goto Members Area</a><br />&nbsp;&nbsp;<a href="/members-videos/">&raquo; Goto Members Videos</a><br />&nbsp;&nbsp;<a href="/logout">&raquo; Logout</a></p>');
                            $('#loginform').slideUp(1000);
							 setTimeout
                				(
                        			function()
                        			{
                                		$('#logged-in').fadeIn(1000);
                        			},
                        			1200
                				);
							
							
						}
					}
			}); //finish ajax
		
	});
	  
	$("table.stripe").each(function(){
		$(this).find("tr:odd").addClass("odd");
		$(this).find("tr:even").addClass("even");
	});
	
	$('a[href^="http://"]').attr({target: "_blank", title: "Opens in a new window"});
	$('a[href^="https://"]').attr({target: "_blank", title: "Opens in a new window"});
	
	//equalHeight($(".ecolumns"));
	
	Cufon.replace('#content h3', { fontFamily: 'meta' });
	
	$("#loginform input").focus(function() {
	if( this.value == this.defaultValue ) {
		this.value = "";
	}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
	}
	});
	
	if(typeof document.body.style.maxHeight === "undefined") {	} 
	else {
        $('li.parent').hover(
			function(){
				$('ul', this).stop(true, true).show()
				$(this).addClass("active");
			},
			function(){ 
				$('ul', this).stop(true, true).hide()
				$(this).removeClass("active");
		});
	}
	
	// NEWS SLIDER
	//------------------------------------
	//Speed of the slideshow
	var speed = 6000;
	
	//You have to specify width and height in #slider CSS properties
	//After that, the following script will set the width and height accordingly
	$('#mask-excerpt, #excerpt li').height($('#slider').height());
	
	//Assign a timer, so it will run periodically
	var run = setInterval('newsscoller(0)', speed);	
	
	$('#excerpt li:first').addClass('selected');

	//Mouse over, pause it, on mouse out, resume the slider show
	$('#slider').hover(
	
		function() {
			clearInterval(run);
		}, 
		function() {
			run = setInterval('newsscoller(0)', speed);	
		}
	);
	//-----------------------------------
	
	// EVENT SLIDER
	//-----------------------------------
	//Speed of the slideshow
	var eventspeed = 6000;
	
	//You have to specify width and height in #slider CSS properties
	//After that, the following script will set the width and height accordingly
	$('#eventmask-excerpt, #eventexcerpt li').height($('#eventslider').height());
	
	//Assign a timer, so it will run periodically
	var eventrun = setInterval('eventsscoller(0)', eventspeed);	
	
	$('#eventexcerpt li:first').addClass('selected');

	//Mouse over, pause it, on mouse out, resume the slider show
	$('#eventslider').hover(
	
		function() {
			clearInterval(eventrun);
		}, 
		function() {
			eventrun = setInterval('eventsscoller(0)', eventspeed);	
		}
	);
	//-----------------------------------

});

function newsscoller(prev) {

	//Get the current selected item (with selected class), if none was found, get the first item
	var current_excerpt = $('#excerpt li.selected').length ? $('#excerpt li.selected') : $('#excerpt li:first');

	//if prev is set to 1 (previous item)
	if (prev) {
		
		//Get previous sibling
		var next_excerpt = (current_excerpt.prev().length) ? current_excerpt.prev() : $('#excerpt li:last');
	
	//if prev is set to 0 (next item)
	} else {
		
		//Get next sibling
		var next_excerpt = (current_excerpt.next().length) ? current_excerpt.next() : $('#excerpt li:first');
	}

	//clear the selected class
	$('#excerpt li').removeClass('selected');
	
	//reassign the selected class to current items
	next_excerpt.addClass('selected');

	//Scroll the items	
	$('#mask-excerpt').scrollTo(next_excerpt, 1000);					
	
}

function eventsscoller(prev) {

	//Get the current selected item (with selected class), if none was found, get the first item
	var current_excerpt = $('#eventexcerpt li.selected').length ? $('#eventexcerpt li.selected') : $('#eventexcerpt li:first');

	//if prev is set to 1 (previous item)
	if (prev) {
		
		//Get previous sibling
		var next_excerpt = (current_excerpt.prev().length) ? current_excerpt.prev() : $('#eventexcerpt li:last');
	
	//if prev is set to 0 (next item)
	} else {
		
		//Get next sibling
		var next_excerpt = (current_excerpt.next().length) ? current_excerpt.next() : $('#eventexcerpt li:first');
	}

	//clear the selected class
	$('#eventexcerpt li').removeClass('selected');
	
	//reassign the selected class to current items
	next_excerpt.addClass('selected');

	//Scroll the items	
	$('#eventmask-excerpt').scrollTo(next_excerpt, 1000);					
	
}


