<!--

$(document).ready(function(){
	//link to nearby hotels or all hotels and log
	$('#infobar div.hotels').css('cursor', 'pointer');
	$('#infobar div.hotels').click(function(){
		var key = $('#itemkey').text();
		var target = $(this).find('.target').attr('href');
		var tpe = $(this).find('.hotellnktpe').text();
		top.location = target;
		$.get('/functions/ajax/loghotellink.php', {'src':'article_ad', 'itemkey':key, 'tpe':tpe});
	});
});


$(document).ready(function(){
	var comment = new Array();
	comment['one'] = 'Avoid!';
	comment['two'] = 'Nothing special';
	comment['three'] = 'Worth a visit';
	comment['four'] = 'Recommended';
	comment['five'] = 'Must-see!';
	//rating
	$('#infobar ul.star-rating a').hover(function(){
		$('#infobar .rating-txt').text(comment[$(this).attr('class')]);
		$('#infobar .title').text('Rate this attraction');
	});
	$('#infobar ul.star-rating a').mouseleave(function(){
		score = $('#infobar .inline-rating .cnt').text();
		$('#infobar .rating-txt').text(score);
		$('#infobar .title').text('Rating');
	});
	$('#infobar ul.star-rating a').one('click', function(){
			var stars = $(this).attr('class');
			var key = $('#itemkey').text();
			$('#infobar div.rating').load('/functions/ajax/ratearticle.php', {'itemkey': key, 'stars': stars}, function(){
				$('#infobar .title').text('Thanks for voting!');
			});
	});
});

$(document).ready(function() {
    $('a.print').click(function()
		{
			var printarticle =  window.open('','PrintWindow','scrollbars=yes,width=600,height=600');
			var html = '<html><head><title>' + document.title + '</title><link type="text/css" href="/css/main.css" rel="stylesheet" /><link type="text/css" href="/css/sub.css" rel="stylesheet" /><link rel="stylesheet" type="text/css" href="/css/print.css" media="print" /></head><body id="article"><form id="printform"><input type="button" value=" Print this page " onclick="window.print();return false;" /></form><h1>' + document.title + '</h1><br /><div id="printarticle"> ' + $('<div />').append($('#data').clone()).html() + '<br /><br /> © www.aviewoncities.com - ' + location.href + '</div></body></html>';
			printarticle.document.open();
			printarticle.document.write(html);
			printarticle.document.close();
	
			return false;
    });
});

// Load article related items
$(document).ready(function(){
	// Check if tab is loaded -> binding needed
	if (!$('#article #ajaxdata').hasClass("hide"))
	{
		var tpe = $("#tabkey").text();
		bindRelated(tpe);
	}
	$('#itemheader .tabs li').click(function(event){
		event.preventDefault();
		var sel = $(this).is('.sel');
		var tpe = $(this).attr('id');
		if (!sel)
		{
			$('#itemheader .tabs li.sel').removeClass("sel").addClass("desel");
			$(this).removeClass("desel").addClass("sel");
		}
		$('#locInfo').hide();
		if (tpe == 'info')
		{
			$('#ajaxdata').hide();
			$('#articledata').show();
		}
		else
		{
			$('#articledata').hide();
			$('#ajaxdata').removeClass().addClass(tpe);
			$('#ajaxdata').show();
			var $content = $('#ajaxdata');
			$content.html('<div class="loading">Loading...</div>');
				
			var key = $('#itemkey').text();
			$.post('/functions/ajax/getarticlerelated.htm', {'itemkey': key, 'src':'article', 'tpe':tpe}, function(data) {
					$content.html(data);
					bindRelated(tpe);
			});
			return false;
		}
  });
});

function bindRelated(tpe)
{
		switch(tpe)
		{
			case "attractions" : bindArticle();break;
			case "hotels" : bindHotel();break;
			case "photos" : bindPhotos();break;
			case "posters" : bindPosters();break;
			case "location" : $('#locInfo').show();break;
			
		}
}

function bindHotel()
{
	var key = $('#itemkey').text();
	
	// Go to hotel target
	$('#ajaxdata.hotels ul.related li').click(function(event){
			var target = $(this).find('.target').text();
			//event.preventDefault();
			parms = fetchHotelParms();
			window.open(target + parms, 'hotel');
			$.get('/functions/ajax/loghotellink.php', {'src':'article_related', 'itemkey':key, 'tpe':'hotel'});
	});
	
	// Map link
	$('#showMap,#nearbymaplnk').click(function(event){
		$('#mapwrapper').show();
		$('#mapwrapper').load('/functions/ajax/getmapitem.php', {'itemkey':key, 'src':'article', 'tpe':'hotels'}, function(){																																																								 					$('#showMap').hide();
					$('#hideMap').show();
					$('#ajaxdata ul.related').css('height','500px');
		});
	});
	// Hide Map
	$('#hideMap').click(function(event){
					$('#mapwrapper').hide();
					$('#hideMap').hide();
					$('#showMap').show();
					$('#ajaxdata ul.related').css('height','');
	});
	
	//log footer links
	$('#ajaxdata.hotels ul.next a').click(function(event){
		var tpe = $(this).attr('class').substr(6);
		var target = $(this).attr('href');
		parms = fetchHotelParms();
		event.preventDefault();
		location.href = target + parms;
		$.get('/functions/ajax/loghotellink.php', {'src':'article_related', 'itemkey':key, 'tpe':tpe});
	});
}

