/* Author: Manuel Castellanos Raboso

*/

$(document).ready(function(){
			
	//When you click on a link with class of poplight and the href starts with a # 
	$('a.poplight[href^=#]').click(function() {
		var popID = $(this).attr('rel'); //Get Popup Name
		var popURL = $(this).attr('href'); //Get Popup href to define size
				
		//Pull Query & Variables from href URL
		var query= popURL.split('?');
		var dim= query[1].split('&');
		var popWidth = dim[0].split('=')[1]; //Gets the first query string value

		//Fade in the Popup and add close button
		$('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"><img src="/_/img/img_close_button.png" class="btn_close" title="Close Window" alt="Close" /></a>');
		
		//Define margin for center alignment (vertical + horizontal) - we add 80 to the height/width to accomodate for the padding + border width defined in the css
		var popMargTop = (($('#' + popID).height() + 80) / 2) - 15;
		var popMargLeft = (($('#' + popID).width() + 80) / 2) - 27;
		
		//Apply Margin to Popup
		$('#' + popID).css({ 
			'margin-top' : -popMargTop,
			'margin-left' : -popMargLeft
		});
		
		//Fade in Background
		$('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
		$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer 
		
		return false;
	});
	
	
	//Close Popups and Fade Layer
	$('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
	  	$('#fade , .popup_block').fadeOut(function() {
			$('#fade, a.close').remove();  
	}); //fade them both out
		
		return false;
	});
	
	var post_container = $("#blog_display");
	
	$(".changer_grid, .changer_list").click( function(event) {
		event.preventDefault();
		$(".changer_grid, .changer_list").children("a").removeClass("selected");
		if($(this).text() == "Grid") {
			$(this).children("a").addClass("selected");
			post_container.stop().animate({"opacity":"0"},400, function() {
				post_container.removeClass("blog_list").addClass("blog_grid").animate({"opacity":"1"},400);
			});
		}
		if($(this).text() == "Columns") {
			$(this).children("a").addClass("selected");
			post_container.stop().animate({"opacity":"0"},400, function() {
				post_container.removeClass("blog_grid").addClass("blog_list").animate({"opacity":"1"},400);
			});
		}
	});
	
	 $("#drop_categories").click(function(event) {
	 	event.preventDefault();
	 	$("#categories_list").slideDown(400).mouseleave( function() {
			$("#categories_list").slideUp(400);
		});
	 });
	
	 $("#drop_tags").click(function(event) {
		event.preventDefault();
	 	$("#tag_list").slideDown(400).mouseleave( function() {
			$("#tag_list").slideUp(400);
		});

	 });
	 
	 $('#commentform').submit(function(event) {
		event.preventDefault();
		$.ajax({
			url: '/_/lib/actions.php',
			data: 'contact=true&u_fname=' + escape($('#contact_name').val()) + '&u_subject=' + escape($('#contact_subject').val()) + '&u_email=' + escape($("#contact_email").val()) + '&u_phone=' + escape($("#contact_phone").val()) + '&u_message=' + escape($("#contact_comment").val()),
			success: function(msg) {
				//alert(msg)
				var obj = jQuery.parseJSON(msg);
				if(obj.type === 'ok')
				{
					$("#returnedMessage").html("<p>" + obj.title + "<br />" +  obj.message + "</p>");
					resetFields();
					
				}
				else
				{
					$("#returnedMessage").html("<p>" + obj.title + "<br />" +  obj.message + "</p>");
				}
			}
		});
	});
	
	$('#contact_comment').limit('170','#commentsLeft','char');
	
});
/*
var keynum, lines = 1;
var fake_indent = '                 ';		
function limitLines(obj, e) {
	// IE
	if(window.event) {
	  keynum = e.keyCode;
	// Netscape/Firefox/Opera
	} else if(e.which) {
	  keynum = e.which;
	}

	if(keynum == 13) {
	  if(lines == obj.rows) {
		return false;
	  }else{
		lines++;
	  }
	}
	if(obj.value == '') {
		obj.value = fake_indent;
	}
	if(obj.value.length < 17) {
		obj.value = fake_indent;
	}
}
*/

var keynum, lines = 1;
var fake_indent = '                  ';		
function limitLines(obj, e) {
	// IE
	if(window.event) {
	  keynum = e.keyCode;
	// Netscape/Firefox/Opera
	} else if(e.which) {
	  keynum = e.which;
	}
	
	var aNewlines = obj.value.split("\n");
	lines = aNewlines.length;
	
	
	if(keynum == 13) {
	  if(lines >= obj.rows) {
		lines = obj.rows;
		return false;
	  }
	}
	
	if ($(obj).attr("id") == "comment" && obj.value.length >= 350) {
		obj.value = obj.value.substring(0, 350);
		lines = obj.rows;
	}
	else if ($(obj).attr("id") == "contact_comment" && obj.value.length >= 200) {
		obj.value = obj.value.substring(0, 200);
		lines = obj.rows;
	}
	
	if(obj.value == '') {
		obj.value = fake_indent;
	}
	if(obj.value.length < 17) {
		obj.value = fake_indent;
	}
	
	
	//console.log(aNewlines.length);
	
}	
  
function resetFields() {
	$('#contact_name').val('');
	$('#contact_subject').val('');
	$("#contact_email").val('');
	$("#contact_phone").val('');
	$("#contact_comment").val(fake_indent);
}









