jQuery(document).ready(function() {
													 
	html.links();
	jQuery.easing.def = "easeInSine";
	
	jQuery("div.gallery a.middle2-close").click(function() {
		var block = jQuery(this).parents(".glass");
		block.slideUp(700, function() {
			jQuery("div.right").animate({marginTop: -62}, 200);			
		});
	});
	
	jQuery("div.left a.middle2-close").click(function() {
		jQuery(this).parents(".glass").slideUp(700);
	});
	
	jQuery("div.right a.middle2-close").each(function(i) {
		var offsets = [440, 220, 0];
		jQuery(this).click(function() {
			jQuery(this).parents(".glass").slideUp(700, function() {
				jQuery(this).parents(".glass-wrapper").children(".glass-header").children("div.h1").animate({left: 41 + offsets[i]}, 400);
			});
		});
	});
	
	jQuery("div#gallery div.h1 a").click(function() {
	  var block = jQuery(this).parents(".glass-wrapper").children(".glass");
		if (block.is(":visible"))
		{
			block.slideUp(700, function() {
				jQuery("div.right").animate({marginTop: -62}, 200);			
			});
		}
		else
		{
			jQuery("div.right").animate({marginTop: 0}, 200, function() {
				block.slideDown(700, function() {
					var g = new ijkGallery(jQuery('div#gallery'));
				});
			});
		}
	});
	
	jQuery("div.left div.h1 a").click(function() {
		var block = jQuery(this).parents(".glass-wrapper").children(".glass");
		if (block.is(":visible"))
		{
			block.slideUp(700);
		}
		else
		{
			block.slideDown(700);
			if (block.children(".middle2-wrapper").children(".middle2-content").hasClass("scroll-pane")) {
				block.children(".middle2-wrapper").children(".middle2-content").jScrollPane({"showArrows": true, "scrollbarOnLeft": true, "scrollbarWidth": 3, "scrollbarMargin": 0});
			}
		}
	});
	
	jQuery("div.right div.h1 a").each(function(i) {
		var times = [450, 300, 0];
		var offsets = [440, 220, 0];

		jQuery(this).click(function() {
			var block = jQuery(this).parents(".glass-wrapper").children(".glass");
			if (block.is(":visible")) {
				block.slideUp(700, function() {
					jQuery(this).parents(".glass-wrapper").children(".glass-header").children("div.h1").animate({left: 41 + offsets[i]}, times[i]);
				});
			}
			else {
				jQuery(this).parents(".glass-wrapper").children(".glass-header").children("div.h1").animate({left: 41}, times[i], jQuery.easing.def, function() {
					jQuery(this).parents(".glass-wrapper").children(".glass").slideDown(700);
					jQuery(this).parents(".glass-wrapper").children(".glass").children(".middle2-wrapper").children(".middle2-content").jScrollPane({"showArrows": true, "scrollbarOnLeft": true, "scrollbarWidth": 3, "scrollbarMargin": 0});
				});
			}
		});
	});

	jQuery(".icon").each(function(i) {
		var times = [450, 300, 0];
		var offsets = [440, 220, 0];

		jQuery(this).click(function() {
			var block = jQuery(this).parents(".glass-wrapper").children(".glass");
			if (block.is(":visible")) {
				block.slideUp(700, function() {
					jQuery(this).parents(".glass-wrapper").children(".glass-header").children("div.h1").animate({left: 41 + offsets[i]}, times[i]);
				});
			}
			else {
				jQuery(this).parents(".glass-wrapper").children(".glass-header").children("div.h1").animate({left: 41}, times[i], jQuery.easing.def, function() {
					jQuery(this).parents(".glass-wrapper").children(".glass").slideDown(700);
					jQuery(this).parents(".glass-wrapper").children(".glass").children(".middle2-wrapper").children(".middle2-content").jScrollPane({"showArrows": true, "scrollbarOnLeft": true, "scrollbarWidth": 3, "scrollbarMargin": 0});
				});
			}
		});
	});
});

