﻿function color_change(target)
{   
    var color = '#206c01 #237701 #267e02 #288602 #2a8e02 #2c9602 #2e9c02 #30a501 #32ac01 #34b301'.split(' ');    
    var tid;
    var cnt=0;
     
    function change()
    {
        if(cnt<10){target.css("background","url('') no-repeat " + color[cnt]); cnt++;}
        else clearInterval(tid);
    }
    tid = setInterval(change, 30);
}

function write_flash(width, height, name)
{
    var html = '';
    html += '<object width="'+ width +'" height="'+ height +'" style="z-index:1;" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0">';    
    html += '    <param name="movie" value="/images/flash/'+ name +'.swf" />';
    html += '    <param name="quality" value="high" />';
    html += '    <param name="wmode" value="transparent" />';
    html += '    <embed wmode="transparent" width="'+ width +'" height="'+ height +'" pluginspage="http://www.macromedia.com/go/getflashplayer" style="z-index:1;" quality="high" src="/images/flash/'+ name +'.swf" type="application/x-shockwave-flash"></embed>';
    html += '</object>';
    document.write(html); 
}

function goto_page(url)
{
    var form = document.createElement('form');
    form.action = url;
    form.method = 'post'; 
    form.submit();
}

function open_pop(openUrl, width, height)
{
    var cw=width;
    var ch=height;
    var sw=screen.availWidth;
    var sh=screen.availHeight;
    var px=(sw-cw)/2;
    var py=(sh-ch)/2;    
    window.open(openUrl,'','toolbar=no,resizable=no,location=no,menubar=no,scrollbars=no,location=no,width='+cw+',height='+ch+', top='+py+', left='+px+'');
}

function seriesString(str, count)
{
    var temp = new Array(str.length);
    var num = 0; 
    for (var i=0; i < str.length; i++)
        temp[i] = parseInt(str.charAt(i));
     
    for (var i=0; i < str.length; i++){
        for (var j=0; j < str.length; j++){
            if(temp[i] == temp[j]){num++;if(num == count) return false;}
            else num = 0;  
        }
        num = 0; 
    }
    return true;
}

function replaceMoney(target_text)
{
    var text = target_text.replace(/,/gi,"")
    str = new String(text);
    this.str = str;
    len = str.length;
    str1 = "";

    for(i=1; i<=len; i++) {
        str1 = str.charAt(len-i)+str1;
        if((i%3 == 0)&&(len-i != 0)) 
            str1 = ","+str1;
    }
    return str1;
}

