// Switch display
function switchDisplay(){
$('a.switch_thumb').toggle(function(){
        $(this).addClass("swap");
        $('ul.display').fadeOut('fast', function() {
            $(this).fadeIn('fast').addClass('grid-view');
        });
    }, function () {
        $(this).removeClass('swap');
        $('ul.display').fadeOut('fast', function() {
            $(this).fadeIn('fast').removeClass('grid-view');
        });
    });
}

// Galleries
function wlGalleries(){
	
	if($('.wl-gallery-container, .wl-content-gallery-container, #flash, #topImage').length < 1){
		$('.content-block').removeClass('nb');
	}

	$('.wl-gallery').each(function(){
	
    	var gal = $(this);
    	
    	if(gal.parents('.wl-content-gallery-container').length >= 1){
    		gal.before('<div class="wl-content-gallery-nav"><a href="#" class="prev">Previous</a> <a href="#" class="next">Next</a></div>').cycle({
				'pause': true,
				'pauseOnPagerHover': true,
				'timeout': 5000,
				'cleartype': true,
				'cleartypeNoBg': true,
				'next': gal.prev('.wl-content-gallery-nav').find('.next'), 
    			'prev': gal.prev('.wl-content-gallery-nav').find('.prev')
			});
    	}else{
    		gal.cycle({
				'pause': true,
				'pauseOnPagerHover': true,
				'timeout': 5000,
				'cleartype': true,
				'cleartypeNoBg': true,
				'pager': gal.prev('.wl-gallery-nav').find('ul'),
				'pagerAnchorBuilder': function(idx, slide){
		        	return gal.prev('.wl-gallery-nav').find('li:eq(' + idx + ') a');
		    	}
			});
    	}
		
	});

	var gnH = $('.wl-gallery-nav').height();

	// Hide gallery nav
	if(typeof document.body.style.maxHeight === "undefined") {
		// if ie6
		$('.wl-gallery-nav').css({'bottom':'-' + parseFloat(gnH+1) + 'px'});
	}else{
		$('.wl-gallery-nav').css({'bottom':'-' + gnH + 'px'});
	}

	// Hide/Show gallery nav on hover
	$('.wl-gallery-container, .wl-small-gallery-container').mouseenter(
		function(){
			if(typeof document.body.style.maxHeight === "undefined") {
				// if ie6
				$(this).find('.wl-gallery-nav').stop().animate({'bottom':'-1px'});
			}else{
				$(this).find('.wl-gallery-nav').stop().animate({'bottom':'0px'});
			}
		}
	).mouseleave(
		function(){
			if(typeof document.body.style.maxHeight === "undefined") {
				// if ie6
				$(this).find('.wl-gallery-nav').stop().animate({'bottom':'-' + parseFloat(gnH+1) + 'px'});
			}else{	
				$(this).find('.wl-gallery-nav').stop().animate({'bottom':'-' + gnH + 'px'});
			}
		}
	);
	
}

// Cycle Quotes
function wlQuotes(){

    $('.wl-quote-list').show().cycle({
    	'pause': true,
    	'speed': 500,
    	'timeout': 10000,
    	'cleartype': true,
    	'cleartypeNoBg': true
    });

}

function convertToSlug(t){
    return t.toLowerCase().replace(/[^\w ]+/g,'').replace(/ +/g,'-');
}

var currHash = "";

function wlTabsSelectPage(){

	if(currHash != window.location.hash || !window.location.hash){

		if(window.location.hash){
		    
			var c = window.location.hash;
			
			$('#tab-nav a').each(function(){
				var tn = $(this);
				var h = '#'+convertToSlug(tn.text());
				if(h === c){
					tn.click();
				}
			});
			
			currHash = window.location.hash;
	
		}else{
		
			var c = $.url.attr('path');
			var s = false;
			
			$('#tab-nav a').each(function(){
				var tn = $(this);
				var h = $.url.setUrl(tn.attr('href')).attr('path');
				
				if(h === c+'index.aspx' && s === false){
					tn.parents('li').addClass('selected');
				}else if(h === c){
					tn.parents('li').addClass('selected').siblings().removeClass('selected');
					s = true;
				}
			});
			
			currHash = window.location.hash
			
		}
	
	}
	
}

