jQuery(function() {
  jQuery('.error').hide();
  jQuery('input.text-input').css({backgroundColor:"#FFFFFF"});
  jQuery('input.text-input').focus(function(){
    jQuery(this).css({backgroundColor:"#FFDDAA"});
  });
  jQuery('input.text-input').blur(function(){
    jQuery(this).css({backgroundColor:"#FFFFFF"});
  });

  jQuery(".button").click(function() {
		// validate and process form
		// first hide any error messages
    jQuery('.error').hide();
		
	  var mailto = jQuery("input#mailto").val();
		if (mailto == "") {
      jQuery("label#mailto_error").show();
      jQuery("input#mailto").focus();
      return false;
    }
		var sender = jQuery("input#sender").val();
		if (sender == "") {
      jQuery("label#sender_error").show();
      jQuery("input#sender").focus();
      return false;
    }
		var from = jQuery("input#from").val();
		if (from == "") {
      jQuery("label#from_error").show();
      jQuery("input#from").focus();
      return false;
    }
	var subject = jQuery("input#subject").val();    
	var id = jQuery("input#id").val();	
	var lang = jQuery("input#lang").val();	
		
	var dataString = 'mailto='+ mailto + '&from=' + from + '&sender=' + sender + '&subject=' + subject+ '&id=' + id + '&lang=' + lang;
		
	jQuery.ajax({
      type: "POST",
      url: "/helper/send.php",
      data: dataString,
      success: function() {
        jQuery('#sendmail_form').html("<div id='message'></div>");
        jQuery('#message').html("<h2>Article sent to " + mailto + "!</h2>")
        .hide()
        .fadeIn(1500, function() {
        });
      }
     });
    return false;
	});
});

jQuery().ready(function() {
  jQuery('#sendmail').jqm({trigger: 'a.sendmailtrigger'});
  

});


$(document).ready(function() {
	//$("img").wTooltip();
	
	/* subnavigation 
		1. hide subnavigation
		2. bind the show/hide function on every subnavigation list
	*/
	$(function() { 
		$(".subnaviblock").hide(); 
	});	
	var $div = $('.menugeneral');
	var $ul = $('ul', $div);
	$('#backgroundinfo', $ul).bind('mouseover.hover', function(){
		var $li = $(this);
		$('.subnaviblock', $ul, $li).show();
		$li.bind('mouseout.out', function(){
			var $li = $(this);
			$('.subnaviblock', $ul, $li).hide();
		});
	});	
	$('a#backgroundclose').click(function() {
	    $('.subnaviblock').hide();
	});
  $('.articlethumb').showFeatureText();
  fadeOutMonthly();
  $('.popper').toggleSingle();
  $('#unfading').toggleFade();
  var animating = true;
   $('input[type="text"]','.newslettersearch').focus(function() {
	  $('.newslettersearch').addClass("newslettersearchactive");
    });
    $('input[type="text"]','.newslettersearch').blur(function() {
     $('.newslettersearch').removeClass("newslettersearchactive");
    });
    
   $('#s').focus(function() {
	  $('.headersearch').addClass("headersearchactive");
    });
    $('#s').blur(function() {
     $('.headersearch').removeClass("headersearchactive");
    });    
  
})

fadeOutMonthly = function() {
	jQuery(this).oneTime(6000, "hide", function() {
		$('.fading').animate({'opacity':'0.4'},1000,function(){});
	});
}
$.fn.toggleFade = function() {
	$(this).hover(function(){
		$('.fading').animate({'opacity':'1.0'},1000, function(){
			//uncomment this and fix it so as not to show duplicates!
			//$(box).next().show();
			//replace show() with hover() and kill $.fn.hidePopup.
		});
	},function(){
	  $('.fading').animate({'opacity':'0.4'},500,function(){
		});   
	});
}
$.fn.toggleSingle = function() { 
	//could theoretically bind a popup to this too.
	$(this).hover(function(){
		animating = true;
		$('.fading').css('opacity','0.4');		
		$('.popup').hide();	
		$('.popup').css('opacity','1');
		var $div = $(this);
		var $popper = $('.fading', $div);
		var $ul = $('.popup', $div);
		if($popper.length > 0){
			$popper.animate({'opacity':'1.0'},500);
			if(animating){
				$ul.delay(1000).fadeIn(); 
			}
			else{
				$('.popup').hide();	
				$('.popup').css('opacity','1');
				//alert('animation stopped');
			}
		}
		else{
			if(animating){
				$ul.stop().delay(1000).fadeIn();
			}
			else{
				$('.popup').hide();
				$('.popup').css('opacity','1');
			}
		}
	}, function(){
		animating = false;	
		var $div = $(this);
		var $ul = $('.popup', $div);
		var $popper = $('.fading', $div);
		$('.popper').css('opacity','1');				
		$popper.css('opacity','0.4');		
		$ul.stop().hide();
		$ul.css('opacity','1');		
	});
}
$.fn.showFeatureText = function() {
  return this.each(function(){    
    var box = $(this);
    var text = $('.withmore',this);

    box.hover(function(){
      text.animate({'bottom':'0px'},500,function(){});
    },function(){
      text.animate({'bottom':'-22px'},500,function(){});    
    });
  });
}
