// jQuery plugin - Dolphin RSS Aggregator

(function($){

	$.fn.dolRSSFeed = function() {

		return this.each( function(){

			

			var $Cont = $(this);

			var iRSSID = parseInt( $Cont.attr( 'rssid' ) || 0 );

			if( !iRSSID )

				return false;

			

			var iMaxNum = parseInt( $Cont.attr( 'rssnum' ) || 0 );

			var iMemID  = parseInt( $Cont.attr( 'member' ) || 0 );

			

			$.getFeed( {

				url: 'get_rss_feed.php?ID=' + iRSSID + '&member=' + iMemID ,

				success: function(feed) {

					if( window.console ) console.log( feed );

					

					var sCode =

						'<div class="rss_feed_wrapper">';

					var iCount = 0;

				function pubdateToBr(pubDate){
				
				 var timestamp   = Date.parse(pubDate);
				 var data_brasil = new Date(timestamp);
				 var dia         = data_brasil.getUTCDate();
				 var mes         = data_brasil.getUTCMonth()+1;
				 var ano         = data_brasil.getUTCFullYear();
				
				 return [dia,mes,ano].join("/");
				}


					for( iItemId in feed.items ) {

						var item = feed.items[iItemId];

						

						var oDate = new Date( item.updated );

						var sDate = pubdateToBr(oDate);

//						var sDate = oDate.toLocaleString();

						var descricao = item.description;

						arrayDescricao = descricao.split(" ");

						for (i=0;i<arrayDescricao.length;i++) {

							if (arrayDescricao[i].length > 18 ) {

								arrayDescricao[i] = arrayDescricao[i].substring(0,18) + '[...]';

							}

						}

//						descricao = arrayDescricao.join(" ");

						sCode +=

							'<div class="rss_item_wrapper">' +

								'<div class="rss_item_header">';

					    if (iCount == 0 && iRSSID == 15) sCode += achaImagemPost('','',item.description,item.link);

						sCode += 

									'<a rel="nofollow" ';

					    if (iCount == 0 && iRSSID == 15) sCode += 'style="font-size: 110%; color: #000022; font-weight: bold"';



						if (iRSSID == 15) varLink = 'http://www.minhascamisas.com.br/wordpress/#post_' + eval(iCount+1); else varLink = item.link;



						sCode +=

									  ' href="' + varLink + '" target="_blank">' + item.title + '</a>' +

								'</div>';

						sCode +=

								'<div class="rss_item_info">' +

									'<span>' +

										( sClockIcon != undefined ? ( '<img src="' + sClockIcon + '" /> ' ) : '' ) +

										sDate +

									'</span>' +

								'</div><div style="overflow; hidden;';

					    if (iCount == 0 && iRSSID == 15) sCode += 'font-size: 105%'; 

						sCode += '" class="rss_item_desc">';



						sCode += tamanhoTexto(iRSSID,descricao,iCount,varLink) + '</div>' +

							'</div>';

					    if (iCount == 0 && iRSSID == 15) sCode += "<hr>";

						

						iCount ++;

						if( iCount == iMaxNum )

							break;

					}

					

					sCode +=

							'<div class="rss_read_more">' +

								temFeed(iRSSID) + 			

								'<a href="' + feed.link + '" target="_blank">' + feed.title + '</a>' +

							'</div>' +

						'</div><div class="clear_both"></div>';

					

					$Cont.html( sCode );

				}

			} );

			

		} );

	};

})(jQuery);



function tamanhoTexto(iRSSID,descricao,iCount,linkFeed) {

	var stripped = descricao.replace(/(<([^>]+)>)/ig,""); 

	if (iRSSID == 19) { // Forum RSS Feed

		var limit = 90;

	} else if (iRSSID == 15) { // blog

		var limit = 155;

	} else if (iRSSID == 100) { // blog - comentários

		var limit = 180;

	} else {

		var limit = 200;

	}

	if (iCount == 0 && iRSSID == 15) limit = limit + 50;





	arrayStripped = stripped.substring(0,limit).split(" ");

	for (i=0;i<arrayStripped.length;i++) {

		posLink = arrayStripped[i].indexOf('http');

		if (posLink > -1) {

			arrayStripped[i] = arrayStripped[i].substring(0,posLink-1) + ' [link]';

		}	 

	}



	if ( descricao.length > limit) {

		arrayStripped.pop();

		stripped = arrayStripped.join(" ")+'... [<a href="' + linkFeed + '">leia mais</a>]';														

	} else {

		stripped = arrayStripped.join(" ")

	}

	

	return stripped;

}



function temFeed(iRSSID) {

	if (iRSSID == 19) { // Forum RSS Feed

		var strFeed = 'http://www.minhascamisas.com.br/comunidade/forum/rss.php';

	} else if (iRSSID == 15) { // blog

		var strFeed = 'http://www.minhascamisas.com.br/wordpress/feed/rss2/';

	} else if (iRSSID == 100) { // blog - comentários

		var strFeed = 'http://www.minhascamisas.com.br/wordpress/comments/feed';

	} else {

		var strFeed = null;

	}

	if (strFeed) {

		feedURL = '<a href="' + strFeed + '" style="color: white; background: #FF8800; font: 700 10px Tahoma, Arial, Helvetica, sans-serif; text-decoration: none;">&nbsp;RSS&nbsp;</a>&nbsp;';

	} else {

		feedURL = '';

	}

	return feedURL;

}



function achaImagemPost(iCount,iRSSID,descricao,URL) {

	var str = descricao;

	var ar = str.match("http://www.minhascamisas.com.br/wordpress/wp-content/uploads/(.*).(.*)\ alt=(.*) title=(.*) width=\"(.*)\" height=\"(.*)\" class");

	var imagem = RegExp.$1.split(".")[0];

	var extensao = RegExp.$1.split(".")[1];

	var largura = RegExp.$5;

	var altura = RegExp.$6;

	

	if (eval(largura) < eval(altura)) {

		var largura_nova = 225;

		var altura_nova = Math.round((225/largura)*altura);

	} else if (eval(largura) > eval(altura)) {

		var largura_nova = 225;

		var altura_nova = Math.round((225/largura)*altura);

	} else {

		var altura_nova = 225;

		var largura_nova = 225;

	}

	

	if (imagem != '') {
	
		//	enderecoSite2 = "http://www.minhascamisas.com.br/wordpress/wp-content/uploads/" + imagem.replace('-'+ largura + 'x' + altura,'') + "-" + largura_nova + "x" + altura_nova + "." + extensao;

		foto = '<div style="margin-bottom: 10px; background-color: #DDDDDD; border-color: gray; border-style: solid; border-width: 1px; height: 147px; text-align: center; background-position: top center; background-repeat: no-repeat; padding: 0; background-image: url(\'http://www.minhascamisas.com.br/wordpress/wp-content/uploads/' + imagem.replace('-'+ largura + 'x' + altura,'') + '-' + largura_nova + 'x' + altura_nova + '.' + extensao + '\')"><a href="' + URL + '" style="text-decoration: none; display: block; height: 100%; width: 100%">&nbsp;</a></div>';

		return foto;

	} else {

		return '';							

	}



  }			

