function rowToggle(id, parent)
{
	if ($('#'+ id).length) {
		if (typeof parent != 'undefined')
		{
			$('a', $(parent).parent()).removeClass('selected');
			$(parent).addClass('selected');
		}
		$('table[id*="row"]').hide();
		$('#'+ id).show();
	}
}

function openWin2(url, name)
{  
    popupWin = window.open(url, name, "width=200,height=100,status=no,toolbar=no,location=yes,resizeable=no,scrollbars=no,menubar=no");
}

$(function()
{
	$.comexParent = $('#comexItems');
	$.comexItems = $('tr', $.comexParent).length;
	$.comexIndex = $('tr:visible:last', $.comexParent).index();

	setInterval(function()
	{
		$('tr:visible', $.comexParent).fadeOut(500);

		setTimeout(function()
		{
			$('tr:eq('+ $.getComexIndex(true) +')', $.comexParent).fadeIn();
		}, 600);
	}, 20000);

	$.getComexIndex = function(first)
	{
		$.comexIndex++;

		if (first == true && $.comexIndex >= $.comexItems) {
			$.comexIndex = 0;
		}

		return $.comexIndex;
	}



	$('.pImages li a').bind('click', function() {
		$('#productImage').attr('src', $('img', $(this)).attr('src').replace('small_', 'page_'));
	});
	if ($('a[rel="fancybox"]').length) {
		$('a[rel="fancybox"]').fancybox();
	}
	$('#productImage').parent().click(function()
	{
		var href = $('img', $(this)).attr('src').replace('page_', '');
		$('a[rel="fancybox"][href="'+ href +'"]').trigger('click');
	});

	$('.detailsBlock li a').bind('click', function()
	{
		var parent = $('#'+ $(this).attr('rel') +'RowBlock');
		if (parent.length)
		{
			$('.detailsBlock div[id*="RowBlock"]').hide();
			$('.detailsBlock li a').removeClass();
			parent.show();
			$(this).addClass('selected');
		}
	});

	if ($('#cartShowDetails').length || $("a[class='showCartEditBlock']").length)
	{
		$("#cartShowDetails, a[class='showCartEditBlock']").fancybox({
			'padding': 0,
			'titlePosition': 'inside',
			'transitionIn': 'fade',
			'transitionOut': 'fade'
		});
	}

	$('.checkout').click(function() {
		if ($('#checkoutForm').length) {
			$('#checkoutForm').removeClass('hide');
		}
	});

	if ($('#cntCart').length) {
		$.add2cart(0);
	}
});



(function($)
{
	var $updatingCart = false;
	$.extend(
	{
		add2cart: function(source_id)
		{
			/* Check this function is work, if is work stop it */
			if ($updatingCart) {
				return false;
			}

			/* Set updateCart true */
			$updatingCart = true;

			/* Update cart */
			updateCart();

			/* Check if null source id */
			/*if (source_id == 0) {
				updateCart();
				return false;
			}*/


			/*var source = $('#product_'+ source_id);
			var target = $('.headCart');

			var shadow = $('#product_' + source_id + '_shadow');
			if(!shadow.attr('id'))
			{
				$('body').prepend('<div id="product_'+ source_id +'_shadow" style="background-color:#ddd;display:none;position: static;top:0px;z-index:9999;border:solid 1px darkgray">&nbsp;</div>');

				var shadow = $('#product_'+ source_id +'_shadow');
			}

			$(document).scrollTop(0);
			$(shadow).css({
				'width': source.css('width'),
				'height': source.css('height'),
				'top': source.offset().top,
				'left': source.offset().left, 
				'opacity': 0.5,
				'position': 'absolute'})
				.show();

			shadow.animate({
					'width': target.innerWidth(),
					'height': target.innerHeight(),
					'top': target.offset().top,
					'left': target.offset().left}, 
					500
				)
				.animate({opacity: 0}, 100, updateCart);
			*/

			function updateCart()
			{
				/* Remove shadow /
				if (shadow) {
					shadow.remove();
				}*/
				$('#cntCart').hide();
				$('#cartLoading').show();

				$.post(conf.virPath +'index.php?m=ajax&p=cart', (source_id ? $('#addToCart').serialize() : {'get': true}), function(items)
				{
					setTimeout(function()
					{
						$('#cartLoading').hide();
						$('#cntCart').show();
						$updatingCart = false;

						/* Check if submited form */
						if (source_id) {
							$('#chooseFabric').val('');
							$('#chooseItem').val('').attr('disabled', true).trigger('change');
							$('option', $('#avaibleupgrades')).each(function() {
								$(this).removeAttr('selected');
							});
						}
					}, 100);

					if (typeof items.html != 'undefined') {
						$('#cartDetails').html(items.html);
						$('#cartShowDetails').trigger('click');
					}

					$('#cntCart .products').html(items.items);
					$('#cntCart .ammount').html('$'+ $().numberFormat(items.price, 2));
				}, 'json');
			}
		}
	});
})(jQuery);

$.fn.ToolTip = function(customclass)
{
	this.mouseover(
		function(e)
		{
			if((!this.title && !this.alt) && !this.tooltipset) return;
			var mouseX = e.pageX || (e.clientX ? e.clientX + document.body.scrollLeft : 0);
			var mouseY = e.pageY || (e.clientY ? e.clientY + document.body.scrollTop : 0);
			mouseX += 10;
			mouseY += 10;
			if(!this.tooltipdiv)
			{
				var div = document.createElement("div");
				this.tooltipdiv = div;
				$(div).addClass(customclass || 'tooltip');
				$(div).html((this.title || this.alt));
				this.title = "";
				this.alt = "";
				$("body").append(div);
				this.tooltipset = true;
			}
			$(this.tooltipdiv).fadeIn("fast").css({left: mouseX + "px", top: mouseY + 3 + "px", zIndex: 999});
		}
	).mousemove(
		function(e)
		{
			if(!this.tooltipset) return;
			var mouseX = e.pageX || (e.clientX ? e.clientX + document.body.scrollLeft : 0);
			var mouseY = e.pageY || (e.clientY ? e.clientY + document.body.scrollTop : 0);
			mouseX += 10;
			mouseY += 10;
			$(this.tooltipdiv).css({left: mouseX + "px", top: mouseY + 3 + "px"});
		}
	).mouseout(
		function()
		{
			if(this.tooltipdiv)
			{
				$(this.tooltipdiv).fadeOut("fast");
			}
		}
	);
	return this;
}
