var FRESH_LOAD = true;

function external_article(){
    var answer = confirm ("You will now be leaving California Bank & Trust’s website.\n\nIn an effort to provide you with current business information, we offer you access to the following online business news article.\n\nPlease note that although CB&T offers access to online business news through this website, CB&T does not monitor its content and does not endorse and is not responsible for the content, guarantees, privacy policy, security, and products and services that are offered or expressed on this website.");

    if(answer){
        return true;
    }
    return false;
}
function external_article_1031(){
    var answer = confirm ("Thank you for your interest in 1031 Exchange Services! In an effort to provide you assistance with tax-deferred exchanges of investment property, we offer Exchange Services, LLC, an affiliate of California Bank & Trust (CB&T). Although Exchange Services, LLC is an affiliate of California Bank & Trust, please note that CB&T does not monitor content on its website and does not endorse and is not responsible for the content, guarantees, privacy policy, security, and products and services that are offered or expressed on this website.");

    if(answer){
        return true;
    }
    return false;
}

$(document).ready(function(){

	//tab code


	//get all possible rss feeds
	$('.rss').click(function(e){
        return false;
		//e.preventDefault();

	        //this call is now made from $('.swap').click to keep tabs and ajax content in sync
		//get_rss_feed(this.href);
	});
	
	$('.swap').click(function(){
		
		//remove all class selected from all
		$('.swap').each(function(){
			$(this).removeClass("selected");
			this.src = this.src.replace('_on.','_off.');
		});
		
		//add selected to new tab
		$(this).addClass("selected");
		this.src = this.src.replace('_off.','_on.');

		$(this).parent().each(function(){
		     get_rss_feed(this.href);
		});
	});
	
	$('.swap').mouseover(function(){
		this.src = this.src.replace('_off.','_on.');
	});
	
	$('.swap').mouseout(function(){
		
		//if class is selected we don't want to deselect it
		if(!$(this).hasClass("selected")){
		   this.src = this.src.replace('_on.','_off.');
		}
	});


	//accordion code

	$('.swap-acc').click(function(){

		if($(this).hasClass("selected")){
			return false;
		}

		//remove all class selected from all
		$('.swap-acc').each(function(){
			$(this).removeClass("selected");
			this.src = this.src.replace('_on.','_off.');
		});

		//add selected to new tab
		$(this).addClass("selected");

		$('.rss-panel').each(function(){
			if($(this).hasClass('rss-hidden')){
			   //do nothing for now
			}
			else{
				
				//$(this).slideUp(750);
				$(this).addClass('rss-hidden');
			}
		});

		$('.rss-panel',$(this).parent().parent()).each(function(){
			
			//$(this).slideDown(750);
			$(this).removeClass('rss-hidden');
            //if($.browser.msie && $.browser.version=="6.0")

		});


		this.src = this.src.replace('_off.','_on.');
	});

	$('.swap-acc').mouseover(function(){
		this.src = this.src.replace('_off.','_on.');
	});

	$('.swap-acc').mouseout(function(){

		//if class is selected we don't want to deselect it
		if(!$(this).hasClass("selected")){
		   this.src = this.src.replace('_on.','_off.');
		}
	});

	$('.rss-tab').click(function(e){
	    e.preventDefault();
		
		var href = this.href;

//		$('#rss-sidebar-icon-link').each(function(){
//			   this.href = href;
//			   return false;
//		});
	});
	
	//get_rss_feed('http://feeds.reuters.com/reuters/businessNews');
	//load first rss function is defined in /includes/rss-tabs.php
	//if load_rss_feed function exists, call it
	if (typeof(window.load_first_rss) == 'function') {
	   load_first_rss();
	}

	get_rss_sidebar('http://feeds.reuters.com/reuters/businessNews','#rss-sidebar-reuters');
//	get_rss_sidebar('http://www.library.ca.gov/crb/crbreports.xml','#rss-sidebar-crb');
	get_rss_sidebar('http://rss.businessweek.com/bw_rss/smallbiz','#rss-sidebar-business-week');

    
});

$(window).load(function(){
   var images = new Array('reuters_on.jpg','crb_on.jpg','business_week_on.jpg','acc_reuters_on.jpg','acc_crb_on.jpg','acc_business_week_on.jpg');

   for(var i=0;i<images.length;i++){
       var image = new Image();

       image.src = '/images/' + images[i];

       var image_off = new Image();
       image_off.src = '/images/' + images[i].replace('_on','_off');
   }
});

function get_rss_feed(rss_link){
   $.post("/rss.php", { link: rss_link},
   function(rss){
     populate_rss_feed(rss,rss_link);
   });
}

