﻿
function tsLan() {
    tsPage(2);
}

function initNavigator(pageCount, preChar, nextChar) {
    var $navi = $('#navigator');
    var $sliderChildren = $('#slider').children();

    $navi.append('<a id="pre" class="pager" onclick="navi(\'-\',event)">' + preChar + '</a>');


    for (var i = 0; i < pageCount; i++) {
        $navi.append('<a class="' + (i == 0 ? "curPager" : "pager") + '" onclick="navi(\'' + (i + 1) + '\',event)">' + (i + 1) + '</a>')
    }

    $navi.append('<a id="next" class="pager" onclick="navi(\'+\',event)" >' + nextChar + '</a>');

    checkPreNextPager($navi, $sliderChildren);
}

var curIdx = 0;
function navi(val, evt) {
    var e = window.event || evt;
    var trigger = e.srcElement || e.target;
    if (trigger) trigger.blur();
    if ((val - 1) == curIdx) return;
    var $sliderChildren = $('#slider').children();
    var $navi = $('#navigator');
    var $naviChildren = $navi.children();

    //儲存舊的page index
    var oldIdx = curIdx;

    //取得新的page index
    if (val == '-' && curIdx != 0) {
        curIdx--;
    } else if (val == '+' && curIdx < $sliderChildren.length) {
        curIdx++;
    } else {
        curIdx = val - 1;
    }

    //舊的分頁按鈕切換class
    $naviChildren.eq(oldIdx + 1).addClass('pager').removeClass('curPager');

    //新的分頁按鈕切換class
    $naviChildren.eq(curIdx + 1).addClass('curPager').removeClass('pager');

    checkPreNextPager($navi, $sliderChildren);

    $sliderChildren.eq(oldIdx).fadeOut(1000);
    $sliderChildren.eq(curIdx).fadeIn(1000);

    //$('#slider').children().eq(1).css('display', 'block');
}

function checkPreNextPager($navi, $sliderChildren) {
    if (curIdx == 0)
        $navi.children('#pre').css('visibility', 'hidden');
    else
        $navi.children('#pre').css('visibility', 'visible');

    if (curIdx == ($sliderChildren.length - 1))
        $navi.children('#next').css('visibility', 'hidden');
    else
        $navi.children('#next').css('visibility', 'visible');
        
}



function deleteConfirm() {
    return confirm('是否確定刪除？刪除後將無法復原');
}

function thumbImg(img, allowMaxWidth) {
    if (!img)
        return;
    if (img.tagName.toLowerCase() != 'img')
        return;

    zw = img.offsetWidth;
    zh = img.offsetHeight;
    zr = zw / zh;

    if (parseInt(zw) > parseInt(allowMaxWidth)) {
        img.style.width = allowMaxWidth;
        img.style.height = (zw / zr) + 'px';
    }
}

//初始化用的共用
function initLightbox(obj, appVirtualPath) {
    $(obj).lightbox({
        fitToScreen: true,
        slideNavBar: false, // slide nav bar up/down between image resizing transitions
        //navBarSlideSpeed: 175, //only available when slideNavBar set to true
        imageClickClose: true,
        disableNavbarLinks: true,
        widthCurrent: 250,
        heightCurrent: 250,
        resizeSpeed: 500,
        fileLoadingImage: appVirtualPath + '/Plugins/jquery-lightbox/images/loading.gif',
        fileBottomNavCloseImage: appVirtualPath + '/Plugins/jquery-lightbox/images/closelabel.gif',
        strings: {
            prevLinkTitle: '上一張圖片',
            nextLinkTitle: '下一張圖片',
            prevLinkText: '&laquo; 上一張',
            nextLinkText: '下一張 &raquo;',
            closeTitle: '關閉',
            image: ' ',
            of: ' / '
        }
    });
}

