﻿
var changeRCTab = function(parent, object, seal_idx, getShow , hoon_idx) {
    if (!seal_idx) seal_idx = -1;
    s_count = $('#rcContent ul li').size();
    
    switch (object) {
        case 'rcTabMySeal':            
            if (_isLoginjs == 'false') {
                alert('Please login first.');
                return;
            }
        case 'rcTabFirst':
            if (s_count < 1) {
                alert('Please, select the Seals first.');
                return;
            }
            break;
    }
    
    // 탭 관련 처리 
    var line = document.getElementById('rcLine');
    $('#' + parent + ' li').removeClass('select');
    $('#' + object).addClass('select');
    // 
    $('#rcMainSeal').hide();
    $('#rcContent').hide();
    $('#rcDesign').hide();
    $('#rcLoader').hide();
    $('#rcMySeal').hide();
    //
    $('#saveRemote').hide();
    $('#getRemote').hide();
    //

    switch (object) {
        case 'rcTabAdd':
            // 담기빽
            line.className = 'line2';
            $('#rcContent').show();
            RCSealList(seal_idx,hoon_idx);
            // 실이 올바르게 추가되었을 경우에만
            if (s_count != 0 || getShow)
                $('#saveRemote').show();
            else
                $('#addYourSeal').show();
            break;
        case 'rcTabMySeal':
            // 나의 실
            line.className = 'line1';
            $('#rcMySeal').show();
            loadSealList(-1, s_count);
            break;
        case 'rcTabFirst':
            // 대표실
            line.className = 'line3';
            loadSealList('', s_count);
            $('#rcMainSeal').show();
            if (s_count == 0) $('#addYourSeal').show();
            else $('#getRemote').show();
            break;
    }

};

var RCSealList = function(seal_idx,hoon_idx) {
    if(!hoon_idx) hoon_idx = '';
    if (seal_idx != -1) {
        // ajax로 session에 넣고 리턴한 결과 값을 삽입한다.
        $.getJSON("/_comm/_addSeal.aspx", { seal_idx: seal_idx , hoon_idx : hoon_idx}, function(json) {
            errno = json.errno;
            if (errno == 2) {
                alert('This Seal had been already added.');
                return;
            }
            else if (errno != 0) {
            alert('Failure to add the Seal.');
                return;
            }
            RCMakeSeal(json.seal_idx, json.sealImg);
            //$('#getRemote').show();
        });
    }
}

var rcAddSeal = function(seal_idx,hoon_idx) {
    // 실이 한개도 없을 때 
    // 가져가기 버튼 
    s_count = $('#rcContent ul li').size();
    if (s_count == 0) {
        $('#addYourSeal').hide();
        $('#saveRemote').show();
        $('#help_popup').show();
    }
    changeRCTab('rcTab', 'rcTabAdd', seal_idx,true,hoon_idx);
}

var rcDelSeal = function(seal_idx) {
    $.getJSON('/_comm/_delSeal.aspx', { seal_idx: seal_idx }, function(json) {
        if (json.errno != 0) {
            alert(json.errno + 'Failure to add the Seal.');
            return;
        }
        $('div#seal_' + seal_idx).parent().remove();
        //
        s_count = $('#rcContent ul li').size();
        if (s_count < 1) {
            $('#addYourSeal').show();
            $('#saveRemote').hide(); 
            $('#help_popup').hide(); 
        }
    }
    );

}


// javascript 가 서버에 요청을 해서 현재 선택된 실 리스트를 가져온다
var loadSealList = function(blog_addr_hash, s_count) {
    if (!blog_addr_hash || blog_addr_hash == '') blog_addr_hash = '';
    // 로더 On
    if (blog_addr_hash == -1) $('#rcMySeal ul').html('');
    else $('#rcMainSeal ul').html('');
    if (s_count != 0) $('#rcLoader').show();

    $.post("/_comm/_getSessSealList.aspx", { blog_addr_hash: blog_addr_hash }, function(json) {
        var fJson = eval(json);
        var count = 0;
        if (fJson.length != null) {
            count = fJson.length;
            for (i = 0; i < count; i++) {
                data = fJson[i].seal_idx;
                if (blog_addr_hash == -1) {
                    RCMakeMySeal(data, fJson[i].sealImg, fJson[i].isMain);
                }
                else {
                    RCMakeSelectedSeal(data, fJson[i].sealImg, fJson[i].isMain);
                    if (fJson[i].isMain == 'True') {
                        main_seal_idx = data;
                    }
                }
            }
        }
        // 로더 Off
        $('#rcLoader').hide();

        if (blog_addr_hash == -1 && count > 0) {
            //$('#saveRemote').show();
        }
        else if (count > 0) {
            $('#getRemote').show();
        }
    }, 'JSON');

}


