$(function(){
  $('#slider>ul').anythingSlider({
    navigationFormatter : function(index, panel) {
      return $('img', panel).clone();
    }
  });
  $ ( ".accordion" ).accordion({
    autoHeight: false,
    active: false,
    collapsible: true
  });
  $("#market_news").tabs({
    fx : {
      opacity: 'toggle',
      duration:250
    }
  });
  //.tabs("rotate",3000)
});


	 function styleaccordions(accordionId){
	//alert(accordionId);
	    $("#"+accordionId).accordion({
		autoHeight: false,
		active: false,
		collapsible: true
		});
	}



    google.load("feeds", "1");
    var count = 0; 
    var allFeedResults= new Array();
    var total_feeds = 11;
    var total_sections = 7;
    for(var i=0;i<total_sections;i++){
        allFeedResults[i]= new Array();
    }
    
    function init(){
        //$("#tabs").tabs( { fx: {  opacity: 'toggle' , duration:250	} } ).tabs("rotate",3000);     
        getFeeds("http://feeds.reuters.com/reuters/globalmarketsNews","http://www.ft.com/rss/markets","http://syndication.financialexpress.com/rss/98/world-news.xml",0); 
        getFeeds("http://economictimes.feedsportal.com/c/33041/f/534033/index.rss","http://syndication.financialexpress.com/rss/97/economy.xml",1); 
        getFeeds("http://economictimes.feedsportal.com/c/33041/f/534037/index.rss","http://syndication.financialexpress.com/rss/96/markets.xml",2); 
        getFeeds("http://economictimes.feedsportal.com/c/33041/f/534040/index.rss","http://feeds.feedburner.com/IpoAlertBlog",3);
        getFeeds("http://www.livemint.com/SectionRssfeed.aspx?Id=18","http://www.livemint.com/SectionRssfeed.aspx?Id=21",4);
        getFeeds("http://www.livemint.com/SectionRssfeed.aspx?Id=21","http://www.livemint.com/SectionRssfeed.aspx?Id=21",5);
        getFeeds("http://www.commodityonline.com/rssfeed/topstorynews_rss.xml","http://syndication.financialexpress.com/rss/375/commodities.xml",6);                
    }
    function getFeeds() 
    {
        var index = getFeeds.arguments[getFeeds.arguments.length-1];
       // alert("hi!");
        var i=0;
        for(var i=0;i<getFeeds.arguments.length-1;i++){
            feedFromGoogle(getFeeds.arguments[i],index,i);
       }  
    }
    
    function feedFromGoogle(){
        var index = feedFromGoogle.arguments[1];
        var i = feedFromGoogle.arguments[2];
        var feed = new google.feeds.Feed(feedFromGoogle.arguments[0]);
            feed.setNumEntries(10);
            feed.load(function(result){
                if (!result.error) 
                    {  
                        allFeedResults[index][i]=result;
                        count++;
                        if(count==total_feeds) 
                            window.setTimeout("showAndStyleFeeds()",1000);
                    }
            });
     }       
    var tmp;
    function showAndStyleFeeds()
    {   
        
        for(var i=0;i<total_sections;i++)
        {
           var containerID = "accordion"+(i+1);
           for(var j=0;j<11;j++)
           {
                 try{
                    rand_feed = allFeedResults[i][Math.floor(Math.random()*allFeedResults[i].length)];
                    var entry = rand_feed.feed.entries[j];

                    if(j==0 || entry.title != tmp.title)
                    {   
                    var container=document.getElementById(containerID);
                    container.innerHTML += "<h3><a href='#'>"+entry.title+"</a></h3>";
                    container.innerHTML += "<div>"+stripHTML(entry.content)+"<a class='tab-links' href='"+entry.link+"' style='color:Blue' target=_blank >Read Full Story</a></div>";
                    }
                    tmp = entry;
                 }catch(e){}
           }
           styleaccordions(containerID);
        }
    }
    
    function displayFeeds(rand_feed,containerID,j){   
        var entry = rand_feed.feed.entries[j];

           if(j==0 || entry.title != tmp.title)
            {   
                var container=document.getElementById(containerID);
                container.innerHTML += "<h3><a href='#'>"+entry.title+"</a></h3>";
                container.innerHTML += "<div>" + stripHTML(entry.content) + "<a class='tab-links' href='" + entry.link + "' style='color:Blue' target=_blank >Read Full Story</a></div>";
           }
           tmp = entry;
    }                       

    function stripHTML(value){
        var re= /<\S[^><]*>/g;
        return value.replace(re, "");
    }
    
    	$(function() {
    	$ ( ".accordion" ).accordion({
		autoHeight: false,
		active: false,
		collapsible: true
		});
		 $("#tabs").tabs( { fx: {  opacity: 'toggle' , duration:250	} } ); //.tabs("rotate",3000);
        init(); 
	});

