function urlencode(url) {
	return escape(url).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
}

function urlencode_lite(url) {
	return url ? url.replace(' ','%20') : '';
}

function slug(slugcontent) {
    slugcontent = slugcontent.toLowerCase();
    var   accents={a:/\u00e1/g,e:/u00e9/g,i:/\u00ed/g,o:/\u00f3/g,u:/\u00fa/g,n:/\u00f1/g}
    for (var i in accents) slugcontent = slugcontent.replace(accents[i],i);

	var slugcontent_hyphens = slugcontent.replace(/\s/g,'-');
	var finishedslug = slugcontent_hyphens.replace(/[^a-zA-Z0-9\-]/g,'');
    finishedslug = finishedslug.toLowerCase();
    return finishedslug;
}

function googleTranslateElementInit() {
  new google.translate.TranslateElement({
    pageLanguage: 'en',
    includedLanguages: 'zh-TW,en,fr,ja,ru'
  }, 'languages');
}
		  
$.fn.imageCycle = function(){
	$(this).each(function(){
		var o = $(this);

		o.nextImage = function(){
			setTimeout(function(){
				var current = $('img:visible', o);
				var next = current.next();
				if(!next.is('img')){
					next = $('img:first', o);
				}
				current.fadeOut(1000, function(){
					next.fadeIn(1000, function(){
						o.nextImage();
					});
				});
			}, 6000);
		};
		
		o.nextImage();

	});
};

$.fn.homepageSplash = function(){
	$(this).each(function(){
		var o = $(this);
		var images = $('img', o);
		var images_loaded = 0;
		images.each(function(){
			var target_image = $(this);
			$(new Image)
				.bind('load', function(){
					images_loaded++;
					if(images_loaded === images.length){
						o.startAnimation();
					}
				})
				.attr('src', target_image.attr('src'));
		});
		
		o.startAnimation = function(){
			$('.image-tl, .image-br', o).imageCycle();
			setTimeout(function(){
				$('.image-m', o).imageCycle();
			}, 500);
			setTimeout(function(){
				$('.image-tr, .image-bl', o).imageCycle();
			}, 1000);
		};

	});
};

$.fn.homepageBanners = function(){
	$(this).each(function(){
		var o = $(this);
		var current = null;
		
		o.loadCurrent = function(){
			current = $('div.active', o);
			
			setTimeout(function(){
				current.fadeIn(400, function(){
					setTimeout(function(){
						o.unloadCurrent();
					}, 5000);
				});
			}, 400);
		};

		o.unloadCurrent = function(){
			current.fadeOut(400, function(){
				o.setNext();
			});
		}

		o.setNext = function(){
			var next = $('div.active', o).removeClass('active').next();
			if(next.is('div')){
				current = next.addClass('active');
			}else{
				current = $('div:first', o).addClass('active');
			}
			o.loadCurrent();
		}

		current = $('div:first', o).addClass('active');
		$('div.banner-strip', o).css('opacity', '0.6');
		o.loadCurrent();
		
	});
};

$.fn.homepageFeatured = function(){
	$(this).each(function(){
		var o = $(this).parent();
		o.v = {
			arrows:  $('a.feature-prev, a.feature-next', o),
			list:  $('ul', o),
			item_width: $('li:first', o).outerWidth(true)
		};

		o.bind({
			mouseenter: function(){ o.v.arrows.fadeTo(100, 0.5); },
			mouseleave: function(){ o.v.arrows.fadeTo(100, 0); }
		});

		o.v.arrows.bind({
			mouseenter: function(){ target = $(this); target.fadeTo(100, 1); },
			mouseleave: function(){ target = $(this); target.fadeTo(100, 0.5); },
			
			click: function(){
				if($(this).parent().parent().is('.gallery')){
					_gaq.push(['_trackEvent', 'Elite Resorts', 'Click', +$(this).attr('title')]);					
					//pageTracker._trackEvent('Elite Resorts', 'Click', $(this).attr('title'));
				}else{
					_gaq.push(['_trackEvent', 'Homepage', 'Click', 'Features - '+$(this).attr('title')]);	
					//pageTracker._trackEvent('Homepage', 'Click', 'Features - '+$(this).attr('title'));
				}
				if($(this).is('.feature-next')){
					o.v.list.css('display', 'none');
					o.v.list.append($('li:first', o.v.list));
					o.v.list.css('display', 'block');
				}else if($(this).is('.feature-prev')){
					o.v.list.css('display', 'none');
					o.v.list.prepend($('li:last', o.v.list));
					o.v.list.css('display', 'block');
				}
				return false;
			}
		});

	});
};