function bindArticle()
{
	// Go to article target
	$('#ajaxdata.attractions ul.related li').click(function(){
			var target = $(this).find('.target').text();
			top.location = target;
	});
	var key = $('#itemkey').text();
	// Map link
	$('#showMap').click(function(event){
		$('#mapwrapper').show();
		$('#mapwrapper').load('/functions/ajax/getmapitem.php', {'itemkey':key, 'src':'article', 'tpe':'attractions'}, function(){																																																								 					$('#showMap').hide();																																																		 					$('#hideMap').show();
					$('#ajaxdata ul.related').css('height','500px');
		});
	});
	// Hide Map
	$('#hideMap').click(function(event){
					$('#mapwrapper').hide();																																															 					$('#showMap').show();																																																		 					$('#hideMap').hide();
					$('#ajaxdata ul.related').css('height','');
	});
}

function bindBuilding()
{
	// Go to article target
	$('#buildingbox .txt ul.related li').click(function(){
			var target = $(this).find('.target').text();
			top.location = target;
	});
	
}

function bindPhotos()
{
	
	// Select a photo thumb
	$('#ajaxdata.photos ul.thumbs > li').click(function(){
			var key = $(this).find("span.photokey").text();
			var curkey = $("#photokeycur").text();
			if (curkey != key)
				showPhoto(key);
	});
	
	//next
	$('#photo').click(function(){
			var key = $("#photokeynext").text();
			showPhoto(key);
	});
	
	//previous
	$('#photo .prev').click(function(){
			var key = $("#photokeyprev").text();
			showPhoto(key);
	});
	
	//load votes
	getSetVotes();
	
}

function getSetVotes()
{
	img = $("#photo > img").attr('src').split('t.jpg').join('s.jpg');

	$('#votebox').load('/functions/ajax/ratephoto2.php', {'img':img, 'tpe': 'thumbs'},function(){
		$('#votebox img.up').one('click', function(){
			if (document.cookie.length > 0)
			{
				$('#votebox').load('/functions/ajax/ratephoto2.php', {'img':img, 'tpe':'thumbs', 'rate':'up'}, function(){
				});
			}
			else
			{
				alert('Sorry, you must have cookies enabled for this functionality');
			}
		});
		$('#votebox img.down').one('click', function(){
			if (document.cookie.length > 0)
			{
				$('#votebox').load('/functions/ajax/ratephoto2.php', {'img':img, 'tpe':'thumbs', 'rate':'down'}, function(){																																
				});
			}
			else
			{
				alert('Sorry, you must have cookies enabled for this functionality');
			}
		});
	});
}

function showPhoto(key)
{
	//loading
	$("#photoload").show();
	
	var $curkey = $('#ajaxdata.photos ul.thumbs > li > span.photokey:contains(' + key + ')');
	var lbl = $curkey.parent().find(".photolbl").html();
	var src = $("#photo > img").attr("src");
	var pos = src.lastIndexOf('/');
	src = src.substring(0, pos + 1);
	
	//replace photo
	$("#photo > img").attr("src", src + key + 's.jpg').one("load", function() {  
		$("#photoload").hide();
		$("#photolbl > span").html(lbl); //replace label
	}); 
	$("#photokeycur").text(key); //replace key
	
	//show next and previous?
	var nextkey = $curkey.parent().next().find(".photokey").text();
	var prevkey = $curkey.parent().prev().find(".photokey").text();
	var firstkey = $curkey.parent().parent().find(".photokey:first").text();
	nextkey = nextkey == "" ? firstkey : nextkey;
	$("#photokeynext").text(nextkey); //replace key
	$("#photokeyprev").text(prevkey); //replace key
	
	//select current
	$('#ajaxdata.photos ul.thumbs > li').removeClass("thumbsel");
	$curkey.parent().addClass("thumbsel");
	
	//Show / hide arrows
	if (nextkey != '')
		$('#photo .next').css('visibility', 'visible');
	else
		$('#photo .next').css('visibility', 'hidden');
	
	if (prevkey != "")
		$('#photo .prev').css('visibility', 'visible');
	else
		$('#photo .prev').css('visibility', 'hidden');
		
	getSetVotes(); //load vote data
}


