	$(document).ready(function() {
		si(tbs[tb]); tb++;
		setInterval(function() { si(tbs[tb]); tb++; if (tb > (tbs.length - 1)) tb = 0; }, 20000);
	});
	/* Show large image in modal */
	function sl(src)
	{
		$.get('index.php', { page: 'Content', action: 'Image', src: src }, function (ret) {
			$('#modal').html(ret);
			showModal();
		});
	}
	/* Show small info */
	function si(ael)
	{
		$.get('index.php', { page: 'Content', action: 'Tiny', target: ael }, function (ret) {
			$('#sel > ul > li > a').each(function(id, el) {
				$(el).removeClass('active');
			});
			$('#sel > ul > li > #t'+ael).addClass('active');
			$('#info_content').html(ret);
		});
		return $('#sel > ul > li > #t'+ael);
	}
	/* Show full product info in #prod */
	function sp(catalog_id, model_id)
	{
		$.get('index.php', { page: 'Product', action: 'Info', catalog_id: catalog_id, model_id: model_id }, function (ret) {
			$('#prod').html(ret);
		});
	}
	/* Show full product info in modal */
	function smp(catalog_id, model_id)
	{
		$.get('index.php', { page: 'Product', action: 'View', catalog_id: catalog_id, model_id: model_id }, function (ret) {
			$('#modal').html(ret);
			showModal();
		});
	}
	/* Load "More info" data */
	function li(catalog_id)
	{
		$.get('index.php', { page: 'Product', action: 'More', catalog_id: catalog_id }, function (ret) {
			$('#options').fadeOut('fast', function() {
				$('#description').html(ret);
			});
		});
	}
	/* Show modal */
	function showModal()
	{
		var x = ($(window).width() - $('#modal').width()) / 2;

		var v1 = $(window).height();
		var v2 = $('#modal').height();
		
		if (v2 < v1)
		{
		  var y = (($(window).height() - $('#modal').height()) / 2) + $(window).scrollTop();
		}
		else
		{
		  y = $(window).scrollTop();
		  y = y+45;
		}

		$('#modalbg').css('left', 0);
		$('#modalbg').css('top', 0);
		$('#modalbg').width($(document).width());
		$('#modalbg').height($(document).height());
		$('#modalbg').bind('click', function(e) {
			hideModal();
		});
		$(document).bind('keydown', function(e) {
			if (e.keyCode == 27)
				hideModal();
		});
		//$('#modalbg').fadeIn('fast');
		$('#modalbg').show();
		$('#modal').css('left', x);
		$('#modal').css('top', y);
		$('#modal').css('value1', y);
		$('#modal').fadeIn('fast');
	}
	/* Hide Modal */
	function hideModal()
	{
		$('#modalbg').unbind('click');
		$(document).unbind('keydown');
		//$('#modalbg').fadeOut('fast');
		$('#modalbg').hide();
		$('#modal').fadeOut('fast', function() {
			$('#modal').text('');
		});
	}
	/* Choose Manufacturer, hide modal */
	function cha(manufacturer_id, text)
	{
		$('#model').text('--- malli ---');
		$('#model_select').val(0);
		$('#make').text(text);
		$('#make_select').val(manufacturer_id);
		hideModal();
	}
	/* Choose Model, hide modal */
	function chb(model_id, text)
	{
		$('#model').text(text);
		$('#model_select').val(model_id);
		hideModal();
		$('#search_form').submit();
	}
	/* Show modal select for Manufacturer */
	function sea()
	{
		$.get('index.php', { page: 'Select', action: 'Manufacturer' }, function (ret) 
		{
			$('#modal').html(ret);
         		$('#modal').css('height', 600);
			showModal();
		});
	}
	/* Show modal select for Model */
	function seb()
	{
		var manufacturer_id = $('#make_select').val();
		$.get('index.php', { page: 'Select', action: 'Model', manufacturer_id: manufacturer_id }, function (ret) 
		{
			$('#modal').html(ret);
         		$('#modal').css('height', 900);
			showModal();
		});
	}
	/* Delete from cart */
	function dp(cart_catalog_id)
	{
		var node = $('#p'+cart_catalog_id);
		$.get('index.php', { page: 'Cart', action: 'Delete', cart_catalog_id: cart_catalog_id }, function (ret) {
			$('#amount'+cart_catalog_id).val(0);
			node.fadeOut();
			$('#sum').html(ret);
		});
	}