var RCMakeSeal = function(seal_idx, sealImg) {
    var joneText = '<li><div id="seal_' + seal_idx + '" class="seal"><img src="/images/seal/' + sealImg + '" width="50" height="50" alt="resealMote"/></div>';
    joneText += '<div class="button"><a href="#del" onclick="rcDelSeal(\'' + seal_idx + '\');return false"><img src="/images/common/btn_minus.png" width="23" height="24" alt="Delete"/></a></div>';
    joneText += '</li>';
    $('#rcContent ul').append(joneText);
    setPngforIE6($('#rcContent ul'));
}
var RCMakeSelectedSeal = function(seal_idx, sealImg,isMain) {
    var joneText = '<li onMouseOver="showMain(\'' + seal_idx + '\');" onMouseOut="hideMain(\'' + seal_idx + '\')"><div id="seal_' + seal_idx + '" class="seal"><a href="#" onclick="setSelectSeal(\'' + seal_idx + '\');return false"><img src="/images/seal/' + sealImg + '" width="50" height="50"/></a></div>';
    if (isMain == 'True')
        joneText += '<div onclick="setSelectSeal(\'' + seal_idx + '\');return false"  id="sseal_' + seal_idx + '" class="s_button"><a href="#" onclick="setSelectSeal(\'' + seal_idx + '\');return false"><img src="/images/remote/icon_crown.png" width=\"24\" height=\"26\" alt="MainSelect"/></a></div>';
    else
        joneText += '<div onclick="setSelectSeal(\'' + seal_idx + '\');return false"  id="sseal_' + seal_idx + '" class="button"><a href="#" onclick="setSelectSeal(\'' + seal_idx + '\');return false"><img src="/images/remote/icon_crown.png" width=\"24\" height=\"26\" alt="MainSelect"/></a></div>';
    joneText += '</li>';
    $('#rcMainSeal ul').append(joneText);
    setPngforIE6($('#rcMainSeal ul'));
}

var RCMakeMySeal = function( seal_idx , sealImg ,isMain) 
{
    var joneText = '<li><div id="seal_' + seal_idx + '" class="seal"><img src="/images/seal/' + sealImg + '" width="50" height="50"/></div>';
    if(isMain == 'True')
        joneText += '<div id="sseal_' + seal_idx + '" class="s_button"><img width=\"24\" height=\"26\" src="/images/remote/icon_crown.png" alt="MainSelect"/></div>';
    joneText += '</li>';
    $('#rcMySeal ul').append(joneText);
    setPngforIE6($('#rcMySeal ul'));
}

var showMain = function(seal_idx) {    
    $('#rcMainSeal ul li div#sseal_' + seal_idx).show();
}
var hideMain = function(seal_idx) {
    if (main_seal_idx != seal_idx)
    $('#rcMainSeal ul li div#sseal_' + seal_idx).hide();
}
var setSelectSeal = function(seal_idx) {
    $.getJSON('/_comm/_setSelectedSeal.aspx', { seal_idx: seal_idx }, function(json) {
        if (json.errno != 0) {
            alert('Failure to change your Crown Seal.');
            return;
        }
        $('#rcMainSeal ul li div.button').hide();
        $('#rcMainSeal ul li div.s_button').hide();
        $('#rcMainSeal ul li div#sseal_' + json.seal_idx).show();
        main_seal_idx = json.seal_idx; 
    }
    );
}

