var photoHeight = 0;
var minZoom = 0;
var selectedImage = 0;

//
// zoomovanie
//
function ZoomPhotoTo(object,value) {
    if(photoHeight <= 0){
        photoHeight = object.innerHeight();
    }
    
    var delta = Math.round((parseInt(Math.round(photoHeight*value/100)-parseInt(object.innerHeight(),10),10))/100);
    ZoomPhoto(object,delta);
}
function ZoomPhoto(object,delta) {
    if(photoHeight <= 0){
        photoHeight = object.innerHeight();
    }
    
    var actualHeight = object.innerHeight();
    object.removeAttr('width');
    object.attr('height',parseInt(actualHeight,10)+parseInt(delta*100,10));
    
    // obmedzenie na min a max zoom
    var minHeight       = object.parent().innerHeight();
    var actualHeight    = object.innerHeight();
    var minWidth        = object.parent().innerWidth();
    var actualWidth     = object.innerWidth();
    
    if(actualHeight > photoHeight && photoHeight > 0){
        object.removeAttr('width');
        object.attr('height',photoHeight);
    }
    
    if(actualHeight < minHeight && actualWidth < minWidth){
        if((minHeight - actualHeight) > (minWidth - actualWidth)){
            object.removeAttr('height');
            object.attr('width',minWidth);
            
        } else {
            object.removeAttr('width');
            object.attr('height',minHeight);
        }
    }
    
    // prepocitanie umiestnenia pri zmenseni
    var actualHeight    = object.innerHeight();
    var actualWidth     = object.innerWidth();
    
    if(actualHeight < minHeight){
        object.css('top',Math.round((minHeight-actualHeight)/2)+'px');
    } else if((parseInt(object.css('top').replace('px',''),10)+actualHeight) < minHeight) {
        object.css('top',minHeight-actualHeight);
    } else if(parseInt(object.css('top').replace('px',''),10) > 0){
        object.css('top','0px');
    }
    
    if(actualWidth < minWidth){
        object.css('left','0px');
    } else if((parseInt(object.css('left').replace('px',''),10)+parseInt(actualWidth,10)) < minWidth) {
        object.css('left',minWidth-actualWidth);
    } else if(parseInt(object.css('left').replace('px',''),10) > 0){
        object.css('left','0px');
    }
    
    var percentage = Math.round(actualHeight/photoHeight*100);
    minZoom = Math.round(minHeight/photoHeight*100);
    var minZoomW = Math.round(minWidth/(photoHeight/(actualHeight/actualWidth))*100);
    
    if(minZoomW < minZoom){
        minZoom = minZoomW;
    }
    
    $('#zoom-value').text(percentage+'%');
    $("#zoom-slider").slider('option','min',minZoom);
    $("#zoom-slider").slider('value',percentage);
}
