/* Author: 

*/

var speed = 1000;
var scrollSpeed = 500;
var trans = 'easeOutExpo';

$(function()
{
	init_stockists();

	$('#container').fadeIn(speed);
	if($('#intro').length)
	{
		$('#intro').fadeIn(4000);
		$('header,#slideshow').hide();
		setTimeout(function()
		{
			$('#intro').fadeOut(2000,function()
			{
				$('header').fadeIn(2000,function()
				{
					$('#slideshow').fadeIn(2000);
				});	
			});
		},6000);
	}
	
	$('#basket').load('/shop/ajax_basket');
	init_drops();
	
});

var init_drops = function()
{
	$('.active a').click(function(e)
	{
		e.stopPropagation();
		$(this).parent().parent().find('ul').show();
	});
	
	$(document).click(function(e)
	{
		$('.drop ul').hide();
	});
}

var overlayExists = false;
var retVal;
var paper = false;
var bigImage,circle;

function createOverlay(item,id)
{
	/*
	paper = Raphael(document.getElementById('overlay'),200,200);
	var svg = "http://www.w3.org/2000/svg";
	var xlnk = "http://www.w3.org/1999/xlink";
	var defs = document.getElementsByTagName('defs')[0];
	var svgcanv=document.getElementsByTagName('svg')[0];

	var clippath=document.createElementNS(svg,"clipPath");
	clippath.setAttribute("id","clip");
	svgcanv.appendChild(clippath);

	bigImage = paper.image("images/item-large.jpg",0,0,1300,3250).attr({fill:"#111"});
	bigImage.node.setAttribute("clip-path","url(#clip)");
	        
	circle = paper.circle(100,100,100).attr({fill:"#F00"});
	circle.node.id = id;
	var cpx=document.getElementById(id);
	clippath.appendChild(cpx);
	overlayExists = true;
	retVal = bigImage;
	return bigImage;
	*/
}

openItem = 0;



theTimer = 0;

function viewZoom(item)
{
	clearTimeout(theTimer);
	var id = $(item).attr('id') + '-svg';
	
	var image = createOverlay($(item).attr('id'),id);
	var svg = $('svg');
	
	var off = $(item).offset();
	var wRatio = (200 / 1300) * 100;
	var hRatio = (500 / 3250) * 100;
	
	var offSet = (off.left + 100) > ($(window).width()) / 2 ? -150 : 150;
	/*
	$(document).mousemove(function(e)
	{
		$('#overlay').fadeIn();
		var t = (e.pageY - 100);
		var l = (e.pageX - 100);
		
		// get mouse pos over smaller image
		var mx = e.pageX - off.left;
		var my = e.pageY - off.top;

		// get mouse pos over larger image plus half the width of the mask
		var maskY = 0 - Math.round(((my / hRatio) * 100));
		var maskX = 0 - Math.round(((mx / wRatio) * 100));		

		image.node.setAttribute('x',maskX + 100);
		image.node.setAttribute('y',maskY + 100);
		
		$('#overlay').css({'top':t,'left':l+offSet});
		
		var show = true;
		if((e.pageX) > (off.left + 200) || (e.pageX) < off.left) show = false;
		if((e.pageY) > (off.top + 600) || (e.pageY) < off.top) show = false;
		if(!show)
		{
			svg.detach();
			$(this).unbind('mousemove');
			$('#overlay').hide();
		}
	});
	*/
}

function init_zoom()
{
	$('#product-view img').hover(function()
	{
		$('#product-zoom').fadeIn();
		var offset = $(this).offset();
		var wRatio = (240 / zoomWidth) * 100;
		var hRatio = (518 / zoomHeight) * 100;
		$(document).mousemove(function(e)
		{
			var t = e.pageY;
			var l = e.pageX;
			var tpos = t - offset.top;
			var lpos = l - offset.left;
			
			var bigTop = Math.round((tpos / hRatio) * 100) - 285;
			var bigLeft = Math.round((lpos / wRatio) * 100) - 195;
			//console.log(tpos);
			
			$('#product-zoom img').css(
				{
					'left':-bigLeft+'px',
					'top':-bigTop+'px'
				});
			
		});
	},function()
	{
		$('#product-zoom').fadeOut();
	});
}

function buy(id,collection)
{
	var w = $(window).width();
	var h = $(window).height();
	
	var t = (h / 2) - 309;
	var l = (w / 2) - 392;
	
	$('#buy').css({
		'top':t+'px',
		'left':l+'px'
	}).fadeIn('',function()
	{
		// load buy data
		$(this).load('/ajax/buy/'+id+'/'+collection,function()
		{
			init_zoom();
			init_drops();
			init_buy_nav();
		});
	});
	$('#buy-bg').fadeTo('',0.7);
}

