function Archiver(m)
{
	this.model 	= m;
	var now    	= new Date();
	this.year 	= this.y2k(now.getYear());
	this.epoch	= {year:08, week:25}
	EventManager.addEventListener("onModelData", this);
}

Archiver.prototype.y2k = function (number) 
{ 
	return (number < 1000) ? number + 1900 : number; 
}


Archiver.prototype.onModelData = function(evt)
{
	/**
	 *	coupon
	 */
	
	if(evt.model.hasCoupon == "true")
	{
		var coupon = document.getElementById("printCoupon");
		var url = "http://switch.atdmt.com/action/070108_JCP_WIS_Coupons_Click";
		/*
		if(evt.model.couponLink != "" && evt.model.couponLink != "null") {
			url = evt.model.couponLink;
		} else {
			url = "http://switch.atdmt.com/action/070108_JCP_WIS_Coupons_Click";
		}
		*/
		var linkz = document.getElementById("links");
	//	alert(url);
		linkz.innerHTML += '<li><a href="' + url + '" target="_blank" class="navBtn nav3" alt="print coupon image" title="print coupon"></a></li>';
		
	}
	
	/**
	 *	banner and link
	 */
	var banner = document.getElementById("dealsImg");
	var bannerLink = document.getElementById("dealsHref");
	
	banner.src = "images/saleImages/"+ evt.model.banner;
	bannerLink.href = evt.model.bannerLink;
}

Archiver.prototype.setPrintCoupon = function()
{
	
}


Archiver.prototype.setSource = function(img, timestamp)
{
	var episode = this.getHashValueAt(0);
	// if deeplinking
	if(episode)
	{
		if(episode.indexOf("_") != -1) src = episode;
		if(episode.indexOf("_") == -1) src = String(this.year).slice(2)+"_"+episode;
		if(!this.isValidEpisode(src)) src = this.model.getEpisode(0).date;
		
	}else{
		// IF NO DEEP LINKING
		var year 		= Number(timestamp.split(" ")[0]);
		var month		= Number(timestamp.split(" ")[1]);
		var day  		= Number(timestamp.split(" ")[2]);
		
		var exp	 		= this.model.getExpirationDate().split("-")
		var expYear 	= Number(exp[0]);
		var expMonth	= Number(exp[1]);
		var expDay 		= Number(exp[2]);
		var promo		= this.model.getEpisode(0).date;
		var nonPromo	= "generic";
		var src			= promo;
		
		// if exact same year and month we need to compare by days
		if(year == expYear && month == expMonth) if(day < expDay) src = promo;
		if(year == expYear && month == expMonth) if(day == expDay) src = nonPromo;
		if(year == expYear && month == expMonth) if(day > expDay) src = nonPromo;

		// if same year and current month is less than promo month we know promo is active
		// otherwise it would meet condiditon above
		if(year == expYear && month < expMonth) src = promo;
		
		// if current year is less than the promo year we know promo is active
		// eg a december video promo expries Jan of following year
		if(year < expYear) src = promo;
	} 
	
	img.src = "images/saleImages/"+src+".png";
	alert(img.src);
	if(this.model.getPrintCoupon() == 0) document.getElementById("printCoupon").innerHTML = "";			
}


Archiver.prototype.getHashValueAt = function(index)
{
	var href	= window.location.href;
	if(href.indexOf("#/") == -1) return null;
	var params 	= href.split("#/");

	if(params.length>=2) params = params[1].split("/") ;
	
	if(params.length>=1){
		for(var i = 0; i<= params.length; i++)
		{
			if(i==index) return params[i];
		}
		return null;
	}else{
		return null;
	}
}

Archiver.prototype.isValidEpisode = function(e)
{
	var episodes = this.model.getEpisode();
	
	for(var i in episodes)
	{
		var validEpisode = episodes[i].date
		if(validEpisode == e) return true;
	}
	return false;
	
}
