function init()
{
	$("#product_list .product").equalizeCols();
	$("#product_list .product h3").equalizeCols();
	
	$("#products_services #directions > div").equalizeCols();
	
	if ($('#basket table.products tbody tr').length > 0)
	{
		$.tablesorter.defaults.widgets = ['zebra'];
		$("#basket table.products").each(function(){$(this).tablesorter({
				sortList: [[0,0],[2,0]]
			});
		});
	}
	
	if ($('#basket table.services tbody tr').length > 0)
	{
		$.tablesorter.defaults.widgets = ['zebra'];
		$("#basket table.services").each(function(){
			$(this).tablesorter({
				sortList: [[0,0]]
			});
		});
	}
	
	$('a[rel=external]').attr('target', '_blank');
	
	$('#basket table.products thead th:last, #basket table.services thead th:last').unbind('click').css('cursor', 'arrow');
	
	$('#checkAllProducts').click(function() {
		if (this.checked)
		{
			$('#basket table.products tbody input').attr('checked', 'checked');
			$(this).attr('checked', 'checked');
		}
		else
		{
			$('#basket table.products tbody input').removeAttr('checked');
			this.checked = null;
		}
	});
	
	$('#checkAllServices').click(function() {
		if (this.checked)
		{
			$('#basket table.services tbody input').attr('checked', 'checked');
			$(this).attr('checked', 'checked');
		}
		else
		{
			$('#basket table.services tbody input').removeAttr('checked');
			this.checked = null;
		}
	});
	
	$('#basket table input[type=text]').focus(
		function () {
		this.oldValue = this.value;
		}
	);
	
	$('#basket table.products input[type=text]').change(function(){
		if (/^[\d]+$/.test(this.value))
		{
			price = parseFloat($(this).parent().siblings('td:eq(1)').html());
			priceTotal = this.value * price;
			priceOld = (parseFloat(this.oldValue) * price);
			priceDiff = priceTotal - priceOld; 
			
			$(this).parent().siblings('td:eq(2)').html(priceTotal.toFixed(2));
			
			this.oldValue = this.value;
			$.ajax({
			  type: 'POST',
			  url: conf[0] + '/shop/basket/add?set=true',
			  data: $(this).serialize()
			});
			
			totalCount = 0;
			$('#basket table.products td input[type=text]').each(function(){
				totalCount = totalCount + parseInt($(this).attr('value'));
			});

			$('#checkout p strong:eq(0)').html(totalCount);
			oldPrice = parseFloat($('#checkout p strong:eq(1)').html());
			oldPrice = oldPrice + priceDiff;
			$('#checkout p strong:eq(1)').html(oldPrice.toFixed(2));			
		}
		else
		{
			this.value = this.oldValue;
			alert(conf[2]);
		}
	});
	
	$('#checkout a.btwb[href=#]').click(function(){$('#checkout form div').css('display', 'block');});
	
	$('#product .thumbnails a img').click(replaceImage);
	
	if ($('#checkout form dd.error').length > 0)
	{
		$('#checkout a.btwb').trigger('click');
	}
	
	if ($('#catalogue ul li:last').attr('class') == 'current')
	{
		$('#catalogue ul li:last').attr('class', 'lastcurrent');
	}
	else
	{
		$('#catalogue ul li:last').attr('class', 'last');
	}
	
	$('#services_tree > ul > li > strong').click(function(){
		if ($(this).parent().is('.last'))
		{
			$(this).parent().attr('class', 'lastopen');
			return;
		}
		if ($(this).parent().is('.lastopen'))
		{
			$(this).parent().attr('class', 'last');
			return;
		}
		if ($(this).parent().is('.open'))
		{
			$(this).parent().removeAttr('class');
			return;
		}
		$(this).parent().attr('class', 'open');
	});
	$('#services_tree > ul > li > ul').attr('class', 'none');

	$('#related_services > ul > li:last').attr('class', 'last');
	
	$('#search_form').submit(function(){
		if ($('#search').val().length > 3)
		{
			return true;
		}
		//alert('Search query too short!');
		alert(conf[3]);
		return false;
	});
	
	$('#index_page #directions dl a').each(function(){
	    var href = $(this).attr('href');
	    $(this).parent().parent().css('cursor', 'pointer').click(function() {       
	        window.location = href;
	    });
	});
	
	$('#cart').click(function(){window.location = $('#cart p a:first').attr('href');});
	$('#cart').css('cursor', 'pointer');
	
	$('#print').click(function(){
		window.location = window.location + '?print=1';
		return false;
	});
	
	//$('#leasing').css('cursor', 'pointer').click(function(){window.open(conf[0] + '/shop/leasing_calculator', 'leasing', 'height=277,width=200,location=0,menubar=0,resizable=0,scrollbars=0,status=0,toolbar=0');});
	
	if ($.browser.msie && $.browser.version <= 7)
	{
		$('#product .main + h3').css('margin-top', '-22px');
	}
}