function init_buy_nav()
{
	$('#buy .buy-nav a').click(function(e)
	{
		$('#buy .buy-nav li').removeClass('selected');
		$(this).parent().addClass('selected');
		e.preventDefault();
		var href = $(this).attr('href');
		if($(this).attr('id') == 'item-info')
		{
			$('#ajaxed').fadeOut('',function()
			{
				$('#product-details').fadeIn();
			});
		} else {
			$('#product-details,#ajaxed').fadeOut('',function()
			{
				$.post(href,{ajax:true},function(res)
				{
					$('#ajaxed').html(res).fadeIn();
				});
			});
		}
	});
}

function addtobasket()
{
	var size = activeSize;//$('#size').val();
	var qty = activeQty;//$('#qty').val();
	//console.log(size,qty);
	$('#basket').load('/shop/buy/' + size + '/' + qty);
}

function hideBuy()
{
	$('#buy-bg,#buy').fadeOut('',function()
	{
		$('#buy').html('');
	});
}

function load_view(large,full)
{
	var i = new Image();
	$(i).load(function()
	{
		$('#product-view img').attr('src',$(this).attr('src'));
	});
	i.src = large;
	
	var f = new Image();
	$(f).load(function()
	{
		$('#product-zoom img').attr('src',$(this).attr('src'));
	});
	f.src = full;
}

var fsOpen = false;

function showFS(id)
{
	var ft = 2;
	var $item = $('#item-'+id);
	$nextItem = $item.next();
	$prevItem = $item.prev();
	var $fs = $('#fullscreen');
	var wh = $(window).height();
	$fs.width($(window).width());
	$fs.height(wh);
	
	if(fsOpen)
	{
		$('#fs-info,img.full').fadeOut(500);
		ft = 500
	}
	setTimeout(function()
	{
		// load buy data
		$fs.load('/ajax/fullscreen/'+id,function()
		{
			$('#fs-info,img.full').hide();
			$(this).find('.close').width($(window).width() - 70);
			//$('.close').width($(window).width() - 70);
			$('footer').addClass('fs').width($(window).width() - 70);;
			$(window).resize();
			var top = ($(window).height() / 2) - 105;
			$('#fs-next,#fs-prev').css('top',top+'px');


				$('#fs-next').click(function()
				{
					nextFSItem();
				});
				$('#fs-prev').click(function()
				{
					prevFSItem();
				});


				hideBuy();
			if($nextItem.length) $('#fs-next').show();
			else $('#fs-next').hide();
			if($prevItem.length) $('#fs-prev').show();
			else $('#fs-prev').hide();
			
			// set image height
			$('img.full').css('height',(wh - 120));
			

			$(this).fadeIn(500,function()
			{
				$('#fs-info,img.full').fadeIn(500);
			});
			fsOpen = true;
		});
	},ft)
	
}

var nextFSItem = function()
{
	var id = parseInt($nextItem.attr('id').replace('item-',''));
	showFS(id);
}

var prevFSItem = function()
{
	var id = parseInt($prevItem.attr('id').replace('item-',''));
	showFS(id);
}

function hideFS()
{
	$('footer').removeClass('fs').css('width','890px');
	$('#fullscreen').fadeOut('',function()
	{
		$(this).html('');
	});
}

function init_overlay()
{
	$(document).mousemove(function(e)
	{
		var l = e.pageX + 10;
		var t = e.pageY + 10;
		$('#info').css({
			'top':t+'px',
			'left':l+'px'
		});
	});
	
	$('.product').hover(function()
	{
		var d = $(this).find('.details').html();
		$('#info').html(d).stop(true,true).fadeIn();
	},function()
	{
		$('#info').stop(true,true).fadeOut();
	});
}

function init_stockists()
{
	if($('.stockists').length)
	{
		$('#content ul').hide();
		$('#content h3').each(function()
		{
			$(this).hover(function()
			{
				$(this).addClass('over');
			},function()
			{
				$(this).removeClass('over');
			});
			$(this).click(function()
			{
				var n = $(this).next();
				if(n.get(0).tagName == 'UL')
				{
					if(n.is(':visible')) 
					{
						n.slideUp();
						$(this).removeClass('selected');
					}
					else {
						n.slideDown();
						$(this).addClass('selected');
					}
				}
			});
		});
	}
}

