/*BlogAggregatorActions.js is the file used to manage blog aggregation.  It is called from within BlogAggregator.ascx.*/

//Threshold telling us when we should resize blog images.
var maxWidth = 500;

//Constant used for calculating image resize ratio.
var resizeWidth = 450;

//Name of the currently selected blog.
var blogSelected ="dnd";

//Creation of blog aggregator object.
var igxBlogAggregator = new IgxBlogAggregator();

//Function called by blog aggregator to retrieve blog specified in each associated ASCX control.
function IgxBlogAggregator ()
{
    //Sample function for showing a particular blog post - references a div on control inclusion page called "blogDetailAreaInsiders".
    this.showPost = function(evt)
    {
        moviePlayer.UnloadMovie();
        
        var post = null;
        
        if(evt.srcElement)                
            post = evt.srcElement;                                
        else if(evt.target)        
            post = evt.target;
        
       var plugin = document.getElementById("SilverlightControl");
        
        plugin.content.findName("C3_btn_back").Visibility = "Visible";
        plugin.content.findName("C3_btn_home_off").Visibility = "Collapsed";
        
        if(post)
        {
            var postData = post.parentNode.innerHTML;           
            var blogNode = post.parentNode.parentNode; 
            
            if (blogSelected == "dnd")
            {
                var descArea = document.getElementById("blogDetailAreaInsiders");
               
                plugin.content.findName("C3_hdr_dnd_png").Visibility = "Collapsed";
                plugin.content.findName("C3_hdr_dnd_blogs_png").Visibility = "Visible";
                plugin.content.findName("C3_hdr_dnd_intervivews_png").Visibility = "Collapsed";
                
                var scrollBar = document.getElementById("div_scroll");
                
                if (scrollBar)
                {
                    scrollBar.style.display = "inline";
                }
                
                createSilverlight_HtmlScrollbar("div_scroll","blogDetailAreaInsiders");
            }
            
            document.getElementById("div_scroll").style.display="inline";
            descArea.innerHTML = postData.replace(/igxPostDescriptionHidden/g,"igxPostDescription");
            descArea.style.display="inline";  
            setImageSizes();
        }
    }
    
    //Sample function for showing a complete blog - references a div on control inclusion page called "blogDetailAreaInsiders".
    this.showBlog = function(evt)
    {
        var blogTitle = null;
        
        if(evt.srcElement)                
            blogTitle = evt.srcElement;                                
        else if(evt.target)        
            blogTitle = evt.target;
        
        if(blogTitle)
        {
            var blogData = blogTitle.parentNode.parentNode.innerHTML;                              
            
            if (blogSelected == "dnd")
            {
                var descArea = document.getElementById("blogDetailAreaInsiders");
            }
                     
            descArea.innerHTML = blogData.replace(/igxPostDescriptionHidden/g,"igxPostDescription");
            descArea.style.display="inline";
        }
    }
};


//Resize blog images to ensure they fit inside the blog area for HSW.
function setImageSizes()
{
    var images = document.getElementsByTagName("img");
    
    if (images.length > 0)
    {
        for (var i=0; i < images.length; i++)
        {
            if (images[i].width >= maxWidth)
            { 
                if (images[i].src != "http://" + window.location.hostname + "assets/images/SampleHeader.png" && images[i].src != "http://" + window.location.hostname + "assets/images/SampleFooter.png")
                {
                    var newHeight = (resizeWidth/images[i].width) * images[i].height;
                    images[i].width = resizeWidth;
                    images[i].height = newHeight;
                }
            }
        }
    } 
}