function prepareLoginForm()
{
	$('#login_form .controlls a[rel!=logout]').each(function(){
		/*$(this).attr('rel', $(this).attr('href'));
		$(this).attr('href', '#')*/
		$(this).click(function(){openPopup($(this).attr('href'));return false;});
	});
	
	$('#login_form button[type=submit]').click(function(){
		if ($('#username').val().length <= 0 || $('#password').val().length <= 0)
		{
			return false;
		}
	});
}

function openPopup(url)
{
	window.open(url, 'tekaPopup', 'width=492,height=550,status=0,resizable=0,toolbar=0,location=0,scrollbars=1');
}

function prepareRegistrationSwitch(switchId)
{
	$('#' + switchId).change(function(){
		if (this.value == 0)
		{
			$('form dl .private').css('display', 'block');
			$('form dl .juridic').css('display', 'none');
		}
		else if (this.value == 1)
		{
			$('form dl .private').css('display', 'none');
			$('form dl .juridic').css('display', 'block');
		}
	});
	
	$('#' + switchId).trigger('change');
}

function prepareRegistrationCheck(checkId)
{
	$('#' + checkId).click(registrationCheck);
	
	registrationCheck();
}

function registrationCheck()
{
	if ($('#register').is(':checked'))
	{
		$('form dl .registration').css('display', 'block');
	}
	else
	{
		$('form dl .registration').css('display', 'none');
	}
}

function replaceImage()
{
	mainSrc = $('#product > .main > img').attr('src');
	mainSrc = mainSrc.substring(0, mainSrc.length - 9);
	currentSrc = $(this).attr('src');
	currentSrc = currentSrc.substring(0, currentSrc.length - 9);
	
	$('#product > .main > img').attr('src', currentSrc + '/large.jpg')
	$(this).attr('src', mainSrc + '/icon.jpg');
}

function initIE6()
{
	$('#checkout').css('top', $('#cartItems > div').height() + 11 + 'px');
	$('#checkout #userType').change(function(){$('#checkout button.btwb').css('top', $('#checkout').height() - 100 + 'px');});
	$('#checkout #register').click(function(){$('#checkout button.btwb').css('top', $('#checkout').height() - 100 + 'px');});
	$('#checkout button').click(function(){this.form.submit();});
	if (jQuery.browser.version == '6.0')
	{
		recalculateBasketHeight();
		$('#checkout .btwb').click(recalculateBasketHeight);
		$('#checkout #userType').click(recalculateBasketHeight);
		$('#checkout #register').click(recalculateBasketHeight);
		makeFirstRow();
		$('#basket table thead th').click(makeFirstRow);
	}
	
	$('#login_form p input').wrap('<b class="wrap"></b>');
	$('#search_form #search').wrap('<b class="wrap"></b>');
}

function makeFirstRow()
{
	$('#basket table.products tbody tr, #basket table.services tbody tr').css('background-image', 'none');
	$('#basket table.products tbody tr:first, #basket table.services tbody tr:first').css('background-image', 'url(http://www.nova.lv/release/tekatelekom/Application/media/img/border_dot.gif)').css('background-position', '0% 0%');
}

function recalculateBasketHeight()
{
	h = 11;
	h += $('#cartItems > div').height();
	h += $('#checkout').height();
	h += 'px';
	$('#basket #content').css('height', h);
	
	$('#checkout .filter > p:first').css('border', 'none').css('position', 'absolute').css('left', '0px');
}

function preparePopups()
{
	$('div.popup a.close').click(function(){
		$(this).parent.css('display', 'none');
		return false;
	});
}

function prepareGallery()
{
	$('.gallery a').lightBox({
		imageLoading: conf[1] + '/img/lightbox-ico-loading.gif',
		imageBtnClose: conf[1] + '/img/lightbox-btn-close.gif',
		imageBtnPrev: conf[1] + '/img/lightbox-btn-prev.gif',
		imageBtnNext: conf[1] + '/img/lightbox-btn-next.gif'
	});
}

$(init);
