﻿/// <reference path="jquery-1.4.2.min.js" />
/// <reference path="jquery.animate.clip.js" />

$(document).ready(function() {
	General.init();

	if ($(".carousel").length > 0) Carousel.init();

	if ($(".gallery").length > 0)
	{
		Slideshow.init();
		VideoGallery.init();
	}
});

$.expr[":"].econtains = function(obj, index, meta, stack) {
	return (obj.textContent || obj.innerText || $(obj).text() || "").toLowerCase() == meta[3].toLowerCase();
}

var General = function()
{
	return {
		init : function()
		{
			$("#search .btn, #search-refine .btn").hover(General.btnOverHandler, General.btnOutHandler);
		},

		btnOverHandler : function(btn)
		{
		    this.src = this.src.replace("-over", "");
		},

		btnOutHandler : function(btn)
		{
		    this.src = this.src.replace(".png", "-over.png");
		}
	}
}();

var Carousel = function()
{
	var _tabSize = 120;
	var _visibleTabs = 7;
	var _position = 0;
	var _container = null;
	var _total = 0;
	
	return {

		init : function()
		{
			_container = $(".carousel ul");

			_total = $("a", _container).length;
			
			$(".carousel a.next").click(function() {
				Carousel.next();
				return false;
			});
			$(".carousel a.prev").click(function() {
				Carousel.prev();
				return false;
			});
		},

		next : function()
		{
			Carousel.move(1);
		},

		prev : function()
		{
			Carousel.move(-1);
		},

		move : function(num)
		{
			_position += num;

			if (_position < 0) _position = 0;
			if (_position > _total - _visibleTabs) _position = _total - _visibleTabs;

			num = _position * _tabSize;

			_container.animate({marginLeft: -num + "px"}, 200)
		},
		
		select : function(link)
		{
			$(".carousel li").removeClass("active");
			$(link).parent().addClass("active");
		}

	}
}();

var Slideshow = function()
{
	var _container = null;
	var _toggle = null;
	var _lastImg = null;
	var _curImg = null;
	var _ival = null;
	var _playing = false;
	var _fading = false;
	var _curNum = 0;
	var _images = [];

	return {

	init : function()
	{
		_container = $(".gallery .media");
		_curImg = _lastImg = $(".gallery .media img");
		_toggle = $(".gallery .slideshow-control a");

		$(".gallery .photos a").each(function(index) {
			var img = new Image();
			img.src = $(this).attr("href");

			_images[index] = img;
		});

		$(".gallery .photos ul").delegate("a", "click", function() {
			Slideshow.show($(this).text(), true);
			return false;
		});

		_toggle.click(function() {
			Slideshow.toggle($(this));
			return false;
		});
	},

	start : function()
	{
		if (_lastImg == null) Slideshow.show(_curNum);

		_playing = true;
		_fading = false;
		_toggle.text("Stop slideshow");
		_ival = setInterval("Slideshow.next()", 3000);
	},

	stop : function()
	{
		_playing = false;
		_toggle.text("Start slideshow");
		clearInterval(_ival);
	},

	toggle : function(link)
	{
		if (_playing)
		{
			Slideshow.stop();
		}
		else
		{
			Slideshow.start();
		}
	},

	next : function()
	{
		var num = _curNum + 1;

		if (num == _images.length) num = 0;

		Slideshow.show(num);
	},

	show : function(num, stop)
	{
		if (stop) Slideshow.stop();

		if (_fading) return;

		VideoGallery.clear();
		_toggle.show();

		if (typeof num == "string")
		{
			num = Number(num) - 1;
		}

		if ((_curImg != null) && (num == _curNum))
		{
			return;
		}

		$(".gallery .photos li").removeClass("active");
		$(".gallery .photos a:econtains('" + (num + 1) + "')").parent().addClass("active");

		_curNum = num;

		if (_curImg == null)
		{
			_container.html("");
			_curImg = $(_images[num]).appendTo(_container);
		}
		else
		{
			_fading = true;
			_curImg = $(_images[num]).appendTo(_container).hide().fadeIn(300, Slideshow.fadeEnd);
		}
	},

	fadeEnd : function()
	{
		if (_lastImg != null) _lastImg.remove();
		_lastImg = _curImg;
		_fading = false;
	},

	clear : function()
	{
		Slideshow.stop();

		$(".gallery .photos li").removeClass("active");

		if (_lastImg != null) _lastImg.remove();
		if (_curImg != null) _curImg.remove();

		_lastImg = null;
		_curImg = null;
		_curNum = 0;
		_toggle.hide();
	}

	}
}();

var VideoGallery = function() {
	var _ival = null;

	return {
		init : function()
		{
			if ($(".gallery .videos").length == 0)
			{
				Slideshow.start();
				return;
			}

			$(".gallery .videos ul").delegate("a", "click", function() {
				VideoGallery.show(Number($(this).text()) - 1);
				return false;
			});

			_ival = setTimeout("VideoGallery.show(0)", 1000);
		},

		show : function(num)
		{
			Slideshow.clear();
			$(".gallery .media").html(arVideos[num]);

			$(".gallery .videos li").removeClass("active");
			$(".gallery .videos a:econtains('" + (num + 1) + "')").parent().addClass("active");
		},

		clear : function()
		{
			clearInterval(_ival);
			$(".gallery .videos li").removeClass("active");
		}
	}

}();
