function loadHTMLTpl(tpl, container_id, callonload) {
	$.get('/getTpl.php', {tpl: tpl}, function(data) {
		var s = '<div id='+container_id+' style="display: none;">'+data+'</div>';
		$("body").append(s);
		callonload();
	});
}

function fit_image() {
	var iw = $("#image_box").width();
	var ih = $("#image_box").height();
	var cw = $("#image_box").attr('w');
	var ch = $("#image_box").attr('h');
	var il = 0;
	var it = 0;
	if(cw/ch > iw/ih) {
		iw = Math.round(ih*cw/ch);
		il = Math.round((iw-$("#image_box").width())/2)
	} else {
		ih = Math.round(ch*iw/cw);
		it = Math.round((ih-$("#image_box").height())/2)
	}
	$("#image_box_i").width(iw).height(ih).css('left', -1*il).css('top', -1*it);
}

function hideImageBox() {
	$("#image_box").fadeOut(500);
}

function showImageBox(type, id, w, h, title) {
	var a = $("#image_box");
	if($(a).length == 0) {
		$('body').append('<div id="image_box"><div id="close_img" onclick="hideImageBox();"></div><div id="image_box_p"></div><div id="image_box_c"><img id="image_box_i"></div><div id="image_box_t"></div></div>');
		a = $("#image_box");
		$("#image_box_i").load(function() {
			fit_image();
			$("#image_box_i").css('opacity', 100);
			var iw = $("#image_box").attr('w');
			var ih = $("#image_box").attr('h');
			var il = Math.round(($(window).width()-iw)/2);;
			var it = Math.round(($(window).height()-ih)/2+$(document).scrollTop());
			$("#image_box").animate({
				width: iw,
				height: ih,
				left: il,
				top: it
			}, {
				step: function() {
					fit_image();
				},
				duration: 500,
				complete: function() {
					$("#close_img").show();
					$("#image_box_i").css('top', 0).css('left', 0);
					$("#image_box_t").css('margin-top', $("#image_box").height()-100).show();
					$("#image_box").height($("#image_box").height()+60);
				}
			});
		});
	}

	$(a).removeClass('active').width(100).height(100).attr('w', w).attr('h', h).find('#image_box_c').css('background-image', 'url("/media/'+type+'/small/'+id+'.jpg")').width(100).height(100);
	$("#image_box_p").show();
	$("#image_box_i").css('opacity', 0).attr('src', '/media/'+type+'/large/'+id+'.jpg?'+Math.random());
	$("#close_img").hide();
	var l = Math.round(($(window).width()-100)/2);
	var t = Math.round(($(window).height()-100)/2+$(document).scrollTop());
	$("#image_box_t").html(title).hide();
	$(a).css("left", l).css('top', t).show();
}

function _showOfferImage() {
            var o = $("#image_view");            
            if($(o).length == 0) {
                var s = '<div class="photo_shadow" id="image_view" onclick="hideLargeImage(this);" style="overflow: hidden; position: absolute; z-index: 10; background-size: cover;">\
                    <div id="close_img"></div>\
                    <div id="img_name"></div>\
                    <img style="margin: 0px; padding: 0px;">\
                </div>';
                $('body').append(s);
            } else {
                $(o).show();
            }
            o = $("#image_view");
            var w = $(this).attr('w');
            var h = $(this).attr('h');
            if(w/h > 1) {
                $(o).find('img').width(100*w/h).height(100);
            } else {
                $(o).find('img').width(100).height(100*h/w);
            }
            var l = Math.round(($(window).width()-w)/2);
            var t = Math.round(($(window).height()-h)/2+$(document).scrollTop());
            $(o).width(100).height(100).css('left', ($(this).position().left+5)+'px').css('top', ($(this).position().top+3)+'px')
            $(o).find('img').hide().attr('src', $(this).attr('path')+'/large.jpg').load(function() {
                $(o).find('img').show();
                $(o).attr('l', $(o).position().left).attr('t', $(o).position().top);
                animateShow(o, w, h, l, t);
            });
        }
        
        function animateShow(o, w, h, l, t) {
            $(o).animate({                                        
                    width: w,
                    height: h,
                    left: l,
                    top: t
                },
                {
                    step: function() {
                     $(this).children('img').width($(this).width()).height($(this).height());
                },
                duration: 500,
                complete: function() {
                    $(this).addClass('active');
                }
            });                                
        }
        function hideLargeImage(e) {
            $(e).removeClass('active');
            $(e).animate(
                {width: 100, height: 100, left: $(e).attr('l'), top: $(e).attr('t')},
                {
                    step: function() {
                        $(this).children('img').width($(this).width()).height($(this).height());
                },
                duration: 500,
                complete: function() {
                    $(e).find('img').hide();
                    $(e).hide();
                }
            });
        }
        
        function resizeImg(i, w, h) {
            var iw = $(i).width();
            var ih = $(i).height();
            if(iw > ih) {
                $(i).height(h);                
                $(i).width(h*iw/ih);
            } else {
                $(i).width(w);
                $(i).width(w*ih/iw);
            }
        }

function sendMessage() {
                    var a = $("#sendmsg_form");
                    if($(a).length == 0) {
                        var s = '<div id="sendmsg_form"><textarea></textarea></div>';
                        $('body').append(s);
                        a = $("#sendmsg_form");
                        $(a).dialog({
                            autoOpen: false,
                            title: 'Отправить сообщение',
                            open: function() {
                                $(this).find('textarea').val('');
                            },
                            buttons: [{
                                text: 'Отправить сообщение',
                                click: function() {
                                    if($(this).find('textarea').val().length == 0) {
                                        alert('Вы должны ввести сообщение');
                                    }
                                    $.getJSON(document.location, {send_message: $(this).find('textarea').val()}, function(data) {
                                        if(typeof data.status == 'undefined' || data.status != 'ok') {
                                            if(typeof data.status != 'undefined') {
                                                alert(data.status);
                                            } else {
                                                alert('Internal error');
                                            }
                                        } else {
                                            var s = '<tr align="left">\
                                            <td >'+data.author+'</td>\
                                            <td >'+data.date+'</td>\
                                            <td >'+data.text+'</td>\
                                            </tr>';
                                            $("#nomessages").hide();
                                            $("#messagelist").append(s);
                                            $("#sendmsg_form").dialog('close');
                                        }
                                    });
                                }
                            }]
                        });
                    }
                    $(a).dialog('open');
                }

		function formatInput(v, f) {
                    if(typeof f != 'undefined' && f == 's') {
                        return v;
                    }
                    var _v = parseFloat(v);
                    if(isNaN(_v)) {
                        return '';
                    }					
                    v = _v.toFixed(2);
                    var d1 = v.substr(0, v.indexOf('.'));
                    var d2 = v.substr(v.indexOf('.')+1);
                    var d = '';
                    var d3 = new Array();
                    var i = d1.length;
                    while(i > 0) {
                        if(i > 3) {
                            d3.push(d1.substr(i-3, 3));
                        } else {
                            d3.push(d1.substr(0, i));
                        }
                        i -= 3;
                    }
                    d = d3.reverse().join(' ');
					if(typeof f != 'undefined' && f == 'i') {
						v = d;
					} else {
						v = d+','+d2;
					}
                    return v;
                }
