function FormatNumberBy3(num, decpoint, sep) {
  // check for missing parameters and use defaults if so
  if (arguments.length == 2) {
    sep = ",";
  }
  if (arguments.length == 1) {
    sep = ",";
    decpoint = ".";
  }
  // need a string for operations
  num = num.toString();
  // separate the whole number and the fraction if possible
  a = num.split(decpoint);
  x = a[0]; // decimal
  y = a[1]; // fraction
  z = "";


  if (typeof(x) != "undefined") {
    // reverse the digits. regexp works from left to right.
    for (i=x.length-1;i>=0;i--)
      z += x.charAt(i);
    // add seperators. but undo the trailing one, if there
    z = z.replace(/(\d{3})/g, "$1" + sep);
    if (z.slice(-sep.length) == sep)
      z = z.slice(0, -sep.length);
    x = "";
    // reverse again to get back the number
    for (i=z.length-1;i>=0;i--)
      x += z.charAt(i);
    // add the fraction back in, if it was there
    if (typeof(y) != "undefined" && y.length > 0)
      x += decpoint + y;
  }
  return x;
}

$(function(){
	$('a[rel="popup"]').click(function(){
		window.open($(this).attr('href'), 'popup', 'height=640,width=800,scrollbars=yes,resizable=yes');
		return false;
	});
	
	$('a[rel="banner"]').click(function(){
		window.open($(this).attr('href'), 'popup', 'height=734,width=1000,scrollbars=no,resizable=no');
		return false;
	});
	
	$('a[rel="submit"]').click(function(){
		$(this).parents('form').submit();
	});
	
	$('a[rel="lightbox"]').lightBox({
		imageLoading: config[1] + '/img/loading.gif',
		imageBtnClose: config[1] + '/img/close.gif',
		imageBtnPrev: config[1] + '/img/prev.gif',
		imageBtnNext: config[1] + '/img/next.gif'
	});
	
	$('a[rel="tech"]').click(function(){
		window.open($(this).attr('href'), 'popup', 'height=640,width=505,scrollbars=yes');
		return false;
	});
});

function intro(elm)
{
	if (elm == 'cars')
	{
		document.getElementById('intro').style.backgroundImage = 'url(' + config[1] + '/img/intro_left.gif)';
		document.getElementById('ch2').src = config[2] + '/intro_komerctransports_off.jpg';
		document.getElementById('ch1').src = config[2] + '/intro_vieglas_automasinas_on.jpg';
	}
	else
	{
		document.getElementById('intro').style.backgroundImage = 'url(' + config[1] + '/img/intro_right.gif)';
		document.getElementById('ch1').src = config[2] + '/intro_vieglas_automasinas_off.jpg';
		document.getElementById('ch2').src = config[2] + '/intro_komerctransports_on.jpg';
	}
}

function p_img(elm)
{
	for(i=1; i<=20; i++)
	{
		if (elm == 'img_' + i)
			document.getElementById(elm).className = 'active';
		else
			if (document.getElementById('img_' + i) != null)
				document.getElementById('img_' + i).className = '';
	}
}

