var articleTimeout = 6000;
var currentArticle = 1;

function articlesTicker()
{	
	$("#articles #articletitle" + currentArticle).fadeOut("fast", function()
	{
		if (++currentArticle > $("#articles .title div").size())
			currentArticle = 1;
			
		$("#articles #articletitle" + currentArticle).fadeIn("fast", function()
		{
			setTimeout("articlesTicker();", articleTimeout);
		});
	});
}
function toggle(what){
	alert(what);
}
$(document).ready(function() {

	$('div.accordion> div.entry').hide();
	
	$('div.accordion> h3').click(function() {
	
		// Hakun molested this 20/05/09.
		var pick = 0;
		for(var i=1;i<4;i++){ 
			if(this.id == 'accordion-header-'+i)pick = i;
			document.getElementById('r'+i).checked = false;
			document.getElementById('accordion-header-'+i).className = 'outer';
			document.getElementById('accordion-content-'+i).style.display = 'none';
		}
		this.className = 'outer selected';
		document.getElementById('accordion-content-'+pick).style.display = 'block';
		document.getElementById('r'+pick).checked = true;
		
		
		
		return true;
		var $parent = $(this).parent().attr("id");
		var $id = $(this).attr("name").split("-")[2];

		var $nextDiv = $(this).next();
		var $visibleSiblings = $nextDiv.siblings('div:visible');
				
		if ($visibleSiblings.length )
		{
			$visibleSiblings.slideUp(50, function() {
				$('#' + $parent + '> h3').removeClass('selected');
				showAccordionEntry($nextDiv, $parent, $id); // highlight cur
				//$nextDiv.nextAll("h3").addClass('selected'); // highlight all below
			});
		}
		else
		{
			showAccordionEntry($nextDiv, $parent, $id);
		}
	});

	// expand last
	$('div.accordion').each(function() {
		$(this).children('h3:last').click();
	});

	//
	// Tag Cloud click behaviour
	//
	$('.taglink').click(function() {
		var tagText = $(this).html();

		// Get location of destination
		var destCoords = GetCoords( this );
		var distance = 200;
		var destX = destCoords[0] - distance;
		var destY = destCoords[1] - distance;
		
		// Create a duplicate tag to animate (this prevents disturbing the
		// layout of the tag cloud)
		$(this).clone().attr( 'class', 'tmpTag select-free' )
				.css( 'position', 'absolute' )
				.css( 'color', '#000' )
				.css( $(this).offset() )
				//.css( 'background-color', '#f00' )
				.css( 'z-index', '9999' )
				.css( 'text-decoration', 'none' )
				.html(tagText + '<!--[if lte IE 6.5]><iframe></iframe><![endif]-->')
				.appendTo( $('body') );

		// Change the dest selector(s) on packages to match clicked tag		
		$('.destlist').each(function(){
			LoadDestSelector( tagText.toLowerCase(), $(this) );
		});
		 
		destX = $('div.accordion> div.entry:visible .destlist').offset().left;
		destY = $('div.accordion> div.entry:visible .destlist').offset().top;

		// Do a nice animation... sorry, visual feedback to user :)
		$('.tmpTag').animate(
			{ left: destX, top: destY, opacity: 0.1, fontSize: 13 }, // params
			1000, // speed
			function(){  // callback, remove the duplicate elmment at the end
				$('.tmpTag').fadeOut('fast', function(){
					$('.tmpTag').remove();
				});
			}
		);
		
		// Return false to prevent link from being followed
		return false;
	});
	
	setTimeout("articlesTicker();", articleTimeout);
});


/** Loops through the options and sets value to the option that corresponds with destName
*
* @param string destName         Destination to match
* @param jQuery destSelectObj    The select box object
*/
function LoadDestSelector( destName, destSelectObj )
{
	destName = destName.toLowerCase();
	destName = destName.replace(/&nbsp;/gi, " ");

	destSelectObj.children().each( function() {
		var tempstr = $(this).text();
		tempstr = tempstr.toLowerCase();
		tempstr = tempstr.replace(/&nbsp;/gi, " ");
		
		var retVal = tempstr.indexOf( destName );
		
		if( retVal > -1 )
		{	$(this).parent().attr('value', $(this).attr('value') );
		}
	});
}

/** Find the actual coordinates of an element
*/
function GetCoords( obj )
{
	var curleft = curtop = 0;
	if( obj.offsetParent )
	{	do 
		{	curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
		while( obj = obj.offsetParent )
	}
	return [curleft,curtop];
}

function showAccordionEntry($nextDiv, $parent, $id)
{
	$nextDiv.slideDown(300); // 10
	$("#" + $parent + "> h3[name=accordion-header-" + $id + "]").addClass('selected');
}

/** Navigate to destination in research centre
*/
function gotoDest( destName )
{
	//destName = destName.toLowerCase();
	//window.location = "/researchcentre.php?dest=" + destName;
	
	if (destName)
	{
		destName = destName.replace(/ /gi, "_");
		window.location = "/resorts/" + destName + "-holiday_offers.html";
	}
	else
	{
		window.location = "/researchcentre.php";
	}
}

/** Navigate to destination in holiday reviews
*/
function gotoVideos( destName )
{
	if (destName)
	{
		destName = destName.replace(/ /gi, "_");
		window.location = "/videoreviews/holidays-to_" + destName + ".html";
	}
	else
	{
		window.location = "/videos.php";
	}
}

function validate()
{
	var errors = [];
	
	if ($('#w').val() == "")
		errors.push("No resort selected");
	
	if (parseInt($('#c').val()) + parseInt($('#i').val()) > 4)
		errors.push("Children + Infants must not exceed 4 people");
			
	return displayerrors(errors);
}

function autoIframe(frameId)
{
	try
	{
		/*var frame = document.getElementById(frameId);
		var innerDoc = (frame.contentDocument) ? frame.contentDocument : frame.contentWindow.document;
		var objToResize = (frame.style) ? frame.style : frame;
		objToResize.height = innerDoc.body.scrollHeight + 10;
		alert(frame.contentWindow.document.height);*/
		var iframe = parent.document.getElementById(frameId);
		var styleheight = (iframe.contentDocument.body.scrollHeight) + "px";
		iframe.style.height = styleheight;
		alert(iframe.style.height);
	}
	catch(err)
	{
		window.status = err.message;
	}
}