contentview_ImagePreview.js 14.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/// <reference path="common.js" />

/*====Variable====*/
var slideshowImgFrom = 0;
var slideshowImgTo = 4;
var slideshowImageCollection = [];
var slideshowClickFlg = false;
var slideshowInitFlg = true;
var slideshowSelectedIndex = 0;
//0: next - 1: prev
var slideshowControlToggleFlg = 0;
var totalRecord;
var slideshowSelectFirstIndex = 0;
var slideshowSelectLastIndex = 4;
var slideshowMainCurrIndex = 0;
/*==========================================*/
function refreshSlideShowValue(){
Vo Duc Thang committed
18 19 20 21 22 23 24 25 26 27 28 29
    slideshowImgFrom = 0;
    slideshowImgTo = 4;
    slideshowImageCollection = [];
    slideshowClickFlg = false;
    slideshowInitFlg = true;
    slideshowSelectedIndex = 0;
    //0: next - 1: prev
    slideshowControlToggleFlg = 0;
    totalRecord;
    slideshowSelectFirstIndex = 0;
    slideshowSelectLastIndex = 4;
    slideshowMainCurrIndex = 0;
30 31 32 33 34 35 36 37 38 39 40 41 42 43
};


//Show Image Preview
function showImagePreview(targetId, imgList) {
    refreshSlideShowValue();
    //Check if imageList is not null
    if (imgList != null && imgList != 'undefined' && imgList.length > 0) {
        slideshowImageCollection = setImageSource(imgList);
        //Check if targetId is not null
        if (targetId != null && targetId != 'undefined') {
            if (slideshowImageCollection.length == 1) {
                targetId.html('');
                targetId.append('<div id="slideWrapper">'
Vo Duc Thang committed
44
                + '</div>');
45 46

                var mainImg = $('#slideWrapper');
Vo Duc Thang committed
47
                //START FIXCSS
48 49 50 51

                var cssObj = { 'background-image': 'url(' + slideshowImageCollection[0].thumbnail + ')',
                    'background-repeat': 'no-repeat',
                    'background-size': 'contain',
Vo Duc Thang committed
52
                    'background-position': 'center'
53
                };
Vo Duc Thang committed
54 55 56
                mainImg.addClass('imagePreview-one');
                //END FIXCSS

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

                mainImg.css(cssObj);

                mainImg.parent().css('padding-top', '0px');

            } else {
                renderSlideShowBackground(targetId);
                renderSelectImage();
                renderMainImage(0);
                handleImagePreviewEvent();
            }
        }
    }
    else {
        renderSlideShowBackground(targetId);
        $('.main-control').css('visibility', 'hidden');
        $('.slideshow-control').css('visibility', 'hidden');
    }
    optimizeSizeImagePreview();
};

//Set image source for slide show
function setImageSource(source){
Vo Duc Thang committed
80 81 82 83 84 85 86 87
    var oldSource = source;
    var newSource = [];
    
    for(var nIndex = 0; nIndex < oldSource.length; nIndex++){
        newSource.push({index : nIndex, image : oldSource[nIndex], thumbnail : oldSource[nIndex]});
    }
    
    return newSource;
88 89 90 91
};

//render background to show image preview
function renderSlideShowBackground(targetId){
Vo Duc Thang committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    var targetDiv = targetId;
    targetDiv.html('');
    targetDiv.append('<div id="slideWrapper">'
    +'  <div class="gallery-image" id="gallery-image">'
    +'      <div class="main-control" id="main-control-prev"> </div>'
    +'      <div id="main-img" id="mainThumbnail"><div class="mainThumbnail"></div></div>'
    +'      <div class="main-control" id="main-control-next"> </div>'
    +'  </div>'
    +'  <div class="gallery-thumb">'
    +'      <div id="control-prev" class="slideshow-control"><img src="././img/arrow-left.gif"></div>'
    +'      <div id="selector-img">'
    +'      </div>'
    +'      <div id="control-next" class="slideshow-control"><img src="././img/arrow-right.gif"></div>'
    +'  </div>'
    +'</div>'   
    );

    $(window).resize(function () {

        optimizeSizeImagePreview();
    });

    //handleImagePreviewEvent();
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
};
function optimizeSizeImagePreview() {
    var maxW = "798";
    var ratio = 1.566;
    var maxH = maxW / ratio;



    if ($("#dialog").width() < maxW || $("#dialog").height() < maxH) {
        
        if ($("#dialog").width() < maxW) {
            $("#dialog").css('width', maxW + 'px');
            $("#dialog").css('height', maxH + 'px');
        }
        if ($("#dialog").height() < maxH) {
            $("#dialog").css('height', maxH + 'px');
            $("#dialog").css('width', maxW + 'px');
        }
    }
    else {
        $("#dialog").css('width', maxW + 'px');
        $("#dialog").css('height', maxH + 'px');
    }
    $("#dialog").center();
};

