// Вывод дива посередине экрана
function Trans() {
	var map=this;
	$divShad=jQuery('<div></div>');
  	$divShad.css({ background:'#000000', opacity:'0.7', position:'absolute', top:'0', left:'0', zIndex:'1000', width:'100%' }).hide().click(function(){ map.closeMap(); });
	jQuery('body').append($divShad);
	this.initMap();
} 

Trans.prototype.initMap=function() {
	var map=this;
	$ShadCont=jQuery('<div></div>');
	$ShadCont.css({ position:'absolute', background:'#ffffff', zIndex:'1001', border:'10px solid #c0c0c0', width:'300px', overflow:'hidden', height:'100px', textAlign:'center', display:'none' }).attr('id','contShad').hide();
	
	//$closeMap=jQuery('<img>');
	//$closeMap.attr('id','closeMenu').attr('width','10').attr('height','10').attr('src','/img/map/close.png').attr('title','close map').attr('alt','close map').css({ position:'absolute', bottom:'0', right:'0', padding:'10px', cursor:'pointer' }).click(function(){ map.closeMap(); })
	//$ShadCont.append($closeMap);
	$('.closeLink').live('click', function() {
		map.closeMap(); 
	});
	
	var successAdd = 'Товар успешно добавлен в корзину<br>Для перехода к оформлению заказа нажмите на значок корзины вверху страницы.';

	$('.basketButton1').live('click', function() { 
			var var_id = $(this).prev().attr('rel');
			var unselected_doug = $(this).prev().attr('alt');
			var montBlanc = $(this).prev().attr('name');
			var data = 'var_id=' + var_id + '&unselected_doug=' + unselected_doug + '&articulType=' + montBlanc;
			$.post('/scripts/adder.php', data, function(msg) {
				cart = msg.split(' ');
				$('.basket .totalCount').text(cart[0]);
				$('.basket .totalPrice').text(cart[1]);
				if (cart[2]==1) {
					successAdd = 'Невозможно добавить товар в коризну.';
				}	
			});
			$mapText="<table align='center' cellpadding='0' cellspacing='5' border='0' width='100%' height='100%'><tr><td valign='middle' align='center' style='font-size:10px;font-family:Verdana, sans-serif;color:#000000;'>"+ successAdd +"<br><br><a  class='closeLink' style='font-size:10px;font-family:Verdana, sans-serif;color:#000000; cursor:pointer;'>закрыть окно</a></td></tr></table>";
			$ShadCont.html($mapText);
			map.showMap(); 			
	});

	$('.basketButton2').live('click', function() {
			var atlant_id = $(this).prev().attr('rel');
			var data = 'atlant_id=' + atlant_id;
			$.post('/scripts/adder.php', data, function(msg) {
				cart = msg.split(' ');
				$('.basket .totalCount').text(cart[0]);
				$('.basket .totalPrice').text(cart[1]);		
				if (cart[2]==1) {
					successAdd = 'Невозможно добавить товар в коризну.';
				}	
			});
			$mapText="<table align='center' cellpadding='0' cellspacing='5' border='0' width='100%' height='100%'><tr><td valign='middle' align='center' style='font-size:10px;font-family:Verdana, sans-serif;color:#000000;'>"+ successAdd +"<br><br><a  class='closeLink' style='font-size:10px;font-family:Verdana, sans-serif;color:#000000; cursor:pointer;'>закрыть окно</a></td></tr></table>";
			$ShadCont.html($mapText);
			map.showMap(); 			
			
	});	

	$('.basketButton3').live('click', function() {
			var box_id = $(this).parent().attr('rel');
			var data = 'box_id=' + box_id;
			$.post('/scripts/adder.php', data, function(msg) {
				cart = msg.split(' ');
				$('.basket .totalCount').text(cart[0]);
				$('.basket .totalPrice').text(cart[1]);		
				if (cart[2]==1) {
					successAdd = 'Невозможно добавить товар в коризну.';
				}	
			});
			$mapText="<table align='center' cellpadding='0' cellspacing='5' border='0' width='100%' height='100%'><tr><td valign='middle' align='center' style='font-size:10px;font-family:Verdana, sans-serif;color:#000000;'>"+ successAdd +"<br><br><a  class='closeLink' style='font-size:10px;font-family:Verdana, sans-serif;color:#000000; cursor:pointer;'>закрыть окно</a></td></tr></table>";
			$ShadCont.html($mapText);
			map.showMap(); 			
			
	});		
	jQuery('body').append($ShadCont);
}


Trans.prototype.showMap=function() {
	var so = 'abra';
	$divShad.css({ height:jQuery(document).height() }).show();
	coords=this.getPage();
   	hei=$ShadCont.outerHeight();
	marg=coords[2]+(coords[0]-hei)/2;
    if (hei>=coords[0]) marg=coords[2];
	if ((hei+marg)>=coords[1]) $divShad.css({ height:hei+marg+50+'px' });
   	$ShadCont.css({ left:(jQuery(document).width()-$ShadCont.outerWidth())/2+'px', top:-hei+'px' }).show().animate ({ top:marg+'px' }, { queue: false, duration: 900 });
}

Trans.prototype.closeMap=function() {
	$ShadCont.hide();
	$divShad.hide();
}

