/*
 * jQuery EasIng v1.1.2 - http://gsgd.co.uk/sandbox/jquery.easIng.php
 *
 * Uses the built In easIng capabilities added In jQuery 1.1
 * to offer multiple easIng options
 *
 * Copyright (c) 2007 George Smith
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 */
jQuery.extend( jQuery.easing,
{
	easeinout: function(x, t, b, c, d) {
		if (t < d/2) return 2*c*t*t/(d*d) + b;
		var ts = t - d/2;
		return -2*c*ts*ts/(d*d) + 2*c*ts/d + c/2 + b;		
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	},
	easeOutSine: function (x, t, b, c, d) {
		return c * Math.sin(t/d * (Math.PI/2)) + b;
	}
});

$(document).ready(function() {
	$(function(){
		try {
			$(".gallerybox a, .gallerybox4 a, .gallerybox5 a")
				.parent().append("<a class='overlay' style='position:relative;margin:-346px 0 0 0;width:134px;height:346px;background:#e3c3eb;display:block;z-index:2;'></a>");

			$(".overlay")
				.each(function(){
					$(this).css({ opacity: 0 });
					$(this).attr("href", $(this).parent().children("a").attr("href"));
				})
				.mouseover(function(){
					$(this).parent().children("a").children("span").stop().animate({ paddingLeft: 15 }, 200, "easeOutExpo");
					$(this).stop().animate({ opacity: 0.4 }, 200, "easeOutExpo");
				})
				.mouseout(function(){
					$(this).parent().children("a").children("span").stop().animate({ paddingLeft: 5 }, 800, "easeOutExpo");
					$(this).stop().animate({  opacity: 0 }, 3000, "easeOutExpo");
				});
				
			$("#close")
				.mouseover(function(){
					$(this).stop().animate({ opacity: 0.7 }, 200, "easeOutExpo");
				})
				.mouseout(function(){
					$(this).stop().animate({  opacity: 1 }, 1000, "easeOutExpo");
				});
				
			// GALLERY
			var imageCount = -1;
			var currentImage = 0;
			var galleryComplete = false;
			var imageAnimation = true;

			$("#gallery div:eq(0)")
				.css({ opacity: 0 })
				.stop().animate({ opacity: 1 }, 1000, "linear", function(){ imageAnimation = false; });

			$("#gallery div").each(function(index){
				imageCount++;
			});

			$("#arrowleft")
				.css("opacity", 0.4)
				.removeAttr("href")
				.attr("nohref", "")
				.addClass("arrowleft_inactive")
				.click(function(){
					if (!imageAnimation) {
						if (currentImage != 0) {
							imageAnimation = true;
							$("#gallery div:eq(" + currentImage + ")").animate({ opacity: 0 }, 800, "easeOutExpo", function(){ $(this).css("display", "none"); });
							$("#gallery div:eq(" + (currentImage - 1) + ")")
								.css({
									"display": "block",
									"opacity": 0
								})
								.animate({ opacity: 1 }, 500, function(){ imageAnimation = false; });

							currentImage--;
							if (currentImage == 0) {
								$(this)
									.css("opacity", 0.4)
									.removeAttr("href")
									.attr("nohref", "")
									.addClass( "arrowleft_inactive");
							}
							if ($("#arrowright").css("opacity") == 0.4) {
								$("#arrowright")
									.css("opacity", 1)
									.removeAttr("nohref")
									.attr("href", "#")
									.removeClass("arrowright_inactive");
							}
						}
						if (galleryComplete) galleryComplete = false;
					}
					return false;
				})

			$("#arrowright")
				.click(function(){
					if (!imageAnimation) {
						if (!galleryComplete) {
							imageAnimation = true;
							$("#gallery div:eq(" + currentImage + ")").animate({ opacity: 0 }, 800, "easeOutExpo", function(){ $(this).css("display", "none"); });
							$("#gallery div:eq(" + (currentImage + 1) + ")")
								.css({
									"display": "block",
									"opacity": 0
								})
								.animate({ opacity: 1 }, 500, function(){ imageAnimation = false; });
							
							currentImage++;
							if (currentImage == imageCount) {
								galleryComplete = true;
								$(this)
									.css("opacity", 0.4)
									.removeAttr("href")
									.attr("nohref", "")
									.addClass("arrowright_inactive");
							}
							if ($("#arrowleft").css("opacity") == 0.4) {
								$("#arrowleft")
									.css("opacity", 1)
									.removeAttr("nohref")
									.attr("href", "#")
									.removeClass( "arrowleft_inactive");
							}
						}
					}
					return false;
				})
		} catch(e) {}
	});
});