//render select image
function renderSelectImage(){
Vo Duc Thang committed
143 144 145 146 147 148 149 150 151 152
    var selectImg = $('#selector-img');
    
    $.each(slideshowImageCollection, function(i, image){
        if(slideshowImgFrom <= i && i <= slideshowImgTo){
            selectImg.append('<div class="thumbnail" imageId="'+ image.index +'" style="display:block; background-image : url('+image.thumbnail+')"></div>');
        }
        else{
            selectImg.append('<div class="thumbnail" imageId="'+ image.index +'" style="display:none; background-image : url('+image.thumbnail+')"></div>');
        }
    });
153 154 155 156
};

//Handle Image Preview Event
function handleImagePreviewEvent(){
Vo Duc Thang committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    
    $('#control-next').click(imageSelectNextFunction);
    
    $('#control-prev').click(imageSelectPrevFunction);
    
    $('#main-control-next').click(imageMainSelectNextFunction);

    $('#main-control-prev').click(imageMainSelectPrevFunction);
    
    $('.thumbnail').click(selectImgClickFunction);
    
    $('.thumbnail').mouseenter(selectImgMouseEnterFunction);
    
    $('.thumbnail').mouseleave(selectImgMouseLeaveFunction);
    
    $('.main-control').css('opacity','0');
    
    
    $('#main-control-next').mouseleave(mainControlNextMouseLeaveFunction);
    
    
    $('#main-control-prev').mouseleave(mainControlPrevMouseLeaveFunction);
    
    if (isTouchDevice() == false) {
        //$('.main-control').mouseenter(mainControlMouseEnterFunction);
        $('#main-control-next').mouseenter(mainControlNextMouseEnterFunction);
        $('#main-control-prev').mouseenter(mainControlPrevMouseEnterFunction);
    }
    
    //$('.main-control').mouseleave(mainControlMouseLeaveFunction);
    
    //$('.thumbnail:first').css('box-shadow','0px 7px 7px #555');
    $('.thumbnail:first').addClass('activeThumb');
    handleDispNextPrevButton();
191 192 193
};

function mainControlNextMouseEnterFunction(){
Vo Duc Thang committed
194
    $(this).css('opacity','0.5');
195 196 197
};

function mainControlNextMouseLeaveFunction(){
Vo Duc Thang committed
198
    $(this).css('opacity','0');
199 200 201
};

function mainControlPrevMouseEnterFunction(){
Vo Duc Thang committed
202
    $(this).css('opacity','0.5');
203 204 205
};

function mainControlPrevMouseLeaveFunction(){
Vo Duc Thang committed
206
    $(this).css('opacity','0');
207 208 209 210 211
};

//Image Preview next icon function
function imageSelectNextFunction(){

Vo Duc Thang committed
212 213 214 215 216 217 218 219 220 221 222 223
    if(slideshowSelectLastIndex < slideshowImageCollection.length - 1){
        $('#selector-img div:eq('+slideshowSelectFirstIndex+')').animate({width: 'hide'},
            {step: function(now, fx){                                       
                        $('#selector-img div:eq('+slideshowSelectLastIndex+')').animate({width: 'show'});                       
                }
            }
        );
        
        slideshowSelectFirstIndex = slideshowSelectFirstIndex + 1;
        slideshowSelectLastIndex = slideshowSelectLastIndex + 1;
        handleDispNextPrevButton();
    }
224 225 226 227
};

