var NewsController = {
    currentState : 0,		
    currentCategoryID : 0,
    currentArticleIdx : 0,
    
    // UI state
    UILIST : 1,
    UICONTENTS : 2,
    UIPHOTOLIST : 3,
    UIPHOTOCONTENTS : 4,
    
    // Process
    LIST_CATEGORY_1 : 301,
    LIST_CATEGORY_2 : 302,
    LIST_CATEGORY_3 : 303,
    LIST_CATEGORY_4 : 304,
    LIST_CATEGORY_5 : 305,
    LIST_CATEGORY_6 : 306,
    LIST_CATEGORY_LEFT : 100,
    LIST_CATEGORY_RIGHT : 101,
    LIST_START_CONTENTS : 109,
    LIST_ARTICLE_UP : 102,
    LIST_ARTICLE_DOWN : 103,
    LIST_FIRST_TITLE_UP : 104,
    LIST_LAST_TITLE_DOWN : 105,
    
    CONTENTS_ARTICLE_LEFT : 200,
    CONTENTS_ARTICLE_RIGHT : 201,
    CONTENTS_SCROLL_UP : 202,
    CONTENTS_SCROLL_DOWN : 203,
    CONTENTS_RETURN : 204,

	SHOW_INFO : 500,

}

NewsController.create = function() {
    DataMgr.create();
    KeyHandler.create();
    UIList.create();
    UIContents.create();
}
NewsController.showLoader = function(){
	document.getElementById("UIPreloader").style.display = 'block';
}
NewsController.hideLoader = function(){
	document.getElementById("UIPreloader").style.display = 'none';
}
NewsController.start = function(categoryID) {
    this.currentCategoryID = categoryID;
    DataMgr.sendRequest(this.currentCategoryID, function (categoryID) {
        NewsController.showList();
		NewsController.hideLoader();
		document.getElementById("UISplashImageEnter").style.display = 'block';
    });
}

NewsController.showList = function() {
 this.currentState = this.UILIST;				// set current UI state
    UIList.setDataObj(DataMgr.getListData(this.currentCategoryID, this.currentArticleIdx));		// set data object
    UIList.setTitleIdx(this.currentArticleIdx%UIList.TITLE_MAX_NUM);	// set title index to be highlight
    UIList.adjustScrollBar(this.currentArticleIdx, DataMgr.getCategoryData(this.currentCategoryID).arrArticles.length);
    UIList.show();
//    alert("NewsController.showList() End");
}

NewsController.refreshList = function() {
    UIList.setDataObj(DataMgr.getListData(this.currentCategoryID, this.currentArticleIdx));
    UIList.setTitleIdx(this.currentArticleIdx%UIList.TITLE_MAX_NUM);
    UIList.adjustScrollBar(this.currentArticleIdx, DataMgr.getCategoryData(this.currentCategoryID).arrArticles.length);
    UIList.refresh();
}

NewsController.showContents = function() {
    this.currentState = this.UICONTENTS; 
    UIContents.setDataObj(DataMgr.getContentsData(this.currentCategoryID, this.currentArticleIdx));
	setTimeout("UIContents.adjustScrollBar()", 0);
    UIContents.show();
}

NewsController.changeCategory = function() {
    UIList.blurTitle(UIList.getTitleIdx());
    //alert(this.currentCategoryID);
    UIList.adjustScrollBar(this.currentArticleIdx, DataMgr.getCategoryData(this.currentCategoryID).arrArticles.length);
    this.refreshList();
	NewsController.hideLoader();
}

NewsController.request = function (REQUEST, option) {
    switch (REQUEST) {        
        // UIList
        case this.LIST_CATEGORY_LEFT:
            this.currentArticleIdx = 0;
            if (--this.currentCategoryID < 0) 
                this.currentCategoryID = DataMgr.getCategoryList().length - 1;
            if (DataMgr.getCategoryData(this.currentCategoryID) 
            && DataMgr.getCategoryData(this.currentCategoryID).bReady) {
                this.changeCategory();
            }
            else {
                DataMgr.sendRequest(this.currentCategoryID, function (categoryID) {
                    NewsController.changeCategory();
                });
            }
            break;
        case this.LIST_CATEGORY_RIGHT:
            this.currentArticleIdx = 0;
            if (++this.currentCategoryID > DataMgr.getCategoryList().length - 1) 
                this.currentCategoryID = 0;
            if (DataMgr.getCategoryData(this.currentCategoryID) 
            && DataMgr.getCategoryData(this.currentCategoryID).bReady) {
                this.changeCategory();
            }
            else {
                DataMgr.sendRequest(this.currentCategoryID, function (categoryID) {
                    NewsController.changeCategory();
                });
            }
            break;
        case this.LIST_START_CONTENTS:
            UIList.hide();
            this.showContents();				
            break;
        case this.LIST_ARTICLE_UP:
            var totalArticleCount = DataMgr.getCategoryData(this.currentCategoryID).arrArticles.length;
            if (--this.currentArticleIdx < 0) {
                this.currentArticleIdx = totalArticleCount - 1;
            }
            UIList.articleUp();
            break;
        case this.LIST_ARTICLE_DOWN :
            var totalArticleCount = DataMgr.getCategoryData(this.currentCategoryID).arrArticles.length;
            if (++this.currentArticleIdx > totalArticleCount - 1) {
                this.currentArticleIdx = 0;
            }
            UIList.articleDown();
            break;
        case this.LIST_FIRST_TITLE_UP:
            UIList.setDataObj(DataMgr.getListData(this.currentCategoryID, this.currentArticleIdx));
            UIList.setTitleIdx(UIList.getLastTitleIdx());
            UIList.adjustScrollBar(this.currentArticleIdx, DataMgr.getCategoryData(this.currentCategoryID).arrArticles.length);
            UIList.refresh();
            break;
        case this.LIST_LAST_TITLE_DOWN:
            UIList.setDataObj(DataMgr.getListData(this.currentCategoryID, this.currentArticleIdx));
            UIList.setTitleIdx(this.currentArticleIdx%UIList.TITLE_MAX_NUM);
            UIList.adjustScrollBar(this.currentArticleIdx, DataMgr.getCategoryData(this.currentCategoryID).arrArticles.length);
            UIList.refresh();
            break;

        // UIContents
        case this.CONTENTS_ARTICLE_LEFT:
            if (--this.currentArticleIdx < 0) {
                this.currentArticleIdx = DataMgr.getCategoryData(this.currentCategoryID).arrArticles.length -1;
            }
            UIContents.setDataObj(DataMgr.getContentsData(this.currentCategoryID, this.currentArticleIdx));
			setTimeout("UIContents.adjustScrollBar()", 0);
            UIContents.refresh();
            break;
        case this.CONTENTS_ARTICLE_RIGHT:
            if (++this.currentArticleIdx > DataMgr.getCategoryData(this.currentCategoryID).arrArticles.length - 1) {
                this.currentArticleIdx = 0;
            }
            UIContents.setDataObj(DataMgr.getContentsData(this.currentCategoryID, this.currentArticleIdx));
			setTimeout("UIContents.adjustScrollBar()", 0);
            UIContents.refresh();
            break;
        case this.CONTENTS_SCROLL_UP:
            setTimeout("UIContents.adjustScrollBar()", 0);
            break;
        case this.CONTENTS_SCROLL_DOWN:
            setTimeout("UIContents.adjustScrollBar()", 0);
            break;
        case this.CONTENTS_RETURN:
            UIContents.hide();
            this.showList();
            break;
        case this.SHOW_INFO:
            UIList.showInfo();
        default:
            break;
    }
}