Diary = function(target) 
{   
    this.yol = [ '일','월','화','수','목','금','토' ];
    this.mon = [31,28,31,30,31,30,31,31,30,31,30,31];
    this.Day;
    this.Year;
    this.Month;
    this.Yoil;
    this.fd;
    this.ld;
    this.curMonth = true;
    this.NowDate;
    this.Element = target; 
    this.Target = $(target);
};
Diary.prototype = {
 
    newDate : function(){        
        if(this.Target.val() == ''){this.D=new Date();}
        else{this.D=new Date(this.Target.val().split('-')[0], parseInt((this.Target.val().split('-')[1].substring(0,1) == '0' ? this.Target.val().split('-')[1].substring(1,2) : this.Target.val().split('-')[1])) - 1, this.Target.val().split('-')[2]);}
         
        this.NowDate = this.D; //현재 날짜 복사해서 넣어둠
        this.Year=this.D.getFullYear(); //년
        this.Month=this.D.getMonth(); //월
        this.Day=this.D.getDate(); //일
        this.fd=new Date(this.Year,this.Month,1).getDay(); //첫 요일
        this.ld=new Date(this.Year,this.Month+1,0).getDay(); //끝 요일
    },

    confirmCurDay : function(){
        if(this.Year == this.NowDate.getFullYear() && this.Month == this.NowDate.getMonth()) return true;
        else return false;
    },
    nextDate : function(){
        if(this.Month == '11') {
            this.Year++;
            this.Month = 0;  
        }
        else {
            this.Month++;
        }
        this.fd=new Date(this.Year,this.Month,1).getDay(); //첫 요일
        this.ld=new Date(this.Year,this.Month+1,0).getDay(); //끝 요일
    },
    prevDate : function(){
        if(this.Month == "0") {
            this.Year--;
            this.Month = 11;
        }
        else {
            this.Month--;
        }
        this.fd=new Date(this.Year,this.Month,1).getDay(); //첫 요일
        this.ld=new Date(this.Year,this.Month+1,0).getDay(); //끝 요일
    },
    prevYearDate : function(){         
        this.Year--;
        this.fd=new Date(this.Year,this.Month,1).getDay(); //첫 요일
        this.ld=new Date(this.Year,this.Month+1,0).getDay(); //끝 요일 
    },
    prevYearDiary : function(){
        this.prevYearDate();
        this.printDiary();
    },
    nextYearDate : function(){
        this.Year++;
        this.fd=new Date(this.Year,this.Month,1).getDay(); //첫 요일
        this.ld=new Date(this.Year,this.Month+1,0).getDay(); //끝 요일 
    },
    nextYearDiary : function(){
        this.nextYearDate();
        this.printDiary();
    },
    curDiary : function(){
        this.newDate();
        this.printDiary();
    },
    nextDiary : function(){
        this.nextDate();
        this.printDiary();
    },
    prevDiary : function(){
        this.prevDate();
        this.printDiary();
    },
    TopPosition : function(){
        var top = 0;
        var _target = this.Element;
        if(_target.offsetParent) {
            while(1) {
                top += _target.offsetTop;
                if(!_target.offsetParent) break;
                _target = _target.offsetParent;
            }
        }
        else if(_target.y) top += _target.y;
        return top; 
    },
    LeftPosition : function() {
        var left = 0;
        var _target = this.Element;
        if(_target.offsetParent) {
            while(1) {
                left += _target.offsetLeft;
                if(!_target.offsetParent) break;            
                _target = _target.offsetParent;
            }
        }
        else if(_target.x) left += _target.x;
        return left - 20;
    },    
    printDiary : function(){
         
        if($('#diary').attr('id') != undefined) $('#diary').empty();
        else $('body').append('<div id="diary" class="diary">').focus();        
        
        tag = "";
        tag += "<ul>";
        tag += "     <li>";
        tag += "         <img src=\"/images/button/prev_d.gif\" alt=\"\" class=\"next\" onclick=\"diary.prevYearDiary();\" />";
        tag += "         &nbsp;<label>"+this.Year+"</label>&nbsp;"; 
        tag += "         <img src=\"/images/button/next_d.gif\" alt=\"\" class=\"next\" onclick=\"diary.nextYearDiary();\" />&nbsp;&nbsp;&nbsp;";
        tag += "         <img src=\"/images/button/prev_d.gif\" alt=\"\" class=\"next\" onclick=\"diary.prevDiary();\" />";
        tag += "         &nbsp;<label style=\"width:100px;\">"+((this.Month+1).toString().length == 1 ? '0' : '') + (this.Month+1)+"</label>&nbsp;";
        tag += "         <img src=\"/images/button/next_d.gif\" alt=\"\" class=\"next\" onclick=\"diary.nextDiary();\" />&nbsp;&nbsp;&nbsp;";
        tag += "         <img src=\"/images/button/close_d.gif\" alt=\"\" class=\"next\" onclick=\"$('#diary').remove();\" />"; 
        tag += "     </li>";
        tag += "     <li style=\"background-color:#f6f6f6; margin-top:5px;\">";
        tag += "<table cellpadding=\"0\" cellspacing=\"0\">";
        tag += "     <tbody>";
        tag +="          <tr>";
        var cnt=0;
        for(j=0;j<this.yol.length;j++)
        {
            if (j==0) { tag+="<th style='color:red;'>"+this.yol[j]+"</th>"; }
            else if(j==6) { tag+="<th style='color:blue;'>"+this.yol[j]+"</th>"; }
            else { tag+="<th>"+this.yol[j]+"</th>"; }
        } 
        tag += "         </tr>";
        tag += "         <tr>";
        for(i=0;cnt<this.mon[this.Month];i++)
        {
            if(i % 7 == 0 && i >= 7) tag+="</tr><tr>";
            if(i<this.fd) tag+="<td style=\"cursor:default;\">&nbsp;</td>";
            else 
            {
                cnt++;
                tag += "<td valign=middle onclick=\"";
                tag += "$('#"+ this.Target.attr('id') +"').val('" + this.Year + "-" + ((this.Month+1).toString().length == 1 ? '0' : '') + (this.Month+1) + "-" + ((cnt).toString().length == 1 ? '0' : '') + (cnt) + "');";
                tag += "$('#diary').remove();\"";
                if(cnt==this.Day && this.confirmCurDay()) tag+="class=\"today\"";
                else { 
                    if(i%7==0) tag+="class=\"sunday\"";
                    else if(i%7==6) tag+="class=\"tuesday\"";
                }
                tag += ">"+cnt+"</td>"; 
            }
        }
        tag += "         </tr>";
        tag += "     </tbody>";
        tag += "</table>";
        tag += "     </li>"; 
        tag += "</ul>"; 
        $('.diary').css({top : this.TopPosition() + 20 , left : this.LeftPosition()}).html(tag);
    }
}



// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
    var top = 0;
    if(target.offsetParent) {
        while(1) {
            top += target.offsetTop;
            if(!target.offsetParent)
                break;
            target = target.offsetParent;
        }
    }
    else if(target.y)
        top += target.y;    
    return top;
}



// build out the divs, set attributes and call the fade function //
WriteMsg = function()
{
    this.MSGTIMER = 35;
    this.MSGSPEED = 5;
    this.MSGOFFSET = 3;
    this.MSGHIDE = 3;
};
WriteMsg.prototype = {
    inlineMsg : function(target,string,autohide) {
        var self = this;   
        var msg;
        var msgcontent;
        if(!document.getElementById('msg')) {
            msg = document.createElement('div');
            msg.id = 'msg';
            msgcontent = document.createElement('div');
            msgcontent.id = 'msgcontent';
            document.body.appendChild(msg);
            msg.appendChild(msgcontent);
            msg.style.filter = 'alpha(opacity=0)';
            msg.style.opacity = 0;
            msg.alpha = 0;
        }
        else {
            msg = document.getElementById('msg');
            msgcontent = document.getElementById('msgcontent');
        }
        msgcontent.innerHTML = string;
        msg.style.display = 'block';
        var msgheight = msg.offsetHeight;
        var targetdiv = document.getElementById(target);
        targetdiv.focus();
        var targetheight = targetdiv.offsetHeight;
        var targetwidth = targetdiv.offsetWidth;
        var topposition = this.topPosition(targetdiv) - ((msgheight - targetheight) / 2);
        var leftposition = this.leftPosition(targetdiv) + targetwidth + this.MSGOFFSET;         
        msg.style.top = topposition + 'px';
        msg.style.left = leftposition + 'px';
        clearInterval(msg.timer);
        msg.timer = setInterval(function(){self.fadeMsg(1)}, this.MSGTIMER);
        if(!autohide) {
            autohide = this.MSGHIDE;  
        }
        window.setTimeout(function(){self.hideMsg()}, (autohide * 1000));
    },
        
    hideMsg : function(msg){
        var self = this; 
        var msg = document.getElementById('msg');
        if(!msg.timer) {
            msg.timer = setInterval(function(){self.fadeMsg(0)}, this.MSGTIMER);
        } 
    }, 
    fadeMsg : function(flag){
        var self = this; 
        if(flag == null) {flag = 1;}
        var msg = document.getElementById('msg');
        var value;
        if(flag == 1) {value = msg.alpha + this.MSGSPEED;} 
        else {value = msg.alpha - this.MSGSPEED;}
        
        msg.alpha = value;
        msg.style.opacity = (value / 100);
        msg.style.filter = 'alpha(opacity=' + value + ')';
        if(value >= 99) {
            clearInterval(msg.timer);
            msg.timer = null;
        }
        else if(value <= 1) {
            msg.style.display = "none";
            clearInterval(msg.timer);
        }
    },
    topPosition : function (target) {
        var top = 0;
        if(target.offsetParent) {
            while(1) {
                top += target.offsetTop;
                if(!target.offsetParent)
                    break;
                target = target.offsetParent;
            }
        }
        else if(target.y)
            top += target.y;    
        return top;
    },
    leftPosition : function(target) {
        var left = 0;
        if(target.offsetParent) {
            while(1) {
                left += target.offsetLeft;
                if(!target.offsetParent)
                    break;            
                target = target.offsetParent;
            }
        }
        else if(target.x)
            left += target.x;
        return left;
    } 
}

