// cufon config
function initCufon()
{
	Cufon.set('fontFamily', 'HelveticaNeue');
	// header replacements
	//Cufon.replace('#header label');
	Cufon.replace('.main-nav li a strong', { hover: true });
	Cufon.replace('.heading-outer1 h2');
	Cufon.replace('.heading-outer2 h2');
	Cufon.replace('.links-list a', { hover: true });
	Cufon.replace('#toolbar a', { hover: true });
	Cufon.replace('.submittaf', { hover: true });
	Cufon.replace('.heading1');
	Cufon.replace('.heading-outer3 h3');
	Cufon.replace('.more-link2 em');
	Cufon.replace('.heading-outer4 h2');
	Cufon.replace('.heading-outer5 h3');
	Cufon.replace('.heading-outer6 h3');
	Cufon.replace('.heading-outer7 h2');
	Cufon.replace('.pages-prev');
	Cufon.replace('.pages-next');
}


function initOntheMove() {
	if($('#entry-form').length) {
		$("#entry-form input[type='text'], #entry-form textarea").hint();
	}
}

function initToolbar(){
	$('a.print').click(function(){
		window.print();
	});

	$('.submittaf').click(function(){
		$('#tellafriend_form').submit();
	});
}


$(document).ready(function(){
	initCufon();
	initGallery(".gallery-init");
	linkNewWindow();

	initOntheMove();

	initToolbar();

});


var duration = 4000;
var gallery;
var timer;
var autoplay = true;
function initGallery(holder)
{
	gallery = $(holder).find("div.gallery ul");
	gallery.number = $(gallery).find("li").length;
	if(gallery.number > 1)
	{
		gallery.width = $(gallery).find("li").eq(0).width();
		gallery.current = 0;
		gallery.thumbnails = $(holder).find(".gallery-nav li");
		gallery.text = $(holder).find(".gallery-text li");

		$(gallery.text).css({"opacity": 0, "display": "none"});
		$(gallery.text).eq(0).css({"opacity": 1, "display": "block"});

		$(holder).find(".play-link").click(function()
		{
			autoplay = true;
			play();
			clearTimeout(timer);
			timer = setTimeout(function()
			{
				rotate();
			}
			, duration);
			return false;
		});

		$(holder).find(".stop-link").click(function()
		{
			if(autoplay)
			{
				autoplay = false;
				clearTimeout(timer);
			}
			else
			{
				autoplay = true;
				timer = setTimeout(function()
				{
					rotate();
				}
				, duration);
			}
			return false;
		});

		$(gallery.thumbnails).each(function(i, el)
		{
			$(el).click(function()
			{
				//autoplay = false;
				if(gallery.current != i)
				{
					$(gallery).animate({left: -i*gallery.width + "px"}, { duration: 400, queue:false });

					$(gallery.thumbnails).removeClass("active");
					$(el).addClass("active");

					$(gallery.text).css({"opacity": 0, "display": "none"}).eq(i).css({"opacity": 0, "display": "block"});
					$(gallery.text).eq(i).animate({"opacity": 1});
					clearTimeout(timer);

					gallery.current = i;

					timer = setTimeout(function()
					{
						rotate();
					}
					, duration);
				}
				return false;
			});
		});

		if(autoplay)
		{
			timer = setTimeout(function()
			{
				rotate();
			}
			, duration);
		}
	}
}

function play()
{
	if(++gallery.current >= gallery.number)
	{
		gallery.current = 0;
		$(".small-gallery").find(".stop-link").trigger('click');
	}

	$(gallery).animate({left: -gallery.current * gallery.width + "px"}, { duration: 400, queue:false });
	$(gallery.thumbnails).removeClass("active");
	$(gallery.thumbnails).eq(gallery.current).addClass("active");

	$(gallery.text).css({"opacity": 0, "display": "none"}).eq(gallery.current).css({"opacity": 0, "display": "block"});
	$(gallery.text).eq(gallery.current).animate({"opacity": 1}, 700);
	clearTimeout(timer);
}
function rotate()
{
	if(autoplay)
	{
		play();
		timer = setTimeout(function()
		{
			rotate();
		}
		, duration);
	}
}

function linkNewWindow(){
	var hostname = window.location.hostname;
	hostname = hostname.replace("www.","").toLowerCase();
	var a = document.getElementsByTagName("a");
	this.check = function(obj){
		var href = obj.href.toLowerCase();
		return (href.indexOf("http://")!=-1 && href.indexOf(hostname)==-1) ? true : false;
	};
	this.set = function(obj){
		obj.target = "_blank";
		obj.className = obj.className + " external";
	};
	for (var i=0;i<a.length;i++){
		if(check(a[i])) set(a[i]);
	};
};

