$(document).ready(function() {
	var lightboxOpen = false;
	if($.browser.msie && $.browser.version < 7) {
		var IE6 = true;
	} else {
		var IE6 = false;
	}
	if($.browser.msie) {
		var IE = true;
	} else {
		var IE = false;
	}
	
	$('.lb-trigger').click(function() {
		for(var x=0;x<$('.lb-trigger').length;x++) {
			$('.lb-trigger:eq('+x+')').addClass(x+'');
		}
		lightboxOpen = true;
	
		/* updated the codebase to support slide selection as the 2nd position class element, if number object **/	
		var classArray	= $(this).attr("class").split(' ');
		var imgIndex = ( typeof parseInt(classArray[1]) == 'number' ) ? parseInt(classArray[1]) - 1 : parseFloat($(this).attr("class").charAt($(this).attr("class").length - 1));
		
		/* Create Ordered List of Displayable Gallery Elements */
		var images;
		function createImages() {
			images = new Array();
			var galleryEls = $('.lb-gallery').children();
			
			var y=0;
			for(var x=0;x<galleryEls.length;x++) {
				if($(galleryEls[x])[0].tagName == "A") {
					images[y] = $(galleryEls[x]).children();
					y++;
				} else if($(galleryEls[x])[0].tagName == "EMBED") {
					images[y] = $(galleryEls[x]);
					y++;
				} else if($(galleryEls[x])[0].tagName == "OBJECT") {
					images[y] = $(galleryEls[x]);
					y++;
				}
			}
		}
		createImages();
		
		$('html, body').css("overflow","hidden");
		$('body').append('<div class="lightbox"></div>');
		$('.lightbox').append('<div class="curtain"></div>');
		$('.lightbox .curtain').height($('body').height());
		
		/* Create & Center-Align the Central Element, Append Image Element */
		$('.lightbox').append('<div class="the-middle"></div>');
		$('.the-middle').append('<div class="image-container"></div>');
		$('.image-container').append('<img class="the-image" src="'+$(images[imgIndex]).attr("src")+'" alt="'+$(images[imgIndex]).attr("alt")+'" title="'+$(images[imgIndex]).parent().attr("href")+'" />');
		if($(images[imgIndex]).parent().attr("href") == "#") {
			$('.the-image').attr("title","");
		}
		if($(images[imgIndex])[0].tagName == "EMBED" || $(images[imgIndex])[0].tagName == "OBJECT") {
			$('.lightbox .the-image').css("display","none");
					
			var theFlash = $(images[imgIndex]).wrap('<div></div>').parent().html();
			$(theFlash).appendTo('.lightbox .image-container');
			$(images[imgIndex]).parent().replaceWith($(images[imgIndex]).parent().html());
			createImages();
		}
		$('.lightbox .the-middle').css("left",($('.lightbox').width()/2) - ($('.lightbox .image-container').outerWidth()/2) + "px");
		
		/* Create the Image Dropshadow */
		if(!IE6) {
			$('.image-container').append('<div class="shadow n"></div>');
			$('.image-container').append('<div class="shadow ne"></div>');
			$('.image-container').append('<div class="shadow e"></div>');
			$('.image-container').append('<div class="shadow se"></div>');
			$('.image-container').append('<div class="shadow s"></div>');
			$('.image-container').append('<div class="shadow sw"></div>');
			$('.image-container').append('<div class="shadow w"></div>');
			$('.image-container').append('<div class="shadow nw"></div>');
			
			$('.the-middle').append('<div class="shadow title-n"></div>');
			$('.the-middle').append('<div class="shadow title-ne"></div>');
			$('.the-middle').append('<div class="shadow title-e"></div>');
			$('.the-middle').append('<div class="shadow title-se"></div>');
			$('.the-middle').append('<div class="shadow title-s"></div>');
			$('.the-middle').append('<div class="shadow title-sw"></div>');
			$('.the-middle').append('<div class="shadow title-w"></div>');
			$('.the-middle').append('<div class="shadow title-nw"></div>');
		}

		/* Create & Center-Align the Title Element */
		$('.the-middle').append('<div class="the-title"><span class="text">'+$(images[imgIndex]).attr("alt") + " (" + (imgIndex + 1) + " of " + images.length + ")"+'</span><img class="close" src="/images/'+si_lang_code+'/demo_close_btn.png" alt="Close Image Viewer" /></div>');
		if($(images[imgIndex])[0].tagName == "EMBED" || $(images[imgIndex])[0].tagName == "OBJECT") {
			$('.lightbox .the-title .text').text($(images[imgIndex]).attr("title") + " (" + (imgIndex + 1) + " of " + images.length + ")");
		}
		if(IE6) {
			$('.lightbox .the-title').width($('.lightbox .image-container').width());
		}
		
		/* Create & Position the Navigation Arrows */
		$('.lightbox').append('<img class="nav-left" src="/images/lightbox/demo_arrow_left.png" alt="Previous Image" />');
		$('.lightbox').append('<img class="nav-right" src="/images/lightbox/demo_arrow_right.png" alt="Next Image" />');
		if(IE6) {
			$('.lightbox .nav-left, .lightbox .nav-right').ifixpng();
		}
		
		if(images.length == 1) {
			$('.lightbox .nav-left, .lightbox .nav-right').css("display","none");
		}
		
		$('.lightbox .nav-left').css("left",parseFloat($('.lightbox .the-middle').css("left")) - 57 + "px");
		$('.lightbox .nav-right').css("left",parseFloat($('.lightbox .the-middle').css("left")) + $('.lightbox .image-container').outerWidth() + 6 + "px");
		$('.lightbox .nav-left, .lightbox .nav-right').css("top","245px");
		
		/* Place the Image Element Above Screen and Make it Visible */
		$('.lightbox .the-middle').css("top",$('.lightbox .the-middle').outerHeight() * -1 + "px");
		$('.lightbox .the-middle').css("display","block");
		
		/* Intro Animation */
		$('.lightbox .the-middle').css("top","145px");
		if(!IE) {
			$('.lightbox').animate({
				opacity: 1
			}, 'normal');
		}
		/* ORIGINAL ANIM
		$('.lightbox .the-middle').animate({
				"top": "145px"
		}, 'normal',function() {
			$('.lightbox .the-middle').animate({
					"top": "125px"
			}, 'normal');
		});*/
				
		var navLeftX = $('.lightbox .nav-left').css("left");
		var navRightX = $('.lightbox .nav-right').css("left");
		
		function pulseLeft() {
			if($('.lightbox .nav-left').css('left') == navLeftX) {
				$('.lightbox .nav-left').animate({
					'left': parseFloat(navLeftX) - 5 + "px"
				}, 'normal', function(){$(this).trigger('mouseover');});
			} else {
				$('.lightbox .nav-left').animate({
					'left': navLeftX
				},'normal', function(){$(this).trigger('mouseover');});
			}
			$('.lightbox .nav-left').mouseout(function() {
				$('.lightbox .nav-left').stop();
				$('.lightbox .nav-left').animate({
					"left": navLeftX
				}, 50);
			});
		}
		$('.lightbox .nav-left').bind("mouseover", pulseLeft);
		
		function pulseRight() {
			if($('.lightbox .nav-right').css('left') == navRightX) {
				$('.lightbox .nav-right').animate({
					'left': parseFloat(navRightX) + 5 + "px"
				}, 'normal', function(){$(this).trigger('mouseover');});
			} else {
				$('.lightbox .nav-right').animate({
					'left': navRightX
				},'normal', function(){$(this).trigger('mouseover');});
			}
			$('.lightbox .nav-right').mouseout(function() {
				$('.lightbox .nav-right').stop();
				$('.lightbox .nav-right').animate({
					"left": navRightX
				}, 50);
			});
		}
		$('.lightbox .nav-right').bind("mouseover", pulseRight);
		
		/* Navigation Action */
		function nudgeArrows() {
			$('.lightbox .nav-left').animate({
				"left": ($('.lightbox').outerWidth()/2) - ($('.lightbox .image-container').outerWidth()/2) - 57 + "px"
			}, 50);
			$('.lightbox .nav-right').animate({
				"left": ($('.lightbox').outerWidth()/2) - ($('.lightbox .image-container').outerWidth()/2) + $('.lightbox .image-container').outerWidth() + 6 + "px"
			}, 50, function() {
				navLeftX = parseFloat($('.lightbox .the-middle').css("left")) - 57 + "px";
				navRightX = parseFloat($('.lightbox .the-middle').css("left")) + $('.lightbox .the-middle').outerWidth() + 6 + "px";
			});
		}
		
		$('.lightbox .nav-left').click(function() {
			$('.lightbox .nav-left, .lightbox .nav-right').unbind("mouseout");
			$('.lightbox .nav-left, .lightbox .nav-right').unbind("mouseover");
			
			if(imgIndex > 0) {
				imgIndex--;
			} else {
				imgIndex = images.length - 1;
			}
			$('.lightbox .image-container').animate({
				"left": "-=100px",
				"opacity": "hide"
			}, 'fast', function() {
				if($(images[imgIndex])[0].tagName == "EMBED" || $(images[imgIndex])[0].tagName == "OBJECT") {
					$('.lightbox .the-image').css("display","none");
					$('.lightbox .image-container embed, .lightbox .image-container object').remove();
					
					var theFlash = $(images[imgIndex]).wrap('<div></div>').parent().html();
					$(theFlash).appendTo('.lightbox .image-container');
					$(images[imgIndex]).parent().replaceWith($(images[imgIndex]).parent().html());
					createImages();
					
					$('.lightbox .the-title .text').text($(images[imgIndex]).attr("title") + " (" + (imgIndex + 1) + " of " + images.length + ")");
				} else if($(images[imgIndex])[0].tagName == "IMG") {
					$('.lightbox .image-container embed, .lightbox .image-container object').remove();
					$('.lightbox .the-image').css("display","block");
					$('.lightbox .the-image').attr("src",$(images[imgIndex]).attr("src"));
					$('.lightbox .the-image').attr("alt",$(images[imgIndex]).attr("alt"));
					if($(images[imgIndex]).parent().attr("href") != "#")
						$('.lightbox .the-image').attr("title",$(images[imgIndex]).parent().attr("href"));
					else
						$('.lightbox .the-image').attr("title","");
					$('.lightbox .the-title .text').text($(images[imgIndex]).attr("alt") + " (" + (imgIndex + 1) + " of " + images.length + ")");
				}
				if(IE6) {
					$('.lightbox .image-container').css("width","auto");
					$('.lightbox .the-title').width($('.lightbox .image-container').width());
					$('.lightbox .image-container').width($('.lightbox .the-title').width());
				}
								
				$('.lightbox .the-middle').css("left",$('.lightbox').outerWidth()/2 - ($('.lightbox .image-container').outerWidth()/2) + "px");
				nudgeArrows();
				$('.lightbox .image-container').css("left","100px");
				$('.lightbox .image-container').animate({
					"left": "0px",
					"opacity": "show"
				}, 'fast', function() {
					if(($('.lightbox .image-container').outerHeight() + 245) > $('.lightbox').height()) {
					   $('.lightbox .curtain').height($('.lightbox .image-container').outerHeight() + 245);
					}
				    $('.lightbox .curtain').width($('.lightbox').width());
					$('.lightbox .nav-left').bind("mouseover", pulseLeft);
					$('.lightbox .nav-right').bind("mouseover", pulseRight);
				});
				
			});
		});
		$('.lightbox .nav-right').click(function() {
			$('.lightbox .nav-left, .lightbox .nav-right').unbind("mouseout");
			$('.lightbox .nav-left, .lightbox .nav-right').unbind("mouseover");
			
			if(imgIndex < (images.length-1)) {
				imgIndex++;
			} else {
				imgIndex = 0;
			}
			$('.lightbox .image-container').animate({
				"left": "+=100px",
				"opacity": "hide"
			}, 'fast', function() {
				if($(images[imgIndex])[0].tagName == "EMBED" || $(images[imgIndex])[0].tagName == "OBJECT") {
					$('.lightbox .the-image').css("display","none");
					$('.lightbox .image-container embed, .lightbox .image-container object').remove();
					
					var theFlash = $(images[imgIndex]).wrap("<div></div>").parent().html();
					$(theFlash).appendTo('.lightbox .image-container');
					$(images[imgIndex]).parent().replaceWith($(images[imgIndex]).parent().html());
					createImages();
					
					$('.lightbox .the-title .text').text($(images[imgIndex]).attr("title") + " (" + (imgIndex + 1) + " of " + images.length + ")");
				} else {
					$('.lightbox .image-container embed, .lightbox .image-container object').remove();
					$('.lightbox .the-image').css("display","block");
					$('.lightbox .the-image').attr("src",$(images[imgIndex]).attr("src"));
					$('.lightbox .the-image').attr("alt",$(images[imgIndex]).attr("alt"));
					if($(images[imgIndex]).parent().attr("href") != "#")
						$('.lightbox .the-image').attr("title",$(images[imgIndex]).parent().attr("href"));
					else
						$('.lightbox .the-image').attr("title","");
					$('.lightbox .the-title .text').text($(images[imgIndex]).attr("alt") + " (" + (imgIndex + 1) + " of " + images.length + ")");
				}
				if(IE6) {
					$('.lightbox .image-container').css("width","auto");
					$('.lightbox .the-title').width($('.lightbox .image-container').width());
					$('.lightbox .image-container').width($('.lightbox .the-title').width());
				}
				
				$('.lightbox .the-middle').css("left",($('.lightbox').outerWidth()/2 - ($('.lightbox .image-container').outerWidth()/2)) + "px");
				nudgeArrows();
				$('.lightbox .image-container').css("left","-100px");
				$('.lightbox .image-container').animate({
					"left": "0px",
					"opacity": "show"
				}, 'fast', function() {
					if(($('.lightbox .image-container').outerHeight() + 245) > $('.lightbox').height()) {
					   $('.lightbox .curtain').height($('.lightbox .image-container').outerHeight() + 245);
					}
					$('.lightbox .curtain').width($('.lightbox').width());
					$('.lightbox .nav-left').bind("mouseover", pulseLeft);
					$('.lightbox .nav-right').bind("mouseover", pulseRight);
				});
			});
		});
		$('.the-image').mouseover(function() {
			if($(images[imgIndex]).parent().attr("href") != "#") {
				window.status = $(images[imgIndex]).parent().attr("href");
				$(this).css("cursor","pointer");
			}
		});
		$('.the-image').mouseout(function() {
			window.status = "";
			$(this).css("cursor","default");
		});
		$('.the-image').click(function() {
			if($(images[imgIndex]).parent().attr("href") != "#")
				window.open($(images[imgIndex]).parent().attr("href"));
		});
		$('.lightbox .close').click(function() {
			/* Outro Animation */
			$('.lightbox').animate({
				opacity: 0
			}, 'fast', function() {
				$('.lightbox').remove();
				$('html, body').css("overflow","auto");
			});
			/*
			ORIGINAL ANIM
			$('.lightbox .the-middle').animate({
				"top": "100px"
			}, 'fast', function() {
				$('.lightbox .the-middle').animate({
					"top": $('.lightbox .the-middle').outerHeight() * -1 + "px"
				}, 'fast', function() {
					$('.lightbox').remove();
					$('html, body').css("overflow","scroll");
				});
			});
			*/
			lightboxOpen = false;
		});
		return false;
	});
});
