$(document).ready(function(){
						   
	$('.widget:last-child').addClass("last");
	$('.widget:first-child').addClass("first");
	
	$('blockquote').each(function(){
		var start = '<span class="quote start">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>';
		var end = '<span class="quote end">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>';
		if ($(this).find('> p').size()>0){
			$(this).find('> p').prepend(start);
			$(this).find('> p').append(end);
		}
		else {
			$(this).prepend(start);
			$(this).append(end);
		}
	});
	
	
	/*
		Main menu dropdown	
	*/
	$("#mainMenu li").hover(
	function(){
		$(this).find("ul").slideDown(300);
		
	},
	function(){
		$(this).find("ul").slideUp(300);

	});
	
	
	/*
		Dropdowns (subscriber services page)
	*/
	$('.dropdown h3').click(function(){
		$(this).toggleClass('on');
		$(this).parent().find('.content').slideToggle(250);
		
		return false;
	});
	
	if (window.location.hash!=""){
		$(window.location.hash).find('h3').trigger('click');
	}
	
	
	if ($('#print-subscribers .content').is(":visible")){
		$(window).scrollTop(500);
	}
	
	/*
	Login Form
	*/
	function showLoginForm(){
		$('#loginForm .overlay').css('opacity',0.7);
		$('#loginForm').fadeIn(250);
		$('body').scrollTop(0);
		$('body').css('overflow','hidden');
		$('#loginForm .new_email input').val("");
		return false;
	}	
	$(window).bind('scroll',function(e){	
		$('#loginForm').css('top',$(window).scrollTop());
	});
	
	if (window.location.hash=="#login") { showLoginForm();}
	$('a[href="#login"]').click(showLoginForm);	
	
	$('#loginForm .close').click(function(){$('#loginForm').fadeOut(250); $('body').css('overflow','visible');})
	$(document).keydown(function(e) {  
	    if (e.keyCode == 27) {
			$('#loginForm').fadeOut(250);
			$('body').css('overflow','visible');
		}
	});
	
	$('#loginForm form input').keyup(function(e) {
		if(e.keyCode == 13)
			$(this).parents("form").trigger("submit");		
	});
	
	$('#loginForm form').submit(function(even){
		even.preventDefault();
		e = $(this);
		
		var ajaxurl = e.find("input[name='ajax']").val();
		$.get(ajaxurl,{
				"log":e.find("input[name='log']").val(),
				"pwd":e.find("input[name='pwd']").val(),
				"new_email":e.find("input[name='new_email']").val(),
				"new_email_check":e.find("input[name='new_email_check']").val()
		},function(data){
			if (data=="success"){
				window.location.href = e.find("input[name='redirect_to']").val();
			}
			else if(data=="noemail"){
					e.find('.new_email').fadeIn();
					e.find('.error').html("Please provide your email address");
			}
			else{
				e.find(".error").html(data);
			}
		});
		
		
		return false;
	});

	
	
	/*
		View all archives button (blog)
	*/
	$('a.view-archives').click(function(){
		$(this).parent().find('.last12').hide();
		$(this).parent().find('.all').slideDown(250);
		$(this).remove();
		
		return false;
	});
	
	
	
	/*
		Subscribe form validation
	*/
	var subscribeForm = $('#subscribeForm form');
	
	
	function warningMsg(text){		
		return '<span style="display:none"; class="warning">'+text+'</p>';		
	}
	function displayWarnings(){
		$('.warning').fadeIn(500);
		return $('.warning').size();
	}
	function checkMail(email){
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (filter.test(email)) {
			return true;
		}
		return false;
	}
	
	subscribeForm.find('input[name="type"]').change(function(){
		var target = $('#'+$(this).attr('id')+'-desc');
		$(this).parents('.typeSelection').find('.type:visible').fadeOut(250,function(){
			target.fadeIn(250);
		});
		
	});
	
	
		
	
	subscribeForm.bind('submit',function(){
		$(this).find('.warning').remove();
		
			
		ajaxurl = $('input[name="ajax"]').val();
		
		
		
		subscribeForm.find('input[type="text"], input[type="password"]').each(function(event){
			
			var e = $(this);
			
			
			if (e.attr("name")=="address2" || e.attr("name")=="company"){
				
			}			
			else if (e.val()==""){
				e.after(warningMsg('This field is required'));
			}
			else {
				//AJAX checks (only if not NULL)
				
				if(e.attr('name') == "username" || e.attr('name') == "email"){				
					$.get(ajaxurl,{field:e.attr('name'),value:e.val()},function(data){
						if (data!="valid") e.after(warningMsg(data),true); displayWarnings();						
					});
				}
				
			}
		});
		
		if ($(this).find('input[name="password"]').val()!=$(this).find('input[name="password_check"]').val()){
			$(this).find('input[name="password_check"]').after(warningMsg('Passwords do not match'));
		}
		else if($(this).find('input[name="password"]').val().length < 5 && $('input[name="password"]').val()!=""){
			$(this).find('input[name="password_check"]').after(warningMsg('Password too short. Minimum 5 characters.'));			
		}
		
		if (!checkMail($(this).find('input[name="email"]').val()) && $(this).find('input[name="email"]').val()!="")
			$(this).find('input[name="email"]').after(warningMsg('Invalid email address'));
		

		if($(this).find('input[name="tos"]').attr('checked')!="checked")
			$(this).find('input[name="tos"]').parent().after(warningMsg('You have to agree to proceed.'));
		
		
		if (displayWarnings()>0){	
			return false;
		}
		else{
			return true;
		}
										 
	});
	
	
	
	
});
