jQuery(document).ready(function(){
var $templ_path = jQuery("meta[name=ap_menu]").attr('content');
if ($templ_path == ""){$templ_path = 'http://chest.amberpanther.com/img/m-arrow-up.gif';}else{$templ_path = $templ_path + '/images/arrow-up.gif'; }
ap_menu($templ_path); 
login_control();
contact_form();
spend_time_and_resources_to_fix_ie6();
newsletter_mask();
});


function ap_menu($arrow_path)
{
		jQuery("#nav a").removeAttr('title');
		jQuery("#nav ul").css({display: "none"}); // Opera Fix
		
		
		
		var $headers=jQuery("#nav").find("ul").parent()
		$headers.each(function(i){
			var $curobj=jQuery(this)
			var $subul=jQuery(this).find('ul:eq(0)')
		
			var $innerheader=$curobj.children('a').eq(0)
			$innerheader=($innerheader.children().eq(0).is('span'))? $innerheader.children().eq(0) : $innerheader //if header contains inner SPAN, use that
			$innerheader.append(
				'<img src="'+$arrow_path+'" style="border:0; padding-left: 5px" />'
			)
			
		}) //end $headers.each()
		
		jQuery(" #nav li").hover(function()
		{
			jQuery(this).find('ul:first').css({visibility: "visible",display: "none"}).show("slow");
		}
		,function()
		{
			jQuery(this).find('ul:first').css({visibility: "hidden"});
		});
		
}

function login_control(){
	var jButton = jQuery("#loginbutton");
	
	jButton.click(function(){
		var jLogin = jQuery("#loginwrap");
		if (!jLogin.hasClass("userloggedin")){
			if (jLogin.hasClass("hiddenlogin")){
				jLogin.animate({marginTop : "0px"},1000,"linear").removeClass("hiddenlogin");
				var t= setTimeout('jQuery("#logbut-text").text("Close")',1000);;
			}else{
				jLogin.animate({marginTop : "-81px"},1000,"linear").addClass("hiddenlogin");
				var t= setTimeout('jQuery("#logbut-text").text("Log in")',1000);
			}
		}
		
		});
	

}


function contact_form(){
	if(!(jQuery.browser.msie && parseInt(jQuery.browser.version) < 7)){ // ajax contact form disabled in ie6, it does work but ie6 shifts layout on error :P
		var template_url = jQuery("meta[name=ap_menu]").attr('content');
		var my_error;
		var re_checked = false;
		jQuery(".ajax_form #send").bind("click", function(){
			my_error = false;
			//reCaptcha
			var challengeField = jQuery("input#recaptcha_challenge_field").val();
					var responseField = jQuery("input#recaptcha_response_field").val();
					var html = jQuery.ajax({
						type: "POST",
						url: template_url+"/ajax_recaptcha.php",
						data: "Send=true&recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField,
						async: false
						}).responseText;
					if(html != "perfect") {
						jQuery("#reCapthaStatus").html("The security code you entered did not match. Please try again.");
						//var RecaptchaOptions = { theme : 'red', lang : 'en' , tabindex : 5 };
						//Recaptcha._init_options(RecaptchaOptions);
						Recaptcha.reload();
						my_error = true;
						//alert('recapthac wrong');
					}else{
						
						//alert('recapthac right');
						//Add the Action to the Form
						//jQuery("form").attr("action", "http://action/to/the/form_handler");
						//Indicate a Successful Captcha
						//$("#reCapthaStatus").html("Success!");
						// Uncomment the following line in your application
						//return true;
					}
				
			
			//rest of fields
			jQuery(".ajax_form #name, .ajax_form #message, .ajax_form #email").each(function(i){
	
			var value = jQuery(this).attr("value");
			var check_for = jQuery(this).attr("id");
			var surrounding_element = jQuery(this).parent();
				if(check_for == "email"){
					if(!value.match(/^\w[\w|\.|\-]+@\w[\w|\.|\-]+\.[a-zA-Z]{2,4}$/)){
						surrounding_element.attr("class","").addClass("error");
						my_error = true;
					}else{
						surrounding_element.attr("class","").addClass("valid");	
					}
				}
				
				if(check_for == "name" || check_for == "message"){
					if(value == ""){
						surrounding_element.attr("class","").addClass("error");
						my_error = true;
					}else{
						surrounding_element.attr("class","").addClass("valid");	
					}
				}
				
				//if(!re_checked){
				//	re_checked == true;
					
				//}
				
				if(jQuery(".ajax_form #name, .ajax_form #message, .ajax_form #email").length  == i+1){
					if(my_error == false){
						jQuery(".ajax_form").slideUp(400);
				
						var $datastring = "ajax=true&Send=true&re_verified=true";
						jQuery(".ajax_form input, .ajax_form textarea").each(function(i){
							var $name = jQuery(this).attr('name');	
							var $value = jQuery(this).attr('value');
							$datastring = $datastring + "&" + $name + "=" + $value;
						});
																		
									
						jQuery(".ajax_form #send").fadeOut(100);	
						jQuery.ajax({
								type: "POST",
								url: template_url+"/send.php",
								data: $datastring,
								success: function(response){
									jQuery(".ajax_form").before("<div class='ajaxresponse' style='display: none;'></div>");
									jQuery(".ajaxresponse").html(response).slideDown(400); 
									jQuery(".ajax_form #send").fadeIn(400);
									jQuery(".ajax_form #name, .ajax_form #message, .ajax_form #email , .ajax_form #website").val("");
								}
							});
					} 
				}
			});
		return false;
		});
	}
}