$.fn.styledMultiSelect = function(){
	$(this).each(function(){

		var select = $(this).css('display', 'none');
		var o = select.after('<div class="select-multiple"><ul></ul></div>').next();
		
		var v = {
			list: $('ul', o)
		};
		
		$('option', select).each(function(){
			var target = $(this);
			var tooltip = target.attr('title');
			target.removeAttr('selected');
			v.list.append('<li><a class="'+target.val()+'" href="">'+target.text()+'</a>'+(tooltip.length > 0 ? '<input class="tooltip" value="'+escape(tooltip)+'" />' : '')+'</li>');
		});

		$('a', v.list)
			.bind('click', function(){
				var target = $(this);
				var parent = target.parent();
				var selected = parent.is('.selected');
				var total = $('li.selected', v.list).length;
				var option = target.attr('class');
	
				if(selected){
					parent.removeClass('selected');
					$('option[value='+option+']', select).removeAttr('selected');
				}else if(total < 3){
					parent.addClass('selected');
					$('option[value='+option+']', select).attr('selected', 'selected');
				}
	
				return false;
			})
			.bind('mouseenter', function(){
				var label = $(this).text();
				var offset = $(this).parent().position();
				var tooltip = $('input.tooltip', $(this).parent());
				
				if(tooltip.length > 0){
					o.closest('.form-field').prepend('<div style="top:'+(Math.ceil(offset.top)+60)+'px; left:180px;" class="master-tooltip"><h5>'+label+'</h5><p>'+unescape(tooltip.val())+'</p></div>');
				}
			})
			.bind('mouseleave', function(){
				$('div.master-tooltip', o.closest('.form-field')).remove();
			});
		
		o.jScrollPane({ showArrows: true, scrollbarWidth: 15 });

	});
};

$.fn.styledCheckboxSlider = function(){
	$(this).each(function(){
		var input = $(this).css('display', 'none');
		var o = input.after('<div class="slider" />').next();
		var field = o.closest('.slider-field');
		var value = o.after('<p class="value">0</p>').next();
		
		$('label', field)
			.bind('mouseenter', function(){
				$('.tooltip', field).css('display', 'block');
			})
			.bind('mouseleave', function(){
				$('.tooltip', field).css('display', 'none');
			});
		
		o.slider({
			value:0,
			range: 'min',
			min: 0,
			max: 100,
			slide: function(event, ui) {
				value.text(ui.value);
			},
			stop: function(event, ui){
				if(ui.value > 87) ui.value = 100;
				else if(ui.value > 62) ui.value = 75;
				else if(ui.value > 37) ui.value = 50;
				else if(ui.value > 12) ui.value = 25;
				else ui.value = 0;
				
				o.slider('value', ui.value);
				input.val(ui.value);
				value.text(ui.value);
			}
		});


	});
};

$.fn.renderHeaders = function(){
	var counter = 0
	$(this).each(function(){
		if(swfobject.hasFlashPlayerVersion('9.0.0')){
			var original = $(this);
			var target = $(this);
			counter++
			target = $('a', target).length ? $('a', target).addClass('flash-link') : target;
	
			var text = target.text();
			var inner = target.html('<div class="inner" />').find('.inner');
	
			if(!inner.attr('id')){
				inner.attr('id', 'header-'+slug(text)+'-'+counter);
			}
	
			var width = 594;
			var height = 21;
	
			if(original.is('h4') > 0){
				var width = Math.ceil(width*0.8);
				var height = Math.ceil(height*0.8);
			}else if(original.is('label') > 0){
				var width = Math.ceil(width*0.7);
				var height = Math.ceil(height*0.7);
			}

			swfobject.createSWF(
				{ data:"tpl/swf/text.swf?author_name="+urlencode(text)+(target.is('a') ? '&is_link=true' : ''), width: width, height: height, wmode: 'transparent', allowScriptAccess: 'always' },
				{ wmode: 'transparent' },
				inner.attr('id')
			);
			
		}
	});
};