//Image preview prev icon function
function imageSelectPrevFunction(){
Vo Duc Thang committed
228 229 230 231 232 233 234 235 236 237
    var fixedIndex = slideshowSelectFirstIndex - 1;
    
    if(slideshowSelectFirstIndex > 0){
        
        $('#selector-img div:eq('+fixedIndex+')').animate({width: 'show'});
        $('#selector-img div:eq('+slideshowSelectLastIndex+')').animate({width:'hide'});
        slideshowSelectFirstIndex = slideshowSelectFirstIndex - 1;
        slideshowSelectLastIndex = slideshowSelectLastIndex - 1;
        handleDispNextPrevButton();
    }
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255

};
var slideshow_isTransaction = false;

//Main Image next icon function
function imageMainSelectNextFunction() {

    if (isTouchDevice() == true) {
        $('#main-control-next').css('opacity', '0.5');
    }


    if (slideshow_isTransaction == false) {
        slideshow_isTransaction = true;
        var fixedIndex = eval(slideshowMainCurrIndex) + 1;

        if (slideshowMainCurrIndex < slideshowImageCollection.length - 1) {
            $('#main-img div').animate(
Vo Duc Thang committed
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
            { "left": "-=100%" },
            {
                /*duration: "slow",*/
                complete: function () {
                    $('#main-img div').hide();
                    renderMainImage(fixedIndex);
                    $('#main-img div').css('left', '+=300%');
                    $('#main-img div').show();
                    $('<img/>').attr('src', slideshowImageCollection[fixedIndex].thumbnail).load(function () {
                        $('#main-img div').animate({ left: '0%' });
                    });
                    slideshowMainCurrIndex = eval(slideshowMainCurrIndex) + 1;
                    syncImageMainWithSelectImage();
                    handleDispNextPrevButton();
                }
            }
        );
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
        }
    }
};

//Main Image prev icon function
function imageMainSelectPrevFunction() {
    if (isTouchDevice() == true) {
        $('#main-control-prev').css('opacity', '0.5');
    }

    if (slideshow_isTransaction == false) {
        slideshow_isTransaction = true;
        var fixedIndex = eval(slideshowMainCurrIndex) - 1;

        if (slideshowMainCurrIndex > 0) {
            $('#main-img div').animate(
Vo Duc Thang committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
            { "left": "+=100%" },
            {
                duration: "medium",
                complete: function () {
                    $('#main-img div').hide();
                    renderMainImage(fixedIndex);
                    $('#main-img div').css('left', '-=300%');
                    $('#main-img div').show();
                    $('<img/>').attr('src', slideshowImageCollection[fixedIndex].thumbnail).load(function () {
                        $('#main-img div').animate({ left: '0%' });
                    });
                    slideshowMainCurrIndex = eval(slideshowMainCurrIndex) - 1;
                    syncImageMainWithSelectImage();
                    handleDispNextPrevButton();
                }
            }
        );
306 307 308 309 310 311 312 313 314 315 316 317 318

        }
    }
};

//Selector Image click function
function selectImgClickFunction() {
    if (slideshow_isTransaction == false) {
        slideshow_isTransaction = true;
        var imgIndex = $(this).attr('imageId');

        if (imgIndex > slideshowMainCurrIndex) {
            $('#main-img div').animate(
Vo Duc Thang committed
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
            { "left": "-=100%" },
            {
                duration: "medium",
                complete: function () {
                    $('#main-img div').css('display', 'none');
                    renderMainImage(imgIndex);
                    $('#main-img div').css('left', '+=300%');
                    $('#main-img div').css('display', 'block');
                    $('<img/>').attr('src', slideshowImageCollection[imgIndex].thumbnail).load(function () {
                        $('#main-img div').animate({ left: '0%' });
                    });

                    slideshowMainCurrIndex = imgIndex;
                    handleDispNextPrevButton();
                }
            }
        );
336 337 338
        }
        else if (imgIndex < slideshowMainCurrIndex) {
            $('#main-img div').animate(
Vo Duc Thang committed
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
            { "left": "+=100%" },
            {
                duration: "medium",
                complete: function () {
                    $('#main-img div').css('display', 'none');
                    renderMainImage(imgIndex);
                    $('#main-img div').css('left', '-=300%');
                    $('#main-img div').css('display', 'block');
                    $('<img/>').attr('src', slideshowImageCollection[imgIndex].thumbnail).load(function () {
                        $('#main-img div').animate({ left: '0%' });
                    });
                    slideshowMainCurrIndex = imgIndex;
                    handleDispNextPrevButton();
                }
            }
        );
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
        }
        else {
            slideshow_isTransaction = false;
            slideshowMainCurrIndex = imgIndex;
            handleDispNextPrevButton();
        }

        //$('.thumbnail').css('box-shadow', '');
        $('.thumbnail').removeClass('activeThumb');
        //$(this).css('box-shadow', '0px 7px 7px #555');
        $(this).addClass('activeThumb');
        slideshowClickFlg = true;
    }
};

