/* slideshow object */
function SlideShowInProgram() {
	
	/* PRIVATE METHODS AND VARIABLES */
	
		/* internal reference to object (external is lowercase ss) */
		var SS = this;
		
		/* build the AJAX iframe */
		function buildAJAXIframe() {
			try {
				document.domain = 'webmd.com';
				$('#ContentPane3').append('<div id="ajaxFrame"><iframe src="http://img.webmd.com/slideshow_fp/slideshow_fp_frame.html" width="0" height="0" id="xmlFrame" name="xmlFrame"></iframe></div>');
			} catch(e) {}
		}
		
		/* build the slides container */
		function buildSlides() {
			var output = '<div id="ss_inprg_slides">';
			var slides = [];
			$('.ss_inprg_slide_img').each(function(i, val) {
				output += '<div class="ss_inprg_slide_fmt" id="ss_inprg_slide'+(i+1)+'">';
				/* load the first two slides and the last slide */
				if (i < 2 || i == $('.ss_inprg_slide_fmt img').size()) {
					output += '<img alt="'+val.title+'" src="'+val.href+'" />';
				} else {
					output += '<img alt="'+val.title+'" src="" />';	
				}
				output += '</div>';
				slides[i] = val.href;
			});
			output += '</div>';
			$('#ss_inprg_rdr').append(output);
			
			/* on page load, preload the slide images for slides 3 through second-to-end */
			$(document).ready(function() {
				$('.ss_inprg_slide_fmt img').each(function(i, val) {
					if (i >= 2 || i < $('.ss_inprg_slide_fmt img').size()) {
						$(val).attr('src', slides[i]);
					}
				});
			});
		}
		
		/* count slides */
		function countSlides() {
			return $('.ss_inprg_slide_fmt').size();
		}
		
		/* fade out last slide and caption, fade in new slide and caption */
		function doFade(n) {
			if (!SS.config.in_transition) {
				SS.config.in_transition = true;
				var nextSlideID, prevSlideID, nextCaptionID, prevCaptionID;
								
				$('.ss_inprg_slide_fmt').each(function(i, val) {
					if (i == n) nextSlideID = val.id;
					if (i == SS.config.current_slide) prevSlideID = val.id;
				});
				
				$('.ss_inprg_caption_fmt').each(function(i, val) {
					if (i == n) nextCaptionID = val.id;
					if (i == SS.config.current_slide) prevCaptionID = val.id;
				});
				
				$('#' + nextSlideID).show();
				
				$('.ss_inprg_slide_fmt').css('background','none');
				
				$('#' + prevSlideID + ' img').fadeTo(SS.config.fade_speed, 0);
				$('#' + nextSlideID + ' img').fadeTo(SS.config.fade_speed, 0.99, function() {
					$('#' + prevSlideID).hide();
					SS.config.in_transition = false;
					$('.ss_inprg_slide_fmt').css('background','#000');	
				});
								
				$('#' + prevCaptionID).fadeOut(SS.config.fade_speed);
				$('#' + nextCaptionID).fadeIn(SS.config.fade_speed);
				
				
				refreshAds(n);
				SS.config.current_slide = n;
				pageView();
				refreshCounter();
			}
		}
		
		/* hitting the next button on the last slide triggers this instead of a fade */
		function lastFrameLink() {
			window.location.href = $('#ss_inprg_last_lnk').attr('href');
		}
		
		function pageView() {
		    var pageNumber, pageURL;

		    /* Register Pageview */
		    try {
			pageNumber = SS.config.current_slide + 1;

                        // Remove query string and hash from the URL,
                        // then add the current slide number as a hash
                        pageURL = document.location.href.replace(/[\?#].*/, '') + '#' + pageNumber; 

			wmdPageview(pageURL);
			
			/* load xml in the content frame */
			window.xmlFrame.getFile();
		    }
		    catch(e) {}
		}
		
		// refresh ads on the page
		function refreshAds(slideNumber) {
		    var transTileId = Math.round(99999999*Math.random());
		
		    // Grab the top divs for both ads on the page
		    $("#bannerAd_fmt, #leftAd_fmt, #rightAd_fmt").each( function() {

                        // Find the ad within the div
                        var ad = $(this).children("iframe[id*=Ad_Iframe]");

			// Remove everything from the div except for the ad
			$(this).children().not(ad).remove();

                        // Remove children within the ad iframe
                        ad.children().remove();

			// Update the src for each ad
			var strSrc = ad.attr("src");
                        if (!strSrc) { return; }

			strSrc = strSrc.replace(new RegExp("transactionID=[0-9]+"), "transactionID=" + transTileId);
			strSrc = strSrc.replace(new RegExp("tile=[0-9]*"), "tile=" + transTileId);

			/* slide ad targeting */
                        var next = slideNumber+1;
                        strSrc = strSrc.replace(/frame=[0-9]*/, "frame=" + next);
			if (strSrc.indexOf('frame=') == -1) {
                            strSrc += '&frame=' + next;
                        }
			if(next == 1) { strSrc = strSrc.replace(/&frame=[0-9]*/, ""); }

                        // Update the src without adding to the page history
			ad[0].contentWindow.location.replace(strSrc);
		    });
		}
		
		/* refresh the counter, create it if it doesn't exist */
		function refreshCounter() {
			var counterCode = '<span>' + (SS.config.current_slide+1) + '</span><span>/</span><span>' + countSlides() + '</span>';
			if (!document.getElementById('ss_inprg_counter')) {
				$('#ss_inprg_rdr').append('<div id="ss_inprg_counter">' + counterCode + '</div>');
			} else {
				$('#ss_inprg_counter').html(counterCode);
			}
		}
	
	/* PUBLIC METHODS AND VARIABLES */
		
		/* initiate object */
		this.init = function() {
			
			/* configuration */
			this.config = {
				current_slide : 0,
				fade_speed : 1000,
				in_transition : false
			}
			
			/* build the slides container */
			buildSlides();
			
			/* build the iframe for AJAX pageviews */
			buildAJAXIframe();
			
			/* fade slide images to zero opacity, but don't hide */
			$('.ss_inprg_slide_fmt img').fadeTo(1, 0);
			
			/* create the counter */
			refreshCounter();
			
			/* show the first slide image */
			$('#ss_inprg_slide1 img').fadeTo(1, 0.99);
			
			/* unhide several elements */
			$('#ss_inprg_slides, #ss_inprg_slide1, #ss_inprg_nav, #ss_inprg_caption1').show();
		}
		
		/* next slide */
		this.next = function() {
			var c = this.config.current_slide;  /* current slide index */
			var t = countSlides()-1;  /* total slides */
			(c == t) ? lastFrameLink() : doFade(c+1);  /* go to the next slide */
			return false;
		}
		
		/* previous slide */
		this.prev = function() {
			var c = this.config.current_slide; /* current slide index */
			var t = countSlides(); /* total slides */
			(c == 0) ? doFade(t-1) : doFade(c-1); /* go to the previous slide */
			return false;
		}
}

/* create instance of the SlideShowInProgram object */
var ss = new SlideShowInProgram();
// JavaScript Document
