﻿
// imageviewer.jp
// 画像表示ページのメインスクリプト

var mouseMode = -1;
var mouseWheelCount = 0;
var curTsWidth = 0;
var curTsHeight = 0;

// ページ読み込み時に実行する初期化処理
$(document).ready(function() {

    // リサイズを有効にする。
    $('#imageFlame').resizable({
        resize: function(event, ui) {
            ShowDispSize();
        },
        start: function(event, ui) {
        },
        stop: function(event, ui) {
            UpdateJpegResize(ui.size.width, ui.size.height);
        }
    });

    ShowDispSize();

    ChangeMouseMode(0);

    // 画像を読み込んだときの処理
    $('#mainImage').load(function() { ImageLoadCompleted(); });

    // マウスホイールを有効にする。
    $('#mainImage').mousewheel(function(event, delta) {
        MouseWheeling(delta);
    });

    // テレスコープダイアログ
    $('#dlgTelescope').dialog({
        //        position: [tsLeft, tsTop],
        minWidth: 50,
        minHeight: 50,
        resizeStart: function(event, ui) {
            curTsWidth = ui.size.width;
            curTsHeight = ui.size.height;
        },
        resize: function(event, ui) {
        },
        resizeStop: function(event, ui) {
            ResizeTelescope(ui);
        },
        dragStop: function(event, ui) {
            $('#hTsLeft').val(ui.position.left);
            $('#hTsTop').val(ui.position.top);
        }

    });

    // 最初は閉じておく
    $('#dlgTelescope').dialog('close');

    // テレスコープ内の表示領域ボックス
    $("#tsDispRegion").draggable({
        drag: function(event, ui) {
        },
        start: function(event, ui) {
        },
        stop: function(event, ui) {
            UpdateJpegDragOnTelescope(ui.position.left, ui.position.top);
        }
    });

    // メイン画像表示
    UpdateJpeg(0, -1, "");

    var pageCount = parseInt($('#hPageCount').val());

    // 複数ページの場合
    if (pageCount > 1) {
        $(window).resize(function() { AdjustThumbnailArea(); });

        AdjustThumbnailArea();

        SetPageNavButtons();

        // 最初のサムネイル表示
        var curPage = $('#hPageNo').val();
        ShowThumbnails(parseInt(curPage));
    }

    // テレスコープ
    if (parseInt($('#hTelescope').val()) == 1) {
        ToggleTelescope();
    }

});

function ChangeMouseMode(mode)
{
	if (mode == mouseMode) return false;

	// まず、現状のを外す
	if (mouseMode == 0)
	{
		$("#mainImage").draggable('destroy');
	}
	else if (mouseMode == 1)
	{
		$("#mainImage").imgAreaSelect({
			disable: true,
			hide: true
		});
	}

	mouseMode = mode;

	if (mode == 0) {
	    $("#btnMouseDrag").attr("src", "image/hands_on.png");
	    $("#btnMouseAreaSelect").attr("src", "image/selectable_img.png");
	
		$("#mainImage").css("cursor","move");
		$("#mainImage").draggable({
		    drag: function(event, ui) {
		    },
		    start: function(event, ui) {
		    },
		    stop: function(event, ui) {
		        UpdateJpegDrag(ui.position.left, ui.position.top);
		    }
		});
	}
	else
	{
	    $("#btnMouseDrag").attr("src", "image/draggable_img.png");
	    $("#btnMouseAreaSelect").attr("src", "image/diagonal_on.png");

	    $("#mainImage").css("cursor", "crosshair");
		$("#mainImage").imgAreaSelect({
		    enable: true,
		    handles: true,
		    autoHide: true,
		    onSelectStart: function(img, ui) {
		    },
		    onSelectChange: function(img, ui) {
		    },
		    onSelectEnd: function(img, ui) {
		        if (ui.width > 1 && ui.height > 1) {
		            UpdateJpegDisplayRegion(ui.x1, ui.y1, ui.width, ui.height);
		        }
		    }
		});
	}
	
	return false;
}

function ShowDispSize()
{
    $('#txtDispW').val($("#imageFlame").width());
    $('#txtDispH').val($("#imageFlame").height());
}


function MouseWheeling(delta)
{
	if (delta > 0)
	{
	    mouseWheelCount++;
	    UpdateJpeg(0, 11, '0.1');
	}
	else
	{
		mouseWheelCount--;
		UpdateJpeg(0, 12, '0.1');
    }
}

// テレスコープ
function ToggleTelescope()
{
	var dlg = $('#dlgTelescope');

	if (dlg.dialog('isOpen')) {
	    dlg.dialog('close');
	}
	else {
	    $('#tsDispRegion').show();

	    var tsLeft = parseInt($('#hTsLeft').val());
	    var tsTop = parseInt($('#hTsTop').val());

	    if (tsLeft < 0 || tsTop < 0) {
	        var pos = $('#imageFlame').position();
	        tsLeft = pos.left;
	        tsTop = pos.top + parseInt($('#imageFlame').css('margin-top'));
	    }

	    dlg.dialog('option', 'position', [tsLeft, tsTop]);
	    dlg.dialog('open');
	}

}

function ResizeTelescope(ui) {
    UpdateTelescope(ui.size.width - curTsWidth, ui.size.height - curTsHeight);
}

function SaveTelescopeLoc(ui) {
}

function ToggleRefUrl() {
    var refDiv = $('#refUrl');
    refDiv.toggle(500);
}



jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};



