jQuery(document).ready(function() {
	SetExternalLinks();
	SetRollOvers();
	Equalise($('.page-pod-item'));
	Equalise($('.gallery .album-content'));
	SetBiggerClick();

	//Alpine only
	SetEmailLinks();
	InitVidoLinks();
	InitGalleries();
});

//==================================================================== GENERAL FUNCTIONS ====================
function SetExternalLinks(){
	//Purpose: checks all <a>'s in the doc, if it has a rel attribute of 'external', sets to open in new window
	$('a[rel=external]').click(function(){ window.open(this.href); return false; });
}

function Equalise(elms) {
    // usage:
    // Equalise($(".list li"));
    // Equalise($("#nav, #content"));
    var highest = 0;
    for (i = 0; i < elms.length; i++) { if ($(elms[i]).height() > highest) highest = $(elms[i]).height(); }
    for (i = 0; i < elms.length; i++) { if ($(elms[i]).height() < highest) $(elms[i]).height(highest); }
}

function SetRollOvers() {
	// usage: add the class 'rollover' to image elements
	jQuery("input[type=image].rollover, a.rollover img, img.rollover").hover(function() {
		$(this).attr("src", jQuery(this).attr("src").replace(/(\.[^.]+)$/, 'Hi$1'));
	}, function() {
		$(this).attr("src", jQuery(this).attr("src").replace(/Hi(\.[^.]+)$/, '$1'));
	});
}
function SetBiggerClick() {
	// make a '.clickable' element activate the first link it contains (when clicking anywhere on it)
	$(".clickable").biggerlink(); // requires: jquery.biggerlink.pack.js
}

function SetEmailLinks() {

	var winterDNS = 'winterpark.co.nz';
	var alpineDNS = 'alpinesprings.co.nz';

	if ($('body.winter-park').length > 0) {
		$('a').each(function() {
			if ($(this).attr('href').indexOf("@" + alpineDNS) > 0) {
				var link = $(this).attr('href');
				var newlink = link.replace(alpineDNS, winterDNS);
				$(this).attr('href', newlink);
				var html = $(this).html().replace("mailto:","");
				var newhtml = html.replace(alpineDNS, winterDNS);
				$(this).html(newhtml);
				//alert("link: " +newlink +" html: " +  newhtml);
			}
		});
	} else if ($('body.springs-spa').length > 0) {
		$('a').each(function() {
			if ($(this).attr('href').indexOf("@" + winterDNS) > 0) {
				var link = $(this).attr('href');
				var newlink = link.replace(winterDNS, alpineDNS);
				$(this).attr('href', newlink);
				var html = $(this).html().replace("mailto:", "");
				var newhtml = html.replace(winterDNS, alpineDNS);
				$(this).html(newhtml);
			}
		});
	}
}

//plays the videos gallery in the slideshow
function InitVidoLinks() {
	var link = '<a href="#" id="play-video">Play video</a>';
	$('.location-pod').prepend(link);
	$('.home-intro-holder').append(link);

	$('#play-video').click(function() {
		flashMovie = document.getElementById("SlideshowPro");
		flashMovie.sspPlayVid();
		return false;
	});
}

function InitGalleries() {
	//== Layout and Style ==
	
		//hide & style galleries
		$('div.gallery').addClass('gallery-closed');
		$('.gallery-closed').find('.gallery-items').hide();
		$('.gallery-closed .album-content').css({ 'cursor': 'pointer' });
		
		//add java only expand link
		$('.gallery .album-content').append('<a class="more-link" href="#">View Gallery Thumbnails</a>');
		
		//hover styles
		$('.gallery-closed').hover(
			function() {
				$(this).addClass('gallery-hover');
			}, function() {
				$(this).removeClass('gallery-hover');
			}
		);

	//== Fucntionality ==
	
		//expand function
		$('.gallery .album-content .more-link').click(function() {
			var gall = $(this).parent().parent();

			if (gall.hasClass('gallery-closed')) {
				gall.addClass('gallery-open');
				gall.removeClass('gallery-closed');
				gall.find('ul').fadeIn('fast');
			} else {
				gall.addClass('gallery-closed');
				gall.removeClass('gallery-open');
				gall.find('ul').fadeOut('fast');
			}
			
			if ($(this).html() == "View Gallery Thumbnails") {
				$(this).html('Hide Gallery Thumbnails');
			} else {
				$(this).html('View Gallery Thumbnails');
			}
			return false;
		});
		
		//link whole area to expand function
		$('.gallery-closed .album-content').click(function() {
			$(this).find('.more-link').trigger('click');
		});


	//== Thick Box addon ==
	
		//Set gallery groups
		$('.gallery').each(function() {
			var title = $(this).find('h3').html();
			title = title.replace(/ /g, "-");
			$(this).find('.gallery-items li a').attr('rel', 'gallery-'+title);
		});
		
		//Set thickbox to images
		$('.gallery-closed').find('.gallery-items li a').addClass('thickbox');
}