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

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
	---------------------*/
	
	// Page in
	jQuery('#webpage').fadeIn(function()
	{
		//jQuery('#header').css('opacity', 0.4);
		
		jQuery('#header').animate(
			{top: 30}, {duration: 500, easing: "easeOutCubic", complete: function()
			{
				letsLoadTheContent();
			}
		});
		jQuery('#footer').animate({bottom: -2}, {duration: 500, easing: "easeOutCubic"});
	});
	
	// Page out
	//jQuery('a.ajax').click(function() { return pageOut(jQuery(this)); });
	//jQuery('.main-menu li a').click(function() { return pageOut(jQuery(this)); });
	
	function pageOut(anchor)
	{
		jQuery('#content').fadeOut(function()
		{
			jQuery('#header').animate(
				{top: '-100'}, {duration: 500, easing: "easeOutCubic", complete: function()
				{
					jQuery('#webpage').fadeIn(function() { location.href = anchor.attr('href'); });
				}
			});
			jQuery('#footer').animate({bottom: '-100'}, {duration: 500, easing: "easeOutCubic"});
		});
		
		return false;
	}
	
	/*var int_opacity = 1;
	$(document).bind('mousemove',function(e)
	{
		if (e.pageY < 140) int_opacity = 1;
		else if (e.pageY < 143) int_opacity = 0.95;
		else if (e.pageY < 146) int_opacity = 0.90;
		else if (e.pageY < 149) int_opacity = 0.85;
		else if (e.pageY < 152) int_opacity = 0.80;
		else if (e.pageY < 155) int_opacity = 0.75;
		else if (e.pageY < 158) int_opacity = 0.70;
		else if (e.pageY < 161) int_opacity = 0.65;
		else if (e.pageY < 164) int_opacity = 0.60;
		else if (e.pageY < 167) int_opacity = 0.55;
		else if (e.pageY < 170) int_opacity = 0.50;
		else if (e.pageY < 173) int_opacity = 0.45;
		else if (e.pageY >= 173) int_opacity = 0.40;
		
		if (e.pageY >= 205 && jQuery('#header').css('opacity') == '1' && jQuery('#header').is(':animated')) jQuery('#header').animate({ opacity: 0.4 }, 200);
		jQuery('#header').css('opacity', int_opacity);
	});*/
});

// Center content
var shouldWeCenterTheContent = false;
jQuery(window).bind("resize", centerContent);
function centerContent()
{
	if (shouldWeCenterTheContent)
	{ 
		var contentMargin = ((jQuery(window).height()-jQuery('#content').height()+40)/2);
		if (contentMargin < 150) contentMargin = 150;
		jQuery('#content').css('margin-top', contentMargin);
	}
}

/* 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, ' ')) };
}