function bindPosters()
{
	$("#posterimg").one("load", function() {  	
		//position loaded poster
		h = $("#posterimg").height();
		$("#posterimg").css("margin-top", (440 - h) / 2);
		$("#posterimg").show();
	});
	
	//first add nr of posters
	$("#postertab").text("Posters (" + $("#postertot").text() + ")");
	
	// Select a poster thumb
	$('#ajaxdata.posters ul.thumbs > li').click(function(){
			var key = $(this).find("span.posterkey").text();
			var curkey = $("#posterkeycur").text();
			if (curkey != key)
				showPoster(key);
	});
	
	//next poster
	$('#photo .next').click(function(){
			var key = $("#posterkeynext").text();
			showPoster(key);
	});
	
	//previous poster
	$('#photo .prev').click(function(){
			var key = $("#posterkeyprev").text();
			showPoster(key);
	});
	
	//next posterpage
	$('#posterthumbs .next').click(function(){
			var pageNr = $("#posterpagecur").text();
			var nextPage = parseInt(pageNr) + 1;
			showPosterPage(nextPage);
	});
	
	//previous posterpage
	$('#posterthumbs .prev').click(function(){
			var pageNr = $("#posterpagecur").text();
			var prevPage = parseInt(pageNr) - 1;
			showPosterPage(prevPage);
	});
	
	//more info button
	$("#photo > a").mouseover(function () {
		w = $("#posterimg").width();
		h = $("#posterimg").height();
		$("#postermore").css("margin-top", (440 - h) / 2);
		$("#postermore").css("left", 744 - ((692 - w) / 2));
		$("#postermore").css("overflow", "hidden");
		$("#postermore").show();
	});
	$("#photo > a").mouseout(function () {
		$("#postermore").hide();
	});
}

function showPoster(key)
{
	if (key == 'nextPage')
		$('#posterthumbs .next').click();
	if (key == 'prevPage')
		$('#posterthumbs .prev').click();
	$("#posterimg").hide();
	$("#posterlbl").html("&nbsp;")
	//loading
	$("#posterload").show();
	//get image url
	var $curkey = $('#ajaxdata.posters ul.thumbs > li > span.posterkey:contains(' + key + ')');
	var imageUrl = $curkey.parent().find(".imageurl").text();
	var label = $curkey.parent().find(".label").html();
	var target = $curkey.parent().find(".target").text();
	
	//replace poster
	$("#posterimg").attr("src", imageUrl).one("load", function() {  
		$("#photo > a").attr("href", "http://www.allposters.com/gallery.asp?" + target);
		h = $("#posterimg").height();
		$("#posterimg").css("margin-top", (440 - h) / 2);
		$("#posterload").hide();
    $("#posterimg").show();
		$("#posterlbl").html(label); //replace label
	});
	
	$("#posterkeycur").text(key); //replace key
	
	//show next and previous?
	var nextkey = $curkey.parent().next().find(".posterkey").text();
	var prevkey = $curkey.parent().prev().find(".posterkey").text();
	$("#posterkeynext").text(nextkey); //replace key
	$("#posterkeyprev").text(prevkey); //replace key
	//select current
	$('#ajaxdata.posters ul.thumbs > li').removeClass("thumbsel");
	$curkey.parent().addClass("thumbsel");
	//get page nr
	var curPage = $("#posterpagecur").text();
	var maxPage = $("#posterpagemax").text();
	//Show / hide arrows
	if (nextkey != '' || parseInt(maxPage) > parseInt(curPage))
	{
		$('#photo .next').css('visibility', 'visible');
		if (nextkey == '')
			$("#posterkeynext").text('nextPage'); //replace key
	}
	else
		$('#photo .next').css('visibility', 'hidden');
	
	if (prevkey != "" || parseInt(curPage) > 1)
	{
		$('#photo .prev').css('visibility', 'visible');
		if (prevkey == '')
			$("#posterkeyprev").text('prevPage'); //replace key
	}
	else
		$('#photo .prev').css('visibility', 'hidden');
}

function showPosterPage(pageNr)
{
	$("#posterload").show();
	
	var $content = $('#article #ajaxdata');
	var key = $('#itemkey').text();
	$.post('/functions/ajax/getarticlerelated.php', {'itemkey': key, 'src':'article', 'tpe':'posters', 'page':pageNr}, function(data) {
		$content.html(data);
		bindPosters();
		$("#posterload").hide();
	});
}

//Get hotel parameters such as currency & language and add to target url
function fetchHotelParms()
{
	var parms = "";
	var cur = document.getElementById("currency");
	if (cur != null) parms += "&currencyCode=" + cur.value
	var lan = document.getElementById("locale");
	if (lan != null) parms += "&locale=" + lan.value
	return "";//return parms;
}


//-->