
// For Input Forms

function clearText(srcObj){
if(srcObj.title == srcObj.value) srcObj.value = "";
}

function writeText(srcObj){
if(srcObj.value == "") srcObj.value = srcObj.title;
}

function validateEmail(elementValue){
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	return emailPattern.test(elementValue);
}

$(document).ready(function() {
	$('#enroll-now select').parent().addClass('selector');
	$('#gform_wrapper_1 input:text, #gform_wrapper_1 textarea').each(function() {
		$(this).attr('title', $(this).val());
		
		$(this).focus(function() {
			clearText(this);
		}).blur(function() {
			writeText(this);
			$(this).keyup();
		}).keyup(function() {
			if ($(this).val() == $(this).attr('title') || $(this).val() == '' || $(this).val().length < 2)
			{
				$(this).parent().addClass('gfield_error');
			}
			else if ($(this).attr('title') == 'Email' && validateEmail($(this).val()) == false)
			{
				$(this).parent().addClass('gfield_error');
			}
			else
			{
				$(this).parent().removeClass('gfield_error');
			}
		});
	});
	
	$('#gform_1').submit(function(event)
	{
		event.preventDefault();
		
		var gform_1_ready = true;
		
		$(this).find('input:text, textarea').each(function()
		{
			if ($(this).val() == '' || $(this).val() == $(this).attr('title'))
			{
				gform_1_ready = false;
				$(this).animate({opacity: 0.2}, function() {
					$(this).parent().addClass('gfield_error');
					$(this).animate({opacity: 1}, function() {
						$(this).focus();
					});
				});
				return false;
			}
		});
		
		if (gform_1_ready)
		{
			$.post( $(this).attr('action'), $(this).serialize(), function(data) {
				$('#gform_wrapper_1').parent().append('<div id="gforms_confirmation_message">Thanks for contacting us! We will get in touch with you shortly.</div>');
				$('#gform_wrapper_1').remove();
			} );
		}
	});
});



// Slideshow

function slideSwitch() {
	if (sliding)
	{
		return false;
	}
	else
	{
		sliding = true;
	}
	
    var $active = $('.slideshow div.slides.active');

    if ( $active.length == 0 ) $active = $('.slideshow div.slides:last');

    // use this to pull the divs in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('.slideshow div.slides:first');

    // uncomment below to pull the divs randomly
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
		sliding = false;
        });
}

var sliding = false;
$(document).ready(function() {
	if ($('.slideshow').length > 0)
	{
		setInterval( "slideSwitch()", 7000 );
	}
	
	$('.slideshow .arrow').click(function()
	{
		if (sliding)
		{
			return false;
		}
		
		slideSwitch();
		return false;
	});
});


// Dropdown

$(document).ready(function() {

	$("#dd1.dropdown dt").find('a').click(function(event) {
		event.preventDefault();
		if ($('#dd1.dropdown dd .toggle').is(':hidden')) {
			$('#dd1.dropdown dt').find('a').addClass('active');	
		} else {
			$('#dd1.dropdown dt').find('a').removeClass('active');	
		}
		$("#dd1.dropdown dd .toggle").slideToggle()
		$("#dd2.dropdown dd .toggle").slideUp();
		$("#dd3.dropdown dd .toggle").slideUp();
	});
			
			
	$("#dd2.dropdown dt").find('a').click(function(event) {
		event.preventDefault(); 
		if ($('#dd2.dropdown dd .toggle').is(':hidden')) {
			$('#dd2.dropdown dt').find('a').addClass('active');	
		} else {
			$('#dd2.dropdown dt').find('a').removeClass('active');	
		}
		$("#dd2.dropdown dd .toggle").slideToggle();
		$("#dd1.dropdown dd .toggle").slideUp();
		$("#dd3.dropdown dd .toggle").slideUp();
	});
	
	$("#dd3.dropdown dt").find('a').click(function(event) {
		event.preventDefault(); 
		if ($('#dd3.dropdown dd .toggle').is(':hidden')) {
			$('#dd3.dropdown dt').find('a').addClass('active');	
		} else {
			$('#dd3.dropdown dt').find('a').removeClass('active');	
		}
		$("#dd3.dropdown dd .toggle").slideToggle();
		$("#dd1.dropdown dd .toggle").slideUp();
		$("#dd2.dropdown dd .toggle").slideUp();
	});
	
	$(document).bind('click', function(e) {
		var $clicked = $(e.target);
		if (! $clicked.parents().hasClass("dropdown") && $('.dropdown dd .toggle').is(':hidden')) {
			$(".dropdown dd .toggle").slideUp();
			$('.dropdown dt').find('a').removeClass('active');	
		}
	});
      function getSelectedValue(id) {
          return $("#" + id).find("dt a span.value").html();
      }

});