function init_journal()
{
	var count = 1;
	$selected = $('.post').first();
	$selected.addClass('selected');
	$next = $selected.next('.post');
	$prev = $selected.prev('.post');
	
	var p = $('#journal').offset();
	$selected.css('left',p.left+'px');
	$('.post').each(function()
	{
		$(this).css('position','absolute');
	});
	
	$(window).resize(function()
	{
		var w = $(window).width();
		$('#posts').css(
		{
			'top':(p.top + 50)+'px',
			'width':w+'px'
		});	
		var off = parseInt($next.find('img').css('margin-left')) + 40;
		$next.css('left',(w - off)+'px');
		$('#next').css('right','auto');
		$('#next').css('left',(w-16)+'px');
		$selected.css('left',((w/2) - 445)+'px');
	});
	
	$(window).trigger('resize');
	
	init_next_post();
		
	$selected.fadeIn();
	$next.fadeIn();
	$selected.find('.post-text').fadeIn();

}

var init_next_post = function()
{
	if($next == null) return false;
	$next.unbind('click');
	$next.click(function()
	{
		var nextLeft = parseInt($selected.css('left'));
		var selectedLeft = 890 - (parseInt($selected.find('img').css('margin-left')) + 40);
		
		$selected.removeClass('selected');
		$next.addClass('selected');
		
		$selected.find('.post-text').fadeOut(speed);
		$selected.animate({
			'left':-selectedLeft+'px'
		},speed,'easeOutQuint');
		
		if($prev != null)
		{
			$prev.fadeOut();
		}
		
		$next.animate({
			'left':nextLeft+'px'
		},speed,'easeOutQuint',function()
		{
			$(this).find('.post-text').fadeIn(speed);
			$prev = $selected;
			$selected = $next;
			var next = $next.next('.post');
			if(next.length)
			{
				var w = $(window).width();
				var off = parseInt(next.find('img').css('margin-left')) + 40;
				next.css('left',(w - off)+'px').fadeIn();
				$next = next;
			} else
			{
				$next = null;
				$('#next').fadeOut();
			}
				init_next_post();
				init_prev_post();
				$selected.unbind('click');
		});
	});
}

var init_prev_post = function()
{
	if($prev == null) return false;
	$prev.unbind('click');
	$prev.click(function()
	{
		var w = $(window).width();
		var nextLeft = parseInt($selected.css('left'));
		var selectedLeft = w - (parseInt($selected.find('img').css('margin-left')) + 40);
		$selected.find('.post-text').fadeOut(speed);
		$selected.animate({
			'left':selectedLeft+'px'
		},speed,'easeOutQuint');
		
		$selected.removeClass('selected');
		$prev.addClass('selected');
		
		if($next != null)
		{
			$next.fadeOut();
		}
		
		$prev.animate({
			'left':nextLeft+'px'
		},speed,'easeOutQuint',function()
		{
			$(this).find('.post-text').fadeIn(speed);
			$next = $selected;
			$selected = $prev;
			var prev = $prev.prev('.post');
			if(prev.length)
			{
				var off = 890 - (parseInt(prev.find('img').css('margin-left')) + 40);
				prev.css('left',-(off)+'px').fadeIn();
				$prev = prev;
			} else
			{
				$prev = null;
				$('#prev').fadeOut();
			}
				init_next_post();
				init_prev_post();
				$selected.unbind('click');
		});	});
	

}


function init_collection()
{
	$items = $('.item');
	
	// set collection width
	$('#items').width(($items.length * 144) + 500);
	$items.fadeIn();
	
	$items.each(function()
	{
		var off = $(this).position();
		$(this).data('initialOffset',off.left);
		
		$(this).hover(function()
		{
			if(!$(this).hasClass('selected') && !$(this).hasClass('next')) $(this).find('img').stop().fadeTo(200,0.5);
		},function()
		{
			$(this).find('img').stop().fadeTo(200,1);
		});
		
		$(this).click(function()
		{
			if(!$(this).hasClass('selected') && !$(this).hasClass('next')) selectItem($(this));
		});
	});
	
	if($('#items').width() > 960)
	{
		$('#next').fadeIn();
	}
	
	$('#next').hover(function()
	{
		$('#prev').fadeIn();
		scrollItemsLeft();
	},function()
	{
		stopScrolling();
	});
	
	$('#prev').hover(function()
	{
		$('#next').fadeIn();
		scrollItemsRight();
	},function()
	{
		stopScrolling();
	});
	
}

var isOpening = false;
var $currentItem = 0;

