var MB_OK  = '확인';
var MB_YES = '예';
var MB_NO  = '아니요';
var MB_CLOSE = '닫기';

var COMMON_SERVICE_URI      = '/service/common.php';
var ACCOUNTBOOK_SERVICE_URI = '/service/accountbook.php';
var DIARY_SERVICE_URI       = '/service/diary.php';

/**
 * 이미지 미리 불러오기
 * @since 2009-01-10
 */
function preloadImages() {
	var d=document; 
	if (d.images) {
        if (!d.MM_p) {
            d.MM_p = new Array();
        }
        var i, j = d.MM_p.length, a = preloadImages.arguments;
        for (i = 0; i < a.length; i++) {
            if (a[i].indexOf("#") != 0) {
                d.MM_p[j] = new Image();
                d.MM_p[j++].src = a[i];
            }
        }
    }
}

/**
 * 새 윈도우 (IE전용)
 * @since 2009-01-18
 */
function openModal(paramUrl, paramWidth, paramHeight, paramStyle, paramParams) {
    if (paramStyle) {
        if (paramStyle.substr(paramStyle.length-1) != ';') paramStyle += ';';
    } else {
        paramStyle = '';
    }
    if (!paramParams) {
        paramParams = document;
    }
    return window.showModalDialog(paramUrl, paramParams
            , "dialogWidth: " + paramWidth + "px; "
            + "dialogHeight: " + paramHeight + "px; "
            + "status: 0; center: 1; help: 0; "
            + "dialogHide: 0;"
            + paramStyle);
}

/**
 * 새 윈도우 (범용)
 * @since 2009-01-18
 */
function openWindow(paramUrl, paramWidth, paramHeight, paramStyle, paramName) {
    function topPosition() {
        return typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
    }

    function leftPosition() {
        return typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
    }

    var nPosX;
    var nPosY;

    if (!paramName) {
        paramName = 'winPop';
    }

    if (dojo.isIE) {
        paramHeight -= 0;
    }

    nPosX = (screen.availWidth - paramWidth) / 2;
    nPosY = (screen.availHeight - paramHeight) / 2;
    
    if (paramStyle) {
        if (paramStyle.indexOf('scrollbars') == -1) {
            paramStyle += ',scrollbars=no';
        }
    }

    var popupWindow = window.open(paramUrl
                                 ,paramName
                                 ,'top=' + nPosY 
                                 +',left=' + nPosX 
                                 +',width=' + paramWidth 
                                 +',height=' + paramHeight
                                 +',toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=no,copyhistory=no'+paramStyle
                                 );
    popupWindow.focus();
}

/**
 * 팝업 열기
 * @since 2009-01-10
 */
function openPopup(paramUrl, paramWidth, paramHeight, paramStyle, paramParams, paramName) {    
    if (dojo.isIE) {
        openModal(paramUrl, paramWidth, paramHeight, paramStyle, paramParams);
    } else {
        openWindow(paramUrl, paramWidth, paramHeight, paramStyle, paramName);
    }
}

/**
 * 회원 프로필 보기
 * @since 2009-08-12
 */
function openProfile(user_id) {
    openWindow('/common/popup.profile.php?user_id='+user_id, 500, 610, '', 'profile');
}

/**
 * 프리미엄 서비스 신청
 * @since 2009-09-20
 */
function openSettlement() {
    openWindow('/common/popup.settlement.php', 500, 320, '', 'settlement');
}

/**
 * 쪽지보내기
 * @since 2009-08-11
 */
function openSendMessage(to_id, to_name, title) {
    to_name = encodeURIComponent(to_name);
    if (title) {
        title = encodeURIComponent(title);
    } else {
        title = '';
    }
    openWindow('/common/popup.send.message.php?to_id='+to_id+'&to_name='+to_name+'&title='+title, 600, 440, '', 'send_message');
}

/**
 * 메일보내기
 * @since 2009-08-11
 */
