/* Author: Ash Crooks, Web Designer & Developer (http://ash.crooks.me/)
*/

jQuery.noConflict(); //noConflict just incase and for future-proofing
jQuery(document).ready(function($){

	/*
 * call this function on a scrollable node to add some more clones on the right side of the carousel
 * as the jQuery Tools Scrollable plugin is build to just show one item on screen at once (not
 * multiple small elements). This function fills up the empty space right to the carousel with dummy
 * nodes
 * @param int addItems [OPTIONAL] define the number of clone items to aditionally add to the wrapper
 * @return void
 */
$.fn.scrollableAddClones = function(addItems) {
  // grab scrollable plugin
  var scrollable;
  if (!(scrollable = $(this).data('scrollable')) || !scrollable.getConf().circular)
    return;
  // grab scrollable elements and remember it's count
  var nodes = scrollable.getItems();
  var length = nodes.length;
  // grab class for the nodes
  var clonedClass = scrollable.getConf().clonedClass;
  // get wrap object to append the clones to
  var wrap = scrollable.getItemWrap();
  // fill as much nodes as needed for 500 pixels
  if (!addItems) addItems = Math.ceil(500 / nodes.eq(1).width());
  // create fake container to add the clones to (max 15 clones)
  var newNodesAppend = $('<span />');
  for (var i = 1; i <= (addItems < 15 ? addItems : 15); i++)
    nodes.eq(i % length).clone().addClass(clonedClass).appendTo(newNodesAppend);
  // insert HTML
  newNodesAppend.children().appendTo(wrap);
}

	$('#menu-backdrop').hover(function(){
		$('#menu').fadeOut();
		$(this).hide();
	}, function(){

	});

	$('header nav #nav20 a').hover(function()
	{
		$('#menu').slideDown();
		$('#menu-backdrop').show();

		$('#menu').hover(function(){

		}, function(){
			$(this).fadeOut();
			$('#menu-backdrop').hide();
		})
	}, function(){

	});

	$('#menu .cont').scrollable({
		mousewheel: true,
		circular: true
	});
	$('#menu .cont').scrollableAddClones();

	//set list heights so they are all the same
	var tallest=0;
	$('#carousel').children('li').each(function(){
		if($(this).height() > tallest){
			tallest = $(this).height();
		}
	}).css('height', tallest);

	$('#menu .cont').css('height', $('#carousel').first('li').outerHeight());

	//move the menu back to its place but hide it
	$('#menu').hide().css('top', '0px');


	if($('#photo img').length > 1)
	{
		var i=1, timer,
		changeTo = 0,
		fadeDur = 500,
		waitDur = 6000,
		pel = '#photo-bg',
		current = '#p1',
		index = 1,
		total = $('img', pel).length,
		bel = '#photo ul';

		// ############# INIT
		$('img', pel).each(function(){
			$(this).data('index', i);
			i++;
		});

		$('li a[href='+current+']', bel).addClass('current');

		$(bel).show();

		// ############# CHANGE FUNC
		function changePhoto(changeTo)
		{
			clearTimeout(timer);

			$(current).fadeOut(fadeDur);

			if(changeTo == 0){
				if(index == total){
					index = 1;
				}else{
					index++;
				}
			}else{
				index = changeTo;
			}

			current = '#p'+index;

			setTimeout(function(){

				$(current).fadeIn(fadeDur).css('display', 'block');
				$('li a', bel).removeClass('current');
				$('li a[href='+current+']', bel).addClass('current');

				setTimeout(function(){

					timer = setTimeout(function(){
						changePhoto(0);
					}, waitDur);

				}, fadeDur);

			}, fadeDur/2);
		}

		// ############# ON CLICK
		$('a', bel).click(function(){
			changeTo = $($(this).attr('href'), pel).data('index');
			changePhoto(changeTo);
			return false;
		})

		// ############# START
		timer = setTimeout(function(){
			changePhoto(0);
		}, waitDur);
	}


	if($('.borderradius #product .image').length == 1)
	{
		var imagesrc, imageheight;
		imagesrc = $('.borderradius #product .image img').attr('src');
		imageheight = $('.borderradius #product .image img').height();
		//alert(imagesrc);
		$('.borderradius #product .image img').parent().css('background', 'url("'+imagesrc+'") no-repeat center center');//.css('height', imageheight);
		$('.borderradius #product .image img').remove();
	}

	if($('#product .image').length > 0){
		function setHeader(){
			var hgroup = $('#fancybox-wrap hgroup');
			var title = $('#fancybox-wrap #fancybox-title');
			var height = hgroup.height() + 10;

			title.css('left', 'auto').css('top', '-'+height);
			$('#fancybox-wrap').css('padding-top', height).css('top', 20);

			setTimeout(function(){
				$('#fancybox-wrap').css('top', 20);
			}, 300);
		}

		$('#product .image a').fancybox({
			'padding'			:	0,
			'autoScale'		: false,
			'speedIn'			:	600,
			'speedOut'		:	200,
			'overlayOpacity' : 0.9,
			'overlayColor':	'#000',
			'titleShow'		: true,
			'title'				: '<hgroup>'+$('#product hgroup').html()+'</hgroup>',
			'onComplete'	: setHeader
		});
	}

	if($('#product').length == 1 && $('#sidebar ul').length == 1)
	{
		var a, b, c, productheight, additems;
		a = $('#product .image').offset().top;
		b = $('#product .info').offset().top;
		c = $('#product .info').height();

		productheight = b - a;
		productheight = productheight + c;
		productheight = productheight - 17;

		$('#sidebar .cont').height(productheight);
		additems = productheight / 32;

		$('#sidebar .cont').scrollable({
			vertical: true,
			mousewheel: true,
			circular: true,
			prev: '.ansnasnas',
			next: '.more'
		});
		$('#sidebar .cont').scrollableAddClones(additems);
		
	}

	if($('.borderradius #category .image').length > 0)
	{
		$('.borderradius #category .image').each(function()
		{
			var imagesrc, imageheight;
			imagesrc = $('img', this).attr('src');
			imageheight = $('img', this).height();
			$('img', this).parent().css('background', 'url("'+imagesrc+'") no-repeat center center');//.css('height', imageheight);
			$('img', this).remove();
		});
	}

	if($('#category .thumb').length > 0)
	{
		$('.borderradius #category .thumb').each(function()
		{
			var imagesrc;
			imagesrc = $('img', this).attr('src');
			$('img', this).parent().css('background', 'url("'+imagesrc+'") no-repeat center center');
			$('img', this).remove();
		});

		$('#category .item .thumb').css('opacity', '0.7');

		$('#category .item').hover(function()
		{
			$('.thumb', this).css('opacity', '1');
			$(this).css('cursor', 'pointer');
			$('a', this).addClass('hover');
		}, function()
		{
			$('.thumb', this).css('opacity', '0.7');
			$('a', this).removeClass('hover');
		});

		$('#category .item').click(function()
		{
			window.location=$('a', this).attr('href'); return false;
		});
	}

	if($('#category-list .image').length > 0)
	{
		$('.borderradius #category-list .image').each(function()
		{
			var imagesrc;
			imagesrc = $('img', this).attr('src');
			$('img', this).parent().css('background', 'url("'+imagesrc+'") no-repeat center center');
			$('img', this).remove();
		});

		$('#category-list .item').hover(function()
		{
			$(this).css('cursor', 'pointer');
			$('a', this).addClass('hover');
			$(this).siblings('.item').css('opacity', 0.5);
		}, function()
		{
			$('a', this).removeClass('hover');
			$(this).siblings('.item').css('opacity', 1);
		});

		$('#category-list .item').click(function()
		{
			window.location=$('a', this).attr('href'); return false;
		});
	}

	if($('#category-gallery .image').length > 0)
	{
		$('.borderradius #category-gallery .image').each(function()
		{
			var imagesrc;
			imagesrc = $('img', this).attr('src');
			$('img', this).parent().css('background', 'url("'+imagesrc+'") no-repeat left top');
			$('img', this).remove();
		});

		$('#category-gallery .item').hover(function()
		{
			$(this).css('cursor', 'pointer');
			$('h3', this).hide();
			$(this).siblings('.item').css('opacity', 0.5);
		}, function()
		{
			$('h3', this).show();
			$(this).siblings('.item').css('opacity', 1);
		});

		$('#category-gallery .item').click(function()
		{
			window.location=$('a', this).attr('href'); return false;
		});
	}


});//jQuery

