$(document).ready(function(){
	
	//focus clear fields
	$('.default').each(function(){
		$(this).css({'color' : '#aeaeae'});
		var _default = $(this)[0].defaultValue;//document.getElementById($(this).attr('id')).defaultValue;
		$(this).focus(function(){
			$(this).css({'color' : '#000'});
			if($(this).val() == _default){
				$(this).val('');	
			}
		});
		$(this).blur(function(){
			if($(this).val() == ''){
				$(this).val(_default);	
				$(this).css({'color' : '#aeaeae'});
			}
		});						
	});
	//end of clear focus
	
	
	//textarea simple autoexpand
	$('textarea').each(function(){
		$(this).css({overflow: 'hidden'});
		$(this).each(function(){
			if($(this)[0].scrollHeight > $(this).height())	$(this).css({height: $(this)[0].scrollHeight + 50});
		});
		$(this).keyup(expandTextarea);
	});
	
	function expandTextarea(){
		if($(this)[0].scrollHeight > $(this).height()){
			$(this).animate({height: $(this)[0].scrollHeight + 50}, {duration:200, queue:false});
		}
	}
	//end of autoexpand
	

	//generic toggler
	$('.toggler').each(function(){
		$('.toggler').next().hide();
	});
	$('.toggler').click(function(){	
		var _h = parseInt($(this).next().css('height'));//height();
		if(_h > 0 || _h == NaN){
			$(this).removeClass('selected');
		}else{
			$(this).addClass('selected');
		}
		$(this).next().animate({height: 'toggle', opacity: 1}, 300);
		if($(this).children('a').text() == '[-]'){
			$(this).children('a').text('[+]');
		}else{
			$(this).children('a').text('[-]');
		}
		return false;
	});
	$('.toggler_close').click(function(){
		obj = $(this);
		while($(obj).prev('.toggler').length == 0){
			obj = $(obj).parent();	
		}
		$(obj).prev().trigger('click');
		return false;
	});
	//end of generic toggler
	
	
	//simple alert
	$(".alert").animate({opacity: 1}, 3000).animate({opacity: 0 }, {duration: 600, easing: 'easeInQuad'}).animate({height: 0, paddingTop: 0, paddingBottom: 0, borderWidth: 0, marginBottom: 0, marginTop: 0}, {duration: 500, easing: 'easeInQuad', complete: 
		function(){$(this).remove();}
	});
	//end of alert


	$('#frm_subscribe').submit(function(){	
		var r = true;
		if($('#frm_subscribe #email')[0].value.search(/([^\/.]+)@([^\/.]+)\.([^\/.]+)/) == -1){
			$('label.email').css({color: '#f00'})
			r = false;
		}
		
		if($('input.radio:checked').length == 0){
			$('label.radio').css({color: '#f00'});
			r = false;
		}
		
		if(!r) $('#msg').text('Please complete all required information to subscribe').css({opacity: 0, display: 'block'}).animate({opacity: 1});
		
		return r;
	});
	
	if(window.location.hash == '#subscribe-success'){
		$('#msg').text('Subscription Successful').css({width: 200, color: '#000', display: 'block', padding: 10, background: '#e1f8dd', border: '1px solid #67dc53'}).animate({opacity: 1}, 3000).animate({opacity: 0 }, {duration: 600, easing: 'easeInQuad'}).animate({height: 0, paddingTop: 0, paddingBottom: 0, borderWidth: 0, marginBottom: 0, marginTop: 0}, {duration: 500, easing: 'easeInQuad', complete: 
		function(){$(this).remove();}
	});
	}
	
	$('#frm_contact').submit(function(){
		var r = true;
		
		if($('#frm_contact #email')[0].value.search(/([^\/.]+)@([^\/.]+)\.([^\/.]+)/) == -1){
			$('#frm_contact #email').prev('label').css({color: '#f00'})
			r = false;
		}
		
		if($('#frm_contact #name').val() == ''){			
			$('#frm_contact #name').prev('label').css({color: '#f00'});
			r = false;
		}
				
		if(!r) $('#msg').text('Please complete all required information so we can fulfill your request').css({opacity: 0, display: 'block'}).animate({opacity: 1});
		
		return r;
	});


	$('.nav>li>ul').each(function(){
			var left = $(this).parent().offset().left;
			$(this).css({left: left, width: $(this).width()+10});	
				
	}).hover(
		function(){
			$(this).parent().addClass('selected');
		},
		function(){
			$(this).parent().removeClass('selected');		
		}
	);
	
	
		$('embed').attr('wmode', 'transparent').parent().wrap('<div>');
		
	$('#verse').click(function(){
	
	
		var modal = $('<div id="modal"></div>');
		modal.css({opacity: 0});
		$(document.body).append(modal);
		
		var close_btn = $('<a href"#close" class="close">&times;</a>');
		close_btn.click(function(){
			modal.animate({opacity: 0}, {duration: 300, queue: false, complete: function(){$(this).remove()}});
		
			return false;
		});
		
		$.ajax({
			url: '/verse',
			dataType: 'json',
			success: function(data){
				modal.html(data.description);
				modal.prepend(close_btn);
				modal.css({left: ($(window).width() - modal.outerWidth())/2, top: ($(window).height() - modal.outerHeight())/2}).animate({opacity: 1}, {duration: 300, queue: false});	
			}
		});
		
		return false;
	});
	
	
	
	//On Focus and Auto Refill Default Text of Input
		
		/*On Focus Clear Text*/
		$("input[type=text]").focus(function() {
			if ($(this).val() == "enter keywords to search") {
				$(this).val("");
			}
			if ($(this).val() == "enter your email address") {
				$(this).val("");
			}
    	});
		
		/*On Blur Refill Default Value*/
		$("input[type=text]").blur(function() {
			if ($("#q").val() == "") {
				$(this).val("enter keywords to search"); //determines if this value is in the input
			}
			if ($("input.newsletter").val() == "") {
				$(this).val("enter your email address");//determines if this value is in the input
			}
		});
	
		
});



