/*
 * jQuery get wordpress json feed Plugin
 * Copyright 2011, Allan P. Danan
 * Dual licensed under the MIT or GPL Version 2 licenses.
 */
$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});

(function($){
			$.fn.extend({
				//plugin name - getWPRSS
				getWPRSS: function(options) {
					var durl = $(this).attr('rel');
					var rows = $(this).attr('rows');
					var data = $(this).attr('data');
					var dateformat = $(this).attr('dateformat');
					var strtemplate = $(this).children("[rel=template]").html();
					var templatetag = $(this).children("[rel=template]")[0].tagName;
					var container = $(this);
					var urlpostid = $.getUrlVar('p');
					if(urlpostid!=undefined && urlpostid>0){
						durl = durl + "&p="+urlpostid+"&filterby="+data;
					}
					if(dateformat==undefined){
						dateformat="dd/mm/yyyy";	
					}
					var strattributes = ""; 
			        $.each($(this).children("[rel=template]")[0].attributes, function(index, attr) {
						if(attr.name!="rel" && attr.value!="template"){
            				strattributes +=  " "+attr.name+"=\""+attr.value+"\"";
						}
			        }); 
					strtemplate = "<"+templatetag + strattributes + ">" + strtemplate + "</"+templatetag+">";
					$(this).children("[rel=template]").remove();
					
					
					
					$.ajax({
						dataType: "jsonp", 
						url: durl,
						success: function(data) {
								var strpcontent = "";
								  for (var i=0; i < data.length; i++){
							        var temp_item = strtemplate;
									var dexcerpt = data[i].excerpt;
									dexcerpt = dexcerpt.replace(/<\/?[^>]+>/gi, '');
									dexcerpt = dexcerpt.replace(/Continue reading/i,'');
									dexcerpt = dexcerpt.replace(/&rarr;/i,'');
									dexcerpt = dexcerpt.replace(/&hellip;/i,'');
									
									//var d1 = Date.parse(data[i].date);
									var fdate =data[i].date;


										temp_item=temp_item.replace(/TGEGNEWS_WP_ID/gi,data[i].id);
										temp_item=temp_item.replace(/TGEGNEWS_WP_TITLE/gi,data[i].title);
										temp_item=temp_item.replace(/TGEGNEWS_WP_DATE/gi,fdate);
										temp_item=temp_item.replace(/TGEGNEWS_WP_CONTENT/gi,data[i].content);
										temp_item=temp_item.replace(/TGEGNEWS_WP_EXCERPT/gi,dexcerpt);
										temp_item=temp_item.replace(/TGEGNEWS_WP_AUTHOR/gi,data[i].author);
										temp_item=temp_item.replace(/TGEGNEWS_WP_LINK/gi,data[i].permalink);
										strpcontent += temp_item;
										if(rows!=undefined && (rows-1)==i ){
											break;
										}
								  }
								  container.prepend(strpcontent);
						}   
				   });
				   
				   
					
					
				}//getWPRSS: function(options) 
			});
		})(jQuery);

