/*
File: scripts.js
Author: Joseph McCullough (@joe_query)
Date: March 23, 2011
Purpose: General JavaScript for School House
<!-- ////////////////////////////////////////


       0101110000101111001100110
	   111001000110 111001001000
	   01101110111   01010011011
	   0001100010     0110000001
	   001000010       000000110
	   00001011         10000101
	   1110101           1100001
	   0111101101     1100111101
	   0001000000     1110101011
	   1001000100     0000100000
	   0001001000010010000100001
	               0010000000111
				          011001
							 010
							  01
							  
							  
		

//////////////////////////////////////////-->							  
*/

(function($){


//=========================== Books Page =============================//
var theRootDir = "http://www.teachertrainingbooks.com/";
var bookAjax = new POST(theRootDir + "wp-content/themes/schoolhouse/getbooks.php");
var theselectedone = $(".selected");
bookAjax.callback(function(response)
	{
		$("#bookWrapper").html(response);
	});
$("select").selectbox({
	onChange: function (val, inst) {
		if($(theselectedone).length)
		{
			bookAjax.set($(theselectedone).closest("select").attr("name"), $(theselectedone).val());
			theselectedone = false;
		}
		bookAjax.set(inst.id, val);
		bookAjax.getResponse();
	}
});

//Append drop down image to select boxes
var dropDownImg = theRootDir + "images/dropdown.png";
$(".sbSelector").append('<img class="dropdown" src="' + dropDownImg + '" alt="Drop Down">');

//Remove border from first li element
$(".sbOptions").each(function()
{
	$(this).children("li:first").css("borderTop", "0px");
});


//Truncate long strings
var selectedItem = $(".sbSelector span");
$(selectedItem).each(function()
{
	if($(this).text().length > 23)
	{
		$(this).text($(this).text().substr(0,23) + "...");
	}
});


//Make sure there is enough room for the drop down stuff
var numBoxes = $(".sbOptions").children().length;
var boxHeight = $(".sbSelector").height();
$("#shop_nav").css("minHeight", numBoxes * boxHeight);

//Don't show go button
$("#sidebar_search_go").hide();

//---------------------------------------------------------------------
// Clear Search Textbox when clicked
//---------------------------------------------------------------------
$("input[id$='search']").one('focus', function(){
	$(this).val('');
});

//=========================== Contact Page =============================//
//---------------------------------------------------------------------
// Form Validation
//---------------------------------------------------------------------
var form = new Form("#contactform",
{
	hasNone:
	[
		  {id: $("#comments"), types: "mailstrings,html,bbcode"},
		  {id: $("#company"), types: "mailstrings,html,bbcode"},
		  {id: $("#quantity"), types: "mailstrings,html,bbcode"}
	]
});
form.quickform();	

//Check for invalid on focusout
$(form.inputs()).focusout(function()
{
	//If empty and required or invalid, add invalid class
	if( form.invalid($(this)) || (!$(this).val() && $(this).hasClass(form.requiredClass())) )
	{
		$(this).addClass(form.invalidClass());
		$(this).parents("dd").prev().children().addClass(form.invalidClass());
	}
	else
	{
		$(this).removeClass(form.invalidClass());
		$(this).parents("dd").prev().children().removeClass(form.invalidClass());
	}
});
	
//If is currently invalid, check for correction each keystroke
$(form.inputs()).keyup(function()
{
	if( $(this).hasClass(form.invalidClass()) )
	{	
		if(form.valid($(this)))
		{
			$(this).removeClass(form.invalidClass());
			$(this).parents("dd").prev().children().removeClass(form.invalidClass());
		}
	}
});

form.onSend(function()
{
});

form.onClientInvalid(function(response)
{
	$(form.invalid()).addClass(form.invalidClass());
	$(form.invalid()).parents("dd").prev().children().addClass(form.invalidClass());
});

form.onSuccess(function()
{
	form.clear();
	$("#contactform textarea").val("Your message has been sent. Thank you.");		
});


//Make amazon hit box bigger
var amazon = $("#amazon");
	$(amazon).css("cursor", "pointer");

    $(amazon).click(function()
    {
        window.location=$(this).find("a").attr("href");
        return false;
	});

 })(jQuery);

