(function ($) {
    // Create a namespace if it does not exist
	$.nrj = $.nrj ? $.nrj : {};
	$.nrj.matchHeights = function (options) {
		// Extend the default options with those provided.
		var opts = $.extend({}, $.nrj.matchHeights.defaults, options);
		// Return for chaining
		return this.each(function () {
			// Extend with meta plugin if it is included
			opts = $.meta ? $.extend({}, opts, $(this).data()) : opts;

			// Put elements into an array
			var els = opts.elements.split(',');
			if (els.length < 1) return false;

			var tallest = 0;
			var topHeight = 0;
			var newEls = [];

			// iterate over each element, adding its total height to an array,
			// setting the tallest element in the list
			$.each(els, function (i, val) {
				tempTop = $(val).offset().top;
				tempHeight = tempTop + $(val).height();
				newEls[i] = tempHeight;
				
				//alert(tempHeight + " : " + topHeight);
				if (tempHeight > topHeight) {
					tallest = i;
					topHeight = tempHeight;
				}
			});

			// iterate over each element, matching the height of shorter els
			$.each(els, function (i, val) {
				if (i == tallest) return;
				/*if(tallest = 1) { 
					$(els[0]).height($(els[1]).height() - 95);
				} else {
					$(els[1]).height($(els[0]).height());
				}*/
				temp = $(els[i]).height();
				//$(els[i]).height(topHeight);
				
				$(els[i]).height(topHeight - newEls[i] + $(els[i]).height());
			});
		});
	}

	$.nrj.matchHeights.defaults = {
		elements: ""
	}

	$.fn.matchHeights = $.nrj.matchHeights;

})(jQuery);

$(document).ready(function(){
	if($('#right-content').length){
		$().matchHeights({
			"elements": ".pane-left, #right-content"
		});
	} else if($('#right-content-issues').length){
		$().matchHeights({
			"elements": ".pane-left, .pane-right, #right-content-issues"
		});
	} else {
		$().matchHeights({
		"elements": ".pane-left, .pane-right"
		});
	}
		
	
//get updates
	$("#updates_btn").click(function(){
		var email = $("input#update_email").val();
		var zip = $("input#update_zip").val();
		
		if(email == '' || email == 'Email'){
			alert('Please enter an email');
		} else if(!checkMail(email)){
			alert('Please enter a valid email address');			
		} else if(zip == '' || zip == 'Zip'){
			alert('Please enter a zip code');
		} else {		
			var dataString = 'email='+email+'&zip='+zip;
			$.ajax({
				type:"POST",
				url:"get_updates.php",
				data:dataString,
				success:function(msg){
					alert(msg);
					$("input#update_email").val("Email");
					$("input#update_zip").val("Zip");
					//$("#statusMessage").html('Thank You.');
				}
				   
			});			
		}
		return false;
	});
	
	////
	$(".contact_submit").click(function(){
		var element = $(this);
		var department = $('#department :selected').val();
		var name = $("#name").val();
		var email = $("#email").val();
		var message = $("#message").val();
		var newsletter = $('input[name=newsletter]').is(':checked'); //$('#newsletter:checked');
		var newsletter_checked = 0;
		if(newsletter) {
			newsletter_checked = 1;
		} else {
			newsletter_checked = 0;
		}

		var dataString = 'sendTo='+department+'&name='+name+'&email='+email+'&message='+message+'&newsletter='+newsletter_checked;

		if(department == ''){
			alert('Please Select a Department');
		} else if(name=='') {
			alert("Please Enter Your Name");
		} else if (email == ''){
			alert("Please Enter Your Email");
		} else if(!checkMail(email)) {
			alert('Please Enter Valid Email');
		}
		else
		{
			$("#contact_form").show();
			$("#contact_form").fadeIn(400).html('<img src="images/loading.gif" align="absmiddle">');
			$.ajax({
				type: "POST",
				url: "contact_form.php",
				data: dataString,
				cache: false,
				success: function(html){
				$("#loadplace").append(html);
				$("#contact_form").hide("fast");
				}
			});
		}
	return false;
	});
	
	/////////
	$("#btnSubmit").click(function(){
		var twitter_username = $('#username').val();
		var twitter_password = $('#twitter_password').val();
		var twitter_message = $('#twitterMessage').val();
		
		var dataString = 'username='+twitter_username+'&pword='+twitter_password+'&twitter_message='+twitter_message;
		
		$.ajax({
			   type: "POST",
			   url: "updateTwitter.php",
			   data: dataString,
			   success: function(msg){
					$('#twitterResponse').html(msg);
					//alert(msg);  
			   }
		});
		
	return false;
	});

});

function openWin(){ window.open("money-bomb.html",'name','height=250,width=250,scrollbars=no,resizable=no,status=yes'); }

function sortBy(type){ document.location = "?q=all_media&category="+type; }
function checkMail(x) {
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x))	{ return true; }
	else				{ return false; }
}


/**********************************************/
function mOverT(target, ID, doOver) {
	if(doOver) { target.style.backgroundPosition = 'bottom'; }
	document.getElementById(ID).style.display='block';
}

function mOutT(target, ID, doOver) {
	if(doOver) { target.style.backgroundPosition = 'top'; }
	document.getElementById(ID).style.display='none';
}