function newsletter_mask() {	

	jQuery('a[name=newsletterlink]').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
	
		//find the content element
		var id = "#ap_newslcontent";
	
		//get the screen size
		var maskHeight = jQuery(document).height();
		var maskWidth = jQuery(window).width();
	
		//Set mask size to fill up the whole screen
		jQuery('#ap_newslmask').css({'width':maskWidth,'height':maskHeight});
		
		//show the mask		
		jQuery('#ap_newslmask').fadeIn(1000);	
		jQuery('#ap_newslmask').fadeTo("slow",0.85);	
	
		//get the window height and width
		var winH = jQuery(window).height();
		var winW = jQuery(window).width();
              
		//set the content element window to center
		jQuery(id).css('top',  winH/2-jQuery(id).height()/2);
		jQuery(id).css('left', winW/2-jQuery(id).width()/2);
		//transition effect
		jQuery(id).fadeIn(2000); 
		
		
	});
	
	//if close button is clicked
	jQuery('.ap_newslclose, #ap_newslmask').click(function (e) {
		//cancel the link behavior
		e.preventDefault();
		jQuery('.ap_newslwindow').fadeOut(500,function(){
			jQuery('#ap_newslmask').fadeOut(600)
		});
	});
	
	
	//on window resize
	jQuery(window).resize(function(){
		var maskHeight = jQuery(document).height();
		var maskWidth = jQuery(window).width();
		//var maskWidth = jQuery(document).width();
		if (maskWidth < 960) maskWidth = 960;
		jQuery('#ap_newslmask').css({'width':maskWidth,'height':maskHeight});
		var winH = jQuery(window).height();
		var winW = jQuery(window).width();
		jQuery(id).css('top',  winH/2-jQuery(id).height()/2);
		jQuery(id).css('left', winW/2-jQuery(id).width()/2);
	});

}	







function spend_time_and_resources_to_fix_ie6(){
	if((jQuery.browser.msie && parseInt(jQuery.browser.version) < 7)){
		jQuery("#nav, #citations, #leftControl, #rightControl, #searchform #sbutt, #loginbutton, #logbut-text, #logbut-left, #logbut-right, #sidebar, #sociallinks, #bannerads-wrap, #smalllinks, #credit, #findus").addClass("ie6pngfix");
	}
}
