/*** THEME SPECIFIC CONSTANTS ***/
var _hc = 
{
	// Round corners
	cornerSizeHeader: 10,
	cornerSizeContent: 6,
	// Rotating feature
	featureFlipTimeout: 4000,
	featureFlipFadeDuration: 'slow'
}



/*** NON CSS3 ROUND CORNERS ***/

jQuery(window).ready(function()
{
	if (hc_fakeBorderRadius())
	{
		hc_applyRoundCornersFeature(jQuery('#feature'));
		hc_applyRoundCornersFeature(jQuery('#pageBanner'));
	}
});

jQuery(window).load(function()
{
	if (hc_fakeBorderRadius())
	{
		var images = jQuery('#content img');
		images.each(hc_applyRoundCornersContent);
	}
});

function hc_fakeBorderRadius()
{
	// IE 6-8
	var version = parseFloat(jQuery.browser.version);
	if (jQuery.browser.msie && version >= 6 && version <= 8)
		return true;

	// WebKit before 500 (older than Safari 3)
	version = parseFloat(jQuery.browser.version);
	if (jQuery.browser.webkit && version < 500)
		return true;

	// Mozilla Firefox before 3 (and other gecko based browsers)
	version = parseFloat(jQuery.browser.version.substring(0, jQuery.browser.version.lastIndexOf('.')));	
	if (jQuery.browser.mozilla && version < 1.9)
		return true;
			
	return false;
}

function hc_applyRoundCornersContent(i, element)
{
	var content = jQuery('#content');
	var img = jQuery(element);
	var x = img.offset().left - content.offset().left;
	var y = img.offset().top - content.offset().top;
	var w = img.innerWidth();
	var h = img.innerHeight();
	
	content.append('<div class="roundedCorners topleft" style="left: ' + x + 'px; top: ' + y + 'px" />');
	content.append('<div class="roundedCorners topright" style="left: ' + (x + w - _hc.cornerSizeContent) + 'px; top: ' + y + 'px" />');
	content.append('<div class="roundedCorners bottomleft" style="left: ' + x + 'px; top: ' + (y + h - _hc.cornerSizeContent) + 'px" />');
	content.append('<div class="roundedCorners bottomright" style="left: ' + (x + w - _hc.cornerSizeContent) + 'px; top: ' + (y + h - _hc.cornerSizeContent) + 'px" />');
}

function hc_applyRoundCornersFeature( feature )
{
	if (feature.get(0) == undefined)
		return;
	var w = feature.innerWidth();
	var h = feature.innerHeight();

	feature.append('<div class="roundedCorners topleft" style="left:0px; top: 0px" />');
	feature.append('<div class="roundedCorners topright" style="left: ' + (w - _hc.cornerSizeHeader) + 'px; top: 0px" />');
	feature.append('<div class="roundedCorners bottomleft" style="left: 0px; top: ' + (h - _hc.cornerSizeHeader) + 'px" />');
	feature.append('<div class="roundedCorners bottomright" style="left: ' + (w - _hc.cornerSizeHeader) + 'px; top: ' + (h - _hc.cornerSizeHeader) + 'px" />');
}

/*** ROTATING FEATURE IMAGE ***/

// Constants

jQuery(document).ready(function()
{
	// Make sure we don't have anything queued up when we start
	var container = jQuery('#feature').get(0);
	if( container != undefined )
		container.nextIndex = -1;

	// Page DOM is ready, reveal first feature and hide the others
	var features = jQuery('#feature li');
	features.each(function(i, element)
	{
		if (i != 0)
		{
			jQuery(element).hide();
			jQuery(element).css('z-index', 1);
		}
		else
		{
			jQuery(element).fadeIn(_hc.featureFlipFadeDuration);
			jQuery(element).css('z-index', 1);
		}
	});

	// Hook up the indicator links
	featureIndicatorLinks = jQuery('#featureIndicator li a');
	featureIndicatorLinks.each(function(i, link)
	{
		jQuery(link).click(function()
		{
			// Check if we have an animation going on, then queue up the switch
			var container = jQuery('#feature').get(0);
			var currentAnimatingItem = features.eq(container.animatingIndex);
			if (currentAnimatingItem.queue().length > 0)
				container.nextIndex = i;
			else
				hc_showFeature(i);
			// Return false to avoid default link navigation
			return false;
		});
	});

});

jQuery(window).load(function()
{
	// Page is fully loaded, start feature animation

	// Set the index of the current active feature and start flipping
	var container = jQuery('#feature').get(0);
	if (container != undefined)
	{
		container.currentIndex = 0;
		container.animatingIndex = 0;
		container.timeout = setTimeout(hc_showNextFeature, _hc.featureFlipTimeout);
	}	
});

function hc_showNextFeature()
{
	var container = jQuery('#feature').get(0);
	var features = jQuery(container).children('li');
	var nextIndex = container.currentIndex + 1;
	if( nextIndex >= features.size() )
		nextIndex = 0;
	hc_showFeature(nextIndex);
}

function hc_showFeature(index)
{
	// Get container
	var container = jQuery('#feature').get(0);
	
	// Make sure we don't switch automatically soon if this was a manual switch
	clearTimeout(container.timeout);

	// Clicked the active feature
	if (container.currentIndex == index)
	{
		// Restart timeout
		container.timeout = setTimeout(hc_showNextFeature, _hc.featureFlipTimeout);
		return;
	}	

	// Get features
	var features = jQuery(container).children('li');
	
	// Make sure all inactive features are hidden
	features.each(function(i, element)
	{
		if (i != index && i != container.currentIndex)
			jQuery(element).hide();
	});
	
	// Change z-indexes of current and next item
	features.eq(index).css('z-index', 2);
	features.eq(container.currentIndex).css('z-index', 1);

	// Update indicators
	jQuery('#featureIndicator li').each(function(i, indicator)
	{
		if (i == index)
			jQuery(indicator).addClass('active');
		else
			jQuery(indicator).removeClass('active');
	});

	// Fade in next index
	container.animatingIndex = index;
	features.eq(container.animatingIndex).fadeIn(_hc.featureFlipFadeDuration,
		function()
		{
			// Hide previously active feature
			features.eq(container.currentIndex).hide();
			// Update current index
			container.currentIndex = container.animatingIndex;

			// Check if we have a switch queued up
			if (container.nextIndex != -1)
			{
				var nextIndex = container.nextIndex;
				container.nextIndex = -1;
				hc_showFeature(nextIndex);
			}
			else
			{
				// Start flip-switch timeout
				clearTimeout(container.timeout);
				container.timeout = setTimeout(hc_showNextFeature, _hc.featureFlipTimeout);
			}
		});	
}