// Nav Dropdown 


var arrowimages={down:['downarrowclass', 'down.gif', 23], right:['rightarrowclass', 'right.gif']}

var jqueryslidemenu={

animateduration: {over: 200, out: 100}, //duration of slide in/ out animation, in milliseconds

buildmenu:function(menuid, arrowsvar){
	jQuery(document).ready(function($){
		var $mainmenu=$("#"+menuid+">ul")
		var $headers=$mainmenu.find("ul").parent()
		$headers.each(function(i){
			var $curobj=$(this)
			var $subul=$(this).find('ul:eq(0)')
			this._dimensions={w:this.offsetWidth, h:this.offsetHeight, subulw:$subul.outerWidth(), subulh:$subul.outerHeight()}
			this.istopheader=$curobj.parents("ul").length==1? true : false
			$subul.css({top:this.istopheader? this._dimensions.h+"px" : 0})
			
			$curobj.hover(
				function(e){
					var $targetul=$(this).children("ul:eq(0)")
					$targetul.parent().find("a").addClass("active");
					this._offsets={left:$(this).offset().left, top:$(this).offset().top}
					var menuleft=this.istopheader? 0 : this._dimensions.w
					menuleft=(this._offsets.left+menuleft+this._dimensions.subulw>$(window).width())? (this.istopheader? -this._dimensions.subulw+this._dimensions.w : -this._dimensions.w) : menuleft
					if ($targetul.queue().length<=1) //if 1 or less queued animations
						$targetul.css({left:menuleft+"px", width:this._dimensions.subulw+'px'}).slideDown(jqueryslidemenu.animateduration.over)
				},
				function(e){
					var $targetul=$(this).children("ul:eq(0)")
					$targetul.parent().find("a").removeClass("active");
					$targetul.slideUp(jqueryslidemenu.animateduration.out)
				}
			) //end hover
		}) //end $headers.each()
		$mainmenu.find("ul").css({display:'none', visibility:'visible'})
	}) //end document.ready
}
}

//build menu with ID="myslidemenu" on page:
jqueryslidemenu.buildmenu("nav", arrowimages)


// Welcome


$(document).ready(function() {
	$('.welcome .box-content').addClass('less');
	$('.welcome .box-content').find('p').hide();
	$('.welcome .box-content').find('p:eq(0)').show();
	$('.welcome .more-btn1.more').hide();
	$('.welcome .more-btn a').click(function() {
			$('.welcome .box-content').removeClass('less');
			$('.welcome .box-content').addClass('more');	
			$('.welcome .box-content').find('p').show();
			$('.welcome .bb').fadeIn();
			$('.welcome .more-btn').hide();
			$('.welcome .more-btn1.more').show();
			return false;
	});
	$('.close-btn').click(function() {
			$('.welcome .box-content').removeClass('more');
			$('.welcome .box-content').addClass('less');
			$('.welcome .box-content').find('p').hide();
			$('.welcome .box-content').find('p:eq(0)').show();
			$('.welcome .bb').fadeOut();
			$('.welcome .more-btn').show();
			$('.welcome .more-btn1.more').hide();
			return false;
	});
});





// PopUP


var popupStatus = 0;

function disablePopup(){
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$(".popupContent").fadeOut("slow");
		popupStatus = 0;
		
	}
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	$(".popupContent").append("<a class='popupClose'>close</a>");
	$("a.popup").click(function(){
		var popID = $(this).attr("rel");
		//load popup
			if(popupStatus==0){
				$("#backgroundPopup").css({
					"opacity": "0.3"
				});
				$("#backgroundPopup").fadeIn("slow");
				$("#" + popID).fadeIn("slow");
				$(".popupContent").css('visibility','visible');
				popupStatus = 1;
			}
			var windowWidth = document.documentElement.clientWidth;
			var windowHeight = document.documentElement.clientHeight;
			var popupWidth = ($("#" + popID).width() + 0) / 2;
			var popupHeight = ($('#' + popID).height() + 25) / 2;
			//centering
			$("#" + popID).css({
				'margin-top' : -popupHeight,
				'margin-left' : -popupWidth
			});
			return false;
			

	});
				
	//CLOSING POPUP
	$(".popupClose").click(function(){
		disablePopup();
		return false;
	});
	
	$(".close-btn").click(function(){
		disablePopup();
		return false;
	});
	$(".popclose-btn").click(function(){
		disablePopup();
		return false;
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
		return false;
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});


