
// ABENDAGO SITE COMPONENTS

var formtypes = "input,select,textarea,fieldset";
var windowIndex = 0;
var effectSpeed = 400;

var RecaptchaOptions = {
   theme : 'white'
};

function unloadMessage () { return "You will lose your work. Print it or email it now!"; }
function setConfirmUnload (on) { window.onbeforeunload = (on) ? unloadMessage : null; }

function orderWindowIndex (obj) {
	// get the current z-index value so we can use it later
	var currIndex = $(obj).css("z-index");
	// set z-index to temp value
	$(obj).css({"z-index":999});
	// loop windows
	$(".ui-draggable:visible").each(function(){
		// get the z-index value from each window
		var thisIndex = $(this).css("z-index");
		// if this window index is larger than the saved index then reduce by one
		if (thisIndex>currIndex) {
			$(this).css({"z-index":(thisIndex-1)});
		}
	});
	// now that the larger indexes are reduced, this leaves the largest index open for grabs
	$(obj).css({"z-index":windowIndex});
}

function toggleFieldset() {
	var fieldset = $(this).parents("fieldset").nextAll("fieldset").eq(0);
	if ($(this).attr("checked")===true) {
		fieldset.show().attr({"ignore":false}).find(formtypes).each(function(){ $(this).show().attr({"ignore":false}); });
	} else {
		fieldset.hide().attr({"ignore":true}).find(formtypes).each(function(){ $(this).hide().attr({"ignore":true}); });
	}
}

function iFrameReload(frameID,windowID,bParent) {
	var oFrame = (bParent===true)? parent.document.getElementById(frameID):document.getElementById(frameID);
	var oWindow = (bParent===true)? parent.document.getElementById(windowID):document.getElementById(windowID);
	oFrame.contentWindow.location.reload(true);
	$(oWindow).removeClass('loaded');
	return false;
}