Trans.prototype.getPage=function () {
  var coords = [0, 0, 0]; // height of window, document, scroll pos
  // all except IE
  if (window.innerHeight) { coords[0] = window.innerHeight; coords[2] = window.pageYOffset; }
  // IE 6 Strict
  else if (document.documentElement && document.documentElement.clientHeight != 0) { coords[0] = document.documentElement.clientHeight; coords[2] = document.documentElement.scrollTop; }
  else if (document.body) { coords[0] = document.body.clientHeight; coords[2] = document.body.scrollTop; }
  var test1 = document.body.scrollHeight; 
  var test2 = document.body.offsetHeight;
  if (test1 > test2) { coords[1] = document.body.scrollHeight; }
  else { coords[1] = document.body.offsetHeight; }
  if (coords[1] < coords[0]) coords[1] = coords[0];
  return coords;
} 


$(document).ready(function() { 

my_map=new Trans();

function loadSeries (obj) {
	var firm_id = $(obj).val();
	var data = 'firm_id=' + firm_id;
	$.post('/scripts/finder.php', data, function(msg) {
		var res = '<select class="text model" name="model"><option value="">модель и год</option>' + msg + "</select>";
		$('select.model').parent().html(res);
		var selected_serie = $('.cur_serie').val();
		if (selected_serie!=undefined) {
			$('option#'+ selected_serie).attr('selected', 'selected');
		}
	});
}

	$('select.marka').change( function() { loadSeries(this); } );
	
	var selected_firm = $('.cur_firm').val();
	if (selected_firm!=undefined) {
		selected_firm = selected_firm.split('#').join('');
		$('option#'+ selected_firm).attr('selected', 'selected');
		loadSeries($('option#'+ selected_firm)); 
	}	
	
	var selectedPrice = 0;
	var totalPriceValFormatted = '';
	$('.doug').click( function() {

		var price = $(this).parent().parent().find('.priceBaggage').text();
		var id = $(this).attr('id'); 
		var totalPrice = $('#totalPrice' + id);
		var totalPriceVal = $('#totalPrice' + id).text();
		if (totalPriceVal=='0 руб.') {
			var dopBasket = '<img src="/img/bask.png" class="basketButton1" />';
		}
		else {
			var dopBasket = '';
		}
		//console.log(totalPriceVal);
		if (totalPriceVal=='') {
			totalPrice = $('#totalPrice0');
			totalPriceVal = totalPrice.text();
		}
		totalPriceVal = totalPriceVal.split('руб.').join('').split(' ').join('').split(',00').join('');  
		totalPriceVal = Number(totalPriceVal);
		price = price.split('руб.').join('').split(' ').join('').split(',00').join('');
		totalPriceVal += Number(price);
		totalPriceVal -= Number(selectedPrice);
		//console.log(totalPriceVal);
		selectedPrice = Number(price);
		totalPriceValFormatted = totalPriceVal + ' руб.';
		totalPrice.text(totalPriceValFormatted);
		if (totalPriceValFormatted!='0 руб.' && dopBasket!='') {
			totalPrice.parent().append(dopBasket);
		}
		$(this).removeClass('unselected' + id);	
		totalPrice.attr('alt', $('.unselected' + id).attr('rel'));
		$(this).addClass('unselected' + id);	
		//$('.doug').addClass('unselected');
		//$(this).removeClass('unselected');		
	});
	
	
	$('.recount').click( function() {
		var newcount = 0;
		var price1 = 0;
		var newprice2 = 0;
		var totalprice = 0;
		var totalcount = 0;
		var id = 0;
		var newparms = [];
		var data = 'recount=yes&newparams=';
		$.each( $('input.num'), function() { 
			 id = $(this).parent().parent().parent().attr('rel');
			 newcount = $(this).val();
			 newcount = newcount.split('-').join('');		
			 newcount = newcount.split('.').join('');	
			 if (newcount==0) {
				newcount=1;
			 }
			 $(this).attr('value', newcount);
			 price1 = $(this).parent().parent().parent().find('.price1').children().text();
			 price1 = price1.split('-').join(' ');		
			 newprice2 = newcount * price1;
			 $(this).parent().parent().parent().find('.price2').children().text(newprice2);
			 totalcount += Number(newcount);
			 totalprice += newprice2;
			 data += id + '@' + newcount + '@' + newprice2 + '|';
			 //console.log(newprice2);			 
		});
		//console.log(data);
		$('.totalcount').children().text(totalcount);
		$('.totalprice, .totalPrice').children().text(totalprice);
		$('.totalCount').text(totalcount);
		$('.totalPrice').text(totalprice);
		
		
		$.post('/scripts/adder.php', data, function(data) { 	
			cart = data.split(' ');
			$('.basket .totalCount').text(cart[0]);
			$('.basket .totalPrice').text(cart[1]);		
		});

	});

	$('.clearBasket').click( function() {
		if (confirm("Очистить корзину?")) {
				window.location='?del=all';
		}	
	});
	
	$('.showFields input').click(function() {
	  $('.org').toggle('fast');
	});
	

	
	var prevId = 0;
	var id = 0;
	$.each( $('.doug'), function() {
		id = $(this).attr('id');
		if (prevId!=id) {
			selectedPrice = 0;
			$('#' + id).click();
		}
		prevId = id;
		//console.log(id);
	});

});
