var PICPATH = "";	// путь к картинкам (в конце обязательно слеш)
var path_to_imgpreview = "/js/imgpreview.html";//путь к файлу, где будет открываться картинка

var ivlId = -1;
var win = null;
img = new Image();


function imgpreview(imgSrc)
{
	if(win) win.close();
	img = new Image();
	img.src = PICPATH + imgSrc;
	if(img.complete)
		imgComplete();
	else {
		if(ivlId != -1) window.clearInterval(ivlId);
		ivlId = window.setInterval("imgComplete()", 100);

	}
}

function imgComplete() {                      
	if(img.complete) {
		window.clearInterval(ivlId);
		ivlId = -1;
		var WMAX = screen.availWidth - 2, HMAX = screen.availHeight - 26;
// Resize image to fit into the rect (0,0,WMAX,HMAX)
		if(!(WMAX > img.width && HMAX > img.height)) {
			var r1 = img.width / img.height;
			var r2 = WMAX / r1;
			if(r2 > HMAX) {
				img.height = HMAX;
				img.width = HMAX * r1;
			}
			else {
				img.width = WMAX;
				img.height = r2;
			}
		}
		var x, y, w, h;
		x = Math.round((screen.availWidth - img.width) / 2);
		y = Math.round((screen.availHeight - img.height) / 2);
		w = img.width;
		h = img.height;

		var winParams = "left=" + x + ",top=" + y + ",width=" + w + ",height=" + h;
		win = window.open(path_to_imgpreview, "", "toolbar=0,scrollbars=0,location=0,directories=0,status=0,menubar=0,resizable=0," + winParams);
		win.focus();
	}
}
$(document).ready(function () {
	/*var service = $('div.text_content h1').text();
	service = service.toLowerCase();
	$('div.send-link a span').html('Оформить заказ на '+service);*/
	$('ul.prices_list li a.price-list-title').live('click', function () {
		if ( $(this).parent().find('div.price_table').is('.open_price') ) {
			$(this).parent().find('div.price_table').slideUp();
			$(this).parent().find('div.price_table').removeClass('open_price');				
			$(this).parent().find('span.plus-button').html('+');
			console.log('closed');
		}else {
			$(this).parent().find('div.price_table').slideDown();
			$(this).parent().find('div.price_table').addClass('open_price');
			$(this).parent().find('span.plus-button').html('&ndash;');
			console.log('open');
		}
		$('div.price_table span.hide-price-list').live('click', function (){
			$(this).parent().slideUp();	
			$(this).parent().removeClass('open_price');
			$(this).parent().parent().find('span.plus-button').html('+');
			console.log('closed span');
		});
		return false;
	});
	$('span.plus-button').live('click', function (){
		if ( $(this).parent().find('div.price_table').is('.open_price') ) {
			$(this).parent().find('div.price_table').slideUp();
			$(this).parent().find('div.price_table').removeClass('open_price');				
			$(this).parent().find('span.plus-button').html('+');
			console.log('closed');
		}else {
			$(this).parent().find('div.price_table').slideDown();
			$(this).parent().find('div.price_table').addClass('open_price');
			$(this).parent().find('span.plus-button').html('&ndash;');
			console.log('open');
		}		
	});
	$('div.send-link a').live('click', function (){
		$('div.popup-form').css('display', 'block');
		$('div.popup-bg').css('display', 'block');
		$('div.popup-bg').css('width', '100%');
		$('div.popup-bg').css('height', '100%');
		return false;
	});
	$('div.popup-bg').live('click', function (){
		$('div.popup-form').css('display', 'none');
		$('div.popup-bg').css('display', 'none');
	});
	$('div.popup-form button').live('click', function (){
		if ($('input[name=hi-bot]').val()!='') {
			console.log('hi bot');
		}else {
			var text_msg = new Array();
			$('div.popup-form div input').each(function () {
				text_msg.push( $(this).val() );
			});		
			if ($('div.popup-form div textarea').val()!=''){
				console.log('что то есть');
				text_msg.push( $('div.popup-form div textarea').val() );
			}else {
			}
			$.ajax({
				type: 'POST',
				url: '/docs/zakaz/',
				data: { 'txt_message': text_msg },
				cache: false,
				success: function (){
					console.log('data send');
				}				
			});				
		}	
		setTimeout (function (){
			$('div.popup-form').hide();
			$('div.popup-bg').hide();				
		}, 3000);		
		return false;
	});
});
