function flipbook(json,itemwidth,thumbviewwidth,ad_refresh_interval){ // (your json image object, the width of a single thumb box, the width of your thumb view panel, the number of items in your thumb)
	this.json = json; // assign the object and place it in here..
	this.counter = 0; //a 'static' counter to represent how many times the user flipped so far
	this.selectindex = 0; // this is the index value of the current position the fp is set at
	this.horbar_xpos = 0; //the next 2 variables are used to determine the width of the horizontal display area
	this.horbar_xmax = 29600; // this is the current maximum display width of the entire scrollbar, we need to fix this at a later point..
	this.horbar_thumbviewwidth = thumbviewwidth;
	this.horbar_itemwidth =itemwidth;
	this.horbar_itemarray = new Array();
	this.horbar_thumbitemnum = Math.floor(thumbviewwidth/itemwidth);
	this.horbar_thumbitempos = 0; // this is the current position of the horbar based in the index value of the itemarray
	this.isad = false;
	this.selectlimbo = 0;
	this.autoMode = false;
	this.firsttime = true;

	this.partnerRelatedLinks; // load json and assign it over here
	this.partnerRelatedAdvar; // load json and assign it over here
	
	
	this.selectIndex = function(i,isManualClick){
		if (isManualClick && this.autoMode) this.autoToggle();
		if (this.isad) {
			$("#fb_mainbox").empty();
			i = this.selectlimbo;
			$(".moreflipbook_box").hide();
			$(".comments_box").fadeIn("slow");
			this.isad = false;
			$("#vl_blocker").css("z-index",0);
		} else if (this.isAdReady() || (i == json.image.length)){
			this.counter = 0;
			if (i == json.image.length){
				$("#fb_rightpanel").fadeOut("fast",function(){$("#fb_rightrelatedlinkpanel").fadeIn("fast");});
				if (this.autoMode){
					this.autoToggle();
				}
				$("#fb_partnerpromo").hide();
				$(".partnerrelatedlinks_box").hide();
			} else {
				$("#fb_rightpanel").fadeOut("fast",function(){$("#fb_rightadpanel").fadeIn("fast");});				
			}
			$("#fb_mainbox").empty();
			document.getElementById("fb_mainbox").innerHTML = "<span class=\"fb_adTitle\">ADVERTISEMENT</span>"+document.getElementById("fb_hidebox").innerHTML;
			$("#fb_adbox").fadeIn("slow");
			this.isad = true;
			var noauto = false;
			if (i == json.image.length){
				$(".moreflipbook_box").fadeIn("slow");
				$(".comments_box").hide();
				this.renderPosition(i+1,json.image.length);
				noauto = true;
				i = 0;
				$(".fb_secondsremaining").html(99);
			} 
			this.selectlimbo = i;
			if (!this.autoMode) {
				if (!noauto) {
					this.beginCountdown(10);
				}
			} else {
				this.clearCountdownArray();
				this.beginCountdown(10);
			}
			$("#vl_blocker").css("z-index",99);
			$("#vl_blocker").fadeTo("fast", 0.5);

			$("#fb_partnerpromo").hide();
			$(".partnerrelatedlinks_box").hide();

			return false;
		}
		$("#fb_rightpanel").show();
		$("#fb_rightadpanel").hide();
		$("#fb_rightrelatedlinkpanel").hide();
		var oldseleectindex = this.selectindex;
		$("#fb_thumbimg_"+oldseleectindex).css("border-color", "black");
		this.selectindex = i;
		$("#fb_credit").hide();
		$("#fb_captionbox").hide();
		$("#fb_mainbox").hide();
		if (this.json.image[i].credit){
			$("#fb_credit").html("Photo Credit: "+this.json.image[i].credit);
		} else { 
			$("#fb_credit").html(""); 
		}
		if (this.json.image[i].sourcecopyright){
			$("#fb_copyright").html("Copyright Info: "+this.json.image[i].sourcecopyright);
		} else {
			$("#fb_copyright").html("");
		}
		if ((this.json.image[i].credit) && (this.json.image[i].sourcecopyright)){
			$("#fb_credit").addClass("creditExist");
		} else {
			$("#fb_credit").removeClass("creditExist");
		}
		$("#fb_title").html(this.json.image[i].title);
		var v2_sponsoredhtml = "";
		if (!!this.json.image[i].sourceimgurl){
			v2_sponsoredhtml = "<div class=\"fb_sponsoredbox\">Provided By:<br><a href=\""+this.json.image[i].sourceurl+"\"><img src=\""+this.json.image[i].sourceimgurl+"\" /></a></div>";			
		}
		$("#fb_captionbox").html(v2_sponsoredhtml+this.json.image[i].caption);
		$("#fb_description").html(this.json.image[i].description);

		var toppx = (350-this.json.image[i].height)/2;
		var leftpx = (448-this.json.image[i].width)/2;
		$("#fb_mainbox").html("<img id=\"fb_image\" src=\""+this.json.image[i].src+"\" style=\"top:"+toppx+"px; left: "+leftpx+"px;\">");
		$("#fb_credit").fadeIn();// strange, chaining commands got borked in 1.2.3, so I've split it up.. 
		$("#fb_title").fadeIn();
		$("#fb_captionbox").fadeIn();
		$("#fb_description").fadeIn();
		$("#fb_mainbox").fadeIn();

		this.renderPosition(i+1,this.json.image.length);
		this.renderRelatedLinks(this.json.image[i].keywords);

		for(i=0; i < this.json.image.length; i++){
			$("#fb_thumb_"+i).removeClass("fb_horitem_over")
			$("#fb_thumb_"+i).removeClass("fb_horitem_sel");
		} 
		$("#fb_thumb_"+this.selectindex).addClass("fb_horitem_sel");
		if (!this.firsttime){	
			if ('function' == typeof window.refreshAds) {
			   refreshAds();
			}
			if ('function' == typeof window.adRefresh) {
			   adRefresh();
			}
		} else {this.firsttime = false;}
		if (!!this.json.image[this.selectindex].sourceid) {
			this.renderPartnerRelatedLinks(this.json.image[this.selectindex].sourceid,this.json.image[this.selectindex].sourcename);
		} else {
			$("#fb_partnerpromo").hide();
			$(".partnerrelatedlinks_box").hide();
		}
		this.selectCallback();
	}
	this.selectCallback = function(){}
	this.next = function(isManualClick){
		if ((!isManualClick) && (this.autoMode)){
			this.beginCountdown(5);
		} else {
				this.clearCountdownArray("next");
		}
		if (this.selectindex >= this.json.image.length){
			this.selectIndex(0,isManualClick);
		} else { this.selectIndex(this.selectindex+1,isManualClick);}
		this.scrollWhenAuto(this.selectindex+1);
	}
	this.prev = function(isManualClick){
		if (this.selectindex <= 0){
			this.selectIndex(this.json.image.length-1,isManualClick);
			this.scrollWhenAuto(this.json.image.length-1);
		} else { 
			this.selectIndex(this.selectindex-1,isManualClick);
			this.scrollWhenAuto(this.selectindex+1);
		}
	}
	this.autoToggle = function(){
		$(".fb_auto").toggle();
		this.autoMode = !this.autoMode;
		if (this.autoMode) {
			this.beginCountdown(5);
		} else if (!this.isad && !this.autoMode) {
			this.clearCountdownArray("auto Toggle");
		}
	}
	this.skipAd = function(){
		this.clearCountdownArray("skip Ad");
		this.next(false)
	}
	this.renderRelatedLinks = function(str){
		var rlstring = "";
		var arraystr = str.split(", ");
		for (i = 0; i < arraystr.length; i++){
			var termWords = arraystr[i].replace(/\s+$/, '');
			var termUrl = escape(termWords).replace('%20','+');
			rlstring += "<a href=\"/search/?q="+termUrl+"\">"+termWords+"</a>, ";
		}
		rlstring = rlstring.substr(0, rlstring.length-2);
		$("#fb_relatedlinks").html(rlstring);
	}

	this.renderPosition = function(index,range){$("#fb_control_mid").html(index+" of "+(range+1));}


	this.populateThumbbar = function(){
		var thumbbartxt = "";
		for(i=0; i <= this.json.image.length; i++){
			this.horbar_itemarray.push(i*this.horbar_itemwidth);
			var thumbsource = "";
			if (i < this.json.image.length) {
				thumbsource = json.image[i].thumbsrc
			}
			else 
				thumbsource = json.image[0].thumbsrc;
			thumbbartxt += "<div id=\"fb_thumb_"+i+"\" class=\"fb_horitem\" onClick=\"fb.selectIndex("+i+",true)\"><div><img id=\"fb_thumbimg_"+i+"\" class=\"fb_thumbimg\" src=\""+thumbsource+"\"></div></div>";
		}
		$("#fb_horbar").html(thumbbartxt);
		this.horbar_xmax = (this.json.image.length-this.horbar_thumbitemnum)*this.horbar_itemwidth;
		$(".fb_horitem").hover(
			function(){
				if(!($(this).hasClass("fb_horitem_sel"))) $(this).addClass("fb_horitem_over");
			},
			function(){
				if(!($(this).hasClass("fb_horitem_sel"))) $(this).removeClass("fb_horitem_over");
			}
		);
	}
	this.flip = function(startingx,endingx,idname){
		var startx = startingx;
		var endx = endingx; // where we end at
		var deltax = startx-endx; // how much we want to shift over by
		var numframes = 15;
		var framerate = 25;
		var prev_subnavstartx = 0;
		var incrementdifferences = new Array();
		var incrementdifferences_rev;
		for (var i=0; i < numframes; i++) {
			incrementdifferences.push(deltax*Math.sin(i*Math.PI/2/numframes));
		}
		incrementdifferences_rev = incrementdifferences.reverse();
		for (var i=0; i < numframes; i++) {
			startx = startingx-incrementdifferences_rev.pop();
			if (i== numframes-1){startx = endingx;}
			var vartimeout = window.setTimeout("document.getElementById('"+idname+"').style.left = '"+parseInt(startx)+"px';",framerate*i);
		}
	}
	this.scrollLeft = function(){
		var newindex = 0;
		newindex = this.horbar_thumbitempos+this.horbar_thumbitemnum;//3;
		if (newindex > (this.horbar_itemarray.length-this.horbar_thumbitemnum)){//}3)) {
			newindex = (this.horbar_itemarray.length-this.horbar_thumbitemnum);//3);
		}
		var oldx = -this.horbar_itemarray[this.horbar_thumbitempos];
		var newx = -this.horbar_itemarray[newindex];
		this.horbar_thumbitempos = newindex;
		this.flip(oldx,newx,"fb_horbar");
	}
	this.scrollWhenAuto = function(index){
		index--;
		var lowerbound = this.horbar_thumbitempos;
		var upperbound = this.horbar_thumbitempos+(this.horbar_thumbitemnum-1);//2
		if((index < lowerbound) || ( index > upperbound)){
			if (index > (this.horbar_itemarray.length-this.horbar_thumbitemnum)){//}3)){
				index = (this.horbar_itemarray.length-this.horbar_thumbitemnum);//3);
			}
			var oldx = -this.horbar_itemarray[this.horbar_thumbitempos];
			var newx = -this.horbar_itemarray[index];
			this.horbar_thumbitempos = index;
			this.flip(oldx,newx,"fb_horbar");
		}
	}
	this.scrollRight = function(){
		var newindex = 0;
		newindex = this.horbar_thumbitempos-this.horbar_thumbitemnum;//3;
		if (newindex < 0) newindex = 0;
		var oldx = -this.horbar_itemarray[this.horbar_thumbitempos];
		var newx = -this.horbar_itemarray[newindex];
		this.horbar_thumbitempos = newindex;
		this.flip(oldx,newx,"fb_horbar");
	}
	

	// Ad code controller **********************************************
	this.registerAdInterval = function(interval){
		if (interval.hasOwnProperty('length')){
			this.adInterval = interval;
		} else {
			// we're just gonna make it an array with 2 items in it. Sinch
			this.adInterval = new Array(interval,interval);
		}
	}
	this.INDEX_adInterval = 0; // counter to check which index the adInterval array is currently at
	this.STATIC_adInterval = 4;// this is static value used, and must never be changed!
	this.adInterval;
	this.isAdReady = function(){
		var adIntervalCounter = parseInt(this.adInterval[this.INDEX_adInterval]);
		if(!adIntervalCounter){
			adIntervalCounter = this.STATIC_adInterval;
		}
		this.counter++;
		if (this.counter > adIntervalCounter) {
			if (this.selectindex > (this.json.image.length-this.STATIC_adInterval)){
				return false;
			}
			this.INDEX_adInterval++;
			if (this.INDEX_adInterval > (this.adInterval.length-1)){
				this.INDEX_adInterval = 0;
			}			
			this.counter = 0; // resetting the counter back to 0
			return true;
		} else {
			return false;
		}

	}
	var countdownArray = new Array();
	var countdownSecondsRemaining = 0;
	var countdownOn = false;
	this.beginCountdown = function(seconds){
		var oFlipbook = this; // capturing myself here..
		this.countdownOn = true;
		this.countdownSecondsRemaining = seconds;
		$(".fb_secondsremaining").html(seconds);
		fb_countdownArray.push(setInterval(function(){oFlipbook.countdown()},1000));
	}
	this.countdown = function(){
		this.countdownSecondsRemaining--;
		if (this.countdownSecondsRemaining <= 0){this.countdownOn = false;}
		if (this.countdownOn){
			$(".fb_secondsremaining").html(this.countdownSecondsRemaining);
		} else {
			this.clearCountdownArray("call countdown");
			if(((this.autoMode == false) && (this.isad == false))) {
				return false;	
			}
			this.next(false);
			
		}
	}
	this.clearCountdownArray = function(text){
		while (fb_countdownArray.length > 0) {
			window.clearTimeout(fb_countdownArray.pop());
		}
	}
	
	
	/* remove me!!! for something new and shiny*/
	this.registerPartnerRelatedLinks = function(myObject){
		return false;
		this.partnerRelatedLinks = myObject;
	}
	/* remove me!!! for something new and shiny*/


	this.getPRLJSON = function(){
		var myself = this;
		$.getJSON("/api/prl/?linktype=1&num=5",
		        function(data){
					myself.partnerRelatedLinks = data;
		        });

//		$.getJSON("/cm/cosmogirl/media/test/jsonadvar2",
		$.getJSON("/api/prl/?linktype=2&num=10",
				function(data){
					myself.partnerRelatedAdvar = data;
				});
	}

	this.getPRLADVAR = function(source_id){
		// 1) check to see if the partnerRelatedAdvar is null, if it is, we need to invoke this call again..
		var myself = this;
		var thtml ="";
		if (!!this.partnerRelatedAdvar){
			for (var i = 0; i < this.partnerRelatedAdvar.length; i++){
				if (this.partnerRelatedAdvar[i].source_id == source_id){
					thtml = myself.partnerRelatedAdvar[i].link[0].opt;
				}
			}
			$("#fb_partnerpromo").html(thtml);
			$("#fb_partnerpromo").show();
		}else {
//			windows.setTimeout(function(){myself.getPRLADVAR(source_id)},1000);
		}
	}



// a relic from the past, remove this guy!
// a relic from the past, remove this guy!
// a relic from the past, remove this guy!
	this.getPRLADVARold = function(source_id){
		var myself = this;
		$.getJSON("/cm/cosmogirl/media/test/jsonadvar",
			function(data){
				var thtml = "";
				$.each(data, function(i,item){
//					alert(data[i].source_id+" "+source_id);
					if (data[i].source_id == source_id){
//						alert(data[i].link[0].opt);
						thtml = data[i].link[0].opt;
						alert(thtml);
//						$("#fb_partnerpromo").html(data[i].link[0].opt);
					}
//					$("<img/>").attr("src", item.media.m).appendTo("#images");
//					if ( i == 3 ) return false;
				});
				$("#fb_partnerpromo").html(thtml);
				$("#fb_partnerpromo").show();
			});
	}
	
	
	
	this.prlArray = new Array();
	this.renderPartnerRelatedLinks = function(source_id,sourcename){
		if (!sourcename) sourcename= "Partner";
		var myself = this;
		if (!!this.partnerRelatedLinks){
			var prljson;
			for (var i = 0; i < this.partnerRelatedLinks.length; i++){
				if (this.partnerRelatedLinks[i].source_id == source_id){
					prljson = this.partnerRelatedLinks[i];
				}
			}
			if (!!prljson){
                var rhtml = "<ul id=\"relatedlinks_ul\">";
				for (var i = 0; i < prljson.link.length; i++){
					rhtml += "<li><a href=\""+prljson.link[i].url+"\" target=\"_blank\">"+prljson.link[i].text+"</a></li>";
				}
				rhtml += "</ul>";
				$(".partnerrelatedlinks_content").html(rhtml);
				$(".relatedlinkspanel_title").html(sourcename+" links");
				$(".partnerrelatedlinks_box").show();
				this.getPRLADVAR(prljson.source_id);
			} else {
				$(".partnerrelatedlinks_box").hide();
				$("#fb_partnerpromo").hide();
			}
		} else {
			$(".partnerrelatedlinks_box").hide();
			$("#fb_partnerpromo").hide();
			window.setTimeout(function(){myself.renderPartnerRelatedLinks(source_id)},1000)
//			this.prlArray.push();
		}
	}
	this.testing = function(){
		alert("testing!!!!");
		alert(this.partnerRelatedLinks[0].link[0].text);
	}
	
}
var fb_countdownArray = new Array();

