    var ajax_script = '/doAjax';
	
	
	/**
	*
	*  Base64 encode / decode
	*  http://www.webtoolkit.info/
	*
	**/
	 
	var Base64 = {
	 
		// private property
		_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
	 
		// public method for encoding
		encode : function (input) {
			var output = "";
			var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
			var i = 0;
	 
			input = Base64._utf8_encode(input);
	 
			while (i < input.length) {
	 
				chr1 = input.charCodeAt(i++);
				chr2 = input.charCodeAt(i++);
				chr3 = input.charCodeAt(i++);
	 
				enc1 = chr1 >> 2;
				enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
				enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
				enc4 = chr3 & 63;
	 
				if (isNaN(chr2)) {
					enc3 = enc4 = 64;
				} else if (isNaN(chr3)) {
					enc4 = 64;
				}
	 
				output = output +
				this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
				this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
	 
			}
	 
			return output;
		},
	 
		// public method for decoding
		decode : function (input) {
			var output = "";
			var chr1, chr2, chr3;
			var enc1, enc2, enc3, enc4;
			var i = 0;
	 
			input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
	 
			while (i < input.length) {
	 
				enc1 = this._keyStr.indexOf(input.charAt(i++));
				enc2 = this._keyStr.indexOf(input.charAt(i++));
				enc3 = this._keyStr.indexOf(input.charAt(i++));
				enc4 = this._keyStr.indexOf(input.charAt(i++));
	 
				chr1 = (enc1 << 2) | (enc2 >> 4);
				chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
				chr3 = ((enc3 & 3) << 6) | enc4;
	 
				output = output + String.fromCharCode(chr1);
	 
				if (enc3 != 64) {
					output = output + String.fromCharCode(chr2);
				}
				if (enc4 != 64) {
					output = output + String.fromCharCode(chr3);
				}
	 
			}
	 
			output = Base64._utf8_decode(output);
	 
			return output;
	 
		},
	 
		// private method for UTF-8 encoding
		_utf8_encode : function (string) {
			string = string.replace(/\r\n/g,"\n");
			var utftext = "";
	 
			for (var n = 0; n < string.length; n++) {
	 
				var c = string.charCodeAt(n);
	 
				if (c < 128) {
					utftext += String.fromCharCode(c);
				}
				else if((c > 127) && (c < 2048)) {
					utftext += String.fromCharCode((c >> 6) | 192);
					utftext += String.fromCharCode((c & 63) | 128);
				}
				else {
					utftext += String.fromCharCode((c >> 12) | 224);
					utftext += String.fromCharCode(((c >> 6) & 63) | 128);
					utftext += String.fromCharCode((c & 63) | 128);
				}
	 
			}
	 
			return utftext;
		},
	 
		// private method for UTF-8 decoding
		_utf8_decode : function (utftext) {
			var string = "";
			var i = 0;
			var c = c1 = c2 = 0;
	 
			while ( i < utftext.length ) {
	 
				c = utftext.charCodeAt(i);
	 
				if (c < 128) {
					string += String.fromCharCode(c);
					i++;
				}
				else if((c > 191) && (c < 224)) {
					c2 = utftext.charCodeAt(i+1);
					string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
					i += 2;
				}
				else {
					c2 = utftext.charCodeAt(i+1);
					c3 = utftext.charCodeAt(i+2);
					string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
					i += 3;
				}
	 
			}
	 
			return string;
		}
	 
	}
	
	
	
	//global scope vars for doGuideSlideShow
	var current = 0;
    var images = 0
	var animateWidth = 275;
	var ammap = {};
	var slideshowIsSliding = false;
	
	$.fn.centerInClient = function(options) {
	    /// <summary>Centers the selected items in the browser window. Takes into account scroll position.
	    /// Ideally the selected set should only match a single element.
	    /// </summary>    
	    /// <param name="fn" type="Function">Optional function called when centering is complete. Passed DOM element as parameter</param>    
	    /// <param name="forceAbsolute" type="Boolean">if true forces the element to be removed from the document flow 
	    ///  and attached to the body element to ensure proper absolute positioning. 
	    /// Be aware that this may cause ID hierachy for CSS styles to be affected.
	    /// </param>
	    /// <returns type="jQuery" />
	    var opt = { forceAbsolute: false,
	                container: window,    // selector of element to center in
	                completeHandler: null
	              };
	    $.extend(opt, options);
	   
	    return this.each(function(i) {
	        var el = $(this);
	        var jWin = $(opt.container);
	        var isWin = opt.container == window;
	
	        // force to the top of document to ENSURE that 
	        // document absolute positioning is available
	        if (opt.forceAbsolute) {
	            if (isWin)
	                el.remove().appendTo("body");
	            else
	                el.remove().appendTo(jWin.get(0));
	        }
	
	        // have to make absolute
	        el.css("position", "absolute");
	
	        // height is off a bit so fudge it
	        var heightFudge = isWin ? 2.0 : 1.8;
	
	        var x = (isWin ? jWin.width() : jWin.outerWidth()) / 2 - el.outerWidth() / 2;
	        var y = (isWin ? jWin.height() : jWin.outerHeight()) / heightFudge - el.outerHeight() / 2;
	
	        el.css("left", x + jWin.scrollLeft());
	        el.css("top", y + jWin.scrollTop());
	
	        // if specified make callback and pass element
	        if (opt.completeHandler)
	            opt.completeHandler(this);
	    });
	}
	
	function removeSmallRightAdvert() {
		var html = $('.small-right-advert').html();
		var advert = $('#image-Ad').html();
		$('#image-Ad').fadeOut(500,function() {
			$(this).html(html);
			$('.small-right-advert').html(advert);
			$(this).fadeIn(500);
		})
	}
	
	function removeMediumLeftAdvert() {
		var html = $('.medium-left-advert').html();
		var advert = $('#image-Ad').html();
		$('#image-Ad').fadeOut(500,function() {
			$(this).html(html);
			$('.medium-left-advert').html(advert);
			$(this).fadeIn(500);
		})
	}
    
	function nextSlide() {
		$('.next').trigger('click');
	}
	
    $().ready(function() {
		doGuideSlideShow();
		doVideoImg();
		setInterval(nextSlide, 4000);
		doGuideSections();
		doVideoLightbox();
		
		if($('#small-right-inline-advert').length > 0) {
			$('.small-right-advert').hide();
            var advertId = $('.small-right-advert').attr('id');
            advertId = advertId.match(/ad_(\d+)/)[1];
			
			var data = $('.small-right-advert').attr('data');
			var timeOut = data.match(/\d+\:(\d+)/)[1];
            var params = {};
            var flashvars = {};
			params.allowscriptaccess = "always";
			flashvars.strXMLPath = "/xml/inline-advert-xml.php?id="+advertId;
            swfobject.embedSWF("/interface/flash/small-right-inline-advert.swf","small-right-inline-advert","263","180","9.0.0","/interface/flash/expressInstall.swf", flashvars, params, {wmode:'opaque'});
        	
			var imageHTML = $('#image-Ad').html();
			var advert = $('.small-right-advert').html();

			$('.small-right-advert').html(imageHTML);
			$('#image-Ad').html(advert).css({'margin-top':'13px'});
			
			var id = $('.small-right-advert').attr('id');
            id = id.match(/ad_(\d+)/)[1];
            var bits = window.screen.colorDepth;
            var colours = Math.pow (2, bits);
            var width = screen.width
            var height = screen.height
            var query = 'ajaxRequest=1&id='+id+'&colours='+colours+'&bits='+bits+'&width='+width+'&height='+height+'&page='+location.pathname+'&js=1';
            var url = '/tracker';
            
            $.ajax({
                type:   "GET",
                url:    url,
                data:   query
            });

			
			setTimeout("removeSmallRightAdvert()",timeOut*1000);
		}
		
		if($('#medium-left-inline-advert').length > 0) {
			$('.medium-left-advert').hide();
            var advertId = $('.medium-left-advert').attr('id');
            advertId = advertId.match(/ad_(\d+)/)[1];
			
			var data = $('.medium-left-advert').attr('data');
			var timeOut = data.match(/\d+\:(\d+)/)[1];
            var params = {wmode:'opaque'};
            var flashvars = {};
			params.allowscriptaccess = "always";
			flashvars.strXMLPath = "/xml/inline-advert-xml.php?id="+advertId;
            swfobject.embedSWF("/interface/flash/small-right-inline-advert.swf","medium-left-inline-advert","291","185","9.0.0","/interface/flash/expressInstall.swf", flashvars, params, {wmode:'opaque'});
        	
			var imageHTML = $('#image-Ad').html();
			var advert = $('.medium-left-advert').html();
			$('.medium-left-advert').html(imageHTML);
			$('#image-Ad').html(advert);
			
			var id = $('.medium-left-advert').attr('id');
            id = id.match(/ad_(\d+)/)[1];
            var bits = window.screen.colorDepth;
            var colours = Math.pow (2, bits);
            var width = screen.width
            var height = screen.height
            var query = 'ajaxRequest=1&id='+id+'&colours='+colours+'&bits='+bits+'&width='+width+'&height='+height+'&page='+location.pathname+'&js=1';
            var url = '/tracker';
            
            $.ajax({
                type:   "GET",
                url:    url,
                data:   query
            });

			
			setTimeout("removeMediumLeftAdvert()",timeOut*1000);
		}
		
		
		
		if ($('#worldmap').length > 0) {
			swfobject.embedSWF('/interface/flash/ammap/ammap.swf','worldmap','600','390','8','',{
                path: escape('/interface/flash/ammap/'),
                data_file: escape('/interface/flash/ammap/settings/data.xml.php?hideMarkers=1'),
                settings_file: escape('/interface/flash/ammap/settings/ammap_settings.xml'),
                preloader_color: '#999999'
            },{allowscriptaccess: 'always',wmode: "opaque"},{bgcolor: '#ffffff'});
		
		
			$('.countries').change(function() {
				// do some ajax to fetch the co-ords
				moveMap($(this).val());
				
			})
			
			$('.cities').change(function() {
				// do some ajax to fetch the co-ords
				
			})
		
		}
		
		
	});
	
	
	
	function moveMap(id) {

		ammap.reloadData('/interface/flash/ammap/settings/data.xml.php?dataId=1&hideMarkers=1');
	    //ammap.setZoom(response.result.zoom+'%', response.result.zoom_x+'%', response.result.zoom_y+'%', false);
		ammap.clickObject(id);
		//ammap.setColor(response.result.id, '#444444');

	}

	
	function amMapCompleted(map_id){
    	ammap = document.getElementById('worldmap');
   	}
	
	function amReturnData(map_id, data){
		//console.log(unescape(data));
	}
	
	function amRegisterClick(map_id, object_id, title, value){
        if(!object_id) return;
        if(object_id == 'btnBack'){
            //console.log('Back to world map clicked');
			getGuideSearchSections(0);
        }
		$('.countries').val(object_id);
		getCities(object_id); 
		getGuideSearchSections(object_id);
		//ammap.getParam('drag_map');
		//ammap.reloadData('/interface/flash/ammap/settings/data.xml.php?dataId=1&dataId='+object_id+'&hideMarkers=0');
    }

	function amReturnParam(map_id, param){
      //console.log('Param: '+unescape(param));
    }
	
	function amRegisterHover(map_id, object_id, title, value){
        //var map = $('#map')[0];
		//console.log(object_id);
		//alert("me");
        var popupOffset = {x: -250, y:-25};
        ammap.getStageXY();
        ammap.movePopup(pos.x +popupOffset.x,pos.y +popupOffset.y);
    }
    
    function amSetStageXY(map_id, x, y, x_percent, y_percent){
        pos = {x:x,y:y,x_percent:x_percent,y_percent:y_percent};
    }
    
    // new functionality
    function onMenuClose(){
        //console.log('Menu closed');
    }

	function getGuideSearchSections(cityId) {
		$.post(ajax_script,{action:'get_guide_search_categories',id:cityId},function(r) {
            if(r != '') {
                eval('response='+r);
				//console.log(response);
                var html = '';
                for(var i=0; i<response.length; i++) {
					var labelName = response[i].name.substring(0,19);
					if(response[i].name.length > 19) {labelName +='...';}
                    html += '<li><input type="radio" name="area" value="'+response[i].tidy_url+'" id="section_'+response[i].name+'" /><label for="section_'+response[i].name+'">'+labelName+'</label>';
                }
                $('#city-form-sections').html(html);
            }
        });
	}
	
	function doVideoLightbox() {
		if($('.video-box').length > 0) {
			$('.video-box').click(function() {
				$('#video-box-overlay').remove();
				$('body').prepend('<div id="video-box-overlay"></div>');
				$('#video-box-overlay').css({display:'none',background:'#666666',opacity:'0.8',height:$(document).height() +'px',position:'absolute','z-index':2,'width':'100%'});
				$.get($(this).attr('href'),{'ajaxRequest':true},function(r) {
					if(r !='') {
						var closeCenter = $(window).width();
						var closeButtonSize = 58;
						closeCenter = closeCenter / 2 - closeButtonSize;
						var closeHTML = '<a href="#close" id="close-player" style="z-index:10;color:#ffffff;font-weight:bold;position:absolute;top:125px;left:'+closeCenter+'px;">';
						closeHTML +='<img alt="close" src="/cms/interface/images/buttons/close.png"><span style="display:inline-block;padding:0 0 6px;">Close Video</span></a>';
						$('#video-box-overlay').after('<div id="player"></div>');
						$('#player').css({visibility:'hidden',position:'absolute','z-index':5,background:'#000000',height:'433px',width:'900px'});
						$('#player').html(r);
						$('#player').before(closeHTML);
						$('#player').centerInClient();
						$('#video-box-overlay').fadeIn(500,function() {
							$('#player').fadeIn(500);
						});
						
						$('#close-player').click(function() {
							$('#player').fadeOut(500,function() {
								$('#close-player').remove();
								$(this).remove();
								$('#video-box-overlay').fadeOut(500,function() {
									$('#video-box-overlay').remove();
								});
							});
							return false;
						});	
					}
				});
				return false;
			});
		}
	}
	
	function backToCityPage(element) {
		$('.city-back a',element).click(function() {
			$('.current_section').fadeOut(500,function() {
				$('.current_section').removeClass('current_section');
				$('#link_main').fadeIn(500);
				$('#link_main').addClass('current_section');
			});
		});
	}
	
	function doGuideSections() {		
		$('#link_main').addClass('current_section');
		$('.city-guide-link').click(function() {
			var id = $(this).attr('id');
			var sectionId = id.match(/link_(\d+)_\d+_\d+/)[1];
			var countryId = id.match(/link_\d+_(\d+)_\d+/)[1];
			var cityId = id.match(/link_\d+_\d+_(\d+)/)[1];
			
			if (!$('#link_' + sectionId).length) {
			
				$.ajax({
					type: "POST",
					dataType: "json",
					async: false,
					url: "/doAjax",
					data: "action=load_guide_section&sid=" + sectionId + "&cid=" + cityId,
					success: function(r){
						if(r.status == 'success' && r.html != '') {
							var html = Base64.decode(r.html);
							$('#city-info').append(html);
							$('#link_'+sectionId).hide();
							backToCityPage('#link_'+sectionId);
						}
					}
				});
			}
			
			//swap out
			$('.current_section').fadeOut(500,function() {
				$('#link_'+sectionId).fadeIn(500);
                $('.current_section').removeClass('current_section');
                $('#link_'+sectionId).addClass('current_section');
			});
			return false;
			
		});
		
		
	}
	
	function doVideoImg() {
		if ($('.city-video').length > 0) {
			var id = $('.city-video').attr('id');
			id = id.match(/video_(\d+)/)[1]
			var magFlashvars = {};
			magFlashvars.strXMLPath = "/xml/videos-xml.php?id="+id;
			var magParams = {};
			var magAttributes = {};
			magParams.allowscriptaccess = "always";
			magParams.wmode = "transparent";
			swfobject.embedSWF("/interface/flash/magazine-home.swf", "video_"+id, "263", "180", "9.0.0", "/interface/flash/expressInstall.swf", magFlashvars, magParams, magAttributes);
		}
	}
	
	function doGuideSlideShow(){
		images = $('ul#slides li').length;
		
		$('ul#slides').after('<div id="city-slideshow-paging"><a href="#prev" class="prev">&lt;</a>&nbsp;<a href="#next" class="next">&gt;</a>&nbsp;</div>');
		$('ul#slides li').css({
			position: 'absolute',
			top: '0px',
			left: '280px'
		});
		
		$('ul#slides li:nth(0)').css({left:'0px'})
								.addClass('current')
								.remove()
								.insertAfter('ul#slides li:last');
		
		$('.prev').click(function() {
			if (!slideshowIsSliding) {
				prevSlide();
				return false;
			}
		});
		
		$('.next').click(function() {
			if (!slideshowIsSliding) {
				nextSlide();
				return false;
			}
		});
	}
	
	function prevSlide() {
		slideshowIsSliding = true;
		if($('.current').length) {
			$('.current').remove().insertBefore('ul#slides li:nth(0)');
			$('.current').animate({left: "+="+animateWidth+"px"},1000, function() {
    			// Animation complete.
				$(this).removeClass('current').css({left:animateWidth+'px'});
  			});
		}

		$('ul#slides li:last').remove()
							  .insertBefore('ul#slides li:nth(0)')
							  .css({left:'-'+animateWidth+'px'})
							  .animate({left: "+="+animateWidth+"px"},1000,function(){slideshowIsSliding = false;})
							  .addClass('current');
	}
	
	function nextSlide() {
		slideshowIsSliding = true;
		if($('.current').length) {
			$('.current').remove().insertAfter('ul#slides li:last');
			$('.current').animate({left: '-='+animateWidth+'px'},1000,function(){
				$(this).removeClass('current').css({left:animateWidth+'px'});
			});
		}

		$('ul#slides li:nth(0)').remove()
								.insertAfter('ul#slides li:last')
								.animate({left: "-="+animateWidth+"px"},1000,function(){slideshowIsSliding = false;})
								.addClass('current');
	}
	
	
	
	
