$(document).ready(function() {
	
    /**
     * Zmiana obrazków w wyskakujacym okienku
     */
//    $('.block_ui_product_image').click(
//    function() {
//        url = $(this).attr('src');
//        url = url.replace('_3.', '_5.');
//
////        fitImageToScreen(url);
//
//
//    });

    /**
     * Zmiana obrazków po kliknięciu
     */
    $('.img_mini').click(
    function() {
        $(this).children(':first').each(function(){
        	url = $(this).attr('src');
        })
        
        url1 = url.replace('_1.', '_2.');
        $('#product_main_image').attr('src', url1);

        url2 = url.replace('_1.', '_5.');
//        $('#block_ui_product_main_image').attr('src', url2);

        $('#product_main_image_layer').attr('href',url2);
        
//        id = '.produkt_zoom_img_max';
//        new_layer_height = $(id).outerHeight()+30;
//        new_layer_width  = $(id).outerWidth()+40;
//
//
//        $('#TB_ajaxContent').animate({
//            width   : new_layer_width,
//            height  : new_layer_height
//        }, 500);
//
//        $('#TB_window').animate({
//            width: new_layer_width
//        });
        return false;
    });
    
//    $('#product_main_image').click(function(){
//    	caption = $(this).attr('title');
//    	url	    =  $(this).attr('src');
//        url = url.replace('_2.', '_5.');
//
//
////           console.log($('.produkt_zoom_img_mini').outerHeight()+30);
////           new_layer_height = 50;
////           new_layer_width  = 50;
//
//           tb_show(caption, '#TB_inline?height='+100+'&width='+100+'&inlineId=block_ui_images&modal=false');
//           sleep(100);
//
//            fitImageToScreen(url,true,caption);
//            return false;
//    });

});

function fitImageToScreen(tb_pathToImage,show_layer,caption)
{
    
    var imageWidth = 0;
    var imageHeight = 0;

    imgPreloader = new Image();
    imgPreloader.src = tb_pathToImage;
    imgPreloader.onload = loadImage(imgPreloader);
      

//        if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) {
//            marginTop = '-'+(parseInt((new_layer_height / 2),10)+20) + 'px';
//            $('#TB_window').animate({
//               marginTop: marginTop
//            });
//        }

//        $("#TB_window").css({marginLeft: '-' + parseInt((new_layer_width / 2),10) + 'px', width: new_layer_width + 'px'});
//	if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
//		$("#TB_window").css({marginTop: '-' + (parseInt((new_layer_height / 2),10)+20) + 'px'});
//	}

    // End Resizing
}

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}

function loadImage(imgPreloader)
{
//    alert(imgPreloader.complete);
    imgPreloader.onload = null;
      // Resizing large images - orginal by Christian Montoya .
        var pagesize = tb_getPageSize();
        var x = pagesize[0] ;
        var y = pagesize[1] -230;
        imageWidth = imgPreloader.width;
        imageHeight = imgPreloader.height;
        if (imageWidth > x) {
                imageHeight = imageHeight * (x / imageWidth);
                imageWidth = x;
                if (imageHeight > y) {
                        imageWidth = imageWidth * (y / imageHeight);
                        imageHeight = y;
                }
        } else if (imageHeight > y) {
                imageWidth = imageWidth * (y / imageHeight);
                imageHeight = y;
                if (imageWidth > x) {
                        imageHeight = imageHeight * (x / imageWidth);
                        imageWidth = x;
                }
        }


        $('#block_ui_product_main_image').attr('src', url);
        $('#block_ui_product_main_image').attr('width',imageWidth);
        $('#block_ui_product_main_image').attr('height',imageHeight);
        $('.produkt_zoom_img_max').css('width',imageWidth);
        $('.produkt_zoom_img_max').css('height',imageHeight);

        id = '#block_ui_product_main_image';
        new_layer_height = imageHeight +130;
        new_layer_width  = imageWidth +30;


//        console.log('zdjecie'+imageHeight);
//        console.log('warsytawa'+new_layer_height);
//        console.log('ekerna'+y);

        if (new_layer_height > y) {
            if (imageHeight > y) {
                new_layer_height = new_layer_height-30;
            }
        }
//        console.log(x);
//        console.log(y);
//
//        console.log(new_layer_width);
//        console.log(new_layer_height);


        marginLeft = '-'+parseInt((new_layer_width / 2),10)+ 'px';
        marginTop = '-'+(parseInt((new_layer_height / 2),10)+20) + 'px';

        $('#TB_window').animate({
            width: new_layer_width,
            marginLeft: marginLeft,
            marginTop: marginTop
        });

        $('#TB_ajaxContent').animate({
            width   : new_layer_width,
            height  : new_layer_height
        });

}