function inputClear (el) {
	var i = '#'+el;
	if($(i).attr('value') == $(i).attr('title')) {
		$(i).attr('value','');
	}
}
function inputReset (el) {
	var i = '#'+el;
	if($(i).attr('value')== '') {
		$(i).attr('value',$(i).attr('title'));
	}
}


function openBlogPortlet(obj,container) {
	/**
	 * Cache Reference
	 */
	var $portlet	= obj;
	var position	= $portlet.offset();
	var $header		= obj.find('.portlet-header');
	var $content	= obj.find('.portlet-content');
	var $banner		= $header.find('.banner');
	var $inner		= $content.find('.portlet-text .body');

	var temp		= document.createElement('div');
		temp.id = 'heightcalc';
		document.getElementsByTagName('body')[0].appendChild(temp);
	var $temp = $('#heightcalc');
		$temp.css({'position':'absolute','left':-1000});
		$temp.css({'width':511});
		$temp.html($inner.html());
	var newContentHeight = $temp.height()-38;
	$temp.remove();

	/**
	 * Hide the Small Header Image
	 * so we can swap it with the Big one
	 */
	$portlet.css({'z-index':100});
	$header.css({'opacity':0});
	$banner.css({'background-image':$banner.css('background-image').replace('small','large')});
	//console.log('Height: '+$content.find('.portlet-text .body').outerHeight());

	/**
	 * Animate the left position if we're in the last 2 columns
	 */
	var objAnimation = {
		'height':	'+='+newContentHeight,
		'width':	529
	};
	//console.log(position.left - container.offset().left - 4);
	if(position.left - container.offset().left - 4 > 358) {
		objAnimation.left = 358;
	}
	/**
	 * Animate the Outer Container
	 */
	
	$portlet.animate(objAnimation,1200,function(){
		/**
		 * After animated
		 * - call masonry
		 */
		container.masonry();
	});
	/**
	 * Animate the Header
	 */
	$header.animate({
		width:		519
	},1200,function(){
		/**
		 * Header Resized
		 */
		$(this).animate({'opacity':1},250);
	});
	/**
	 * Animate the Banner
	 */
	$banner.animate({
		'width':	519
	},1200,function(){
		/**
		 * Banner Resized
		 */
	});
	/**
	 * Animate the Body
	 */
	$content.animate({
		height:	'+='+newContentHeight,
		width:	511
	},1200,function(){
		/** 
		 * After animated
		 * - fade in the text area
		 * - fade in the close button
		 */
		$content.find('.portlet-text .body').css({'opacity':0,'display':'block'}).animate({
			opacity:	1
		},1200,function(){
			/**
			 * Text Faded In
			 */
			var videoLinks = null;
			if((videoLinks = $content.find('.portlet-text .body .video-link')).length > 0) {
				var css = videoLinks.attr('class').split(' ');
				videoLinks.html(loadVideoYoutube(css[1]));
			}

			//var videoMatches = /\[video code=([^\)]+)\]/.exec($content.find('.portlet-text .body').html());
			//if(videoMatches && videoMatches.length > 0) {
				//var newContent = $content.find('.portlet-text .body').html();
				//var videoLink = videoMatches[0];
				//var videoUrl = videoMatches[1];

				//newContent = newContent.replace(videoLink,loadVideoYoutube(videoUrl));
				//$content.find('.portlet-text .body').html(newContent);
			//}
			$portlet.css({'z-index':1}).addClass('opened');
			$content.find('.close').css({'display':'block'});

			$('html,body').animate({scrollTop: $portlet.offset().top}, {duration:1000,specialEasing:'easeOutBack'});
			animating='no';
		});
	});
}
function closeBlogPortlet(obj,container,nextPortlet) {
	/**
	 * Cache Reference
	 */
	var $portlet	= obj;
	var position	= $portlet.offset();
	var $header		= obj.find('.portlet-header');
	var $content	= obj.find('.portlet-content');
	var $banner		= $header.find('.banner');

	/**
	 * Hide the Big Header Image
	 * so we can swap it with the Small one
	 */
	$portlet.css({'z-index':100});
	$header.css({'opacity':0});
	$banner.css({'background-image':$banner.css('background-image').replace('large','small')});
	$content.find('.close').css({'display':'none'});
	$content.find('.portlet-text .body').css({'opacity':0,'display':'none'});

	/**
	 * Animate the Body
	 */
	$content.animate({
		height:	106,
		width:	153
	},250,function(){
		/** 
		 * After animated
		 */
		$portlet.css({'z-index':1}).removeClass('opened');
	});
	/**
	 * Animate the Banner
	 */
	$banner.animate({
		'height':	160,
		'width':	161
	},250,function(){
		/**
		 * Banner Resized
		 */
	});
	/**
	 * Animate the Header
	 */
	$header.animate({
		height:		160,
		width:		161
	},250,function(){
		/**
		 * Header Resized
		 */
		$(this).animate({'opacity':1},250);
	});
	/**
	 * Animate the Outer Container
	 */
	$portlet.animate({
		'height':	284,
		'width':	171
	},250,function(){
		/**
		 * After animated
		 * - call masonry
		 */
		container.masonry();

		if(nextPortlet && typeof(nextPortlet)=='object') {

			container.data('nextPortlet',nextPortlet);

			setTimeout('portletOpenTrigger()',1200);
		} else {
			//setTimeout(function(){$('html,body').animate({scrollTop: $portlet.offset().top}, {duration:1000,specialEasing:'easeOutBack'});},1200);
			animating='no';
		}
	});
}

$(function() {

/*** Forms ***/

	$('.hide-me').hide();

	var form_inputs = [
		's',
		'email',
		'author',
		'comment'
	];
	var num_inputs = form_inputs.length;
	for(var i=0; i<num_inputs; i++) {
		if(form_inputs[i]) {
			var id='#'+form_inputs[i];
			if($(id).length > 0) {
				$(id).bind({
					click: function() {
						inputClear($(this).attr('id'));
					},
					focus: function() {
						inputClear($(this).attr('id'));
					},
					blur: function() {
						inputReset($(this).attr('id'));
					}
				});
			}
		}
	}
	$("input[type='password']").bind({
		click: function(){
			if($(this).val().length < 1) {
				$(this).parent().find('span').text('');
			}
		},
		focus: function() {
			if($(this).val().length < 1) {
				$(this).parent().find('span').text('');
			}
		},
		blur: function() {
			if($(this).val().length < 1) {
				$(this).parent().find('span').text($(this).attr('title'));
			}
		}
	});

	$('#commentform #submit').click(function(){
		var email = $('#email');
		var author = $('#author');
		var comment = $('#comment');

		var emailError = true;
		var authorError = true;
		var commentError = true;

		if(email.val() != '' && email.val() != email.attr('title') && /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(email.val()) ) {
			emailError = false;
		}
		if(author.val() != '' && author.val() != author.attr('title') && author.val().length > 3) {
			authorError = false;
		}
		if(comment.val() != '' && comment.val() != comment.attr('title') && comment.val().length > 1) {
			commentError = false;
		}

		if(emailError || authorError || commentError) {
			$('.pleaseFill').css({'color':'#e51837'});
			return false;
		}
		$('.pleaseFill').css({'color':'#AFAFAF'});
		return true;
	});

	$('.CommentRow:first').addClass('first-child');


});
