var nr_domain = fnGetDomain(nr_domain);
var nr_load_link=false;
var nr_clicked_link=null;


function fnGetDomain(url) {
   return url.match(/:\/\/(.[^/]+)/)[1];
}

function nr_onload(){
    var nr_url="http://api.nrelate.com/rcw_b/0.02.0/index_homepage.php?domain=" + nr_domain;
    var nr_head=document.getElementsByTagName("head")[0];
    var nr_script=document.createElement("script");
    nr_script.type="text/javascript";
    nr_script.async=true;
    nr_script.src=nr_url;
    nr_head.appendChild(nr_script);
}

function nr_jqueryload(){

jQuery(document).ready(function($) {
	jQuery('body').append('<iframe  id="nr_clickthrough_frame" height="0" width="0" style="border-width: 0px; display:none;" onload="javascript:nr_loadframe();"></iframe>');
	if (jQuery('div.post.hentry').length > 1) {
		jQuery('div.post.hentry').each(function(index){
			if (jQuery('div.post-body').length) {
				jQuery(this).children('div.post-body').append('<div id="nr_'+index+'"></div>');
			}
			else if (jQuery('post-footer').length) {
				jQuery(this).children('div.post-footer').prev().append('<div id="nr_'+index+'"></div>');
			}
		});
	}
    else if (jQuery('div.story').length > 1) {
		jQuery('div.story').each(function(index){
			jQuery(this).children('div.post-body').append('<div id="nr_'+index+'"></div>');
		});
	}
    else if (jQuery('div.post').length > 1) {
		jQuery('div.post').each(function(index){
			jQuery(this).children('div.post-body').append('<div id="nr_'+index+'"></div>');
		});
	}
    else if (jQuery('div.wrap').length > 1) {
		jQuery('div.wrap').each(function(index){
			jQuery(this).children('div.content').append('<div id="nr_'+index+'"></div>');
		});
	}
    else if (jQuery('div.blogPost').length > 1) {
		jQuery('div.blogPost').each(function(index){
			jQuery(this).append('<div id="nr_'+index+'"></div>');
		});
	}
    if (jQuery('h3').length > 1) {
		var first_post = jQuery('h3').html();
		var title_tag = 'h3';
		var nr_url="http://api.nrelate.com/rcw_b/0.02.0/index_homepage.php?domain=" + nr_domain + "&url=" + nr_pageurl + "&first_post=" + first_post + "&title_tag=" + title_tag;
		jQuery.getScript(nr_url);
    }
	else if (jQuery('h2').length > 1) {
		var first_post = jQuery('h2').html();
		var title_tag = 'h2';
		var nr_url="http://api.nrelate.com/rcw_b/0.02.0/index_homepage.php?domain=" + nr_domain + "&url=" + nr_pageurl + "&first_post=" + first_post + "&title_tag=" + title_tag;
		jQuery.getScript(nr_url);
    }
	else if (jQuery('h1').length > 1) {
		var first_post = jQuery('h1').html();
		var title_tag = 'h1';
		var nr_url="http://api.nrelate.com/rcw_b/0.02.0/index_homepage.php?domain=" + nr_domain + "&url=" + nr_pageurl + "&first_post=" + first_post + "&title_tag=" + title_tag;
		jQuery.getScript(nr_url);
    }
	
	//tracking
	jQuery('.nr_rc_link').live('click', function(event){
		event.preventDefault();
		var nr_src_url = window.location.href;
		var nr_iframe_src = "http://api.nrelate.com/rcw_b/track.html";
		var test = jQuery('#nr_clickthrough_frame');
		var nr_iframe = document.getElementById('nr_clickthrough_frame');
		if (jQuery(this).hasClass('nr_ad')) {
			nr_type = 'ad';
		} else if (jQuery(this).hasClass('nr_external')) {
			nr_type = 'external';
		} else {
			nr_type = 'internal';
		}
		nr_iframe_src += "?type=" + nr_type + "&domain=" + escape(nr_domain) + "&src_url=" + escape(nr_src_url) + "&dest_url=" + escape(jQuery(this).attr('href'));
		nr_load_link = true;
		nr_clicked_link = jQuery(this).attr('href');
		nr_iframe.src = nr_iframe_src;
	});
});
}

function checkJquery(timer) {
    // if timer passes 10000ms (10sec), failure to use jQuery. Append to header instead.
    if (timer>10000){
	if(window.attachEvent){window.attachEvent("onload",nr_onload);}
	else if(window.addEventListener){window.addEventListener("load",nr_onload,false);}
	else{document.addEventListener("load",nr_onload,false);}
        return;
    }
    //checks if jQuery is loaded every 100ms. if not, call checkJquery again with 100ms timer incremented up
    //else call nr_url
    if (typeof jQuery == "undefined" || typeof window['jQuery'] == "undefined") {
	timer+=100;
        window.setTimeout("checkJquery("+timer+")", 100);
    } else {
        nr_jqueryload();
    }
}

function nr_rc_fix_css(){
		
		
		jQuery(".nrelate_related").each(function(){
			if (jQuery(this).attr('fixed')) return;
			
			var nr_height=0;
			var _d = new Date();
			var _id = "nr_" + _d.getTime();
			jQuery(this).attr("id", _id);
			
			jQuery("#" + _id + " a.nr_rc_panel").each(function(){
				if(jQuery(this).innerHeight()>nr_height){
					nr_height = jQuery(this).innerHeight();
				}
			});
			jQuery("#" + _id + " a.nr_rc_panel").css("height",nr_height+"px");
			
			jQuery(this).attr('fixed', '1');
		});
		
        jQuery(document).ready(function(){
        //sponsored animation
        jQuery('.nrelate_related .nr_sponsored').hover(
            function(){
                jQuery(this).stop();
                jQuery(this).animate(
                    {'left' : '0px'},
                    200
                );
            },
            function(){
                jQuery(this).stop();
                jQuery(this).animate(
                    {'left' : (jQuery(this).parent().width() - 18) + 'px'},
                    200
                );
            }
        );
        });
}

function nr_loadframe(){
  if(nr_load_link){
    nr_load_link=false;
    window.location.href=nr_clicked_link;
  }
}

if(typeof jQuery == "undefined" || typeof window['jQuery'] == 'undefined'){
        var url = 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js';
        document.write('<scr'+'ipt src="', url, '" type="text/javascript"><\/scr'+'ipt>');
}
checkJquery(0);

//document.write('<scr' + 'ipt src="http://api.nrelate.com/rcw_b/0.02.0/track_onclick.js" type="text/javascript"><\/scr' + 'ipt>');