/*$.fn.handleVideos = function(){
	var counter = 0;
	$(this).each(function(){
		counter++;
		var target = $(this);
		if(!target.attr('id')){
			target.attr('id', 'video-'+counter);
		}
		var src = target.attr('src');
		var srchd = target.attr('srchd');
		var height = parseInt(target.attr('height'));
		var width = parseInt(target.attr('width'));
		var preview = target.attr('preview');

		swfobject.createSWF(
			{ data:"tpl/swf/video.swf", width: width, height: height, wmode: 'transparent', flashvars: 'fullVideoScale=false&playOnStart=true&startVolume=100&autoHideOther=true&image='+preview+'&MediaLink=/'+src+(srchd?'&MediaLink2=/'+srchd:'') },
			{  },
			target.attr('id')
		);
	});
};*/

$().ready(function(){

	$('ul.resort-links').find('.etc a').click(function(e) {
	    e.preventDefault();
	    _gaq.push(['_trackEvent', 'ETC', 'Click', $(this).attr('title')]);	
	});

	$('ul.resort-links').find('.book-now a').click(function(e) {
	    e.preventDefault();
	    _gaq.push(['_trackEvent', 'Book Now', 'Click', $(this).attr('title')]);
	});
	
	$('ul.resort-links').find('.email-now a').click(function(e) {
	    e.preventDefault();
	    _gaq.push(['_trackEvent', 'Email Now', 'Click', $(this).attr('title')]);	
	});
	
	$('ul.resort-links').find('.map a').click(function(e) {
	    e.preventDefault();
	    _gaq.push(['_trackEvent', 'Map', 'Click', $(this).attr('title')]);	
	});
	
	$('ul.resort-links').find('.elite-experience a').click(function(e) {
	    e.preventDefault();
	    _gaq.push(['_trackEvent', 'Elite Experience', 'Click', $(this).attr('title')]);	
	});
	
	$('ul.resort-links').find('.elite-episode a').click(function(e) {
	    e.preventDefault();
	    _gaq.push(['_trackEvent', 'Elite Episode', 'Click', $(this).attr('title')]);	
	});
	
	$('ul.resort-links').find('.my-elite-episode a').click(function(e) {
	    e.preventDefault();
	    _gaq.push(['_trackEvent', 'My Elite Episode', 'Click', $(this).attr('title')]);	
	});		

	$('#elite-resorts-single-resort h4').find('a').click(function(e) {
	    e.preventDefault();
	    _gaq.push(['_trackEvent', 'Elite Resorts', 'Click', $(this).attr('title')]);	
		//pageTracker._trackEvent('Elite Resorts', 'Click', $(this).attr('title'));
	});
	
	$('#featured-resorts-single-featured h4').find('a').click(function(e) {
	    e.preventDefault();
	    _gaq.push(['_trackEvent', 'Featured Resort', 'Click', $(this).attr('title')]);	
		//pageTracker._trackEvent('Featured Resort', 'Click', $(this).attr('title'));
	});	

	$('#elite-experiences-single-experience h4').find('a').click(function(e) {
	    e.preventDefault();
	    _gaq.push(['_trackEvent', 'Elite Experiences', 'Click', $(this).attr('title')]);	
		//pageTracker._trackEvent('Elite Experiences', 'Click', $(this).attr('title'));
	});

	$('#elite-episodes-single-episode h4').find('a').click(function(e) {
	    e.preventDefault();
	    _gaq.push(['_trackEvent', 'Elite Episodes', 'Click', $(this).attr('title')]);
	    //pageTracker._trackEvent('Elite Episodes', 'Click', $(this).attr('title')); 
	});

	$('#elite-living-single-living h4').find('a').click(function(e) {
	    e.preventDefault();
	    _gaq.push(['_trackEvent', 'Elite Living', 'Click', $(this).attr('title')]);	
		//pageTracker._trackEvent('Elite Living', 'Click', $(this).attr('title'));
	});

	$('#elite-luxury-alliance-single-partner h4').find('a').click(function(e) {
	    e.preventDefault();
	    _gaq.push(['_trackEvent', 'Elite Luxury Alliance', 'Click', $(this).attr('title')]);	
		//pageTracker._trackEvent('Elite Luxury Alliance', 'Click', $(this).attr('title'));
	});

	$("a[href^='http']").attr('target','_blank');
	$('#homepage-splash').homepageSplash();
	$('.homepage-banner').homepageBanners();
	$('.homepage-featured .list-container, .gallery .list-container').homepageFeatured();
	$('#header ul li.main, #footer ul li.main')
		.bind('mouseenter', function(){
			$(this).closest('li').addClass('hover');
		})
		.closest('li')
			.bind('mouseleave', function(){
				$(this).removeClass('hover');
			});
	$('#elite-resorts-search select[multiple=multiple]').styledMultiSelect();
	$('#elite-search input.slider').styledCheckboxSlider();

	//$('video').handleVideos();

	$('audio,video').mediaelementplayer({
		success: function(player, node) {
			$('#' + node.id + '-mode').html('mode: ' + player.pluginType);
		}
	});

	$('form#travel-planner').each(function(){
		var target = $(this);

		$('#country', target).bind('change', function(){
			target.trigger('submit');
		});
		$('.form-field-submit', target).remove();
	});

	$('#travel-planner-index h3.expand').find('a').click(function(e){
	    e.preventDefault();	
	    _gaq.push(['_trackEvent', 'Travel Planner', 'Click', $(this).attr('title')]);	
		//pageTracker._trackEvent('Travel Planner', 'Click', $(this).attr('title'));
		var target = $(this).parent().next();
		target.slideToggle(400);
		return false;
	});
	$('#travel-planner-index div.expanded:not(.selected)').css('display', 'none');

	$('#map').each(function(){
		var lat = $(this).find('#lat').val();
		var long = $(this).find('#long').val();
		var title = $(this).find('#title').val();

		var latlng = new google.maps.LatLng(lat, long);
		var myOptions = {
			zoom: 14,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};

		var marker = new google.maps.Marker({
			position: latlng,
			title: title
		});

		var map = new google.maps.Map(document.getElementById("map"), myOptions);
		marker.setMap(map);  

	});

	$('#elite-resorts-single-resort h4, #featured-resorts-single-featured h4, body#elite-episodes-single-episode h4, #elite-luxury-alliance-single-partner h4, #elite-living-single-living h4').each(function(){
		var target = $(this);
		var next = target.next();
		var height = 0;
		if(next.is('div.expand')){
			height = next.outerHeight(true);
			next.css('display', 'none');
			$('a', target).bind('click', function(){
				var link = $(this);
				_gaq.push(['_trackEvent', 'Elite Resort', 'Click', $(this).attr('title')]);
				if(next.is(':visible')){
					next.animate({
						height: 0
					}, 200, function(){
						next.css('display', 'none');
					});
				}else{
					next.css('height', '0px');
					next.css('display', 'block');
					next.animate({
						height: height
					}, 200);
				}
				return false;
			});
		}
	});

	Shadowbox.init({
		overlayColor:'#000',
		overlayOpacity:0.90
	});
	Shadowbox.init({ 
        handleOversize: "resize", 
        onOpen: function() { 
                if((navigator.userAgent.match(/iPhone/i))||(navigator.userAgent.match(/iPad/i))) { 
                        $("#sb-container").css("top", $(window).scrollTop()); 
                        $(window).bind('scroll', function() { 
                                $("#sb-container").css("top", $(window).scrollTop()); 
                        }); 
                } 
        }, 
        onClose: function() { 
                if((navigator.userAgent.match(/iPhone/i))||(navigator.userAgent.match(/iPad/i))) { 
                        $(window).unbind('scroll'); 
                } 
        } 
	}); 

	Cufon.replace('h4, .expand h5, h3, #elite-search label, #elite-search legend, ul.navigation li.main a.main, ul.navigation-top a, ul.navigation-bottom a, ul.resort-links li, p.button');

});

