<!--

$(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});
	});
});

//Article Vote
$(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!');
			});
	});
});

//Print Article
$(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
    var isTab = false;
    if (!$('#article #ajaxdata').hasClass("hide"))
    {
            var tpe = $("#tabkey").text();
            isTab = true;
            bindRelated(tpe);
            $('#article .addthis_toolbox').hide();//Hide addthis if ajaxed tab
    }
    $('#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();
            var $content = $('#ajaxdata');
            if (tpe == 'info')
            {
                $('#articledata').show();
                $('#ajaxdata').hide();
            }
            if (tpe == 'info' && isTab)
            {
                $content = $('#bodydata');
            }
            else
            {
                $('#articledata').hide();
                $('#ajaxdata').removeClass().addClass(tpe);
                $('#ajaxdata').show();
            }

            if (tpe == 'info' && !isTab)
            {
                $('#articledata').show();
                $('#ajaxdata').hide();
            }
            else
            {
                $content.html('<div class="loading">Loading...</div>');

                var key = $('#itemkey').text();
                $.get('/functions/ajax/getarticlerelated.htm', {'itemkey': key, 'src':'article', 'tpe':tpe}, function(data)
                {
                    if (tpe == 'hotels')
                        $.getScript("http://www.aviewoncities.com/js/jquery-ui.js", function() {$.getScript("http://www.aviewoncities.com/js/hotels.js", function() {});});
                        
                    if (tpe == 'info')
                    {
                        $('#bodydata').empty().append(data);
                        $('#bodydata').show();
                    }
                    else
                    {
                        $('#ajaxdata').empty().append(data);
                        $('#ajaxdata').show();
                    }
                    if (tpe == 'location')
                            $('#ajaxdata').prepend($('#locInfo').html());
                        
                    bindRelated(tpe);
                });
                return false;
            }
  });
});


function bindRelated(tpe)
{
		switch(tpe)
		{
			case "attractions" :bindArticle();break;
			case "buildings" :bindBuilding();break;
			case "hotels" :bindHotel();break;
			case "photos" :bindPhotos();break;
			case "posters" :bindPosters();break;
			case "location" :;break;
			
		}
}

function bindHotel()
{
	var key = $('#itemkey').text();
	//openMap(key); //load map by default
	$('#ajaxdata.hotels ul.related li .button').click(function(event){
            event.preventDefault();
            var tpe = $(this).attr("id");
            if (tpe == 'bookbutton')
            {
                var parms = fetchHotelParms(true);
                var id = $(this).find('span.hotelID').text();
                var target = 'http://travel.aviewoncities.com/hotel/propertydetails/' + id + '/ROOMS_RATES?cid=71006';
                window.open(target + parms, 'hotel');
            }
            if (tpe == 'selectbutton')
            {
                parms = "";
                var cur = $("#currency");
                parms += "&currencyCode=" + cur.val();
                var lan = $("#locale");
                parms += "&locale=" + lan.val();
                var target = $(this).closest('li').find('a.link').attr('href');
                window.open(target + parms, 'hotel');
            }
            
            $.get('/functions/ajax/loghotellink.php', {'src':'article_related_' + tpe, 'itemkey':key, 'tpe':'hotel'});
	});
	// Go to hotel target if not clicked on anchor
        
        /*
	$('#ajaxdata.hotels ul.related li').click(function(event){
            var target = $(this).find('a.link').attr('href');
            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){
		openMap(key);
	});
	// Hide Map
	$('#hideMap').click(function(event){
            $('#mapwrapper').hide();
            $('#hideMap').hide();
            $('#showMap').show();
            $('#ajaxdata ul.related').css('height','');
	});
	
	//if hash in url is map, open the map
	if (location.hash == "#map")
		$('#showMap').trigger('click');
	
	//amenities
	$('#ajaxdata .amenityicons > div').mouseover(
            function () {
                var descr = $(this).find('span').text();
                if (descr!= '')
                {
                    var $p = $('#iconpop');
                    
                    $p.text(descr);
                    $p.prependTo($(this));

                    //$p.fadeIn(100);
                }
            });
            /*
	$('#ajaxdata li').mouseleave(
            function () {
                $('#iconpop').fadeOut(300);
            });
            */
	//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','');
	});
	
	//if hash in url is map, open the map
	if (location.hash == "#map")
		$('#showMap').trigger('click');
}

function bindBuilding()
{
	// Go to building target
	$('#ajaxdata.buildings 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':'building', 'tpe':'buildings'}, 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','');
	});
	
	//if hash in url is map, open the map
	if (location.hash == "#map")
		$('#showMap').trigger('click');
	
}

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 openMap(key)
{
	$('#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');
	});
}

function getSetVotes()
{
	var img = $("#photo > img").attr('src').split('s.jpg');
        var pos = img[0].lastIndexOf("/"); 
        var key = img[0].substr(pos + 1, img[0].length);

        $('#votebox').load('/functions/ajax/ratephoto2.php', {'key':key, 'tpe':'thumbs', 'rate':'none'}, function(){
            
            $('#votebox img.rate').one('click', function(){
                if (document.cookie.length > 0)
                {
                    rate = $(this).hasClass("down") ? "down" : "up";
                    $('#votebox').load('/functions/ajax/ratephoto2.php', {'key':key, 'tpe':'thumbs', 'rate':rate}, function(){
                        $("#votebox").addClass("rated");
                        $("#votebox").removeClass("rate");
                    });
                }
                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", 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 = $('#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();
	});
}



// Load areahotels
$(document).ready(function(){
    $('#article .areahotels .label').click(function(event){
        var $content = $(this).parent().find('.content');
        if ($content.is(":visible"))
        {
            $content.hide();
        }
        else
        {
            if ($content.find('.hotel_block'))
            {
                var area = $(this).parent().find('span.area').text();
                $.get('/functions/ajax/getarticlerelated.htm', {'itemkey': 9999999, 'src':'article', 'tpe':'areahotels', 'area': area}, function(data)
                {
                    $content.html(data);
                });
            }
            $content.show();
        }
    });
});

//Get hotel parameters such as currency & language and add to target url
function fetchHotelParms(addDate)
{
	var parms = "";
         
	var cur = $("#currency");
	parms += "&currencyCode=" + cur.val();
	var lan = $("#locale");
	parms += "&locale=" + lan.val();
        
        if (addDate)
        {
            var my = $("#arrivalMonth option:selected").text();
            var y = my.substr(-4);
            var m = $("#arrivalMonth option:selected").val();
            var d = $("#arrivalDay option:selected").val();
            var l = $("#nrOfNights").text();
            var p = $("#room-1-adult-total option:selected").text();
            if (y!='' && m!='' && d!='' && l!='' && p!='')
            {
                m = parseInt(m) + 1;
                m = '0' + m;
                d = '0' + d;
                parms += '&travelDetail=[' + y + m.substr(-2) + d.substr(-2) + '-' + l + ']' + p;
            }
        }
        
        
	return parms;//return parms;
}


//-->
