$(document).ready(function(){	
	// applying the settings
	$("#tour").Accordion({
		active: 'h4.selected',
		header: 'h4.head',
		alwaysOpen: false,
		animated: true,
		showSpeed: 400,
		hideSpeed: 400
	});
	$("h5.roomname").click(function(){
			$("#tourviewer").empty();
			$(this).siblings().clone().appendTo("#tourviewer");
			//$firstcaption = $("#tourviewer img").eq(0).attr("alt");
			makeCycle("#tourviewer a");
			//alert($("#tourviewer a").eq(0).text());
	});
});

function makeCycle($targets){
$("#roomname").css("display", "block");
			$("#tourcaption").css("display", "block");
	$targets = $($targets);
	$targets.each(function(){
		$imageurl = $(this).attr("href");
		$imageroom = $(this).text();
		$imageheight = $(this).attr("rel");
		$imagecaption = $(this).attr("alt");
		//$(this).remove().after('<img alt="'+$imagecaption+'" width="370px" src="'+$imageurl+'" />');
		$(this).empty().append('<img alt="'+$imagecaption+'" height="'+$imageheight+'" width="370px" rel="'+$imageroom+'" src="'+$imageurl+'" />');
	});
	if($targets.length > 1) {
		$targetcount = $targets.length;
		if($("#tournav").length > 0){
				$("#tournav ul").empty();
		} else { $("#tourviewer").before('<div id="tournav"><ul></ul></div>'); }
		$("#tourviewer").cycle({ 
			fx:     'fade', 
			speed:  'fast', 
			timeout: 0,
			next: '#tourviewer img',
			before: function(){ setHeight($(this).height()); },
			after: function(){ 
				$("#roomname").empty().append($(this).children("img").attr("rel")); 
				if($(this).children("img").attr("alt") != "undefined"){
					$("#tourcaption").empty().append($(this).children("img").attr("alt"));
				} else $("#tourcaption").empty(); 
			},
			pager: '#tournav ul',
			delay: 5000
		});
		$("#tournav ul a").wrap('<li></li>');
		$targetmargin = $("#tournav li a").css("margin-right");
		$marginval = $targetmargin.split("p");
		$targetwidth = parseInt($("#tournav li a").width()) + parseInt($marginval[0]);
		$("#tournav").width($targetwidth * $targetcount);
	} else {
		$target = $("#tourviewer a");
		$imageurl = $target.attr("href");
		$imageroom = $target.children("img").attr("rel");
		$imagecaption = $target.attr("alt");
		$imageheight = $target.attr("rel");		
		$target.empty().append('<img alt="'+$imagecaption+'" height="'+$imageheight+'" width="370px" src="'+$imageurl+'" />');
		//setHeight($imageheight);
		//alert($target.attr("rel"));
		//alert("tourviewer has been set to " + $target.children("img").css("height"));
		if($imagecaption != "undefined"){
			$("#tourcaption").empty().append($imagecaption);
		} else $("#tourcaption").empty();
		$("#roomname").empty().append($imageroom);
		$("#tournav ul").empty();
		$("#tourviewer").css("height", "auto").css("height", $imageheight);
		//$("#roomname").empty().append($imageroom);
	}
}

function setHeight(imageheight){
	/*if(imageheight == 0){ // Safari first-time bug
		$("#tourviewer").height(imageheight);
		imageheight = '520px';
		//alert($(this).height());
		//alert($("#tourviewer").children("a").children("img").height());
		//imageheight = $("#tourviewer").children("a").get(0).children("img").height();
	}*/
	//alert("tourviewer is " + imageheight);
	$("#tourviewer").animate({
		height: imageheight
	});
}