﻿
var Main = {
    RememberID : false,
    SelectLogin : function(pID)
    {
	    $('RevUIDLogin').setStyle({display:'none'});
	    $('OpenIDLogin').setStyle({display:'none'});
	    $(pID + 'Login').setStyle({display:'inline'});
    	
	    if(pID == 'RevUID') {
            $('openid_type').setStyle({backgroundImage:'url(/images/common/ico_radio_off.gif)'});
            $('revuid_type').setStyle({backgroundImage:'url(/images/common/ico_radio_on.gif)'});
        }
        else if(pID == 'OpenID') {
            $('openid_type').setStyle({backgroundImage:'url(/images/common/ico_radio_on.gif)'});
            $('revuid_type').setStyle({backgroundImage:'url(/images/common/ico_radio_off.gif)'});
        }    

        $('Main_LoginType').value = pID;
        $(pID.toLowerCase()).focus();
        $(pID.toLowerCase()).style.backgroundColor='#ffffff';
        
        $('Login').setStyle({backgroundImage:'url(/images/login/logout_' + pID + '_bg.gif)'});
    },
    SelectLogin2 : function(pID)
    {
	    $('RevUIDLogin').setStyle({display:'none'});
	    $('OpenIDLogin').setStyle({display:'none'});
	    $(pID + 'Login').setStyle({display:'inline'});
    	
	    if(pID == 'RevUID') {
            $('openid_type').setStyle({backgroundImage:'url(/images/common/ico_radio_off.gif)'});
            $('revuid_type').setStyle({backgroundImage:'url(/images/common/ico_radio_on.gif)'});
        }
        else if(pID == 'OpenID') {
            $('openid_type').setStyle({backgroundImage:'url(/images/common/ico_radio_on.gif)'});
            $('revuid_type').setStyle({backgroundImage:'url(/images/common/ico_radio_off.gif)'});
        }    

        $('Main_LoginType').value = pID;
        $(pID.toLowerCase()).focus();
        $(pID.toLowerCase()).style.backgroundColor='#ffffff';
    },    
    RevuLogin : function() {
        var userid = $('revuid').value;
        var openid = $('openid').value;
        var password = $('pass').value;
        var result = "";

        if ($('Main_LoginType').value == 'RevUID')
        {
            if(userid == "") {
                alert("회원 아이디를 입력해주세요.");
                return;
            }

            if(password == "") {
                alert("비밀번호를 입력해주세요.");
                return;
            }
            result = Opinity.Ajax.Common.Ajax_LoginRevUID(userid, password, this.RememberID).value;
        }
        else if ($('Main_LoginType').value == 'OpenID')
        {
            if(openid == "") {
                alert("오픈 아이디를 입력해주세요.");
                return;
            }

            var result = Opinity.Ajax.AjaxOpenID.OpenIDLogin(openid, this.RememberID).value;

            if (result.ResultCode == 0)
            {
                alert(result.ReturnValue);
                return;
            }
            else
            {
                location.href = result.ReturnValue;
                
                return;
            };
        }        

        if(result.ResultCode == 0) alert(result.Message);
        else
        {
            document.location.reload();
        }
    },
    Logout : function() {
        Opinity.Ajax.Common.Ajax_Logout();
        document.location.reload();
        return;
    },
    Go : function(target) {
        if(target == "Item") 
            document.location.href = "/Items";
        else if(target == "Review")
            document.location.href = "/Reviews";
        else if(target == "ThemeTalk")
            document.location.href = "/ThemeTalk";  
        else if(target == "WishPoll")
            document.location.href = "/WishPoll";
        else if(target == "Collection")
            document.location.href = "/Items/Collection";
        else if(target == "MyRevU")
        {
            var login = Opinity.Ajax.Common.Ajax_GetLoginInfo().value;

            if( login.Login == null )
            {
                alert("로그인해주세요.");
                return;
            }
            else
            {
                if (login.Login.Nickname != "")
                    document.location.href = "/MyRevU/" + login.Login.Nickname;
                else
                {
                    alert("로그인 해 주세요.");
                    return;
                };
            }
        }
        else if (target == "Help")
            document.location.href="/Guide/Help";
        else if (target == "Frontier")
            document.location.href="/Frontier";
            
            return;
    },
    ShowWaitMessage : function(id) {
        if($(id))
            $(id).setStyle({filter:'alpha(opacity=50)','-moz-opacity':'50%','opacity':'0.5'});
    },
    GetItemRanking : function(page)
    {
        Opinity.Controls.Main.CtlItemRanking.GetItemRanking(page, 5, "Main_ItemRanking", Util.AjaxCallback);
    },
    GetFrontierReview : function(FrontierID)
    {
        this.ShowWaitMessage("Main_FrontierReview");
        MasterPage_Main.GetFrontirReview(FrontierID, "Main_FrontierReview", Util.AjaxCallback);
    },
    GetTalkNow : function(Category, PageNo)
    {
        this.ShowWaitMessage("Main_FrontierReview");
        Index.GetTalkNow(Category, PageNo, 8, "Main_TalkNow", Util.AjaxCallback);
    },
    Event : new Array(),
    EventTimer : null,
    EventOn : true,
    EventIndex : 0,
    ShowEvent : function(index) {
        if(index > this.Event.length) index = 1;
        this.EventIndex = index;
        
        if($('EventButton' + index).src.match(/off/gi) != null) {
            for(i=1;i<=this.Event.length;i++) {
                if(i != index)  
                   $('EventButton' + i).src = $('EventButton' + i).src.replace('_on','_off');
                else
                   $('EventButton' + i).src = $('EventButton' + i).src.replace('_off','_on');            
            }
        
            $('Main_Event_Middle').update(this.Event[index - 1]);
        }
    },   
    LoadEvent : function(index) {
        if(index > this.Event.length) index = 1;
        this.EventIndex = index;

        this.ShowEvent(index);
        
        if(this.EventOn)
            this.EventTimer = setTimeout("Main.LoadEvent(" + (index + 1) +")", 5000);
        else
            clearTimeout(this.EventTimer);
    },
    GoEvent : function(idx) {
        if(idx == 0) { 
            this.EventOn = !this.EventOn;
            if(this.EventOn) $('BannerStop').setStyle({background:'none'});
            else $('BannerStop').setStyle({background:'url(/images/main/icon_bn_stop.gif) no-repeat'});
        }
        else {
            this.EventIndex = this.EventIndex + idx;
            if(this.EventIndex > this.Event.length) index = 1;
            else if(this.EventIndex < 0) this.EventIndex = this.Event.length;
        }
        
        this.LoadEvent(this.EventIndex);
    },
    GoMyRevu : function(pValue)
    {
        if (pValue == "" || pValue == "revu") return;
        location.href="/MyRevU/" + pValue;
    },
    profile_over : function()
    {
        $('profile_border').setStyle({background:'url(/Images/Main/main_profile_border.gif) no-repeat'});
    },
    profile_out : function()
    {
        $('profile_border').setStyle({background:''});
    },
    LeftBannerNo : 0,
    LeftBanner : new Array(),
    LoadLeftEventBanner : function()
    {        
        if(this.LeftBanner.length > 0) {
            this.LeftBannerNo++;         
            if(this.LeftBannerNo == this.LeftBanner.length) this.LeftBannerNo = 0;
            $('main_left_event').innerHTML = this.LeftBanner[this.LeftBannerNo];
       }
        
        setTimeout("Main.LoadLeftEventBanner()", 5000);
    },
    CheckRememberID : function() {
        if(this.RememberID == true) {
            this.RememberID = false;
            $('remember_label').setStyle({background:'url(/images/myrevu/ico_unchecked.gif) no-repeat'});
            $('remember_label2').setStyle({background:'url(/images/myrevu/ico_unchecked.gif) no-repeat'});
        }
        else {
            this.RememberID = true;
            $('remember_label').setStyle({background:'url(/images/myrevu/ico_checked.gif) no-repeat'});
            $('remember_label2').setStyle({background:'url(/images/myrevu/ico_checked.gif) no-repeat'});
        }
    },
    ShowHelp : function(target) {
        var y = Util.Y;
        y = y - 100;
        if(y < 0) y = 0;
            
        if(target == 'Frontier')
            $('Layer_Help').setStyle({background:'url(/images/main/img_' + target + '.gif) no-repeat',display:'inline',height:'404px',top:y + 'px'});
        else
            $('Layer_Help').setStyle({background:'url(/images/main/img_' + target + '.gif) no-repeat',display:'inline',height:'344px',top:y + 'px'});
            
        if(target = 'BestReview')
            $('Layer_Help').setStyle({height:'448px'});

        if(target == 'Frontier') $('Layer_Help_MailTo').setStyle({display:'inline'});
        else $('Layer_Help_MailTo').setStyle({display:'none'});
    },
    FrontierID : null,
    FrontierTimer : null,
    Frontiers : null,
    FrontierIndex : 0,
    FrontierReverse : false,
    InitFrontier : function(ids) {
        this.Frontiers = ids.split('$');
        if(this.Frontiers.length > 0) { 
            this.ShowFrontier(this.Frontiers[0]);
            this.ScrollFrontiers();
        }
    },
    ShowFrontier : function(id, flag) {
        if(this.FrontierID != id) {
            $('ChoiceFT').setStyle({backgroundImage:'url(/images/event/frontier/' + id + '_main.jpg)'});
            $('MAIN-FRONTIER-' + id).setStyle({fontWeight:'bold',color:'#444444',backgroundImage:'url(/images/main/frontier_on.gif)',zIndex:'100'});
            $('MAIN-FRONTIER-' + id + '-A').setStyle({color:'#444444'});
            if(this.FrontierID != null) {
                $('MAIN-FRONTIER-' + this.FrontierID).setStyle({fontWeight:'normal',color:'#7d7d7d',backgroundImage:'none',zIndex:'90'});
                $('MAIN-FRONTIER-' + this.FrontierID + '-A').setStyle({color:'#7d7d7d'});
            }
            this.FrontierID = id;
            if(flag && this.FrontierTimer != null) {
                 clearTimeout(this.FrontierTimer);
                 for(i=0;i<this.Frontiers.length;i++) if(this.Frontiers[i] == id) { this.FrontierIndex = i; break;}
                 this.FrontierTimer = setTimeout('Main.ScrollFrontiers()', 10000);
            }
        }
    },
    ScrollFrontiers : function() {
        var PageSize = 5;;
        var Height = 23;
        
        if(this.FrontierTimer != null) {
            var top = 0;
            if($('Frontiers_Middle').style.marginTop) top = parseInt($('Frontiers_Middle').style.marginTop.replace('px',''));
            
            if(this.FrontierReverse) {
                this.FrontierIndex--;
                
                if(this.FrontierIndex < 0) {
                    this.FrontierReverse = false;
                    this.ScrollFrontiers();
                    return;
                }
                else if(Math.abs(top) > (this.FrontierIndex * Height))
                     $('Frontiers_Middle').setStyle({marginTop:top + Height + 'px'});
            }
            else {
                this.FrontierIndex++;
                
                if(this.FrontierIndex >= this.Frontiers.length) {
                    this.FrontierReverse = true;
                    this.ScrollFrontiers();
                    return;
                }
                else if(Math.abs(top) <= ((this.FrontierIndex - PageSize) * Height)) 
                    $('Frontiers_Middle').setStyle({marginTop:top - Height + 'px'});
            }          
            this.ShowFrontier(this.Frontiers[this.FrontierIndex], false);
        }
        this.FrontierTimer = setTimeout('Main.ScrollFrontiers()', 3000);
    },
    ViewBanner : function(id) {
        CtlBannerSubmain.GetEventBanner(id, 'R_SEvent', Util.AjaxCallback);
    },
    GetBestRecommenders : function(target) {
        CtlBestRecommenders.GetBestRecommenders(target, 'R_Bestmen', Util.AjaxCallback);
    },
    GetWeeklyBestReview : function(id) {
        CtlWeeklyBestReviews.GetWeeklyBestReviews(id, 'WeeklyBestReview', Util.AjaxCallback);
    },
    GetSectionReviews : function(n, id) {
        Opinity.Controls.Main.CtlSectionReviews.GetSectionReviews(n, id, 'ReviewSection' + n, Util.AjaxCallback);
    },
    GetFrontierReviews : function(FrontierID)
    {
        this.ShowWaitMessage("C_Frontier");
        Index.GetFrontirReviews(FrontierID, "C_Frontier", Util.AjaxCallback);
    },
    HotIssueSelectedIndex : -1,
    HotIssueItem : function(idx, url, imgurl) {
        if(this.HotIssueSelectedIndex != idx) {
            $('HotIssueImage').src = imgurl;
            $('HotIssueLink').href = url;
            $('ChoiceItem_Sb' + idx).setStyle({border:'solid 1px #E92525'});
            if(this.HotIssueSelectedIndex > -1)
                $('ChoiceItem_Sb' + this.HotIssueSelectedIndex).setStyle({border:'solid 1px #dddddd'});
            this.HotIssueSelectedIndex = idx;
        }
    },
    GetHotIssue : function(page) {
        Index.GetHotIssueItems(page, 5, "ChoiceItem_S", Util.AjaxCallback);
    }
}