$(document).ready(function(){

	// webkit browsers load CSS and JS at the same time so make them wait a bit more
	if (jQuery.browser.safari && document.readyState != "complete") { setTimeout( arguments.callee, 100 ); return; }

	/***********************************************************************
	SIGN UP FOR PONGO NEWS POPUP
	***********************************************************************/

	if ($("div#signupnews").length>0)
	{
		$("div#signupnews").draggable({
			start: function(event,ui){
				orderWindowIndex(this);
			},
			containment: 'body'
		});
		
		$("div#signupnews button.close").click(function(){
			windowIndex --;
			$("div#signupnews").hide();
			return iFrameReload('iframe_signupnews','signupnews',false);
		});

		$("div.bookmark a").click(function(){
		
			var object = $("div#signupnews");

			var oLeft = "685px";
			var oTop = "-50px";
			
			if (object.is(":hidden")===true) {
				windowIndex ++;
				object.css({"z-index":windowIndex,"left":oLeft,"top":oTop}).show();
			}

			return false;
		});

	}
	
	if ($("form#signupnews_form").length>0)
	{
		$("form#signupnews_form").submit(function(){
			var oParentDoc = $(parent.document).find("div#signupnews");
			var parentContent = "div#signupnews_iframe div.content";
			var action = $(this).attr("action");
			//var height = $(parent).height()+"px";
			//$(parent).addClass("loading").css({"height":height});
			$(oParentDoc).addClass("loading");
			$(parentContent).empty();
			//var strFullname = $(this).find("input[name='strFullname']").val();
			//var strEmail = $(this).find("input[name='strEmail']").val();
			var postdata = $(this).serialize();
			$.post(action,postdata,function(data){
				if($(oParentDoc).is(":hidden")===true) {
					windowIndex ++;
					$(oParentDoc).show().css({"z-index":windowIndex});
				}
				orderWindowIndex(oParentDoc);
				//$(parent).removeClass("loading").css({"height":"auto"}).effect("shake",{"times":3,"distance":10},60);
				$(oParentDoc).removeClass("loading").addClass("loaded").effect("shake",{"times":3,"distance":10},60);
				$(parentContent).html(data).append('<br/><button onclick="return iFrameReload('+"'iframe_signupnews','signupnews',true"+');">GO BACK</button>');
			});
			return false;
		});
	}

	/***********************************************************************
	BOOK STORE SAMPLE POEM POPUP
	***********************************************************************/

	if ($("div.samplepoem").length>0)
	{
		$("div.samplepoem").draggable({
			start: function(event,ui){
				orderWindowIndex(this);
			},
			containment: 'body'
		});

		$("div.samplepoem button.close").click(function(){
			windowIndex --;
			$(this).parent().hide();
		});

		$("div.book a.readsample").click(function(){
		
			var parent = $(this).parent();
			var container = $("div.books");
			var object = $("div#"+$(this).attr("rel"));

			var oLeft = ((parent.offset().left)-(container.offset().left)-60)+"px";
			var oTop = ((parent.offset().top)-(container.offset().top)-50)+"px";
			var oWidth = object.width()+"px";
			var oHeight = object.height()+"px";
			
			if (object.is(":hidden")===true) {
				windowIndex ++;
				object.css({"z-index":windowIndex,"left":oLeft,"top":oTop,"width":oWidth,"height":oHeight}).show();
			}
			
			return false;
		});

	}
	
	/***********************************************************************
	SEND A COMMENT POPUP
	***********************************************************************/

	if ($("div#commentbox").length>0)
	{
		$("div#commentbox").draggable({
			start: function(event,ui){
				orderWindowIndex(this);
			},
			containment: 'body'
		});
		
		$("div#commentbox button.close").click(function(){
			windowIndex --;
			$("div#commentbox").hide();
			return iFrameReload('iframe_commentbox','commentbox',false);
		});

		$("a.commentbox").click(function(){
		
			var object = $("div#commentbox");

			var oLeft = "685px";
			var oTop = "160px";
			
			if (object.is(":hidden")===true) {
				windowIndex ++;
				object.css({"z-index":windowIndex,"left":oLeft,"top":oTop}).show();
			}

			return false;
		});

	}
	
	if ($("form#commentbox_form").length>0)
	{
		$("form#commentbox_form").submit(function(){
			//var parent = "div#commentbox";
			var oParentDoc = $(parent.document).find("div#commentbox");
			var parentContent = "div#commentbox_iframe div.content";
			var action = $(this).attr("action");
			//var height = $(oParentDoc).height()+"px";
			//$(oParentDoc).addClass("loading").css({"height":height});
			$(oParentDoc).addClass("loading");
			$(parentContent).empty();
			//var strFullname = $(this).find("input[name='strFullname']").val();
			//var strEmail = $(this).find("input[name='strEmail']").val();
			//var strComment = $(this).find("textarea[name='strComment']").val();
			var postdata = $(this).serialize();
			$.post(action,postdata,function(data){
				if($(oParentDoc).is(":hidden")===true) {
					windowIndex ++;
					$(oParentDoc).show().css({"z-index":windowIndex});
				}
				orderWindowIndex(oParentDoc);
				//$(oParentDoc).removeClass("loading").css({"height":"auto"}).effect("shake",{"times":3,"distance":10},60);
				$(oParentDoc).removeClass("loading").addClass("loaded");
				$(parentContent).html(data).append('<br/><button onclick="return iFrameReload('+"'iframe_commentbox','commentbox',true"+');">GO BACK</button>');
			});
			return false;
		});
	}

	/***********************************************************************
	OTHER STUFF
	***********************************************************************/

	if ($("div.msg_body").length>0)
	{
		$("div.msg_body").hide();
		$("a.expandlink").click(function(){
			$("div.msg_body").slideToggle(600);
		});
	}

	$("fieldset p.checkbox input.expandlink").each(toggleFieldset);
	$("fieldset p.checkbox input.expandlink").click(toggleFieldset);



	if ($("textarea#poemcontent").length>0)
	{
		$("textarea#poemcontent").change(function(){
			setConfirmUnload(true);
		});
	}



	if ($("form#submitactivity").length>0)
	{
		$("form#submitactivity input[type='button']").click(function(){ $(this).parents("form").submit(); });
		$("form#submitactivity").validate({
			appendErrorBesideField : false,
			loopTypes: 'input,select,textarea,label,span',
			callbackFunction: function(){ 
				setConfirmUnload(false); 
				return true;
			}
		});
	}
	
	if ($("form#submitsurvey").length>0)
	{
		$("form#submitsurvey input[type='button']").click(function(){ $(this).parents("form").submit(); });
		$("form#submitsurvey").validate({
			appendErrorBesideField : false,
			loopTypes: 'input,select,textarea,label,span'
		});
	}

	$("a.print").click(function(){
		window.print();
		return false;
	});

});



