// JavaScript Document
$(document).ready(function() {
	function validatePhoneNumber(phoneElement) {
		if (jQuery.trim(phoneElement) == "") {
			return false;
		}		
		var regex = /[0-9]{10}/;		
		if (phoneElement.match(regex)) {
			return true;
		}
		return false;
	}
	function echeck(str) { //Check email address
		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID");
		   return false;
		}
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID");
		   return false;
		}
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
			alert("Invalid E-mail ID");
			return false;
		}
		 if (str.indexOf(at,(lat+1))!=-1){
			alert("Invalid E-mail ID");
			return false;
		 }
		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
			alert("Invalid E-mail ID");
			return false;
		 }
		 if (str.indexOf(dot,(lat+2))==-1){
			alert("Invalid E-mail ID");
			return false;
		 }
		 if (str.indexOf(" ")!=-1){
			alert("Invalid E-mail ID");
			return false;
		 }
		return true;
	}
	
	// Top Navigation Script
	$('.defaultText').defaultText();
	$("ul.main-menu > li").bind("mouseover", navigation_open);
    $("ul.main-menu > li").bind("mouseout",  navigation_timer);
	
	$('ul#slide').cycle({
        fx:      'scrollHorz',
        speed:   1000,
		timeout: 5000,
		next:    '#next',
		prev:    '#prev',
		after:  function(curr, next, opts, fwd) {
			var number = opts.currSlide + 1;
			$('#counts').html(number < 10 ? '0' + number : '' + number);
    	}
    });
	
	$('ul#page-gallery').cycle({
        fx:      'scrollHorz',
        speed:   1000,
		timeout: 5000,
		next:    '#gal-next',
		prev:    '#gal-prev'
    });
	
	$("form#formContact").submit(function(){
		var name = $("form#formContact input[name=name]");
		var phone = $("form#formContact input[name=phone]");
		var email = $("form#formContact input[name=email]");
		var project = $("form#formContact select[name=project] option:selected");	
		if(jQuery.trim(name.val()) == "" || jQuery.trim(name.val()) == "Name") {
			alert("Please enter your name!");
			name.focus();
			return false;
		} else if(echeck(email.val())==false) {
			email.focus();
			return false;
		} else if(project.val() == 0) {
			alert("Please select a department!");
			project.focus();
			return false;
		}	
		return true;
	});
	
	$("form#formDonate").submit(function(){
		var project = $("form#formDonate select[name=project] option:selected");		
		if(project.val() == 0) {
			alert("Please select a project!");
			project.focus();
			return false;
		}
		$(this).attr('action',project.val());
		return true;
	});
	
	$("form#formVolunteer").submit(function(){
		var name = $("form#formVolunteer input[name=name]");
		var email = $("form#formVolunteer input[name=email]");
		var project = $("form#formVolunteer select[name=project] option:selected");	
		if(jQuery.trim(name.val()) == "" || jQuery.trim(name.val()) == "Name") {
			alert("Please enter your name!");
			name.focus();
			return false;
		} else if(echeck(email.val())==false) {
			email.focus();
			return false;
		} else if(project.val() == 0) {
			alert("Please select a project!");
			project.focus();
			return false;
		}		
		return true;
	});
	
	$("form#formSubscribe").submit(function(){
		var email = $("form#formSubscribe input[name=email]");
		if(echeck(email.val())==false) {
			email.focus();
			return false;
		}
		return true;
	});
	
	if($('.first-pharagraph').length != 0) {
		if($('.first-pharagraph').find('p').length > 1) {
			$('.first-pharagraph').find('p, h2, ul').hide();
			$('.first-pharagraph').find('p:first').show();
		} else { $('a.read-more').hide(); }
	}
	$('a.read-more').click(function(){
		if($(this).attr('hide') == 'true') {
			$('.first-pharagraph').find('p, h2, ul').show();
			$(this).attr('hide','false');
			$(this).html('Read Less &gt;');			
		} else if($(this).attr('hide') == 'false') {
			$('.first-pharagraph').find('p, h2, ul').hide();
			$('.first-pharagraph').find('p:first').show();
			$(this).attr('hide','true');		
			$(this).html('Read More &gt;');
		}
	});
	$('#photos img').each(function() {
		$(this).hover(function() {
			$(this).stop().animate({ opacity: 0.7 }, 50);
		},
	   function() {
		   $(this).stop().animate({ opacity: 1.0 }, 50);
	   });
    });
	$('.fancybox').fancybox({		
		titleShow : false,
		width : 640,
		height : 480,
		autoDimensions : false,
		overlayShow : true
	});
	
});