function openSendMail(to_id, to_name) {
    openWindow('/common/popup.send.mail.php?to_id='+to_id+'&to_name='+to_name, 600, 440, '', 'send_message');
}

/**
 * 채팅방
 * @since 2009-12-26
 */
function openChat() {
    openWindow('/chat/', 800, 680, '', 'chat_room_list');
}

/**
 * 회원 사진 크게 보기
 * @since 2009-08-12
 */
function doViewPhoto(photo_url) {
    openWindow('/common/popup.photo.php?photo_url='+photo_url, 50, 50, '', 'userPhoto');
}

/**
 * 받는이 추가
 * @since 2009-08-11
 */
function openToSearch(to_id) {
    openWindow('/common/popup.to.search.php?to_id='+to_id, 600, 500, '', 'to_search');
}

/**
 * 페이지 리스트 onMouseOver
 * @since 2009-01-19
 */
function listOnMouseOver(object) {    
    object.style.backgroundColor = '#dddddd';
}

function listOnMouseOut(object, color) {    
    object.style.backgroundColor = color;
}

function serviceCall(uri, params, func) {
    dojo.xhrPost({
        url: uri
       ,content: params
       ,handleAs: "json"
       ,load: function(response){
            func(response);
        }
       ,error: function(response, ioArgs) {
            console.error("error handler:", response);
        }
    });
}

/**
 * 달력 팝업 초기화
 * @since 2009-03-04
 */
function initCalendar() {	
	for (var i=0; i < arguments.length; i++) {
		var calendarParam = arguments[i];
		var dateControl = document.getElementById(calendarParam[0]);
		Calendar.setup({dateField:calendarParam[0], triggerElement:calendarParam[1]});
		if (!dateControl.value) {
			dateControl.value = calendarParam[2];
		}
	}
}

/**
 * 배열 역순 정렬
 * @since 2009-07-04
 */
function arraySortDesc(arr) {	
	for (var i=0; i < arguments.length; i++) {
		var calendarParam = arguments[i];
		var dateControl = document.getElementById(calendarParam[0]);
		Calendar.setup({dateField:calendarParam[0], triggerElement:calendarParam[1]});
		if (!dateControl.value) {
			dateControl.value = calendarParam[2];
		}
	}
}

/**
 * 창 닫기
 * @since 2009-08-16
 */
function closeTimeout(timeout) {
    if (!timeout) {
        timeout = 100;
    }

    window.setTimeout(function(){window.close();}, timeout);
}

/**
 * 쿠키 저장
 * @since 2009-09-01
 */
function setCookie(name, value, expiredays) {
    var toDate = new Date();

    toDate.setDate(toDate.getDate() + expiredays);
    document.cookie = name + '=' + escape(value) + '; path=/; expires=' + toDate.toGMTString() + ';';
}

/**
 * 쿠키 가져오기
 * @since 2009-09-01
 */
function getCookie(name) {
    var cookieName = name + "=";
    var cookie = document.cookie;

    if (cookie.length > 0) {
        var i = cookie.indexOf(cookieName);
        if (i < 0) {
            return '';
        }
        i += cookieName.length;
        var j = cookie.indexOf(";", i);
        if (j == -1){
            j = cookie.length;
        }
    }
    return unescape(cookie.substring(i, j));
}

function removeCookie(name) {
    var toDate = new Date();
    toDate.setDate(toDate.getDate() -1);

    document.cookie = name + "=; path=/; expires=" + toDate.toGMTString();
}

/**
 * 출석하기
 * @since 2009-09-10
 */
function openAttendance() {
    openWindow('/common/popup.attendance.php', 600, 200, '', 'writeAttendance');
}

/**
 * TEXT ENCODE
 * @since 2009-09-24
 */
function encodeURI(val) {
    return encodeURIComponent(val);
}

/**
 * TEXT DECODE
 * @since 2009-09-24
 */
function decodeURI(val) {
    return decodeURIComponent(val);
}