// Tabs 

$(document).ready(function() {

	//Default Action
	$(".tab-content").hide(); //Hide all content
	//$(".tab-content").css('visibility','hidden');
	$("ul.tabs li:first").addClass("active").show(); //Activate first tab
	$(".tab-content:first").show(); //Show first tab content
	//$(".tab-content:first").css('visibility','visible');
	
	//On Click Event
	$("ul.tabs li").click(function() {
		$("ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab-content").hide(); //Hide all tab content
		var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active content
		return false;
	});
});



// Course Listing 

$(document).ready(function() {
	$('#course .listing ul li').hover(function() {
			$(this).find('.hover').animate({ opacity:'toggle'}, 100)	
			$(this).find('.details').addClass('border');
	}, function() {
			$(this).find('.hover').animate({ opacity:'toggle'}, 100)	
			$(this).find('.details').removeClass('border');
	});		   
	
	$('#course .listing ul li .hover').click(function() {
		
		window.location.href = $(this).parent().find('a').attr('href');
		
		});	
});




// Sidebar-Menu



function initMenu() {
 		  var $eachmenu = $('#sidebar' + '>ul');
		  var $eachsub = $eachmenu.find('ul').parent();
		  $eachsub.children('ul').hide();
		  $eachsub.children('ul.current').show();
		  $('#sidebar ul li a + ul').parent().children('a').click(function(event){
			event.preventDefault();
			var $targetContent = $(this).parent().children('ul:eq(0)');
			if ($targetContent.is(':hidden')) {
				$targetContent.slideToggle('normal');
				$targetContent.parent().find('a').addClass('active');
				$targetContent.parent().find('a:eq(0)').addClass('bold');
			} else {
				$targetContent.slideToggle('normal');
				$targetContent.parent().find('a').removeClass('active');
				$targetContent.parent().find('a:eq(0)').removeClass('bold');
			}
		  });
		  $('#sidebar ul li a.active+ul').show();
		  $('#sidebar ul li a.active+ul').parent().children('a').addClass('active');
		  $('#sidebar ul li a.active+ul').parent().children('a').addClass('bold');
		  /*$eachsub.each(function(i){
				 var $cursub= $(this);
				 $cursub.click(function(event) {
							event.preventDefault();
							var $targetContent = $cursub.children('ul:eq(0)');
							if ($targetContent.is(':hidden')) {
								$targetContent.slideToggle('normal');
								$targetContent.parent().find('a').addClass('active');
								$targetContent.parent().find('a:eq(0)').addClass('bold');
							} else {
								$targetContent.slideToggle('normal');
								$targetContent.parent().find('a').removeClass('active');
								$targetContent.parent().find('a:eq(0)').removeClass('bold');
							}
				});
		  });*/
}
$(document).ready(function() {initMenu();});


// Anchor

function goToByScroll(id){
     	$('html,body').animate({scrollTop: $("#"+id).offset().top},'slow');
}



// Faq Accordion

$(document).ready(function(){
	
$('.modules-container').hide(); 
$('.faq h4').addClass('modules-trigger');
$('.modules-trigger').click(function(){
	if( $(this).next().is(':hidden') ) { 
		$('.modules-trigger').removeClass('active').next().slideUp(); 
		$(this).toggleClass('active').next().slideDown(); 
	}
	else {
		$(this).next().slideUp()
		$('.modules-trigger').removeClass('active').next().slideUp();
	}
	return false; 
});

});

function goToByScroll(id){
      $('html,body').animate({scrollTop: $("#"+id).offset().top},'slow');
}


