// jq tabs plugin
$.fn.btabs = function()
{
	var self = this;
	this.$lis = $('li:has(a[href])', self);
	this.$tabs = this.$lis.map(function() { return $('a', this)[0]; });
	this.$tabs.each(function(i, a)
	{
		$(a).click(function()
		{
			self.$lis.removeClass('active');
			$(this).parent().addClass('active');
			var myhref = this.href;
			var pos = myhref.indexOf('#');
			if (pos < 0 ) return true;
			var mytab = myhref.substring(pos, myhref.length);
			var cpos;
			self.$tabs.each(function(j, ah)
			{
				currhref = this.href;
				if (myhref != currhref)
				{
					cpos = currhref.indexOf('#');
					if (cpos >= 0 )
					{
						currtab = currhref.substring(cpos, currhref.length);
						$(currtab).hide();
					}
				}
			});
			$(mytab).show();
			return false;
		});
	});
}

// MAGNIFIER
$.fn.bambizziZoom = function()
{
	var $s = $('#zoom-on'); //small
	var $b = $('#zoom-img'); //big
	var $l = $('#zoom-lens');
	var $i = $('#zoom-img img');
	var sw = $s.width();

	var sh = $s.height();
	var iw = 0, ih = 0, sx = 0, sy = 0, lw = 0, lh = 0;
	var so = $s.offset();
	var $e;
/*
	//$l.css('opacity', '0.5');

	if ($.browser.msie && $.browser.version < 7)
	{
		var bg = $('#zoom-er').css('background-image').replace(/^url\(\"/, "").replace(/\"\)$/, "");
		$('#zoom-er').css('background', 'transparent');
		$('#zoom-er').css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + bg + '\',sizingMethod=\'scale\')');
	}
*/
	$s.hover(function(e)
	{
		//on
		if ($s.attr('jqbig') == '')
		{
			return false;
		}
//		alert($e.pageX + ' '+ window.event.clientX);
		$l.css('background-image', 'url('+$('#item-photos-current').attr('src')+')');
		$('#zoom-active').css('opacity', '0.5');
		$('#zoom-active').show();
		$('#frm-basket select').hide();
		$s.mousemove(zoomMove);
		$('#zoom-er').fadeIn('normal', function() {
			$i[0].onload = function()
			{
				$i.show();
				iw = this.width;
				ih = this.height;
				sx = iw / sw;
				sy = ih / sh;
				lw = $b.width() / sx;
				lh = $b.height() / sy;
				$l.width(lw);
				$l.height(lh);
//				zoomMove(e);
		};
		$i.attr('src', $s.attr('jqbig'));
		});
	},
	function()
	{
		//off
		if ($s.attr('jqbig') == '')
		{
			return false;
		}
		$('#frm-basket select').show();
		$l.hide();
		$('#zoom-active').hide();
		$s.unbind('mousemove');
		$('#zoom-er').fadeOut('normal');
		$i.hide();

	});
	function zoomMove(e)
	{
		$e = e;
		if (!ih)
		{
			return;
		}
		var x = $e.pageX - so.left;
		var y = $e.pageY - so.top;
		x = ((x - lw / 2 < 0 ) ? 0 : (x + lw / 2 > sw ) ? (sw - lw)  : x - lw / 2);
		y = ((y - lh / 2 < 0 ) ? 0 : (y + lh / 2  > sh ) ? (sh - lh ) : y - lh / 2);
		$l.css({left: x, top: y, display: 'block', backgroundPosition: '-'+x+'px -'+y+'px'});
		$i.css({left: x*sx * -1, top: y* sy * -1});

	}

}

/* MENU */
function bambizziMenu()
{

	/* vertical menu */
	if ($('#menuarea').length)
	{
		//$vmnu = $('#hormenu').clone(true);
		//alert($vmnu.children().length);
		$('#menuarea').prepend('<ul />');
		$ul = $('#menuarea ul:first');
		$('#hormenu').children().each(function(i, o)
		{
			//if (i > 0)
			if (o.className == 'active')
			{
//				alert(o);
				$o = $(o).clone();
				$o.children().each(function(j, oo)
				{
					if (j)
					{
						$(oo).wrap('<div class="second-menu" />');
					}
					else
					{
						//$(oo).removeAttr("style");
						//$(oo).find('span').remove();
						/*
						$(oo).click(function()
						{
							if ($(this).next().length)
							{
								$ul.children().children('div').not($(this).next()).slideUp('fast');
								$ul.children().not(this.parentNode).removeClass('active');
								$(this).next().slideToggle('fast');
								$(this).parent().toggleClass('active');
								return false;
							}
						})
						*/
					}
				});
				$ul.append($o);
			}
		});
		$('.second-menu').cornerz({radius: 8, corners : 'bl br'});
	}
	/**/
}
//$(bambizziMenu);