html = {
	links: function() {
		jQuery("a[rel=external]").attr({target:"_blank"}).addClass("external");
	}
}


function ijkGallery(context, thS, thW)
{ 
 this.initialized = false;

 this.thumbSize = thS ? thS : 12;
 this.thumbWidth = thW ? thW : 70;
 this.thumbPosition = 0;
 this.thumbActive = 0;
	
 this.images = null;
	
 this.offset = 0;

 this.timer = null;
	
 this.left = null;
 this.right = null;
	
 this.initialize = function(context)
 {
  if (!this.initialized)
  {
   this.initialized = true;

			//this.div = jQuery("div.gallery div.scroll");
			//this.scroller = jQuery("div.gallery div.scroller");
			//this.list = jQuery("div.scroller ul");
			//this.thumbs = jQuery("div.gallery li");
			//this.images = jQuery("div.photo img");
			//this.left = jQuery("div.gallery a.arrow-left");
			//this.right = jQuery("div.gallery a.arrow-right");
   this.div = jQuery("div.scroll", context);
   this.scroller = jQuery("div.scroller", context);
   this.list = jQuery("div.scroller ul", context);
   this.thumbs = jQuery("div.scroller li", context);
   this.images = jQuery("div.photo img", context);
   this.left = jQuery("a.arrow-left", context);
   this.right = jQuery("a.arrow-right", context);
   this.details = jQuery('div.details', context);
	
   this.width = this.list.width();
   this.divWidth = this.div.width();

   var ext = this;
			
   this.left.click(function()	{
    ext.click(ext.thumbActive-1);
   });
   this.right.click(function()	{
    ext.click(ext.thumbActive+1);
   });
			
   this.thumbs.each(function(i) {
    var k = i;
    jQuery(this).click(function() {
     ext.click(k);
    });
    jQuery(this).hover(function() {
     if (i == 0 - ext.thumbPosition) {
      ext.start(1);
     }
     else if (i == ext.thumbSize - ext.thumbPosition - 1) {
      ext.start(-1);
     }
    }, function() {
     ext.stop();
    });
   });
  }
 };
	
 this.click = function(i)
 {
  if (i==-1) {
   i = this.images.length - 1;
   this.thumbPosition = -1;
   this.scroller.animate({left: 0 - (70  * (this.thumbs.length - this.thumbSize))}, 300);
  }
  else if (i==this.images.length) {
   i = 0;
   this.thumbPosition = 0;
   this.scroller.animate({left: 0}, 300);
  }
		
  jQuery(this.images.get(this.thumbActive)).removeClass("visible");
  jQuery(this.images.get(i)).addClass("visible");

  jQuery(this.thumbs.get(this.thumbActive)).removeClass("selected");
  this.thumbActive = i;
  jQuery(this.thumbs.get(this.thumbActive)).addClass("selected");
  this.details.html(jQuery('#imageCaption_' + jQuery(this.images.get(this.thumbActive)).parents('[photo_id]').attr('photo_id')).html());
	
  if (this.thumbActive >= this.thumbSize - this.thumbPosition)
   this.scroll(-1);
  else if (this.thumbActive < 0 - this.thumbPosition)
   this.scroll(1);
 };
	
 this.start = function(direction)
 {
  var ext = this;
  this.timer = setInterval(function(){ext.scroll(direction);}, 500);// "this.scroll(" + direction + ")", 500);
 };
	
 this.stop = function()
 {
  clearInterval(this.timer);
 };
	
 this.scroll = function(direction)
 {
  if (this._scrollable(direction))
  {
   this.thumbPosition = this.thumbPosition + direction;
   this.offset = this.thumbPosition * this.thumbWidth;
   this.scroller.animate({left: this.offset}, 300);
  }
 };
	
 this._scrollable = function(direction)
 {
  if (direction < 0)
  {
   if (this.width + this.thumbPosition * this.thumbWidth - 50 > this.divWidth)
    return true;
   else
    return false;
  }
  else
  {
   if (this.thumbPosition >= 0)
    return false;
   else
    return true;
  }
 }

 this.initialize(context);
 return this;
}