function selectItem($el)
{
	if($el.hasClass('open')) return false;

	var oWidth = 600;

	if(isOpening) return false;
	isOpening = true;
	
	if($currentItem)
	{
		$currentItem.animate({
			'width':'144px',
			'margin-top':'100px'
		},speed,'easeOutExpo',function()
	{
		$(this).find('h3').first().show();
	}).removeClass('open');
		$currentItem.find('img').animate({
			'width':'144px',
			'height':'311px'
		},speed,'easeOutExpo');
		
		//$currentItem.find('h3').first().fadeIn();
		$currentItem.find('.item-desc').fadeOut();	
	}
	
	// do we need to move the items div?
	
	var itemsLeft = Math.abs(parseInt($('#items').css('left')));
	//if(itemsLeft < 0) itemsLeft = !itemsLeft;
	var viewportPos = $el.data('initialOffset') - itemsLeft;
	//console.log(itemsLeft,viewportPos);
	if((viewportPos + oWidth) > 960)
	{
		var diff = (viewportPos + oWidth + 144) - 960;
		//console.log($el.data('initialOffset'),itemsLeft,viewportPos,diff)
		$('#items').animate({
			'left':'-=' + diff + 'px'
		},speed,'easeOutExpo');
	}
	
	if(viewportPos < 0)
	{
		var diff = Math.abs(viewportPos);
		$('#items').animate({
			'left':'+=' + diff + 'px'
		},speed,'easeOutExpo');
	}
	
	$el.animate({
		'width':'518px',
		'margin-top':'0px'
	},speed,'easeOutExpo',function()
	{
		$(this).addClass('open');
		$(this).find('.item-desc').fadeIn();
		$currentItem = $(this);
		isOpening = false;
	});
	$el.find('h3').first().hide();
	
	$el.find('img').animate({
		'width':'240px',
		'height':'518px',
		'opacity':'1'
	},speed,'easeOutExpo');
}

function closeItems()
{
	$currentItem.removeClass('open').animate({
		'width':'144px',
		'margin-top':'100px'
	},speed,'easeOutExpo',function()
	{
		$(this).find('h3').first().show();
	});
	$currentItem.find('img').animate({
		'width':'144px',
		'height':'311px'
	},speed,'easeOutExpo');
	$currentItem.find('.item-desc').fadeOut();	
	$currentItem = 0;
}

var isMoving = false;
var stopScrollingLeft = false;
var trans = 'linear';

var scrollItemsLeft = function()
{
	// prevent overscroll
	if(isMoving) return false;
	isMoving = true;
	stopScrollingLeft = false;
	//var trans = 'linear';
	
	if($('.item.open').length) closeItems();
	
	// calculate boundary
	var itemsWidth = $('.item.open').length ? (($items.length * 144) + 374) : $items.length * 144;
	var itemsPos = parseInt($('#items').css('left'));
	var minLeft = 960 - itemsWidth;
	
	// calculate next end position
	var nextPos = itemsPos - 144;
	
	if(nextPos <= minLeft) {
		nextPos = minLeft;
		stopScrollingLeft = true;
		$('#next').fadeOut();
		trans = 'easeOutExpo';
	}
	
	//console.log(trans);	
	$('#items').animate({
		'left':'-=144px'
	},1000,'linear',function()
	{
		isMoving = false;
		trans = 'linear';
		if(!stopScrollingLeft) scrollItemsLeft();
	});
	
}

var scrollItemsRight = function()
{
	// prevent overscroll
	if(isMoving) return false;
	isMoving = true;
	stopScrollingRight = false;
	//var trans = 'linear';
	
	if($('.item.open').length) closeItems();
	
	// calculate boundary
	var itemsWidth = $('.item.open').length ? (($items.length * 144) + 374) : $items.length * 144;
	var itemsPos = parseInt($('#items').css('left'));
	var maxLeft = 0;
	
	// calculate next end position
	var nextPos = itemsPos + 144;
	if(nextPos >= maxLeft) {

		nextPos = maxLeft;
		stopScrollingRight = true;
		trans = 'easeOutExpo';
		$('#prev').fadeOut();
	}
	
	//console.log(trans);	
	$('#items').animate({
		'left':'+=144px'
	},1000,'linear',function()
	{
		isMoving = false;
		trans = 'linear';
		if(!stopScrollingRight) scrollItemsRight();
	});
	
}

function stopScrolling()
{
	trans = 'easeOutExpo';
	stopScrollingLeft = stopScrollingRight = true;
}


$(function()
{
		$('p.close').click(function()
		{
			hideBuy();
			hideFS();
		});
		
		if($('#products').length)
		{
			init_overlay();
		}
		
		if($('#journal').length)
		{
			init_journal();
		}
		
		if($('#carousel').length)
		{
			init_collection();
		}
		
		$('nav a,#subnav a,#categories a,h1 a').click(function(e)
		{
			e.preventDefault();
			var href = $(this).attr('href');
			$('#container').fadeOut(speed,function()
			{
				location = href;
			});
		});
		
		$(window).resize(function()
		{
			$('#fullscreen').width($(window).width());
			$('#fullscreen').height($(window).height());
			$('#fullscreen .close,footer.fs').width($(window).width() - 70);
			$('img.full').css('height',($(window).height() - 120));
			var top = ($(window).height() / 2) - 105;
			$('#fs-next,#fs-prev').css('top',top+'px');
		});
		
		
});




















