(TextAds = function(conf){

	var timeStamp = new Date();
	timeStamp = timeStamp.getTime()

	/*
	 * init()
	 * set configuration and defaults
	 * call loadJSON
	 */
	function init() {
		conf							= 	conf 					||	{};
		conf.limit				=		conf.limit		||	5;
		conf.name					=		conf.name			||	'TextAds';
		conf.JSONsrc			=		conf.JSONsrc	||	'http://ads.eagletribune.com/textads/json_files/classdata.php'
		conf.JSONparams		=		conf.params		||	['time=' + timeStamp,'function=processJSON'];
		conf.wordLimit		=		conf.wordLimit||	20;
		//document.write('<div id="premiumadtarget"></div>');
		loadJSON(conf.JSONsrc + '?' + conf.JSONparams.join('&'));
	}
	
	function refresh() {
		var timeStamp = new Date();
		timeStamp = timeStamp.getTime()
		var wrap = document.getElementById('premiumadbox');
		wrap.parentNode.removeChild(wrap);
		loadJSON(conf.JSONsrc + '?' + conf.JSONparams.join('&') + '&' + timeStamp);
	}

	/*
	 * loadJSON()
	 * attach JSON URL to head of document
	 * when json loads it calls processJSON
	 * which is hard coded to communicate back to TextAds
	 * via processJSON() which is also attached to head
	 */
	function loadJSON(url) {
		var h = document.getElementsByTagName("head")[0];         
		var json = document.createElement('script');
				json.type = 'text/javascript';
				json.src = url;
		h.appendChild(json);
	}
	
	/*
	 * create(arr)
	 * recieves an array of ad objects
	 * and outputs ads on page
	 */
	function create(arr) {
		var wrap = document.createElement('div');
		wrap.id = 'premiumadbox';

		var pop = document.createElement('div');
		pop.id = 'premiumadpopup';
		wrap.appendChild(pop);

		var refreshlink = document.createElement('a');
		refreshlink.setAttribute('href','javascript:textAds.refresh()');
		var refreshtext = document.createTextNode('');
		refreshlink.setAttribute('class','premiumrefresh');
		refreshlink.setAttribute('className','premiumrefresh');
		refreshlink.setAttribute('title','See more ads');
		refreshlink.appendChild(refreshtext);
		wrap.appendChild(refreshlink);

		
		var top = document.createElement('img');
		top.src = 'http://static.cnhi.zope.net/images/top.gif';
		wrap.appendChild(top);
		
		var wrap2 = document.createElement('div');
		wrap2.setAttribute('class','premiumads');
		wrap2.setAttribute('className','premiumads');
		wrap.appendChild(wrap2);
		
		var h4 = document.createElement('h4');
		var h4text = document.createTextNode('Today\'s Classified Ads');
		h4.appendChild(h4text);
		wrap2.appendChild(h4);
		
		for(i=0;i<arr.length;i++) {
			var item = document.createElement('div');
			item.setAttribute('class','premiumad');
			item.setAttribute('className','premiumad');
			var text = arr[i].text.replace("\n", "<br/>");
			
			var popText = text;
			item.txt = popText;
			item.onclick = callPop;
			item.onmouseover = ieMouseOver;
			item.onmouseout = ieMouseOut;
			
			text = text.split(' ');
			
			if(text.length > conf.wordLimit) {
				text.splice(conf.wordLimit,0,'<em class="premiummore">&hellip;more</em>');
				text = text.slice(0,conf.wordLimit+1);
			}
			text = text.join(' ');
			
			item.innerHTML = text;

			wrap2.appendChild(item);
		}

		var all = document.createElement('p');
		all.setAttribute('class','premiumallads');
		all.setAttribute('className','premiumallads');
		var alllink = document.createElement('a');
		alllink.setAttribute('href','http://www.classifiedsnorth.com');
		alllink.setAttribute('target','_blank');
		var alltext = document.createTextNode('See All Ads');
		alllink.appendChild(alltext);
		all.appendChild(alllink);
		
		wrap2.appendChild(all);

		var bot = document.createElement('img');
		bot.src = 'http://static.cnhi.zope.net/images/bottom.gif';
		bot.setAttribute('class','premiumbottom');
		bot.setAttribute('className','premiumbottom');
		wrap.appendChild(bot);
		
		
		document.getElementById('premiumadtarget').appendChild(wrap);
	}

	/*
	 * pop()
	 * attached to ad elements
	 * sends ad text to display to pop up
	 * text is stored in elements .txt property
	 *
	 */
	function pop(o) {
		//console.log('pop');
		if(o.id != 'premiumcaller') {
			if(document.getElementById('premiumcaller')) {
				unpop();
			}
			//setOpacity(document.getElementById('maincontentarea'),30);
			setOpacity(o,30);
			o.id = 'premiumcaller';
			var txt = o.txt;
			var pop = document.getElementById('premiumadpopup');
			pop.innerHTML = '';
			
			var wrap = document.getElementById('premiumadbox');
			var ypos = findPosY(o) - findPosY(wrap);
			ypos += (o.offsetHeight / 2);
			
			var item = document.createElement('div');
			item.id='premiumpopad';
			item.setAttribute('class','premiumad');
			item.setAttribute('className','premiumad');
			item.onmouseover = ieMouseOver;
			item.onmouseout = ieMouseOut;
			item.innerHTML = unescape(txt);
			item.onclick = unpop;
			pop.appendChild(item);
			pop.style.top = "-3000px";
			pop.style.display = 'block';
			popHeight = pop.offsetHeight;
			pop.style.top = ypos-(popHeight/2) + 'px';
			pop.style.display = 'block';
		}else{
			unpop();
			return;
		}
	}


	/*
	 * unpop()
	 * hides popup and resets opacity of the object clicked on
	 */
	function unpop() {
		var pop = document.getElementById('premiumadpopup');
		pop.innerHTML = '';
		pop.style.display = 'none';

		var caller = document.getElementById('premiumcaller');
		setOpacity(caller,100);
		caller.id = '';
	}
	
	/*
	 * setOpacity()
	 * receives an element object and opacity 
	 * int 0-100 and sets the opactity
	 */
	function setOpacity(elem, opacityAsInt) {
		var opacityAsDecimal = opacityAsInt;
		if (opacityAsInt > 100) {
			opacityAsInt = opacityAsDecimal = 100;
		}else if (opacityAsInt < 0) {
			opacityAsInt = opacityAsDecimal = 0; 
		}
		opacityAsDecimal /= 100;
		if (opacityAsInt < 1) {
			opacityAsInt = 1; // IE7 bug, text smoothing cuts out if 0
		}
		elem.style.opacity = (opacityAsDecimal);
		elem.style.filter  = "alpha(opacity=" + opacityAsInt + ")";
	}

	/*
	 * findPosY(obj)
	 * receives element and returns its vertical position
	 */
  function findPosY(obj) {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }

	/*
	 * callPop()
	 * attached to ad elements - calls back to TextAds and pops the ad popup
	 */
	function callPop() {	textAds.pop(this)}
	/*
	 * ieMouseOver() & ieMouseOut
	 * attached to ad elements - changes class of elements so ie will highlight
	 */
	function ieMouseOver() {this.className += ' premiumadhover';}
	function ieMouseOut() {this.className = this.className.replace(' premiumadhover','')}

	/*
	 * return
	 * return is only public accessible function
	 * specific public functions here
	 */
  return {
    init:init,
    create:create,
    pop:pop,
    refresh:refresh
  }
})();
function processJSON(arr){
	textAds.create(arr)
}
