
var blogOffset = 0;
var blogPage = 1;
var buying = false;

$(document).ready(function()
{
	
	// initialise homepage grid
	$('#home').grid('.post',
	{
		col_width:231,
		col_height:354,
		gutter:6
	});
	
	// initialise search
	$('#search input').focus(function()
	{
		if($(this).attr('value') == 'Search') $(this).attr('value','');
	});
	$('#search input').blur(function()
	{
		if($(this).attr('value') == '') $(this).attr('value','Search');
	});
	
	// initialise navigation drop downs
	$('.drop-down p a').click(function()
	{
		var p = $(this).parent().parent();
		var isOpen = p.find('ul').is(':visible');
		$('.drop-down ul').hide();
		$('.drop-down').css('z-index',1);
		if(!isOpen) {
			p.css('z-index',5);
			p.find('ul').show();
			$(window).mouseup(function()
			{
				$('.drop-down ul').hide();
				$(this).unbind('mouseup');
			});
		}
	});
	
	// basket ajax
	
	$('a.buy').click(function(e)
	{
		buying = true;
		e.preventDefault();
		var lnk = this;
		$.get(this.href,function(data)
		{
			$('#basket').html(data);
			$(lnk).parent().html('<p class="small">In your basket</p>');
			buying = false;
		});
	});
	
	$('#basket').load('/shop/ajax_basket',function()
	{
		$(this).fadeIn();
	});
	
	// initialise follow links
	$('#menu .sn a, a.blank').attr('target','_blank');
	
	// Blog feed
	
	$('.blog-prev,.blog-next').hover(function()
	{
		var im = $(this).find('img');
		var src = im.attr('src').replace('.png','-over.png');
		im.attr('src',src);
	},function()
	{
		var im = $(this).find('img');
		var src = im.attr('src').replace('-over.png','.png');
		im.attr('src',src);
	});
	
	$('.blog-next').click(function()
	{
		$('.blog-prev').fadeIn();
		blogOffset += 3;
		blogPage++;
		$('#blog-feed').fadeOut('',function()
		{
			$(this).load('/ajax/blog/' + blogOffset,function()
			{
				initOverlays();
				$(this).fadeIn();
			});
		});
		if(blogPage == blog_pages) {
			$('.blog-next').fadeOut();
		}
	});
	
	$('.blog-prev').click(function()
	{
		$('.blog-next').fadeIn();
		blogOffset -= 3;
		blogPage--;
		$('#blog-feed').fadeOut('',function()
		{
			$(this).load('/ajax/blog/' + blogOffset,function()
			{
				initOverlays();
				$(this).fadeIn();
			});
		});
		if(blogPage == 1) {
			$('.blog-prev').fadeOut();
		}
	});
	
	initOverlays();
	
	$('div.select').each(function()
	{
		var d = this;
		$(this).find('.selected a').click(function()
		{
			
			$(d).css('z-index',100);
			$(d).find('ul').show();
			$(this).addClass('over');
		});
	});
	
	$(document).mouseup(function()
	{
		$('div.select ul').hide();
		$('div.select .selected a').removeClass('over');
	});
	
	$(window).scroll(function()
	{
		var max = 267 + $('#blog-feed').height();
		
		if(max < $(this).scrollTop())
		{
			$('.blog-title h3,.blog-title div').hide();
		} else $('.blog-title h3,.blog-title div').show();
		
		if($(this).scrollTop() > 267)
		{
			$('#twitter-title').hide();
			$('#blog-title').show();
		} else {
			$('#twitter-title').show();
			$('#blog-title').hide();
		}
	});

	
});

var set_shipping = function(text,selector,dest)
{
	$('#shipping_opt').attr('value',dest);
	$('#' + selector).find('.selected a').removeClass('over').html(text);
	$('#' + selector).find('ul').hide();
	$('#' + selector).css('z-index',1);
	$('#basket-form').submit();
}

var set_card = function(text,selector,dest)
{
	$('#card_type').attr('value',dest);
	$('#' + selector).find('.selected a').removeClass('over').html(text);
	$('#' + selector).find('ul').hide();
	$('#' + selector).css('z-index',1);
}

var set_qty = function(qty,selector,input)
{
	
	$('#'+input).attr('value',qty);
	var t = qty > 1 ? ' items' : ' item';
	$('#' + selector).find('.selected a').removeClass('over').html(qty + t);
	$('#' + selector).find('ul').hide();
	$('#' + selector).css('z-index',1);
	$('#basket-form').submit();
}

var initOverlays = function()
{
	//initialise post functionality 
	$('#home .post,#blog-feed .post,#products .post-image,#related .post-image').mouseenter(function()
	{
		$(this).children('.overlay').fadeIn(300);
	});
	$('#home .post,#blog-feed .post,#products .post-image,#related .post-image').mouseleave(function()
	{
		$(this).children('.overlay').fadeOut(300);
	});
	
	$('#blog-feed .post,#related .post-image').click(function()
	{
		var href = $(this).find('a').attr('href');
		location = href;
	});
	
	$('#home .post,#products .post-image').click(function()
	{
		if(!buying){
			var href = $(this).find('a').first().attr('href');
			location = href;
		}
	});
}

var leave_comment = function(f)
{
	if($('#comment_name').attr('value') == '') { alert('Please enter your name'); return false; }
	if($('#comment_email').attr('value') == '') { alert('Please enter your email address'); return false; }
	if($('#comment_text').val() == '') { alert('Please enter a comment'); return false; }
	
	$('#new-comment .submit-red').attr('value','Please wait...');
	var data = $('#new-comment').serialize();
	$.post('/ajax/comment',data,function(d)
	{
		$('#comment-res').fadeOut('',function()
		{
			$(this).html(d).fadeIn();
		});
	});
	return false;
}
