/*
 
 FreshPress - A WordPress Theme Framework
 Developed for Wordpress 3
 
 Copyright (C) 2011 Awkward (http://www.awkwardgroup.com) 
 This product and code may not be reused or redistributed by any other then Awkward Group AB.
 For more information please contact Awkward Group at hello@awkwardgroup.com.
 
*/

jQuery(document).ready(function($)
{
	/* Vertical dropdown
	----------------------*/
	jQuery('.vnav').find('li').each(function()
	{
		jQuery(this).hover(function()
		{
			var menu = jQuery(this).children('ul');
			jQuery(menu).css('left', jQuery(this).width());
			jQuery(menu).css('top', '0');
			jQuery(menu).show();
		}, function()
		{
			jQuery(this).children('ul').hide();
		});
	});
	
	/* Horizontal dropdown
	------------------------
	jQuery('.hnav').children('ul').children('li').each(function()
	{
		jQuery(this).hover(function()
		{
			var menu = jQuery(this).children('ul');
			jQuery(menu).css('top', jQuery(this).height());
			jQuery(menu).slideDown(200);
			
			jQuery(menu).children('li').each(function()
			{
				jQuery(this).hover(function()
				{
					var menu2 = jQuery(this).children('ul');
					jQuery(menu2).css('left', jQuery(this).width());
					jQuery(menu2).css('top', '0');
					jQuery(menu2).show(); 
			
				}, function()
				{
					jQuery(this).children('ul').hide();
				});
			});
			
		}, function()
		{
			jQuery(this).children('ul').stop(true, true).hide();
		});
	});
	*/
	
	/* AJAX Post Load
	-------------------*/
	var $content = '.posts';
	var $nav_wrap = '.paging';
	var $anchor = '.paging .alignleft a';
	var $text = 'Ladda fler inlägg';
	var $next_href = $($anchor).attr('href'); // Get URL for the next set of posts
	
	if (jQuery($anchor).is(':visible'))
	{
		$($nav_wrap).html('<a id="fp-load-more" class="add-corners-white" href="' + $next_href + '"><span>' + $text + '</span></a>');
	}
	
	$('#fp-load-more').click(function(e)
	{
		var anchor = jQuery(this);
		anchor.addClass('loading');
		e.preventDefault();
		
		$.get(anchor.attr('href'), '', function(data)
		{
			var $timestamp = new Date().getTime();
			var $new_content = $($content, data).wrapInner('<div class="fp-loaded" id="fp-' + $timestamp + '" />').html();
			$next_href = $($anchor, data).attr('href');
			$('html,body').animate({scrollTop: $($nav_wrap).position().top}, 'slow');
			$($nav_wrap).before($new_content);
			$('#fp-' + $timestamp).hide().fadeIn('slow');
			$('#fp-load-more').attr('href', $next_href);
			$('.fp-loaded ' + $nav_wrap).remove();
			if (!$next_href) anchor.css('display', 'none');
			anchor.removeClass('loading');
		});
	});
	
	/* Add corner spans to selected elements */
	jQuery('.add-corners-white').each(function() { jQuery(this).addCorners(); });
	jQuery('.add-corners-black').each(function() { jQuery(this).addCorners('black'); });
	jQuery('.add-corners-custom').each(function() { jQuery(this).addCorners('custom'); });
	
	/* Custom functions
	---------------------*/
	
});


/* Category Filter 
--------------------*/

/*jQuery(document).ready(function()
{
	if (jQuery.cookie('fp_category_filter') == null) jQuery.cookie('fp_category_filter', '', { path: "/" });
	//alert(jQuery.cookie('fp_category_filter'));
	
	// Get cookie
	var cookie = jQuery.cookie('fp_category_filter');
		
	// Split cookie into arrray
	var cookieArray = cookie.split(',');
	
	jQuery('a[name="category-filter"]').each(function()
	{	
		// Get ID
		var id = jQuery(this).attr('rel');
		
		// Activate anchors
		for (var i = cookieArray.length-1; i>=0; --i)
		{
			if (cookieArray[i] == id)
			{
				jQuery(this).addClass('active');
			}
		}
		
		// Set click handler
		jQuery(this).click(function(e)
		{
			e.preventDefault();
			setCategoryFilter(id, cookieArray);
		});
	});
});*/


function setCategoryFilter(id, cookieArray)
{
	// Empty cookie variable
	cookie = '';
	
	// Loop trough values to se if it already exists.
	var removeID = false;
	for (var i=cookieArray.length-2; i>=0; --i)
	{
		//newCookieArray = cookieArray[i].split(':');
		if (cookieArray[i] !== id)
		{
			if (cookieArray[i].substring(0,1) != id.substring(0,1) ) cookie += cookieArray[i] + ',';
		}
		else { removeID = true; }
	}
	
	// Add ID if it didn't exists in the cookie
	if (!removeID) cookie += id + ',';
	
	// Set cookie
	jQuery.cookie('fp_category_filter', cookie, { path: "/" });
	
	// Reload the browser
	window.location = fp_blog_url;
}


/* Add corner spans to selected element.
-----------------------------------------*/
jQuery.fn.addCorners = function(type)
{
	if (type == 'black') theType = type;
	else if (type == 'custom')
	{
		theType = type;
		var customClass = jQuery(this).attr('custom');
		var hasCustomClass = false;
		if (customClass != '')
		{
			hasCustomClass = true;
			jQuery(this).addClass(customClass);
		}
	}
	else theType = 'white';
	var myElement = jQuery(this);
	var nw = jQuery('<span />');
	nw.addClass('corners-' + theType + ' nw');
	if (hasCustomClass) nw.addClass(customClass);
	myElement.append(nw);
	var ne = jQuery('<span />');
	ne.addClass('corners-' + theType + ' ne');
	if (hasCustomClass) ne.addClass(customClass);
	myElement.append(ne);
	var se = jQuery('<span />');
	se.addClass('corners-' + theType + ' se');
	if (hasCustomClass) se.addClass(customClass);
	myElement.append(se);
	var sw = jQuery('<span />');
	sw.addClass('corners-' + theType + ' sw');
	if (hasCustomClass) sw.addClass(customClass);
	myElement.append(sw);
}

/* Get Query String Parameter */
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, ' ')) };
}