function MM_swapImgRestore() { //v3.0 
var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_swapImage() { //v3.0
 var i,j=0,x,y,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
 if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}



function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function close_window() {
    window.close();
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}


$(function(){
	$('input.version').click(function(){
		$('#version_name').text($(this).parent().prev().prev().prev().text());
		$('#price_for_version, #price_with_accessories span').text($(this).parent().prev().prev().text() + ' ' + i18n[0]);
		
		var value = $(this).val();
		
		$.ajax({
			url: config[3],
			type: 'POST',
			data: {
				versionsId: value 
			},
			success: function(){}
		});
	});
	if ($('td.checkbox input:checked').length = 0)
	{
		$('td.checkbox input:first').click();
	}
	else
	{
		$('td.checkbox input:checked').click();
	}
	
	prepareInteriors();
	prepareDisks();
	
	$('a.color').hover(function(){
		colorId = $(this).attr('id').substr(6);

		setFlashColour(Colors[colorId].lack, Colors[colorId].contrast, Colors[colorId]);
		
		$('#inputColorName').val(Colors[colorId].name);
	}, 
	function(){
		setFlashColour(currentColor.lack, currentColor.contrast, Colors[colorId]);
	
		$('#inputColorName').val(currentColor.name);
	});
		
	$('a.color').click(function(){
		colorId = $(this).attr('id').substr(6);
		
		$.ajax({
			url: config[3],
			type: 'POST',
			data: {
				colorsId: colorId 
			},
			dataType: 'json',
			success: renderInteriors
		});
		
		currentColor = Colors[colorId];

		$('#inputColorName').val(currentColor.name);
		$('#colorName').text(currentColor.name);
		
		if (currentColor.price > 0)
		{
			$('#colorPrice').text(currentColor.price + ' ' + i18n[1]);
		}
		else
		{
			$('#colorPrice').text('');
		}
		
		recountColors();
		return false;
	});
	
	$('#gallery .photogallery a').click(function(){
		$('#gallery .photogallery a').removeClass('active');
		$(this).addClass('active');
		
		$('#bigphoto').attr('src', $(this).attr('href'));
		
		return false;
	});
	
	$('#modelchoice select').change(function(){
		$(this).parent('form').submit();
	});
});

function setFlashColour(lack, contrast, color)
{
	movie = $('#model_turnaround').get(0);
	
	if (!$(movie).is('div'))
	{
		movie.SetVariable("_root.farbe_lack_ra", lack[0]);
		movie.SetVariable("_root.farbe_lack_rb", lack[1]);
		movie.SetVariable("_root.farbe_lack_ga", lack[2]);
		movie.SetVariable("_root.farbe_lack_gb", lack[3]);
		movie.SetVariable("_root.farbe_lack_ba", lack[4]);
		movie.SetVariable("_root.farbe_lack_bb", lack[5]);
		movie.SetVariable("_root.farbe_lack_aa", lack[6]);
		movie.SetVariable("_root.farbe_lack_ab", lack[7]);
		movie.SetVariable("_root.farbe_kontrast_ra", contrast[0]);
		movie.SetVariable("_root.farbe_kontrast_rb", contrast[1]);
		movie.SetVariable("_root.farbe_kontrast_ga", contrast[2]);
		movie.SetVariable("_root.farbe_kontrast_gb", contrast[3]);
		movie.SetVariable("_root.farbe_kontrast_ba", contrast[4]);
		movie.SetVariable("_root.farbe_kontrast_bb", contrast[5]);
		movie.SetVariable("_root.farbe_kontrast_aa", contrast[6]);
		movie.SetVariable("_root.farbe_kontrast_ab", contrast[7]);
	}
	else
	{	
		//console.log(color.path + '/model_thumb.jpg');
		$('img', movie).attr('src', color.path + '/model_thumb.jpg');
	}
}

function renderInteriors(interiors)
{
	$('ol.interior li').remove();
	$(interiors).each(function(index, item){
		Interiors[item.ModelsInteriorsId] = {
			name: item.ModelsInteriorsName, 
			price: item.ModelsInteriorsPrice,
			path: item.ModelsInteriorsPath 
		};

		$('ol.interior').append($('<li><a href="' + item.ModelsInteriorsPath + '" class="interior" id="interior' + item.ModelsInteriorsId + '" title="' + item.ModelsInteriorsName + '"><img src="' + item.ModelsInteriorsPath + '/thumb.jpg" height="33" width="43" /></a></li>'));
	});
	
	prepareInteriors();
}

function prepareInteriors()
{
	$('a.interior').hover(function(){
		interiorId = $(this).attr('id').substr(8);
		$('#inputInteriorName').val(Interiors[interiorId].name);
		$('#interior_image').attr('src', $(this).attr('href') + '/large.jpg');
	}, 
	function(){
		$('#inputInteriorName').val(currentInterior.name);
		$('#interior_image').attr('src', currentInterior.path + '/large.jpg');
	});

	$('ol.interior li a').click(function(){	
		$('#interior_image').attr('src', $(this).attr('href') + '/large.jpg');
		
		interiorId = $(this).attr('id').substr(8);
		
		$.ajax({
			url: config[3],
			type: 'POST',
			data: {
				interiorsId: interiorId 
			},
			dataType: 'json'
		});
		
		currentInterior = Interiors[interiorId];

		$('#inputInteriorName').val(currentInterior.name);
		$('#interiorName').text(currentInterior.name);
		
		if (currentInterior.price > 0)
		{
			$('#interiorPrice').text(currentInterior.price + ' ' + i18n[1]);
		}
		else
		{
			$('#interiorPrice').text('');
		}
		
		recountColors();
		return false;
	});
}

function prepareDisks()
{
	$('ol.discs li a').hover(function(){
		$('#inputDiscName').val($(this).attr('title'));
		$('#disc_image').attr('src', $(this).attr('href') + '/large.jpg');
	},
	function(){
		$('#inputDiscName').val(currentDisk.name);
		$('#disc_image').attr('src', currentDisk.path + '/large.jpg');
	});
	$('ol.discs li a').click(function(){
		$('#disc_image').attr('src', $(this).attr('href') + '/large.jpg');
		
		diskId = $(this).attr('id').substr(5);
		
		$.ajax({
			url: config[3],
			type: 'POST',
			data: {
				disksId: diskId 
			},
			dataType: 'json'
		});
		
		currentDisk = Disks[diskId];

		$('#inputDiscName').val(currentDisk.name);
		$('#diskName').text(currentDisk.name);
		if (currentDisk.price > 0)
		{
			$('#diskPrice').text(currentDisk.price + ' ' + i18n[1]);
		}
		else
		{
			$('#diskPrice').text('');
		}
		
		recountColors();
		return false;
	});
}

function prepareAccessories()
{
	$('input.accessory').click(function(){
		var status = null;
		
		accessoryId = $(this).val();
		accessoryPrice = parseFloat($('.price', $(this).parent().parent()).text());
		totalesPrice = parseFloat($('#totalPrice').text());
		totalesPrice = parseFloat($('#totalPrice').text());
		accessoryPrice = parseFloat($('.price', $(this).parent().parent()).text());
		
		if ($(this).is(':checked'))
		{
			status = true;
			
			totalesPrice = totalesPrice + accessoryPrice;
		}
		else
		{			
			status = false;
			
			totalesPrice = totalesPrice - accessoryPrice;
		}
			
		$('#totalPrice span').text(totalesPrice.toFixed(2));
	
		$.ajax({
			url: config[3],
			type: 'POST',
			data: {
				accessoriesId: accessoryId,
				status: status
			},
			dataType: 'json'
		});
	});
}

function recountColors()
{
	var verPrice = 0.0;
	var disquePrice = 0.0;
	var coleurPrice = 0.0;
	var interiPrice = 0.0;
	var totalPrice = 0.0;
	
	verPrice = parseFloat($('#versionPrice').text());
	
	coleurPrice = parseFloat($('#colorPrice').text());
	interiPrice = parseFloat($('#interiorPrice').text());
	disquePrice = parseFloat($('#diskPrice').text());
	
	totalPrice = verPrice + coleurPrice + interiPrice + disquePrice;
	
	$('#totalPrice span').text(totalPrice.toFixed(2));
}

function pause(millisecondi)
{
    var now = new Date();
    var exitTime = now.getTime() + millisecondi;

    while(true)
    {
        now = new Date();
        if(now.getTime() > exitTime) return;
    }
}