$(document).ready(function() {

	$('#input_2_6').focus(function(){
		$(this).attr('value', '');
		if('Name' == $(this).attr('value')){ 
		$(this).attr('value', '');
		}
	});
	
	$('#input_2_6').blur(function(){
		if($(this).attr('value') == ""){
			$(this).attr('value','Name');
		};
	});
	
	$('#input_2_3').focus(function(){
		$(this).attr('value', '');
		if('Email' == $(this).attr('value')){ 
		$(this).attr('value', '');
		}
	});
	
	$('#input_2_3').blur(function(){
		if($(this).attr('value') == ""){
			$(this).attr('value','Email');
		};
	});
	
	$('#input_2_2').focus(function(){
		$(this).attr('value', '');
		if('Phone' == $(this).attr('value')){ 
		$(this).attr('value', '');
		}
	});
	
	$('#input_2_2').blur(function(){
		if($(this).attr('value') == ""){
			$(this).attr('value','Phone');
		};
	});
	
	$('#input_2_5').focus(function(){
		$(this).attr('value', '');
		if('Comments' == $(this).attr('value')){ 
		$(this).attr('value', '');
		}
	});
	
	$('#input_2_5').blur(function(){
		if($(this).attr('value') == ""){
			$(this).attr('value','Comments');
		};
	});
//onblur="writeText(this);" onfocus="clearText(this);"	
});

//Submit courses form
function submit_form_data(){
	var form = $('#choose-your-study');
	var ready = true;
	
	form.find('dl').each(function() {
		if ($(this).find('input:checked').length == 0)
		{
			ready = false;
			return false;
		}
	});
	
	if ( ! ready)
	{
		alert('Please ensure all options are correctly filled out.');
		return false;
	}
	
	$.ajax({
		type: "POST",
		url: form.attr( 'action' ),
		data: form.serialize(),
		success: function( response ) {
			$('.form-content').html( response );
		}
	});
	//form.find('select:first').change( function() {
    //} );
}

$(document).ready(function() {
	$('form#f_pathway_submit').submit(function(event) {
		if ($('[name=field]:checked').length == 0)
		{
			event.preventDefault();
			alert('Please select a field of study from the list.');
			return false;
		}
	});
	
	$('form#f_pathway_submit input, form#choose-your-study input').change(function() {
		$(this).parents('.dropdown').find('input[value!="' + $(this).val() + '"]').prop('checked', false);
		
		var text = $(this).parent().text();
		
		if ($(this).is(':checked'))
		{
			$(this).parents('.dropdown').find('dd .toggle').slideToggle();
			
			$(this).parents('.dropdown').find('dt a span').each(function() {
				if ($(this).attr('title') == null)
				{
					$(this).attr('title', $(this).text());
				}
				$(this).text(text);
			});
		}
		else
		{
			$(this).parents('.dropdown').find('dt a span').each(function() {
				$(this).text($(this).attr('title'));
			});
		}
	});
	
	$('form#f_pathway_submit input:checked').each(function() {
		var text = $(this).parent().text();
		$(this).parents('.dropdown').find('dt a span').each(function() {
			$(this).attr('title', $(this).text());
			$(this).text(text);
		});
	});
});

$(document).ready(function() {
	$('#enroll-now select').next('label').css('display','none');
	$('#gform_wrapper_3 input:text').each(function(){
		var text = $(this).next('label').html();
		$(this).next('label').css('display','none');
		if($(this).attr('value')== ""){
			$(this).attr('value',text);
		}
		
		$(this).focus(function(){
			if($(this).attr('value') == text){
			$(this).attr('value','');
			}
		});
		
		$(this).blur(function(){
			if($(this).attr('value') == ''){
			$(this).attr('value',text);
			}$('#enroll-now select')
		});
	});
	
	$('#enroll-now input:button').each(function(e){
		var onclickValue = $(this).attr('onclick');
		var newvalue = onclickValue.replace('jQuery("#gform_3").trigger("submit",[true]); ', " ");
		$(this).attr('onclick',newvalue);
	});
	$('#enroll-now input:button').click(function(e){
		$('#gform_wrapper_3 input:text').each(function(){
			var check_text = $(this).next('label').html();
				if($(this).attr('value') == check_text){
					$(this).attr('value','');
				}
				
		});
		jQuery("#gform_3").trigger("submit",[true]);
	});
	
	//Prepend error image
	var error_img = '<img width="30" height="23" alt="error" src="http://greenwich.bywavehost.com/wp-content/themes/greenwich/images/buttons/error.png">';
	$('#enroll-now .validation_error').prepend(error_img);
	
	var error_dir = '<span class="error-arrow"></span>';
	$('#enroll-now .gfield_error div.ginput_complex span').append(error_dir);
	$('#enroll-now .gfield_error div.ginput_container:not(.ginput_complex)').append(error_dir);
	$('#enroll-now .gf_progressbar_percentage').css('width','');
});