//Render Main Image
function renderMainImage(i) {
    
Vo Duc Thang committed
373 374 375 376 377 378 379 380
    var mainImg = $('#main-img div');
    mainImg.css('background-image', 'url(' + slideshowImageCollection[i].thumbnail + ')');

    slideshow_isTransaction = false;
    if (isTouchDevice() == true) {
        $('#main-control-next').css('opacity', '0');
        $('#main-control-prev').css('opacity', '0');
    }
381 382 383
};

function selectImgMouseLeaveFunction(){
Vo Duc Thang committed
384 385 386 387 388 389 390 391
    if(!slideshowClickFlg){
        //$('this').css('box-shadow','');
        $('this').removeClass('activeThumb');
        slideshowClickFlg = false;
    }
    else{
        //slideshowClickFlg = false;
    }
392 393 394
};

function selectImgMouseEnterFunction(){
Vo Duc Thang committed
395 396 397 398 399
    
    //$('.thumbnail').css('box-shadow','');
    $('.thumbnail').removeClass('activeThumb');
    //$(this).css('box-shadow','0px 7px 7px #555');
    $(this).addClass('activeThumb');
400 401 402
};

//Main control mouse leave function
Vo Duc Thang committed
403 404
function mainControlMouseLeaveFunction(){   
    $('.main-control').css('opacity','0');
405 406 407 408
};

//Main control mouse enter function
function mainControlMouseEnterFunction(){
Vo Duc Thang committed
409
    $('.main-control').css('opacity','0.5');
410 411 412 413
};

//Sync Main Image with select Image
function syncImageMainWithSelectImage(){
Vo Duc Thang committed
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
    var thumbImg = $('.thumbnail[imageId="'+ slideshowMainCurrIndex +'"]'); 

    if(thumbImg.is(':hidden')){
        if(slideshowMainCurrIndex > slideshowSelectLastIndex){
            imageSelectNextFunction();
        }
        else if(slideshowMainCurrIndex < slideshowSelectFirstIndex){
            imageSelectPrevFunction();
        }
    }
    
    //$('.thumbnail').css('box-shadow','');
    $('.thumbnail').removeClass('activeThumb');
    //thumbImg.css('box-shadow','0px 7px 7px #555');
    thumbImg.addClass('activeThumb');
429 430 431 432
};

//Handle display button control
function handleDispNextPrevButton(){
Vo Duc Thang committed
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
    
    if(slideshowSelectFirstIndex > 0){      
        $('#control-prev').css('visibility','visible');
    }
    else{
        $('#control-prev').css('visibility','hidden');
    }
    
    if(slideshowSelectLastIndex < (eval(slideshowImageCollection.length) - 1)){ 
        $('#control-next').css('visibility','visible');
    }
    else{
        $('#control-next').css('visibility','hidden');
    }
    
    if(slideshowMainCurrIndex > 0){     
        $('#main-control-prev').css('visibility','visible');
    }
    else{
        $('#main-control-prev').css('visibility','hidden');
    }
    
    if(slideshowMainCurrIndex < (eval(slideshowImageCollection.length) - 1)){
        $('#main-control-next').css('visibility','visible');
    }
    else{
        $('#main-control-next').css('visibility','hidden');
    }
461 462 463
};