var OTHPhotoMgr =
{
	Popup:null,
	Init:function()
	{
		// Find photos...
		$j('a.photo-popup-link').each(function()
		{
			var G = $j(this).attr('rel');
			if (!G)
				return;
			$j(this).click(function(e){
				e.preventDefault();
				OTHPhotoMgr.Popup = new PhotoPopup($j(this));
				OTHPhotoMgr.Popup.Init();
			});
		});
	}
};

function PhotoPopup(ele)
{
	this.rel = ele.attr('rel');
	this.photos = [];
	this.activePhoto = 0;
	this.start = [ele.attr('href'), ele.children('img.photo-popup-link-thumbnail').attr('src')];
	this.slideshow = false;
	this.slidetimer = null;
	this.slidetime = 5000;

	this.AddPhoto = function(URL, ThumbURL)
	{
		this.photos.push([URL, ThumbURL]);
	};

	this.ToggleSlideshow = function(e)
	{
		e.preventDefault();
		if(this.slideshow)
			return this.StopSlideshow();
		this.Next();
		return this.StartSlideshow();
	};
	this.StartSlideshow = function()
	{
		this.slideshow = true;
		clearTimeout(OTHPhotoMgr.Popup.slidetimer);
		this.slidetimer = setTimeout('OTHPhotoMgr.Popup.Slide();', this.slidetime);
		$j('#photo-popup-slideshow-value').html('Stop');
	};
	this.Slide = function()
	{
		if(!this.slideshow)
			return;
		this.Next();
		this.StartSlideshow();
	};
	this.StopSlideshow = function()
	{
		this.slideshow = false;
		clearTimeout(OTHPhotoMgr.Popup.timer);
		$j('#photo-popup-slideshow-value').html('Start');
	};

	this.Thumb = function(n)
	{
		this.StopSlideshow();
		this.ChangePhoto(n);
	};

	this.Previous = function()
	{
		if(this.slideshow)
			this.StartSlideshow();
		this.ChangePhoto((this.activePhoto == 0) ? (this.photos.length - 1) : (this.activePhoto - 1));
	};

	this.Next = function()
	{
		if(this.slideshow)
			this.StartSlideshow();
		this.ChangePhoto((this.activePhoto == (this.photos.length - 1)) ? 0 : (this.activePhoto + 1));
	};

	this.ChangePhoto = function(n)
	{
		if(this.activePhoto == n)
			return;
		this.activePhoto = n;
		this.ShowPhoto();
	}

	this.Show = function()
	{
		$j('<div id="photo-popup-background"></div>').prependTo(document.body);
		$j('<div id="photo-popup-main"><div id="photo-popup-photo"><div id="photo-popup-loading">Loading...</div></div></div>').prependTo(document.body);
		$j('<a id="photo-popup-photo-previous" href="javascript:OTHPhotoMgr.Popup.Previous();"><span></span></a><a id="photo-popup-photo-next" href="javascript:OTHPhotoMgr.Popup.Next();"><span></span></a>').prependTo($j('#photo-popup-photo'));
		$j('#photo-popup-photo-previous span, #photo-popup-photo-next span').fadeTo(0,0);

		$j('#photo-popup-photo-previous, #photo-popup-photo-next').hover(function(){$j(this).children('span').fadeTo(100,.75);},function(){$j(this).children('span').fadeTo(100,0);});

		this.ShowThumbnails();
		this.ShowControls();
		this.ShowPhoto();
		this.Resize();
	};

	this.ShowPhoto = function()
	{
		$j('#photo-popup-loading').show(0);
		$j('<img class="photo-popup-main-photo" />').attr('src', this.photos[this.activePhoto][0]).bind('load', function()
		{
			$j(this).fadeTo(0,0);
			$j('#photo-popup-photo img.photo-popup-main-photo').remove();
			$j(this).appendTo('#photo-popup-photo');
			$j('#photo-popup-loading').hide(0);
			$j(this).fadeTo(300, 1);
			OTHPhotoMgr.Popup.Resize();
		}).each(function(){
			if(this.complete || (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6))
				$j(this).trigger('load');
		});
		$j('#photo-popup-thumbnails a').removeClass('active');
		$j('#photo-popup-thumbnails a[photo='+this.activePhoto+']').addClass('active');
		$j('#photo-popup-active').html(this.activePhoto+1);
		$j('#photo-popup-total').html(this.photos.length);
	};

	this.ShowThumbnails = function()
	{
		$j('#photo-popup-thumbnails').remove();
		$j('<div id="photo-popup-thumbnails"><div class="reset"></div></div>').insertAfter('#photo-popup-photo');
		for(i=0;i<this.photos.length;i++)
		{
			$j('<a href="javascript:OTHPhotoMgr.Popup.Thumb('+i+');" photo="'+i+'"><span class="photo-popup-thumbnail"><span></span><img src="'+this.photos[i][1]+'" alt="" /></span></a>').appendTo('#photo-popup-thumbnails');
		}
		$j('<div class="reset"></div>').appendTo('#photo-popup-thumbnails');
	};

	this.ShowControls = function()
	{
		var html = '<a id="photo-popup-control-close">CLOSE <span class="icon icon-negitive-margin icon-close"></span></a>';
		html = html + '<a class="gloss-button gloss-button-size-16" href="javascript:OTHPhotoMgr.Popup.Previous();">Previous</a>';
		html = html + '<a class="gloss-button gloss-button-size-16" href="javascript:OTHPhotoMgr.Popup.Next();"><span class="ie-text-shadow">Next</span></a>';
		html = html + '<span class="photo-popup-control photo-popup-control-count">Image <span id="photo-popup-active">1</span> of <span id="photo-popup-total">10</span></span>';
		html = html + '<span class="photo-popup-control"><a href="#" id="photo-popup-slideshow"><span class="icon icon-negitive-margin icon-slideshow"></span> <span id="photo-popup-slideshow-value">Start</span> Slideshow</a></span>';
		if(ROActive)
			html = html + '<span class="photo-popup-control"><a href="'+ROURL+'oth-txt-lnk" target="_blank"><span class="icon icon-negitive-margin icon-calculator"></span> Find and Compare Home Loans</a></span>';
		$j('<div id="photo-popup-controls">'+html+'</div>').insertBefore('#photo-popup-photo');

		$j('#photo-popup-slideshow').click(function(e){OTHPhotoMgr.Popup.ToggleSlideshow(e);});

	};

	this.Close = function()
	{
		this.StopSlideshow();
		$j('#photo-popup-background, #photo-popup-main').remove();
		OTHPhotoMgr.Popup = null;
	};

	this.Resize = function()
	{
		var w = $j('#photo-popup-main').width() / 2;
		$j('#photo-popup-main').css({marginLeft:'-'+(w-3)+'px'});
		$j('#photo-popup-photo-next, #photo-popup-photo-previous').height($j('#photo-popup-photo img.photo-popup-main-photo').height());
	};

	// Begin.
	this.Init = function()
	{
		// Load photos..
		$j('a.photo-popup-link[rel='+this.rel+']').each(function()
		{
			OTHPhotoMgr.Popup.AddPhoto($j(this).attr('href'), $j(this).children('img.photo-popup-link-thumbnail').attr('src'));
		});
		// Work out starting photo..
		for(i=0;i<this.photos.length;i++)
		{
			if(this.photos[i][0] == this.start[0] && this.photos[i][1] == this.start[1])
			{
				this.activePhoto = i;
				break;
			}
		}
		this.Show();
		$j('#photo-popup-background, #photo-popup-control-close').click(function(){OTHPhotoMgr.Popup.Close();});
		$j('#photo-popup-main').css({top:($j(window).scrollTop()+25)+'px'});
		$j('#photo-popup-background').height($j(document).height());
	};
};

$j(function(){OTHPhotoMgr.Init();});

