// JavaScript Document
//nixxie
$(document).ready(function(){
	
	var kw = 'Facial Cream';//default keyword;
		// if its a post get the keyword from the kw variable set in the id
		var kwByElement = $('h1:first').text();
		if(kwByElement.length > 2){kw = kwByElement}
		
		//check querytring for kw - see if we've done a keyword search
		var qkw = $.query.get('kw');
		
		//check querystring for s checks if the user has done a site search
		var s = $.query.get('s');
		
		//check querystring for r
		var res = $.query.get('r');
		
		var aid=5358;
		
		if ( $.query.get('aid')){
		aid = $.query.get('aid');
		}
		
		
		var d=new Date();
		
		//if keyword search is true then set to kw and write in the new h1 and targetcontent div
		if(qkw != ''){
			kw = qkw;
			$('h1:first').before("<h1 style='text-transform:capitalize'>"+kw+"</h1><div class='targetContent'></div>");
			$('.targetContent').append("<span class='infomessage'>Sponsored Links</span>");
			}
		//if site search is true then set to kw
		else if(s != ''){
		kw = s
		$('div.breadcrumb').after("<h1 style='text-transform:capitalize'>Search results for '"+kw+"'</h1><div class='targetContent'></div>");
			$('.targetContent').append("<span class='infomessage'>Sponsored Links</span>");
		}
		
	        //else fall back to default and put the targetcontent div after the first h1
		else {
			$('h1:first').after("<div class='targetContent'></div>");
			$('.targetContent').append("<span class='infomessage'>Sponsored Links</span>");
			}
		
	
	// make the restful ajax call
		
		
			$.ajax({
			type: "GET",
			url: "/wp-content/themes/CustomTheme/xmlproxy.php?kw="+kw+"&res="+res+"&d="+d+"&aid="+aid,
			dataType: "xml",
			cache: false,
			success: parseNixxieXml
			});
	
	});


// the function returned for nixie html
function parseNixxieXml(xml)
	{
	 
	  //find every Ad
	  $(xml).find("result").each(function()
	  {
	   var $ad = $(this);
	   var clickurl = $ad.find('clickurl').text(); 
	   var title = $ad.find('title').text(); 
	   var description = $ad.find('description').text(); 
	   var url = $ad.find('url').text();
	   var html = "<a class='featured' style='cursor:pointer' href='"+clickurl+"' target='_blank'><h3>"+title+"</h3><p>"+description+"</p><span>"+url+"</span></a>";
	   $('.targetContent').append(html);
	  });
	  $('.targetContent a').click(function(){
	  	var link = $(this).attr('href');
		recordOutboundLink(link, '/Outbound/Nixxie/');
	   });
	 
	  
	}