// Ajax Tabs
function wlTabs(){

    /*if ($('#tab-nav ul li.selected').length <= 0) {
        $('#tab-nav ul li:first').addClass('selected');
    }
    
    $('#tab-nav a').each(function(e) {
        var href = $(this).attr('href');
        $(this).attr('href', href + '#' + convertToSlug($(this).text()));
    });*/
    
	if($('#tab-content').length > 0){
	
		//var hash = window.location.pathname.substr(1);

		//var currLink = '#'+$('#tab-nav li.selected a').attr('href').substr(0,$('#tab-nav li.selected a').attr('href').length-5);
		
		var currLink = "";
	
	    $('#tab-nav a').click(function(e){
	    
	    	e.preventDefault();
	    	$(this).parents('ul').find('li').removeClass('selected');
	    	$(this).parents('li').addClass('selected');
	    	$('#tab-content').css('height',$('#tab-content-ajax').height()+'px');
	    	
	    	//for IE text-shadow
	    	$(this).find('span').remove();
	
	    	var thisLink = '#'+convertToSlug($(this).text());
	    	
	    	var href = $(this).attr('href');
	
	    	if(currLink != thisLink){
				var toLoad = href+' #tab-content-ajax';
		    	$('#tab-content-ajax').stop().fadeOut('normal',loadContent);
		    	$('#tab-load').remove();
		    	$('#tab-content').prepend('<span id="tab-load">LOADING...</span>');
		    	$('#tab-load').css('height',$('#tab-content-ajax').height()+'px').hide();
		    	// Only show loader if content takes more than 0.5 second to load
		    	setTimeout(function(){
		    		$('#tab-load').show();
		    	},500);
		    	window.location.hash = convertToSlug($(this).text());
		    	currLink = thisLink;
		    	return false;
		    }
	
	    	function loadContent(){
	    		$('#tab-content').stop().load(toLoad,function(response, status, xhr) {
	    			if (status === "error") {
	    				var msg = "Sorry but there was an error: ";
	    				$("#tab-content").html('<div id="tab-content-ajax" class="tab-error warning"><p>' + msg + xhr.status + ' ' + xhr.statusText + '</p></div>');
	    			}
	    			$('#tab-content-ajax').hide();
					wlViewMore();
					showNewContent();
					// Google tracker code for ajax page
	    			if(pageTracker){
						pageTracker._trackPageview(href);
						//alert(href);
					}
	    		});
	    	}
	
		    function showNewContent(){
		    	hideLoader();
				var newHeight = $('#tab-content-ajax').height();
	    		$('#tab-content-ajax').stop().show().css('visibility','hidden');
	    		$('#tab-content').stop().animate({'height': newHeight},function(){
	    			$('#tab-content-ajax').css('visibility','visible').stop().hide().fadeIn('normal',function(){
	    				if($.browser.msie)this.style.removeAttribute('filter')
	    			});
	    			// Re-execute javascript functions for tab content
					wlQuotes();
					wlDatePicker();
					wlGalleries();
					$('.content-list-item:first').addClass('first');
					// Re-execute text shodows for IE
					if(window.wlTextShadow){
						wlTextShadow();
					}
					if(window.wlFourColLastItem){
						wlFourColLastItem();
					}
					$('#tab-content').removeAttr("style")
	    		});
		    }
	
		    function hideLoader(){
		    	$('#tab-load').fadeOut('fast');
		    }
	
	    });
	    
	    wlTabsSelectPage();
	    
	}
}

