jQuery(document).ready(function(){
	var blogPath = 'http://www.rockpapershotgun.com';
						
	jQuery('#featured-stories li').hover(function(){
		jQuery(this).find('p').slideDown(100);
	}, function(){
		jQuery(this).find('p').slideUp(50);
	})
	
	if (typeof(jQuery.fn.autocomplete) == 'function') {
	jQuery("#tagsearchtext").autocomplete(jQuery('#valuesUrl').val());
	} else {
	}
	
	jQuery('form#tagsearch').submit(function(){
		window.location = blogPath + '/tag/' + jQuery('#tagsearchtext').val().replace(/\s/g,'-');
		return false;
	})
	
	initComments();
	initPlaceholders();
	initModals()
})


function initComments() {
	var $commentListItems = jQuery('.commentlist li');
	var $commentListRootItems = jQuery('.commentlist li:not(li li)');
	$commentListItems.each(function(i){
		jQuery(this).attr('data-position',i);
	})
	
	var pageVal = 50;
	var pageMultiplier = 1;
	var prevVal = 0;
	jQuery('ol.commentlist').addClass('originalcommentlist').removeClass('commentlist');
	
	for(i = 0; i < $commentListRootItems.length;i++) {

		if (jQuery($commentListRootItems[i]).attr('data-position') > (pageVal * pageMultiplier)) {
			// to make a new group:
			var $ol = jQuery(document.createElement('ol'));
			$ol.append($commentListRootItems.slice(prevVal,i+1))
			  .insertBefore(jQuery('.originalcommentlist'))
			  .attr('data-page-number',pageMultiplier)
			  .addClass('commentlist')
			  .attr('id','comments-page-' + pageMultiplier);
			
			pageMultiplier++;
			prevVal = i;
		}
		if (i === $commentListRootItems.length -1) {
			var $ol = jQuery(document.createElement('ol'));
			$ol.append($commentListRootItems.slice(prevVal,i+1))
				.insertBefore(jQuery('.originalcommentlist'))
       			.attr('data-page-number',pageMultiplier)
				.addClass('commentlist')
				.attr('id','comments-page-' + pageMultiplier);
		}
	}
	

	jQuery('ol.originalcommentlist').remove()

	var page = 1;
	if (window.location.hash.match(/comment-\d+/ig)) {
		page = jQuery(window.location.hash).parents('ol')[0].getAttribute('data-page-number');
	} else {
		if (window.location.hash.match(/comment-page-\d/ig)) page = parseInt(window.location.hash.replace(/\D/ig,''));
	}

	jQuery('ol.commentlist:nth-child('+page+')').addClass('activecommentlist').removeClass('inactivecommentlist').siblings().addClass('inactivecommentlist').removeClass('activecommentlist');
	
	if (window.location.hash.match(/comment-\d+/ig)) {
		if (typeof(jQuery.fn.scrollTo) == 'function') jQuery.scrollTo(window.location.hash);
	} else if (window.location.hash == '#comments') {
		if (typeof(jQuery.fn.scrollTo) == 'function') jQuery.scrollTo('#comments');
	}
	buildCommentNavigation(page);
	
}

function buildCommentNavigation(page) {
	var $head = jQuery(document.createElement('h5'));
	$head.text('Page '+ (jQuery('ol.activecommentlist').prevAll('ol').length + 1) +' of '+ jQuery('ol.commentlist').length);
	
	var $ul = jQuery(document.createElement('ul'));
	
	for(i = 0; i < jQuery('.commentlist').length; i++) {
		var $li = jQuery(document.createElement('li'));
		var $a = jQuery(document.createElement('a'));
		$a.text(i+1)
			.attr('href','#comments')
			.click(function(){
				var $a = jQuery(this);
				var pageSelected = $a.text();
				jQuery('ol.activecommentlist').fadeOut(500,function(){
					jQuery(this).removeClass('activecommentlist').addClass('inactivecommentlist');
					$a.parent().addClass('active').siblings().removeClass('active');
					jQuery('ol.commentlist:nth-child('+pageSelected+')').addClass('activecommentlist').removeClass('inactivecommentlist').fadeIn(400);
					window.location.hash = '#comment-page-' + pageSelected;
					if (typeof(jQuery.fn.scrollTo) == 'function') jQuery.scrollTo('#comments')
				})
				$head.text('Page '+ pageSelected +' of '+ jQuery('ol.commentlist').length);
				return false;
			});
		$a.appendTo($li);
		$li.appendTo($ul);
	}
	
	var $pagination = jQuery(document.createElement('div'));
	$pagination.addClass('pagination').append($head).append($ul).insertBefore('#respond').find('li:nth-child('+page+')').addClass('active');
}

function getParameterByName( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

function initPlaceholders() {
	// replicates html5 placeholder attribute in browsers that do not support it
	if('placeholder' in document.createElement('textarea') == false) {
		jQuery('input[placeholder], textarea[placeholder]').each(function(){
			var input = jQuery(this);
			var placeholder = input.attr('placeholder');
			if (input.val() == placeholder) input.addClass('placeholder');
			input
				.focus(function(){
					if(input.val() == placeholder) input.val('').removeClass('placeholder');
				})
				.blur(function(){
					if(input.val() == '') input.val(placeholder).addClass('placeholder');
				});
		});
	};
}

function initModals() {

	jQuery('.entry a img').parent().click(function(){
		var $modalOverlay = $('<div class="modal-overlay"/>');
		var $modalWindow = $('<div class="modal-window"/>');
		var $img = $('<img/>');
		var $loading = $('<img/>');
		$loading.attr('src','http://www.rockpapershotgun.com/wp-content/themes/rockpapershotgun/images/loading.gif').attr('class','loading').appendTo($('body'));
	
		$modalOverlay.appendTo('body');
		$modalWindow.appendTo('body').css('opacity',0);
	
		$modalOverlay.fadeIn(300);
		$img.load( function(){
			$loading.remove()
			var imgwidth = $img.width();
			$modalWindow.css({width:imgwidth,marginLeft:imgwidth*(-0.5)});
	
			$modalWindow.animate({opacity:1},300);
		})
		.attr('src', $(this).attr('href'))
		.appendTo($modalWindow);
	
		//var $caption = $('<div/>')
		//$caption.text($(this).attr('alt')).appendTo($modalWindow);
	
		var $close = $('<a/>')
		$close.appendTo($modalWindow).addClass('close').html('&times;').click(function(){
			$modalOverlay.add($modalWindow).fadeOut(300,function(){
				$modalOverlay.add($modalWindow).remove();
			});
		});

		$modalOverlay.click(function(){
			$modalOverlay.add($modalWindow).fadeOut(300,function(){
				$modalOverlay.add($modalWindow).remove();
			});
		});
		return false;
	});
}