function populate_rss_feed(rss,rss_link){
   $('#rss-feed').empty();
   
   var rsslogo = $(rss).find('image url').text();
   var rsslogoimage = '';

   if(rsslogo != ''){
      var rsslogoimage = '<img src="' + rsslogo + '" id="rss-logo"/>';
   }
   else{
       switch(rss_link){

           case "http://www.library.ca.gov/crb/crbreports.xml":
               var rsslogoimage = '<div style="padding:13px 0px 0px 30px;font-size:20px">California Research Bureau</div>';
               break;
           default:
       }
   }
   
   var rsshead = '<table id="rss-head"><tr><td>' + rsslogoimage + '</td>';
       //rsshead += '<td align="right"><a href="' + rss_link + '"><img src="/images/rss-subscribe.jpg" border="0" id="rss-subscribe"/></a></td></tr></table>';

       switch(rss_link){
           case "http://feeds.reuters.com/reuters/businessNews":
              rsshead += '<td align="right">*Sorted by popularity</td></tr></table>';
              break;
           default:
               rsshead += '<td align="right"></td></tr></table>';
       }
   
   $('#rss-feed').append(rsshead);
   
   $(rss).find('item').each(function(i){
									 
	   var item = $(this);
	   
	   var pub = item.find('pubDate').text();
	   var title = item.find('title').text();
	   
	   var link = item.find('link').text();
	   
	   //get description
	   var description = item.find('description').text();
	   description = shorten_description(description,link);
	   
	   var zebra = '';
	   
	   if(i % 2 != 0){
	      zebra = ' rss-odd';
	   }

       var html = '<a name="article-' + (i + 1) + '"><img src="/images/clear.gif" width="1" height="1" /></a>';
	   html += '<div class="rss-item' + zebra + '">';
       //html += '<a name="article-' + (i + 1) + '"></a>';
	   html += '<div class="rss-pubdate">' + strip_cdata(pub) + '</div>';
	   html += '<div class="rss-title"><a href="' + link + '" target="_blank" onclick="return external_article();">' + strip_cdata(title) + '</a></div>';
	   html += '<div class="rss-description">' + description + '</div>';
       html += '</div>';
	   
	   $('#rss-feed').append(html);
	   
	   if(i >= 9){

           var pos = window.location.href.indexOf('#');

           if(pos != -1 && FRESH_LOAD){
               window.location.hash = window.location.href.substring((pos + 1));
               FRESH_LOAD = false;
           }
           
		   return false;
	   }
   });
}

function shorten_description(description,link){
   description = strip_cdata(description);
   
   var pos = description.indexOf('<div class="feedflare"');
   
   if(pos != -1 && pos < 265){
	   description = description.substring(0,pos);
   }
   else{
	   description = description.substring(0,265);
   }

   var pdf = '';

   if(link.indexOf('.pdf') != '-1'){
      pdf = '<img src="/images/pdficon_small.gif" border="0" height="11" style="padding-top:2px" />';
   }

   description = description.replace(/^\s+|\s+$/g,"");

   //keep . ... from occurring
   if(description[description.length -1] == '.'){
       description = description.substring(0,description.length -1);
   }

   //ie6
   description = description.replace(/\.$/,"");


   return description + ' ...<a href="' + link + '" target="_blank" onclick="return external_article();">Read More' + pdf + '</a>';
}

function shorten_sidebar(description,length){
   description = strip_cdata(description);

   var pos = description.indexOf('<div class="feedflare"');

   if(pos != -1 && pos < length){
	   description = description.substring(0,pos);
   }
   else{
	   description = description.substring(0,length);
   }

   return description;
}

function strip_cdata(str){
   return str.replace('<![CDATA[','').replace(']]>','');	
}

function get_rss_sidebar(rss_link,selector){
   $.post("/rss/rss.php", { link: rss_link, type:'sidebar'},
   function(rss){
	$(selector).html(rss);
   });
}

function populate_rss_sidebar(rss,selector){

   $(rss).find('item').each(function(i){

	   var item = $(this);

	   var pub = item.find('pubDate').text();
	   var title = item.find('title').text();
	       title = shorten_sidebar(title,32);

	   var link = item.find('link').text();

	   //get description
	   var description = item.find('description').text();
	   //description = shorten_sidebar(description,32);

	   var getParam = "?tab=" + selector.replace('#rss-sidebar-','');

	   var zebra = '';

	   if(i % 2 != 0){
	      zebra = ' class="rss-sidebar-odd"';
	   }

       //make an anchor to artilce
       var anchor = '#article-' + (i + 1);

	   var html  = '<tr height="52">';
	       //html += '<td class="rss-padding">&nbsp;</td>';
		   html += '<td width="175"' + zebra + ' >';
		   html += '<div class="rss-sidebar-content"><span class="rss-sidebar-title"><a href="/about/online-news.html' + getParam + anchor + '">' + strip_cdata(title) + '</a></span><br />';
		   html += '<span class="rss-sidebar-desc"><a href="/about/online-news.html' + getParam + anchor + '">' + description + '</a>...'

           if(i >= 2){
              html += '<br /><a class="rss-read-more" href="/about/online-news.html' + getParam + '">Read More</a>';
           }
           
           html += '</span>';
		   html += '<a href="/about/online-news.html' + getParam + anchor + '" class="rss-arrow">&gt;&gt;</a></div>';
		   html += '</td>';
		   //html += '<td class="rss-arrow">&gt;&gt;</td>';
		   html += '</tr>';

	   $(selector).append(html);

	   if(i >= 2){
		   return false;
	   }
   });
}