function wlVirtualTourControls(){

	var flashvars = false;
	
	// Get first tour IVP from the first item in the list and set as 'selected'
	var firstTour = $('.wl-vt-list-item:first').addClass('selected').find('a').attr('href');
	
	// Set up default parameters
	var params = {
		allowfullscreen: 'true',
		allowscriptaccess: 'always',
		wmode: 'opaque',
		flashvars: 'panorama='+ firstTour
	};
	
	// Set up default attributes
	var attributes = {
		id: 'PurePlayer',
		name: 'PurePlayer'
	};

	// Add first flash movie
	swfobject.embedSWF('/images/PurePlayer.swf', 'wl-vt-flash', '500', '380', '8.0.0', '/Images/49-49305expressInstall.swf', flashvars, params, attributes);

	// Change flash movie of click
	$('.wl-vt-list-item a').click(function(e){
		e.preventDefault();
		$('.wl-vt-list-item').removeClass('selected')
		$(this).parents('li').addClass('selected');

		// Empty old flash movie and replace with container div
		$('.wl-vt-container').empty().html('<div id="wl-vt-flash"></div>');

		// Get new tour IVP
		var newTour = $(this).attr('href');
		
		// Set up default parameters
		var params = {
			allowfullscreen: 'true',
			allowscriptaccess: 'always',
			wmode: 'opaque',
			flashvars: "panorama="+ newTour
		};
		
		// Add new flash movie
		swfobject.embedSWF('/images/PurePlayer.swf', 'wl-vt-flash', '500', '380', '8.0.0', '/Images/49-49305expressInstall.swf', flashvars, params, attributes);
	});
	
	setTimeout(function(){
		if($('.wl-vt-list').find('li').length > 4){
			$('.wl-vt-list').addClass('scroll')
		}
	},100);

}

function wlVirtualTour(){

	if($('.virtual-tour').length > 0){
	
		$('.virtual-tour').fancybox({
			padding: 0,
			margin: 0,
			scrolling: 'no',
			overlayColor: '#DED6C4',
			overlayOpacity: 0.8,
			titleShow: false,
			showCloseButton: false,
			transitionOut: 'none',
			transitionIn: 'none',
			centerOnScroll: true,
			onComplete: function(){
				$('.close_modal').click(function(e){
					e.preventDefault();
					$.fancybox.close();
				});
				wlVirtualTourControls();
			}
		});
	
	}
}

function wlDatePicker(){

	$('input.date, .date input, .date-input').datepicker({
		showOn: 'both',
		buttonImage: '/Images/49-32213btn_calendar.png',
		buttonText: 'Select Date',
		buttonImageOnly: true,
		dateFormat: 'dd/mm/yy',
		minDate: +1,
	    maxDate: '31/12/2012'
	});

}

function wlViewMore(){
    var highlights = $('.content-list-item-extra');
	highlights.hide().css({'opacity':'0'});

    highlights.each(function(){
        var link = $(this).parent().find('.content-list-view-more');
        if (link.length === 0) {
            $(this).after('<a href="#" class="content-list-view-more">Show me more</a>');
        }
    });

	$('.content-list-view-more').show();
	$('.content-list-view-more').click(function(e){
        if ($(this).attr('data-original') === undefined) {
            $(this).attr('data-original', $(this).text())
        }
		e.preventDefault();
		$(this).toggleClass('hide-content').prev('.content-list-item-extra').fadeSliderToggle(500);
		$(this).text($(this).text() == 'Close' ? $(this).attr('data-original') : 'Close');
	});
	
    if ($('.extra-content').length > 1)
    {
        $('.extra-content').hide();
    }
    else
    {
        $('.extra-content').show().css({'opacity':'100'});
		$(this).closest('.experience-list-item').toggleClass('open');
        $('.show-extra-content').text('Hide Dates');
        $('.show-extra-content').parent().removeClass("down-arrow");
        $('.show-extra-content').parent().addClass("up-arrow");
    }
    
	$('.show-extra-content').click(function(e){
		e.preventDefault();
		$(this).closest('.experience-list-item').toggleClass('open').find('.extra-content').fadeSliderToggle(500);
		$(this).text($(this).text() == 'Show Dates' ? 'Hide Dates' : 'Show Dates');
        $(this).parent().toggleClass("down-arrow");
        $(this).parent().toggleClass("up-arrow");
	});
	
}

