
var i = 0;
$(document).ready(function() {
    menu.top.alignCenter();
    //menu.subTop.alignCenter();
    gallery.block_image_resize();
    
    $('ul.gallery li a').mouseover(gallery.guidance);
    
    $('div.gallery_open div.image img.zoom').click(gallery.fullsize);
    $('div.gallery_open div.image div.zoom').click(gallery.fullsize);
    $('div.pictures_in_interior div.description img').click(gallery.fullsize2);
    
    $('div.gallery_open div.btn-sale').click(sale.form);
    $('div.gallery_open div.sale-form div.parameters div.submit').click(sale.sendform);
    
    $('ul#gallery li a img').load(function(e) {
        var height = $(this).height() - 75;
        var width = $(this).width() - 75;
        
        $($(this).parent()).css({"margin-top":"-"+height+"px", "margin-left":"-"+width+"px"});
    });
    
    $(window).load(function(e) {
        var length = $('ul#gallery li a').length;
        
        for(var i = 0; i < length; i++) {
            var height = $($('ul#gallery img#img')[i]).height() - 75;
            var width = $($('ul#gallery img#img')[i]).width() - 75;
            
            $($('ul#gallery li a')[i]).css({"margin-top":"-"+height+"px", "margin-left":"-"+width+"px"});
        }
    });
});

var sale = Object();

sale.form = function() {
    $('div.gallery_open div.sale-form').show();
}

sale.sendform = function() {
    var text = $('div.gallery_open div.sale-form div.textarea textarea').val();
    var name = $('div.gallery_open div.sale-form div.parameters div.name input').val();
    var email = $('div.gallery_open div.sale-form div.parameters div.email input').val();
    var captcha = $('div.gallery_open div.sale-form div.parameters div.captcha input').val();
    
    if(text != '' && name != '' && email != '') {
        $.post('/api.php', {"method":"saleForm", "text":text, "name":name, "email":email, "captcha":captcha}, sale.sendform_callback, 'json');
    } else {
        alert('Вы заполнили не все поля!');
    }
}

sale.sendform_callback = function(data) {
    if(data.ok) {
        $('div.gallery_open div.sale-form').html('<div class="description" style="font-size:18px;">'+data.ok+'</div>');
    } else {
        alert(data.error);
    }
}

var gallery = Object();
gallery.out_guidance = function() {
    $(this).css({"position":"static"});
}

gallery.guidance = function() {
    $(this).css({"position":"absolute"});
    
    $(this).mouseout(gallery.out_guidance);
}

gallery.fullsize = function() {
    var link_image = $('div.gallery_open div.image img').attr("filename");
    
    var width = $(window).width() - 50;
    var height = $(window).height() - 50;
    
    var img_width = $('.gallery_open .image img').width();
    var img_height = $('.gallery_open .image img').height();
    
    var new_img_width = width;
    var new_img_height = new_img_width * img_height / img_width;
    
    var wh = 'width="'+width+'"';
    
    if(new_img_height > height) {
        wh = 'height="'+height+'"';
    }
    
    $('body').append('<div id="image_fullsize_bg"></div>');
    $('body').append('<div id="image_fullsize"><img src="/upload/'+link_image+'" '+wh+'/><div id="close">X</div></div>');
    
    gallery.fullsize_resize();
    
    $('div#image_fullsize div#close').click(gallery.fullsize_close);
    $('div#image_fullsize img').click(gallery.fullsize_close);
}

gallery.fullsize2 = function() {
    var link_image = $(this).attr("src");
    
    //var width = 850;
    //var height = 850;
    
    var width = $(window).width() - 100;
    var height = $(window).height() - 100;
    
    var img_width = $(this).width();
    var img_height = $(this).height();
    
    var new_img_width = width;
    var new_img_height = new_img_width * img_height / img_width;
    
    var wh = 'width="'+width+'"';
    
    if(new_img_height > height) {
        wh = 'height="'+height+'"';
    }
    
    //var wh = 'width="850"';
    //
    //if($(this).height() > $(this).width())
    //    wh = 'height="850"';
    
    $('body').append('<div id="image_fullsize" class="image_fullsize2"><img src="'+link_image+'" '+wh+' style="padding:30px 30px 50px;" /><div id="close" style="margin:10px 10px 0 0;">X</div></div>');
    
    gallery.fullsize_resize2();
    
    $('div#image_fullsize div#close').click(gallery.fullsize_close);
    $('div#image_fullsize img').click(gallery.fullsize_close);
}

var fullsize_resize_setTimeout = '';
gallery.fullsize_resize = function() {
    var margin_top = $('div#image_fullsize img').height() / 2;
    var margin_left = $('div#image_fullsize img').width() / 2;
    
    $('div#image_fullsize').css({"left":"50%", "top":"50%", "margin-left":"-"+margin_left+"px", "margin-top":"-"+margin_top+"px"});
    
    setTimeout(gallery.fullsize_resize, 100);
}

gallery.fullsize_resize2 = function() {
    var margin_top = ($('div#image_fullsize img').height() + 80) / 2;
    var margin_left = ($('div#image_fullsize img').width() + 60) / 2;
    
    $('div#image_fullsize').css({"left":"50%", "top":"50%", "margin-left":"-"+margin_left+"px", "margin-top":"-"+margin_top+"px"});
    
    setTimeout(gallery.fullsize_resize2, 100);
}

gallery.fullsize_close = function() {
    $('div#image_fullsize_bg').remove();
    $('div#image_fullsize').remove();
}

gallery.block_image_resize = function() {
    if($('div.gallery_open').length) {
        var image_width = $('div.gallery_open div.image img').width();
        $('div.gallery_open div.image').width(image_width);
        
        setTimeout(gallery.block_image_resize, 50);
    }
}

var menu = Object();
menu.top = Object();
menu.subTop = Object();

menu.top.width = 0;
menu.subTop.width = 0;

menu.top.alignCenter = function() {
    var tmp = $('ul.top-menu').width();
    
    if(menu.top.width != tmp) {
        menu.top.width = tmp;
        
        $('ul.top-menu').css({"left":"50%", "margin-left":"-" + (menu.top.width / 2) + "px"});
    }
    
    setTimeout(menu.top.alignCenter, 1000);
}

menu.subTop.alignCenter = function() {
    var tmp = $('div.sub-top-menu ul').width();
    
    if(menu.subTop.width != tmp) {
        menu.subTop.width = tmp;
        
        $('div.sub-top-menu ul').css({"left":"50%", "margin-left":"-" + (menu.subTop.width / 2) + "px"});
    }
    
    setTimeout(menu.subTop.alignCenter, 1000);
}
