var jBox = {
	// global vars
	animating: false,
	iFrame: [],
	eGal: [],
	nGal: [],
	// Initilize
	init: function(options) {
		//jBox.options = options;
		switch(options.type) {
			case 'ent_gallery':
				jBox.eGal = options; // holds the eGal's gallery info
				jBox.eGal.curIndex = -1;

				if ($.browser.msie && $.browser.version <= 6) {
					jBox.eGal.thumbOffset = (jBox.eGal.thumbOffset * 2);
				}

				jBox.createEntGallery();
				jBox.initEntGallery();
				$(jBox.eGal.buttons).each(function() {
					$(this).click(function() {
						var thisIndex = $(jBox.eGal.buttons).index(this);
						jBox.showEntGallery(thisIndex);
						return false;
					});
				});
			break;
			case 'news_gallery':
				jBox.nGal = options; // holds the eGal's gallery info
				jBox.nGal.curIndex = -1;

				if ($.browser.msie && $.browser.version <= 6) {
					jBox.nGal.thumbOffset = (jBox.nGal.thumbOffset * 2);
				}

				jBox.createNewsGallery();
				jBox.initNewsGallery();
				$(jBox.nGal.buttons).each(function() {
					$(this).click(function() {
						var thisIndex = $(jBox.nGal.buttons).index(this);
						jBox.showNewsGallery(thisIndex);
						return false;
					});
				});
			break;
			case 'iframe':
				var thisIndex = options.id;
				jBox.iFrame[options.id] = options; // this is to hold multiple iframes
				jBox.createIFrame(thisIndex);
				jBox.initIFrame();
				if (options.buttons) {
					$(options.buttons).each(function() {
						$(this).click(function() {
							jBox.showIFrame(thisIndex);
							return false;
						});
					});
				}
			break;
		}
	},
	// showGallery is called from the page thumbs
	showEntGallery: function(index) {
		if (!jBox.animating) { // to be made into animation effects
			if ($.browser.msie && $.browser.version <= 6) {
				$("select").css({visibility: "hidden"});
			}
			$(".jBoxOverlay").css({display: "block", opacity: '0'}).animate({opacity: '0.9'}, 500, function() {
				if (!$.browser.safari) {
					$(".eGal").css({top: $("html").scrollTop() + 10}).animate({opacity: 'show'}, 500);
				} else {
					$(".eGal").css({top: $("body").scrollTop() + 10}).animate({opacity: 'show'}, 500);
				}
			});
		}
		jBox.showEntGalleryByIndex(index);
	},
	showEntGalleryByIndex: function(index) {
		var origIndex = index;
		if (!jBox.animating && index != -1) {
			if (jBox.eGal.curIndex != origIndex) {
				jBox.eGal.curIndex = origIndex;
				$(".eGal .imageHolder").animate({height: jBox.eGal.images[origIndex].height + "px"}, 250);
				$(".eGal .imageHolder img").animate({opacity: '0'}, 250, function() {
					$(".eGal .imageHolder").html(jBox.eGal.images[origIndex]);// push's the image from the preloaded images
					$(".eGal .imageHolder img").animate({opacity: '1'}, 250);
				});
			}
			$(".eGal .contentHolder .source").html(unescape(jBox.eGal.data.info[origIndex].source));
			$(".eGal .contentHolder .caption span").html(unescape(jBox.eGal.data.info[origIndex].caption));
			$(".eGal .navHolder .nav .mask li.selected").removeClass("selected");
			$(".eGal .navHolder .nav .mask li").each(function(i) {
				if (i == index) {
					$(this).addClass("selected");
				}
			});

			$(".eGal .navHolder .pagenation .current").html(origIndex + 1);

			jBox.animating = true;

			index = (origIndex > 0) ? origIndex-1 : origIndex; // checks to see if the index is over 0, if it is, minus it by 1 to make the selected one show on far left
			index = (($(".eGal .navHolder .mask ul li").length - 1) == origIndex) ? index-1 : index; // checks to see if the index is the last thumbnail, and positions it on the far right.

			$(".eGal .navHolder .mask ul").animate({left: '-' + ((jBox.eGal.thumbWidth * index) - jBox.eGal.thumbOffset) + 'px'}, 500, function() {
				jBox.animating = false;
			});
		}
	},
	initEntGallery: function() {
		// sets a variable for the slider width
		var sliderWidth = ($(".eGal .navHolder .mask ul li").length + 1) * jBox.eGal.thumbWidth;
		$(".eGal .navHolder .mask ul").width(sliderWidth);

		// sets the "max" photo's shown
		$(".eGal .navHolder .pagenation .max").html($(".eGal .navHolder .mask ul li").length);

		// binds left and right button
		$(".eGal .navHolder .left").click(function() {
			var curIndex = $(".eGal .navHolder .mask ul li").index($(".eGal .navHolder .mask ul li.selected").prev("li")); // gets the current clicked index
			jBox.showEntGalleryByIndex(curIndex); // shows the current Index
			return false;
		});
		$(".eGal .navHolder .right").click(function() {
			var curIndex = $(".eGal .navHolder .mask ul li").index($(".eGal .navHolder .mask ul li.selected").next("li")); // gets the current clicked index
			jBox.showEntGalleryByIndex(curIndex); // shows the current Index
			return false;
		});
		$(".eGal .navHolder .mask ul li a").click(function() {
			var curIndex = $(".eGal .navHolder .mask ul li").index($(this).parent()); // gets the current clicked index
			jBox.showEntGalleryByIndex(curIndex); // shows the current Index
			return false;
		});
		$(".eGal .navHolder .close").click(function() {
			jBox.hideLightBox();
			return false;
		});
	},
	// creates the elements
	createEntGallery: function() {
		// creates the overlay, and binds the click
		jBox.createOverlay();
		// creates the main structure
		$("body").createAppend('div', {className: 'eGal', style: 'display: none'}, [
			'div', {className: 'adHolder'}, [
				'div', {className: 'advertisement'}, '<iframe height="60" frameborder="0" width="468" src="" name="iframe_iframeAd"></iframe>',
				'div', {className: 'sponser'}, '<iframe height="60" frameborder="0" width="160" src="" name="iframe_iframeSponser"></iframe>'
			],
			'div', {className: 'navHolder'}, [
				'a', {className: 'close', href: '#'}, [
					'img', {src: 'http://images.tvnz.co.nz/tvnz_site_images/images/jb_Close.gif', border: '0'}, ''
				],
				'div', {className: 'navOuter'}, [
					'div', {className: 'navInner'}, [
						'div', {className: 'title'}, unescape(jBox.eGal.data.title),
						'div', {className: 'pagenation'}, 'Displaying photo <span class="current">2</span> of <span class="max">27</span>',
						'div', {className: 'nav'}, [
							'a', {className: 'left', href: '#'}, [
								'img', {src: 'http://images.tvnz.co.nz/tvnz_site_images/images/jb_NavLeft.gif', border: '0'}, ''
							],
							'div', {className: 'mask'}, [
								'ul', {}, ''
							],
							'a', {className: 'right', href: '#'}, [
								'img', {src: 'http://images.tvnz.co.nz/tvnz_site_images/images/jb_NavRight.gif', border: '0'}, ''
							]
						]
					]
				]
			],
			'div', {className: 'contentHolder'}, [
				'div', {className: 'contentOuter'}, [
					'div', {className: 'contentInner'}, [
						'div', {className: 'imageHolder'}, [
							'img', {src: jBox.eGal.data.info[0].img}, ''
						],
						'div', {className: 'source'}, unescape(jBox.eGal.data.info[0].source),
						'div', {className: 'caption'}, [
							'span', unescape(jBox.eGal.data.info[0].caption), ''
						],
					]
				]
			]
		]);
		// push the iframe's ad src in, fixes a FF bug with creating an iframe with a src
		$(".eGal .adHolder .advertisement iframe").attr({src: jBox.eGal.data.advert + "ord=" + ord + "?"});
		$(".eGal .adHolder .sponser iframe").attr({src: jBox.eGal.data.sponser + "ord=" + ord + "?"});
		jBox.eGal.images = []; // creates an image array for the eGal
		// adds the thumbnails
		for (var i = 0; i < jBox.eGal.data.info.length; i++) {
			var liOptions = {}
			if (i == 0) {
				liOptions.className = 'selected';
			}
			$(".eGal .navHolder .nav .mask ul").createAppend('li', liOptions, [
				'a', {href: '#'}, [
					'img', {src: jBox.eGal.data.info[i].thumb, height: 80, width: 145, border: 0}, ''
				]
			]);
			// image pre-loader
			var myImage = new Image();
			myImage.src = jBox.eGal.data.info[i].img;
			jBox.eGal.images.push(myImage);
		}
	},
	// showGallery is called from the page thumbs
	showNewsGallery: function(index) {
		if (!jBox.animating) { // to be made into animation effects
			if ($.browser.msie && $.browser.version <= 6) {
				$("select").css({visibility: "hidden"});
			}
			$(".jBoxOverlay").css({display: "block", opacity: '0'}).animate({opacity: '0.9'}, 500, function() {
				if (!$.browser.safari) {
					$(".nGal").css({top: $("html").scrollTop() + 10}).animate({opacity: 'show'}, 500);
				} else {
					$(".nGal").css({top: $("body").scrollTop() + 10}).animate({opacity: 'show'}, 500);
				}
			});
		}
		jBox.showNewsGalleryByIndex(index);
	},
	showNewsGalleryByIndex: function(index) {
		var origIndex = index;
		if (!jBox.animating && index != -1) {
			if (jBox.nGal.curIndex != origIndex) {
				jBox.nGal.curIndex = origIndex;
				$(".nGal .imageHolder").animate({height: jBox.nGal.images[origIndex].height + "px"}, 250);
				$(".nGal .imageHolder img").animate({opacity: '0'}, 250, function() {
					$(".nGal .imageHolder").html(jBox.nGal.images[origIndex]);// push's the image from the preloaded images
					$(".nGal .imageHolder img").animate({opacity: '1'}, 250);
					$(".nGal .contentHolder .source").html(unescape(jBox.nGal.data.info[origIndex].source));
					$(".nGal .contentHolder .caption").html(unescape(jBox.nGal.data.info[origIndex].caption));
					$(".nGal .contentHolder .captionOuter").animate({width: jBox.nGal.images[origIndex].width, marginLeft: ((640 - jBox.nGal.images[origIndex].width) / 2) + 'px'}, 250);
				});
			}
			$(".nGal .navHolder .nav .mask li.selected").removeClass("selected");
			$(".nGal .navHolder .nav .mask li").each(function(i) {
				if (i == origIndex) {
					$(this).addClass("selected");
				}
			});

			$(".nGal .navHolder .pagenation .current").html(origIndex + 1);

			jBox.animating = true;

			index = (origIndex > 0) ? origIndex-1 : origIndex; // checks to see if the index is over 0, if it is, minus it by 1 to make the selected one show on far left
			index = (index > 0) ? index-1 : index;
			index = (origIndex > 1) ? origIndex-1 : origIndex;
			index = (($(".nGal .navHolder .mask ul li").length - 1) == origIndex) ? index-2 : index; // checks to see if the index is the last thumbnail, and positions it on the far right.
			index = (($(".nGal .navHolder .mask ul li").length - 2) == origIndex) ? index-1 : index;

			$(".nGal .navHolder .mask ul").animate({left: '-' + ((jBox.nGal.thumbWidth * index) - jBox.nGal.thumbOffset) + 'px'}, 500, function() {
				jBox.animating = false;
			});
		}
	},
	initNewsGallery: function() {
		// sets a variable for the slider width
		var sliderWidth = ($(".nGal .navHolder .mask ul li").length + 1) * jBox.nGal.thumbWidth;
		$(".nGal .navHolder .mask ul").width(sliderWidth);

		// sets the "max" photo's shown
		$(".nGal .navHolder .pagenation .max").html($(".nGal .navHolder .mask ul li").length);

		// binds left and right button
		$(".nGal .navHolder .left").click(function() {
			var curIndex = $(".nGal .navHolder .mask ul li").index($(".nGal .navHolder .mask ul li.selected").prev("li")); // gets the current clicked index
			jBox.showNewsGalleryByIndex(curIndex); // shows the current Index
			return false;
		});
		$(".nGal .navHolder .right").click(function() {
			var curIndex = $(".nGal .navHolder .mask ul li").index($(".nGal .navHolder .mask ul li.selected").next("li")); // gets the current clicked index
			jBox.showNewsGalleryByIndex(curIndex); // shows the current Index
			return false;
		});
		$(".nGal .navHolder .mask ul li a").click(function() {
			var curIndex = $(".nGal .navHolder .mask ul li").index($(this).parent()); // gets the current clicked index
			jBox.showNewsGalleryByIndex(curIndex); // shows the current Index
			return false;
		});
		$(".nGal .navHolder .close").click(function() {
			jBox.hideLightBox();
			return false;
		});
	},
	// creates the elements
	createNewsGallery: function() {
		// creates the overlay, and binds the click
		jBox.createOverlay();

		// creates the main structure
		$("body").createAppend('div', {className: 'nGal', style: 'display: none'}, [
			'div', {className: 'adHolder'}, [
				'div', {className: 'advertisement'}, '<iframe height="60" frameborder="0" width="468" src="" name="iframe_iframeAd"></iframe>',
				'div', {className: 'sponser'}, '<iframe height="60" frameborder="0" width="160" src="" name="iframe_iframeSponser"></iframe>'
			],
			'div', {className: 'navHolder'}, [
				'a', {className: 'close', href: '#'}, [
					'img', {src: 'http://images.tvnz.co.nz/tvnz_site_images/images/jb_NewsClose.gif', border: '0'}, ''
				],
				'div', {className: 'navOuter'}, [
					'img', {src: 'http://images.tvnz.co.nz/tvnz_site_images/images/jb_NavTop.gif', border: '0'}, '',
					'div', {className: 'navInner'}, [
						'div', {className: 'title'}, jBox.nGal.data.title,
						'div', {className: 'pagenation'}, 'Displaying photo <span class="current">2</span> of <span class="max">27</span>',
						'div', {className: 'nav'}, [
							'a', {className: 'left', href: '#'}, [
								'img', {src: 'http://images.tvnz.co.nz/tvnz_site_images/images/jb_NewsLeft.gif', border: '0'}, ''
							],
							'div', {className: 'mask'}, [
								'ul', {}, ''
							],
							'a', {className: 'right', href: '#'}, [
								'img', {src: 'http://images.tvnz.co.nz/tvnz_site_images/images/jb_NewsRight.gif', border: '0'}, ''
							]
						]
					],
					'img', {src: 'http://images.tvnz.co.nz/tvnz_site_images/images/jb_NavBot.gif', border: '0'}, ''
				]
			],
			'div', {className: 'contentHolder'}, [
				'div', {className: 'imageHolder'}, [
					'img', {src: jBox.nGal.data.info[0].img}, ''
				],
				'div', {className: 'captionOuter'}, [
					'div', {className: 'captionInner'}, [
						'div', {className: 'source'}, unescape(jBox.nGal.data.info[0].source),
						'div', {className: 'caption'}, unescape(jBox.nGal.data.info[0].caption)
					]
				]
			]
		]);
		// push the iframe's ad src in, fixes a FF bug with creating an iframe with a src
		$(".nGal .adHolder .advertisement iframe").attr({src: jBox.nGal.data.advert + "ord=" + ord + "?"});
		$(".nGal .adHolder .sponser iframe").attr({src: jBox.nGal.data.sponser + "ord=" + ord + "?"});
		jBox.nGal.images = []; // creates an image array for the eGal
		// adds the thumbnails
		for (var i = 0; i < jBox.nGal.data.info.length; i++) {
			var liOptions = {}
			if (i == 0) {
				liOptions.className = 'selected';
			}
			$(".nGal .navHolder .nav .mask ul").createAppend('li', liOptions, [
				'a', {href: '#'}, [
					'img', {src: jBox.nGal.data.info[i].thumb, height: 55, width: 73, border: 0}, ''
				]
			]);
			// image pre-loader
			var myImage = new Image();
			myImage.src = jBox.nGal.data.info[i].img;
			jBox.nGal.images.push(myImage);
		}
	},
	showIFrame: function(indexIFrame) {


		var iframe = jBox.iFrame[indexIFrame];
		var selector = ".jBox." + iframe.id;
		var ovSelector = ".jBoxOverlay";

		if ($.browser.msie && $.browser.version <= 6) {
			$("select").css({visibility: "hidden"});
		}

		$(ovSelector).css({display:'block'});
		$(ovSelector).css({opacity: "0"}).animate({opacity: "0.9"}, 500, function() {
			if ( $(selector).find("iframe").attr("src")!= iframe.src )
				$(selector).find("iframe").attr("src",iframe.src);
			else {
				$(selector).find("iframe").each(function(f) {
					this.contentWindow.location.reload(true);
				});
			}
			$(selector).css({display:'block'});
			$(selector).css({top: $( $.browser.safari ? "body":"html").scrollTop() + 10}).animate({opacity: 'show'}, 500);
		});
	},
	initIFrame: function() {
		$(".jBox .close").click(function() {
			jBox.hideLightBox();
			return false;
		});
	},
	createIFrame: function(indexIFrame) {
		// creates the overlay, and binds the click
		jBox.createOverlay();
		// creates the main structure
		$("body").createAppend('div', {className: 'jBox ' + jBox.iFrame[indexIFrame].id, id: jBox.iFrame[indexIFrame].id, style: 'width: ' + jBox.iFrame[indexIFrame].width + 'px; margin-left: -' + (jBox.iFrame[indexIFrame].width / 2) + 'px; display: none'}, [
			'a', {className: 'close', href: '#', style: 'left: ' + (jBox.iFrame[indexIFrame].width - 35) + 'px'}, [
				'img', {src: 'http://images.tvnz.co.nz/tvnz_site_images/images/jb_Close.gif', border: '0'}, ''
			],
			'div', {className: 'iFrameHolder'}, '<iframe name="' + jBox.iFrame[indexIFrame].id + ' src="'+jBox.iFrame[indexIFrame].src+'" height="' + jBox.iFrame[indexIFrame].height + '" width="' + jBox.iFrame[indexIFrame].width + '" frameborder="0" allowTransparency="true"></iframe>'
		]);
		if (jBox.iFrame[indexIFrame].closeSrc != null && jBox.iFrame[indexIFrame].closeSrc.length>0) {
			var img = new Image();
			$(img).load(function () { $(".jBox." + jBox.iFrame[indexIFrame].id + " .close").css({ left: (jBox.iFrame[indexIFrame].width - img.width) + "px", width: img.width + "px" }).html(this); }).attr({src: 'http://images.tvnz.co.nz/tvnz_site_images/' + jBox.iFrame[indexIFrame].closeSrc, border: '0'});
		}
	},
	createOverlay: function() {
		if (!$(".jBoxOverlay").exists()) {
			if ($.browser.msie && $.browser.version <= 6) {
				var height = (document.body.scrollHeight > document.body.offsetHeight) ? document.body.scrollHeight : document.body.offsetHeight;
				$("body").createAppend('div', {className: 'jBoxOverlay',  style: 'display: none; position: absolute; height: ' + height + 'px;'}, '');
			} else {
				$("body").createAppend('div', {className: 'jBoxOverlay',  style: 'display: none'}, '');
			}
			$(".jBoxOverlay").click(function() {
				jBox.hideLightBox();
			});
		}
	},
	hideLightBox: function(indexIFrame, completionFunc) {
		// note: this hindexIFramees all iFrame Lightboxes or the one identified by indexIFrame if given
		var selector,overlaySelector;
		overlaySelector = ".jBoxOverlay";
		if ( indexIFrame ) {
			selector = "."+indexIFrame+".jBox"
		}
		else {
			selector = ".jBox, .eGal, .nGal";
		}
		$(selector).animate({opacity: 'hide'}, 500);
		if ($(selector+" iframe").length > 0) { $(selector+" iframe").attr({src: ''}); }
		$(overlaySelector).animate({opacity: 'hide'}, 500, function() {
			if ($.browser.msie && $.browser.version <= 6) {
				$("select").css({visibility: "visible"});
			}
			$(selector).css({display:'none'});
			$(overlaySelector).css({display:'none'});
			if ( completionFunc ) {
				completionFunc();
			}
		});
	}
}

jQuery.fn.exists = function(){return jQuery(this).length>0;}