var setSealList = function() {
    // 나의 실이 보이게 하고
    // 로그인 및 갯수 체크
    s_count = $('#rcContent ul li').size();
    if (s_count < 1) {
        alert('Please, select the Seals first.');
        return false;
    }
    $.getJSON('/_comm/_isSpecialUser.aspx', '', function(json) {

        if (json.errno == 1) {
            if (s_count > 9) {
                alert('The number of your Seals exceeds 9.');
                return false;
            }
        } else if (json.errno == 0) {
            if (s_count > 18) {
                alert('The number of your Seals exceeds 18.');
                return false;
            }
        } else if (json.errno == 2) {

        } else {
            if (s_count > 9) {
                alert('The number of your Seals exceeds 9.');
                return false;
            }
        }
        // 로그인 체크
        $.getJSON('/_comm/_isLogin.aspx', '', function(json) {
            // 조건문 순서랑 회원 가입 페이지로의 이동 처리
            if (json.errno == 2) {
                alert('Please, select the Seals first.');
                return;
            } else if (json.errno == 3) {
            alert('Please crown a Seal that represents you best.');
                changeRCTab('rcTab', 'rcTabFirst');
                return;
            } else if (json.errno == 1) {
                alert('Please login first.');
                document.location = '/join.aspx?getseal=true';
                return;
            }
            // save
            $.getJSON('/_comm/_saveSealList.aspx', '', function(json) {
                if (json.errno != 0) {
                    alert('Save failure. Please try again.');
                    return;
                }                
                alert('Seal information has been updated.');
            });
            changeRCTab('rcTab', 'rcTabMySeal');
        });

    });


}

var saveSealList = function() {
    $.getJSON('/_comm/_isLogin.aspx', '', function(json) {
        alert(json.errno);
        if (json.errno == 1) {
            alert('Please login first.');
            return;
        }
        else if (json.errno == 2) {
            alert('Please, select the Seals first.');
            return;
        } else if (json.errno == 3) {
            alert('Please crown a Seal that represents you best.');
            changeRCTab('rcTab', 'rcTabFirst');
            return;
        }
        // save
        $.getJSON('/_comm/_saveSealList.aspx', '', function(json) {
            if (json.errno != 0) {
                alert('Save failure. Please try again.');
                return;
            }
            //            
            location.href = '/HTML/myPage/MySeal.aspx';
        });

    });
}

var saveSealListWale = function() {
    $.getJSON('/_comm/_isLogin.aspx', '', function(json) {
        if (json.errno == 1) {
            alert('Please login first.');
            return;
        }
        else if (json.errno == 2) {
        alert('Please, select the Seals first.');
            return;
        } else if (json.errno == 3) {
        alert('Please crown a Seal that represents you best.');
            changeRCTab('rcTab', 'rcTabFirst');
            return;
        }
        // save
        $.getJSON('/_comm/_saveSealList.aspx', '', function(json) {
            if (json.errno != 0) {
                alert('Save failure. Please try again.');
                return;
            }
            //location.href = '/HTML/myPage/MySeal.aspx';
        });

    });
}

var changeDesign = function(dval) {

    var deVal = $('#designSel').val();
    // 이미지로드 

    var loadImage = Array('shot_blog_gray', 'shot_blog_blue', 'shot_blog_yellow', 'shot_blog_green', 'shot_blog_pink', 'shot_blog_purple');
    $('#rcDesignImg').attr('src', '/images/remote/' + loadImage[deVal] + '.png');

    $.getJSON('/_comm/_setDeign.aspx', { Design: deVal }, function(json) {
        if (json.errno != 0) {
            alert('Save failure. Please try again.');
            return; 
        }
    });
}

/* 배너를 주기적으로 켰다가 껐다.*/
var HelpPopupToggle = function() {
    if (_RemoteGetCloseBut)        return; 
    s_count = $('#rcContent ul li').size();
    if (s_count > 0) {
        $('#help_popup').toggle('slow');
    }
    //help_popup
}

/* 배너를 클릭하면 쿠키를 발급 */
var SetCookiePopupClose = function() {
    // 쿠기가 있으면 안켠다.
    _RemoteGetCloseBut = true;
    setCookie("SealClosePop", "true", 999);
}