//ImgID和ankID為選擇性參數
function initFileUpload(fileUploadID, svrPathSessionKey, appVirtualPath, ImgID, ankID, ASPSESSID) {
    var uploadPath = appVirtualPath + '/Uploads/Temp/';
    $('#' + fileUploadID).uploadify({
    	'uploader': appVirtualPath + '/Uploadify/uploadify.swf',
    	'script': appVirtualPath + '/Uploadify/Upload.ashx',
    	'cancelImg': appVirtualPath + '/Uploadify/cancel.png',
    	'folder': uploadPath,
    	'scriptData': { 'uploadPath': uploadPath, 'svrPathSessionKey': svrPathSessionKey, 'ASPSESSID': ASPSESSID },
    	'auto': true,
    	'multi': false,
    	'fileDesc': 'Image Files (*.jpg;*.png;*.gif;*.bmp;*.jpeg)',
    	'fileExt': '*.jpg;*.png;*.gif;*.bmp;*.jpeg',
    	'queueSizeLimit': 90,
    	'sizeLimit': 4000000,
    	'buttonText': 'Upload Image',
    	'onComplete': function (event, queueID, fileObj, response, data) {
    		if (ImgID && ImgID != '') { //注意，這裡run JQuery無法成功
    			var img = document.getElementById(ImgID);
    			img.src = response;
    			//img.title = response;
    			img.alt = response;
    			if (ankID && ankID != '') {
    				var ank = document.getElementById(ankID);
    				ank.href = response;
    			}
    		}
    	}
    });

}

//function thumbImg(ImgD, FitWidth, FitHeight) {
//    var image = new Image();
//    image.src = ImgD.src;
//    if (image.width > 0 && image.height > 0) {
//        if (image.width / image.height >= FitWidth / FitHeight) {
//            if (image.width > FitWidth) {
//                ImgD.width = FitWidth;
//                ImgD.height = (image.height * FitWidth) / image.width;
//            } else {
//                ImgD.width = image.width;
//                ImgD.height = image.height;
//            }
//        } else {
//            if (image.height > FitHeight) {
//                ImgD.height = FitHeight;
//                ImgD.width = (image.width * FitHeight) / image.height;
//            } else {
//                ImgD.width = image.width;
//                ImgD.height = image.height;
//            }
//        }
//    }
//}

//function thumbImg(obj, method) {
//    if (!obj) {
//        return;
//    }
//    obj.onload = null;
//    file = obj.src;
//    zw = obj.offsetWidth;
//    zh = obj.offsetHeight;
//    if (zw < 2) {
//        if (!obj.id) {
//            obj.id = 'img_' + Math.random();
//        }
//        setTimeout("thumbImg($('" + obj.id + "'), " + method + ")", 100);
//        return;
//    }
//    zr = zw / zh;
//    method = !method ? 0 : 1;
//    if (method) {
//        fixw = obj.getAttribute('_width');
//        fixh = obj.getAttribute('_height');
//        if (zw > fixw) {
//            zw = fixw;
//            zh = zw / zr;
//        }
//        if (zh > fixh) {
//            zh = fixh;
//            zw = zh * zr;
//        }
//    } else {
//        var widthary = typeof imagemaxwidth == 'undefined' ? [] : imagemaxwidth.split('%');
//        if (widthary.length > 1) {
//            fixw = $('wrap').clientWidth - 200;
//            if (widthary[0]) {
//                fixw = fixw * widthary[0] / 100;
//            } else if (widthary[1]) {
//                fixw = fixw < widthary[1] ? fixw : widthary[1];
//            }
//        } else {
//            fixw = widthary[0];
//        }
//        if (zw > fixw) {
//            zw = fixw;
//            zh = zw / zr;
//            obj.style.cursor = 'pointer';
//            if (!obj.onclick) {
//                obj.onclick = function() {
//                    zoom(obj, obj.src);
//                }
//            }
//        }
//    }
//    obj.width = zw;
//    obj.height = zh;
//}



function hideImg(img) {
    img.style.display = 'none';
}
