jQuery(document).ready(function($) {

    // $() will work as an alias for jQuery() inside of this function

	//Login SlideToggle
	$(".login-link, #login-cap").click(function(){
		$("#login-expanded-wrapper").slideToggle(function(){
			if($(this).is(":visible")){
				$(this).parent().children(".members-login").children(".login-link").css({backgroundPosition: "103px 9px"});
			}else{
				$(this).parent().children(".members-login").children(".login-link").css({backgroundPosition: "103px -90px"});
			}
			
		});
		$("#login-expanded-wrapper").removeClass("showme");
	});//Login SlideToggle
	
	$("#logged-in-header").click(function(){
		$("#logged-in-wrapper").toggle();		
	});
	
	//Logged In Menu
	var hoverTimeout;
	$("#logged-in-box").hover(function(){
		clearTimeout(hoverTimeout);
		$("#logged-in-wrapper").show();
	},function(){
		hoverTimeout = setTimeout(function(){
			$("#logged-in-wrapper").hide();			
			clearTimeout(hoverTimeout);
			hoverTimeout = 0;
		}, 600);
	});
	//Logged In Slide Toggle
	
	//Search-bar Functionality
	$("#search, .wpsc_product_search input").css({'font-style':  'italic'});
	$(".wpsc_product_search input").val("Search . . .");
	$("#search, .wpsc_product_search input").focus(function(){
		$(this).val("");
		$(this).css({'font-style':  'normal'});
	});
	$("#search, .wpsc_product_search input").blur(function(){
		$(this).val("");
		$(this).val("Search . . .");
		$(this).css({'font-style':  'italic'});
	});//Search-bar Functionality
	
	//--------------iPad Clicks-------------//
	$("#nav li").each(function(){
		$(this).hover(
			function(){
				$(this).addClass("hovered");
				$("ul", this).show();
				$("ul ul", this).hide();
			},
			function(){
				if( $(this).hasClass('hovered')){
				$(this).removeClass("hovered");
				$("ul", this).hide();
				}
			}
		);
		$(this).click(
			function(){
				$("ul", this).toggle();
			}	
		);
	});
	$("#nav li li").each(function(){
				$(this).hover(
			function(){
				$(this).addClass("hovered");
				$("ul", this).show();
				$("ul ul", this).hide();
			},
			function(){
				if( $(this).hasClass('hovered')){
				$(this).removeClass("hovered");
				$("ul", this).hide();
				}
			}
		);
		$(this).click(
			function(){
				$("ul", this).toggle();
			}	
		);
	});//iPad Clicks
	
//------Member Quick Sign In Reroute------//

$("#guild_id").focus(function(){
	var the_url = "//www.christianwritersguild.com/wp-content/themes/cwg/member-signin.php";
	$("#quick-login").attr("action", the_url);
});

//------FAQ Section------//
$("#faq .answer").hide();
$("#faq .faq-section").hide();

//See Sub-Questions
$("#faq a.sections").click(function(){
	$(this).next().toggle();
	//$(this).next().next().toggle();
	
	//Change background image from plus to minus
	if( $(this).hasClass("dropped") ) {
		$(this).removeClass("dropped");
		$(this).css({backgroundPosition: 'left 2px'});
	}else{
		$(this).css({backgroundPosition: 'left -113px'});
		$(this).addClass("dropped");
	}
});

//See answers
$("#faq li a.q").click(function(){

//Change background image from plus to minus
if( $(this).hasClass("dropped") ) {
	$(this).removeClass("dropped");
	$(this).css({backgroundPosition: 'left 5px'});
}else{
	$(this).css({backgroundPosition: 'left -110px'});
	$(this).addClass("dropped");
}
	$(this).closest("li").children(".answer").toggle();
});

//----Sidebar Slide-downs------//
//Append see more link
$("#recent-blog-posts .see-more").html('<a href="javascript:void(0)">See More Posts (+)</a>');
$("#recent-news-posts .see-more").html('<a href="javascript:void(0)">See More Posts (+)</a>');

//Add class to lis to be hidden
$(".blog-posts-list li, .news-posts-list li").each(function(){
	$(this).addClass("to-hide");
});

var postCount = 0;
var newsCount = 0;
$(".blog-posts-list li").each(function(){
	postCount++;
});
$(".news-posts-list li").each(function(){
	newsCount++;
});
/*
if(postCount < 3)
{
	$("#recent-news-posts .see-more").hide();
}

if(newsCount < 2)
{
	$("#recent-blog-posts .see-more").hide();
}
*/

//Segregate the first li from each list
$("ul.blog-posts-list li").first().removeClass("to-hide");
$("ul.news-posts-list li").first().removeClass("to-hide");

$("ul.blog-posts-list li").first().next().removeClass("to-hide");
$("ul.news-posts-list li").first().next().removeClass("to-hide");

//Hide all but the first
$(".to-hide").hide();
//Click to reveal all posts
$("#recent-news-posts .see-more a").click(function(){
	$("#recent-news-posts .to-hide").toggle();
	if ( $(this).html() == 'See More Posts (+)' ){
		$(this).html('See Fewer Posts (-)');
	}else{
		$(this).html('See More Posts (+)');
	}
});

//Click to reveal all posts
$("#recent-blog-posts .see-more a").click(function(){
	$("#recent-blog-posts .to-hide").toggle();
	if ( $(this).html() == 'See More Posts (+)' ){
		$(this).html('See Fewer Posts (-)');
	}else{
		$(this).html('See More Posts (+)');
	}
});


//News Posts

//Social Media
//$(".hidden-list").hide();
//Show Function
$("#primary a.no-underline").click(function(){
	//Checks to see where the BG position should be
	if( $(this).hasClass("sliden") ) {
		$(this).removeClass("sliden");
		$(this).css({backgroundPosition: "right 7px"});
	}else{
	$(this).addClass("sliden");
	$(this).css({backgroundPosition: "right -108px"});
	}
	//Toggle the lesson blurb
	$(this).parent().parent().next('ul').toggle();
});//Sidebar Slide-downs

//------Lessons slide-downs------//
$(".breakdown .lesson-blurb").hide();
$(".breakdown h3 a.lesson").click(function(){
	if( $(this).hasClass("dropped") ) {
		$(this).removeClass("dropped");
		$(this).css({backgroundPosition: "left 1px"});
	}else{
		$(this).addClass("dropped");
		$(this).css({backgroundPosition: "left -114px"});
	}
	//Toggle the lesson blurb
	$(this).parent().next(".lesson-blurb").toggle();
	
});

//Shopping Cart Sidebar
$(".wpsc_buy_button").click(function(){
	$("#shopping-cart-sidebar").css({display: "block"});
});

//----Same as Billing Option-----//

$("#shippingSameBilling").click(function(){
	if($(this).is(":checked"))
	{
		for(var i= 9; i <=16; i++){
		$("tr.wpsc_checkout_field" + i).hide();
		}
	}else{
		for(var i= 9; i <=16; i++){
		$("tr.wpsc_checkout_field" + i).show();
		}
	} 
});

//Mentors Reveal
$(".mentors-reveal").show();
$(".hidden-mentors").hide();

$(".mentors-show").click(function(){
	var the_title = $(".mentors-show").html();
	if( the_title == "See Mentors (+)")
	{
		$(".mentors-show").html("Hide Mentors (-)");
	}
	if(the_title == "Hide Mentors (-)"){
		$(".mentors-show").html("See Mentors (+)");
	}
	$(".hidden-mentors").toggle();
});

//Craftsman Tabs
$(".tab-block").show();
$(".non-fiction-text").hide();
$("a.tab").click(function(){
	$(".selected").removeClass("selected");
	$(this).addClass("selected");
	
	if($(this).hasClass("fiction"))
	{
		$(".non-fiction-text").hide();
		$(".fiction-text").show();
	}
	
	if($(this).hasClass("non-fiction"))
	{
		$(".fiction-text").hide();
		$(".non-fiction-text").show();
	}
	
});

$(".fiction").click(function(){
	$(".tab-select").css({backgroundPosition: "0"});
});

$(".non-fiction").click(function(){
	$(".tab-select").css({backgroundPosition: "0"});
});

/*
* Comment Form Lead Generation
*/

$(".comment-notes").prepend("To leave a comment, please fill out the information below. Your email will be used to contact you with Guild news and updates. ");
$(".comment-form-email").append("<div id=\"agree-block\"><br/><br/><input type=\"button\" id=\"agreement\" value=\"I Agree\" /></div>");

$("#agreement").click(function(){
	var name = $("#commentform #author").val();
	var email = $("#commentform #email").val();
	var emailVerify = checkEmail(email);
	if(!(name == "") && !(email == "") && emailVerify == true)
	{
		$("#agree-block").html('<br/><br/><img src="//www.christianwritersguild.com/wp-content/themes/cwg/images/loading.gif" alt="Loading..." />');
			$.ajax({
			   type: "POST",
			   url: "//www.christianwritersguild.com/wp-content/themes/cwg/comment-process.php",
			   data: "name=" + name + "&email=" + email + "",
			   success: function(){
			   $("#agree-block").html("");
					$("p.comment-form-url, p.comment-form-comment, p.form-allowed-tags, p.form-submit").show();
			   }
			 });//End Ajax		
	}
	else
	{
		alert("You must fill out both your name and email address. Please confirm they are correct.");
	}
});

//Add Proceed to Checkout Link after add to cart is clicked
$(".wpsc_buy_button").click(function(){
	$(this).parent().parent(".buy-now").append(" <a class=\"proceedtocheckout\" style=\"background: #EEEEEE; -moz-border-radius: 5px; border-radius: 5px; border: 1px solid #AAA; text-decoration: none; position: absolute; bottom:-5px; left: 91px; padding: 5px;\" href=\"https://www.christianwritersguild.com/store/checkout/\">Proceed to Checkout</a>");
});

/*
*	InPrint Rotating Box
*/

var count = 0;
var inPrintTotal = $(".inprint-item").length;
$(".inprint-next").click( function(){
	if( !$(".inprint-"+count).is(":animated") )
	{
		if(count < inPrintTotal-1)
		{
			$(".inprint-"+count).fadeOut(function(){
				count++;
				$(".inprint-"+count).fadeIn();		
			});
		}else{
			$(".inprint-"+count).fadeOut(function(){
				count = 0;
				$(".inprint-"+count).fadeIn();
			});
		}
	}
});

$(".inprint-prev").click(function(){
	if( !$(".inprint-"+count).is(":animated") )
	{
		if( count > 0 )
		{
			$(".inprint-"+count).fadeOut(function(){
				count--;
				$(".inprint-"+count).fadeIn();		
			});
		}else
		{
				$(".inprint-"+count).fadeOut(function(){
				count = inPrintTotal-1;
				$(".inprint-"+count).fadeIn();
			});
		}
	}
});

/*
* Two sided lesson button
*/

$(window).resize(function(){
	var theHeight = $(document).height();
	var theWidth = $(document).width();
	$(".overlay").css({height: theHeight, width: theWidth});
});

$(".sb-button").live('click', function(){
	var theHeight = $(document).height();
	var theWidth = $(document).width();
	$(".overlay").css({height: theHeight, width: theWidth});
	$(".overlay").show();
	$(".overlay-window").fadeIn("slow");
	var theContent = '#' + getWindowContent();
	$(".overlay-content").html($(theContent).html());
});

$(".left-button-fiction").click(function(){
	$(".overlay").show();
	var theHeight = $(document).height();
	var theWidth = $(document).width();
	$(".overlay").css({height: theHeight, width: theWidth});
	$(".overlay-window").fadeIn("slow");
});

$(".left-button-nonfiction").click(function(){
	$(".overlay").show();
	var theHeight = $(document).height();
	var theWidth = $(document).width();
	$(".overlay").css({height: theHeight, width: theWidth});
	$(".overlay-window").fadeIn("slow");
});

/*
*	ShadowBox Buttons
*/

$(".sb-button").click(function(){
	$(".overlay").show();
	$(".overlay-window").fadeIn("slow");
});

/*
*	Lessons Shadowbox Window
*/

$(".exit-button, .overlay").click(function(){
	$(".overlay-content").html('');
	$(".overlay, .overlay-window").fadeOut();
	$(".overlay, .overlay-window").hide();
});

/*
*	Alternating Table Row Shading
*/

$(".color-table tr:odd").css("background-color", "#EEE");

if($.browser.msie && $.browser.version == '7.0'){
	var zIndexNumber = 500;
	$('div').each(function() {
		$(this).css('zIndex', zIndexNumber);
		zIndexNumber -= 5;
	}); 
	
}

}); //End Document Ready

function checkEmail(inputvalue){	
    var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
    if(pattern.test(inputvalue)){         
		return true; 
    }else{   
		return false;
    }
}

function setLocation(url){
window.location.href = url;
}

function setWindowContent(content_id){
	window.windowContent = content_id;
}

function getWindowContent(){
	return window.windowContent;
}
