/*
	This file is intended as a storage location for common site functions,
	features, possibly setting, and overall basic site functionality. Please 
	make an effort to keep it clean of rift-raft, development code, or code 
	that does not pertain to the base functionality of the site.

	Remember to comment!
*/

////

/* Common Functions */
function setHeaderHeight() {
	var weather = document.getElementById('weather');
	var header = document.getElementById('header-container');
	if(parseInt(weather.offsetHeight) > parseInt(header.offsetHeight)) {
		$(header).css('height', parseInt(weather.offsetHeight) + 5);
	}
}


function setAddthis() {
	var addthis = document.getElementById('addthis');
	/*var url = 'http://s7.addthis.com/js/250/addthis_widget.js#username=enskycorp';
	$('script[src="' + url + '"]',c).remove();
	$(c).append('<script type="text/javascript" src="'+url+'"></script>');*/
	$('a.addthis_button',addthis).attr('addthis:url',location.href);
}

// Code for when the DOM is ready to be manipulated
$(document).ready(function() {
	 // Alter the header height if the weather widget gets too big
	setHeaderHeight();
	setAddthis();
	 // Add corners to video area
	$('#stage, #contact').corner();
	$('#up').corner('top');
	$('#down').corner('bottom');
	 
	 // Reset the video list's scroll position
	 document.getElementById('list').scrollTop = 0;
	 
	 //	Unobstructive method for new window popup
	 //	Just give a <a> tag the class 'new-window' and jquery will do the rest
	 // Should work for any elements dynamically added
	 //	Example: <a href="http://www.google.com" rel="new-window">

	$('a[rel*="new-window"]').live('click', function() {
		window.open(this.href,'_blank');
		return false;
	});
	
	// Setup the scrolling for both the up and down buttons
	$('#down').hover(function() {
			// Stop any animation on the video list
			$('#list').stop(true);
			var timer = setInterval(function() {
			var list = document.getElementById('list');
			list.scrollTop = list.scrollTop + 5;
		},
		10);
		$(this).data('timer', timer);
	},
	function() {
		clearInterval($(this).data('timer'));
	});
	$('#up').hover(function() {
			// Stop any animation on the video list
			$('#list').stop(true);
			var timer = setInterval(function() {
			var list = document.getElementById('list');
			list.scrollTop = list.scrollTop - 5;
		},
		10);
		$(this).data('timer', timer);
	},
	function() {
		clearInterval($(this).data('timer'));
	});

	// Set the video list equal to the height of the stage
	// Get stage's height
	var stage = document.getElementById('stage');
	if(stage != null) {
		stage = stage.offsetHeight;
		// Trim the height of the up and down buttosn from the height
		var up = document.getElementById('up');
		var down = document.getElementById('down');
		if (up != null && down != null) {
			var trimmed =  stage - up.offsetHeight - down.offsetHeight;
			// Set the height
			$('#video-strip').height(stage).children('#list').height(trimmed);
		}
	}
});

// Code for when window/page has finished loading
// Non-essential code should be placed here, such as the weather widget
$(window).load(function() {
	// Grab weather information and replace #weather's content every 15 minutes
	setInterval(function() { 
		$.get('weather.php', function(data) { 
			$('#weather').html(data);
			setHeaderHeight();
		}, 'html')}		
	, 15 * 60000);
});