/* variants */
bDetails = {
	colors : new Array(),
	sizes : new Array(),
	sizes_i : new Array(),
	split : '|',
	sel : '',
	init : function(artnum)
	{
		var i = 0;
		var selected = '';
		for ( p in bVariants )
		{
			tmp =  bVariants[p].name.split(this.split);
			bVariants[p].size = tmp[tmp.length-1];
			delete(tmp[tmp.length-1]);
			tmp.length--;
			bVariants[p].color = tmp.join(this.split);
			if (i == 0 || (parseInt(bVariants[p].size) < lastsize && bVariants[p].stock != '0'))
			{
				this.sel = bVariants[p];
				lastsize = parseInt(bVariants[p].size);
			}
			this.colors[bVariants[p].color] = new Array();
			this.sizes[bVariants[p].size] = new Array();
			i++;
		}
		if (i == 0)
		{
			$('#item-order').hide();
			$('#item-colors').css('display', 'none');
			$('#item-colors-hr').css('display', 'none');
			$('#item-nostock').html(VARIANTS_NOSTOCK).show();
			return;
		}

		for(c in this.colors)
		{
			this.colors.length++;
			for ( p in bVariants )
			{
				if (bVariants[p].color == c)
				{
					this.colors[c][bVariants[p].size] = bVariants[p].size;
				}
			}
		}
		for(s in this.sizes)
		{
			this.sizes.length++;
			for ( p in bVariants )
			{
				if (bVariants[p].size== s)
				{
					this.sizes[s][bVariants[p].color] = bVariants[p].color;
				}
			}
		}
		// sizes sort
		i = 0;
		for(p in this.sizes)
		{
			this.sizes_i[i] = p;
			i++;
		}
		this.sizes_i.sort(this.sortNumber);
		if (typeof(bVariants[artnum]) == 'object')
		{
			this.sel = bVariants[artnum];
		}
		if (this.colors.length == 1)
		{
			$('#item-colors').css('display', 'none');
			$('#item-colors-hr').css('display', 'none');
		}
		this.drawcolors();
		this.drawsizes();
		this.recount('color', this.sel.color);
	},
	drawcolors: function()
	{
		var html = '';
		var i = 0;
		for(p in this.colors)
		{
			html += '<li id="item-colors-' + i + '"><a href="#">' + p + '</a></li>';
			i++;
		}
		$('#item-colors ul').html(html);
		$(function() {$('#item-colors ul li a').click(function(){ bDetails.recount('color', this.innerHTML); return false; }) });
	},
	drawsizes: function()
	{
		var html = '';
		var i = 0;
		for (i = 0; i<this.sizes_i.length; i++)
		{
			html += '<li id="item-sizes-' + i + '"><a href="#">' + this.sizes_i[i] + '</a></li>';
		}
		$('#item-sizes ul').html(html);
		$(function() {$('#item-sizes ul li a').click(function(){ bDetails.recount('size', this.innerHTML); return false; }) });
	},
	recount: function(type, val)
	{
		var name = (type == 'color') ? val + this.split + this.sel.size : this.sel.color + this.split + val;
		var tmp;
		$('#item-nostock').hide();
		$('#item-order').show();
		for(p in bVariants)
		{
			//alert(bVariants[p].name.length + ' ' + name.length);
			if (bVariants[p].name == name)
			{
				// found
				tmp = bVariants[p];
				break;
			}
			else if (bVariants[p][type] == val)
			{
				tmp = bVariants[p];
			}
		}
		this.sel = tmp;
		this.select();
	},
	select: function()
	{
		$('#item-artnum').html(this.sel.artnum);
		$('#item-price span').html(this.sel.price);
		$('#item-price-sale span').html(this.sel.price);
		$('#frm-basket input[name="aid"]').val(this.sel.id);
		// dirty update amount
		var oldam = $('#frm-basket select[name="am"]').val();
		var selhtml = '';
		for(i = 1; i <= this.sel.stock; i++)
		{
			selhtml += '<option' + ((oldam == i) ? ' selected="selected"' : '') + '>' + i + '</option>';
		}
		$('#frm-basket select[name="am"]').html(selhtml);
		if (selhtml == '')
		{
			$('#item-order').hide();
			$('#item-nostock').html(VARIANTS_NOSTOCK).show();
		}
		var i = 0;
		$('#item-colors ul li').attr('class', '');
		for(c in this.colors)
		{
			if (this.sel.color == c)
			{
				// active
				$('#item-colors-' + i).attr('class', 'active');
			}
			else if (this.sizes[this.sel.size][c])
			{
				// inactive
				$('#item-colors-' + i).attr('class', 'inactive');
			}
			i++;
		}
		$('#item-sizes ul li').attr('class', '');
		for(i=0; i<this.sizes_i.length; i++)
		{
			s = this.sizes_i[i];
			if (this.sel.size == s)
			{
				// active
				$('#item-sizes-' + i).attr('class', 'active');
			}
			else if (this.colors[this.sel.color][s])
			{
				// inactive
				$('#item-sizes-' + i).attr('class', 'inactive');
			}
		}
	},
	sortNumber : function (a, b)
	{
		return a - b;
	}
}