function wlCheckAv(){

	if($('a.checkav').length > 0){
	
	    $("a.checkav").fancybox({
	    	width: 350,
	    	height: 145,
			overlayShow: true,
			overlayColor: '#E6E6CC',
			overlayOpacity: 0.6,
			hideOnContentClick: false,
			padding: 0,
			enableEscapeButton: true,
			titleShow: false,
			transitionOut: 'none',
			transitionIn: 'none',
			showCloseButton: false,
			onComplete: function(){
				wlDatePicker();
				$(".close_modal").click(function(e){
					e.preventDefault();
					$.fancybox.close();
				});
			}
		});
		
	}
	
}

$(document).ready(function(){

	wlViewMore();
	wlQuotes();
	wlTabs();
	wlVirtualTour();
	wlDatePicker();
	wlGalleries();
	wlCheckAv();
	switchDisplay();
	
	// Kick's off the search results pagination 
	$('div.experiences-results').quickPager();

    // Fix placeholder attribute for non-supported browsers
    $('input[placeholder]').placeholder();

    $('.content-list-item:first').addClass('first');
    
    if( $('#main-content').hasClass('property') ){
		//$('#sidebar h3:first').addClass('property-logo');
    }
    
    var charLength;
    $('#se-keyword-box').bind('keyup', function(){

		charLength = parseFloat($(this).val().length);
		
		if(charLength >= 3){
			$('.se-keyword-results').fadeIn();
		}else if(charLength < 1){
			$('.se-keyword-results').fadeOut();
		}
		
		//alert(charLength);
		
	}).keypress(function(e){
		if(e.keyCode == 13 || e.charCode == 13){
			e.preventDefault();
		}
	});
	
});

$(window).load(function(){

	// Add Classes to Google Custom Search
	$('#search input[type="text"]').addClass('input-box google-cs-box');
    $('#search input[type="submit"]').addClass('google-cs-btn').wrap('<span class="input-btn"></span>');

    $(window).hashchange(function(){
    
    	wlTabsSelectPage();
    
    });

});

/*
CSS Browser Selector v0.4.0 (Nov 02, 2010)
Rafael Lima (http://rafael.adm.br)
http://rafael.adm.br/css_browser_selector
License: http://creativecommons.org/licenses/by/2.5/
Contributors: http://rafael.adm.br/css_browser_selector#contributors
*/
function css_browser_selector(u){var ua=u.toLowerCase(),is=function(t){return ua.indexOf(t)>-1},g='gecko',w='webkit',s='safari',o='opera',m='mobile',h=document.documentElement,b=[(!(/opera|webtv/i.test(ua))&&/msie\s(\d)/.test(ua))?('ie ie'+RegExp.$1):is('firefox/2')?g+' ff2':is('firefox/3.5')?g+' ff3 ff3_5':is('firefox/3.6')?g+' ff3 ff3_6':is('firefox/3')?g+' ff3':is('gecko/')?g:is('opera')?o+(/version\/(\d+)/.test(ua)?' '+o+RegExp.$1:(/opera(\s|\/)(\d+)/.test(ua)?' '+o+RegExp.$2:'')):is('konqueror')?'konqueror':is('blackberry')?m+' blackberry':is('android')?m+' android':is('chrome')?w+' chrome':is('iron')?w+' iron':is('applewebkit/')?w+' '+s+(/version\/(\d+)/.test(ua)?' '+s+RegExp.$1:''):is('mozilla/')?g:'',is('j2me')?m+' j2me':is('iphone')?m+' iphone':is('ipod')?m+' ipod':is('ipad')?m+' ipad':is('mac')?'mac':is('darwin')?'mac':is('webtv')?'webtv':is('win')?'win'+(is('windows nt 6.0')?' vista':''):is('freebsd')?'freebsd':(is('x11')||is('linux'))?'linux':'','js']; c = b.join(' '); h.className += ' '+c; return c;}; css_browser_selector(navigator.userAgent);

