﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference name="/js/common.js"/>

// *************************** ARTICLE ONLY FUNCTIONS *************************** //

var userTriggeredArticle = false;

function onPageStateChanged(hash) {

    var articleId = -1;
 
    if (hash.indexOf("=") > 0) {
        var pair = hash.split("=");
        if (pair && pair.length == 2 && pair[0] == "a" && parseInt(pair[1],10)>-1) {
            articleId=parseInt(pair[1],10);
        }
    }

    if (articleId > -1 && cat > -1) {
        if (userTriggeredArticle == false) {
            API.get_articleDetails(articleId, cat, true, true, get_articleDetails_ServerReturn, OnError_ServerReturn);
        } else {
            userTriggeredArticle = false;
        }
        
    } else {
    
        //find current Tab text and use it for document title
        var activeTab = $("span.ajax__tab_active span span span");
        if (activeTab.length > 0) {
            document.title = String.format("Roland Articles: {0}", activeTab[0].innerHTML);
        }
        
        //find any visible full article in a visible tabbed container, if no tabs get first visible full article
        var divFullArticle = $(".ajax__tab_panel div.content div.singlearticle:visible")
        if (divFullArticle.length == 0) {
            divFullArticle = $("div.singlearticle:visible[id!='']");
        }
        
        if (divFullArticle.length > 0 ) { 

            var userControlId = divFullArticle[0].id.substring(0,divFullArticle[0].id.lastIndexOf("_"));

            //hide links
            var links = $("#" + userControlId + "_articleLinks");
            if(links.length>0) {
                links[0].style.display = "none";
            }

            //hide full article and show article list
            var list = $("#" + userControlId + "_dlProductList");
            if (list.length > 0) {
                divFullArticle[0].style.display="none";
                list[0].style.display = "";
            }
        }
    }

    //get banner for article or category
    API.get_banner("sidepromo: || sidepromo-RCA:",articleId == -1 ? null : articleId, cat == -1 ? null : cat, get_banner_ServerReturn,get_bannerError_ServerReturn);

}
function get_banner_ServerReturn(bannerHtml) {
    if (bannerHtml.length > 0 && bannerHtml.indexOf("no parameters supplied!") == -1) {
        $get("sidePromoHolder").innerHTML = bannerHtml;
        $get("sidePromoHolder").style.display="block";
        
        var swfCallStart = bannerHtml.indexOf("var so");
        if (swfCallStart > 0) {
            var swfCallEnd = bannerHtml.indexOf("/* ]]> */");        
            var scrpt = bannerHtml.substring(swfCallStart,swfCallEnd);
            eval(scrpt); //call SWFObject to write flash object
        }
    } else {
        $get("sidePromoHolder").style.display="none";
    }

    //Fix IE6+ bug that appends # to document title after flash injection
    if ($.browser.msie && document.title.indexOf('#') > -1) { 
        document.title = document.title.substring(0,document.title.indexOf('#')); 
    }
    
}
function get_bannerError_ServerReturn() {
   // alert('could not get banner');
}

function getArticle(articleId) {
    pageBusy.show("topOfList");
    userTriggeredArticle = true;
    API.get_articleDetails(articleId, cat, true, true, get_articleDetails_ServerReturn, OnError_ServerReturn);
}
function get_articleDetails_ServerReturn(ArticleDetails) {
    pageBusy.hide("topOfList");

    //We need to automatically select any tab that contains the article.

    //use jQuery to find all links in article summary 
    var links = $("table.articlesummarylist a");
    for (i = 0; i < links.length; i++) {

        // if the link's onclick attribute contains the given ArticleDetails.ArticleId
        var linkUrl = links[i].getAttributeNode("onclick");
        if ( linkUrl && linkUrl.nodeValue && linkUrl.nodeValue.startsWith(String.format("javascript:getArticle({0})",ArticleDetails.ArticleId)) ) {

            //find the user control ID prefix containing the article summary list 
            var ucIdStartPos = links[i].id.indexOf("_ucArticles") + 11;
            var currUcId = links[i].id.substring(0,links[i].id.indexOf("_",ucIdStartPos));

            //if the article summary list is further divided by expandable subcategories then expand 
            //the category containing the full article
            var divCatParent = $("#" + currUcId + "_dlProductList").parent("div");
            if ( divCatParent.length > 0 && divCatParent[0].id.startsWith("panel") ) {
                divCatParent[0].style.display="";
            }

            //If the article summary list user control parent is a tab panel, select the tab panel.
            var userControlParent = links[i].id.substring(0,links[i].id.indexOf("_ucArticles"));
            if (userControlParent.indexOf("_TabPanel") > -1) {
                var tabIdStartPos = links[i].id.indexOf("_TabPanel") + 11;
                var thisTab = $find(links[i].id.substring(0,links[i].id.indexOf("_",tabIdStartPos)));
                thisTab.get_parent().set_activeTab(thisTab);
            }
            
            //adding history point will trigger onPageStateChanged() above only if user triggers the get_articleDetails() call
            if (userTriggeredArticle == true) {
			    $.historyLoad( "a=" + ArticleDetails.ArticleId);
            }            
            break;
        }
    }    
    //var divFullArticleHolders = $(".singlearticle");

    var divFullArticle = $get(currUcId + "_divFullArticle");
    divFullArticle.innerHTML = (ArticleDetails!=null && ArticleDetails.HTML!=null) ? ArticleDetails.HTML : "";
    divFullArticle.style.display = (ArticleDetails==null) ? "none" : "";

    $get(currUcId + "_articleLinks").style.display = (ArticleDetails==null) ? "none" : "";
    $get(currUcId + "_dlProductList").style.display = (ArticleDetails==null) ? "" : "none";
    $get(currUcId + "_linkToArticle").href = String.format("/articles/?a={0}", ArticleDetails.ArticleId)

    if (ArticleDetails!=null) {
        document.title =  String.format("Roland Article: {0} ({1})", ArticleDetails.Title,ArticleDetails.ArticleId)
    }
}

//function selectTab(tabId) {
//    //get collection of tabs on page
//    var tabs = $(".ajax__tab_panel");

//    //if specified tab is within range of tabs on page, then select it. tabs[] is zero-based, tabId is one-based
//    if (tabs[tabId - 1] != null) {
//        var thisTab = $find(tabs[tabId - 1].id);
//        if (thisTab != null && thisTab != thisTab.get_parent().get_activeTab()) {
//            thisTab.get_parent().set_activeTab(thisTab);
//        }
//    }
//}

//function getCurrentTabIndex() {
//    //find current selected tab
//    var tab = $(".ajax__tab_panel:visible")
//    return tab.length==0 ? -1 : $find(tab[0].id).get_tabIndex() + 1;
//}


if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();


