soundManager.onload = function() {
	soundManager.createSound({
		id: 'musicElement',
		url: 'assets/player/08 Paris (Aeroplane Remix).mp3'
	});
	
	var autoPlay = Util.readCookie('musicAutoPlay');
	
	if (autoPlay != 'no') {
		soundManager.play('musicElement');
	}
}

var Application = function() {
	this.elementsList = ['imageSliderHomepage', 'propertySplash', 'menuRight'];
	this.elementsMap = ['headerMap', 'mapRight', 'mapPropertyDots'];
	
	var autoPlay = Util.readCookie('musicAutoPlay');
	this.soundIsPlaying = (autoPlay != 'no');
	
	if (typeof(thisIsIe6) != 'undefined' && thisIsIe6) {
		this.elementsList.push('menuRightBackgroundIe6');
	}
	
	this.selectedProperty = false;
	
	this.ajaxURL = '/ajax/';
}

Application.prototype.init = function() {
	soundManager.url = 'assets/player/swf/';
	$('#soundPlayerButton').attr('src', 'assets/images/sound_' + (this.soundIsPlaying ? 'on' : 'off') + '.gif');
	
	if (typeof(imageSliderSettings) == 'undefined') imageSliderSettings = {};
	
	if (typeof(thisIsIe6) != 'undefined' && thisIsIe6) {
		$('#mapRight').css('display', 'none');
	}
	
	imageSliderSettings['imageSliderHomepage'] = {
		holdFor: 			5000,
		transitionSpeed: 	1000,
		dataSource: 		new Application.SliderDataSourceProperty()
	};
	
	// Initialise slider manager
	var imageSliderManager = ImageSlider.SliderManager.getInstance();
	imageSliderManager.initSliders();
	
	var hm = Application.HashManager.getInstance();
	hm.startObserving();
	hm.addListener('hashChanged', this.hashChanged.bind(this));
	
	$('#navigation .button').click(this.navButtonClicked);
	$('#buttonMap').click(this.showMap.bind(this));
	$('#buttonList').click(this.showList.bind(this));
	$('#buttonMapLight').click(this.showMap.bind(this));
	$('#buttonListLight').click(this.showList.bind(this));
	
	
	
	var self = this;
	
	$('#homeButton').click(function() {
		var hm = Application.HashManager.getInstance();
		hm.setHash('');
	});
	
	$('#navigation .button').mouseover(function() {
		$(this).addClass('hover');
	});
	$('#navigation .button').mouseout(function() {
		$(this).removeClass('hover');
	});
	$('#menuRight .menuItem').mouseover(function() {
		$('#' + this.id + ' .hover').css('display', 'block');
		$('#' + this.id + ' .normal').css('display', 'none');
		
	});
	$('#menuRight .menuItem').mouseout(function() {
		if (this.id.split('_')[1] == self.selectedProperty) return;
		
		$('#' + this.id + ' .hover').css('display', 'none');
		$('#' + this.id + ' .normal').css('display', 'block');
	});
	
	$('#menuRight .menuItem').click(function() {self.menuItemClicked(this.id);});
	
	$('#mapPropertyDots .dot').mouseover(function() {self.mouseOverDot(this.id);});
	$('#mapPropertyDots .dot').mouseout(function() {self.mouseOutDot(this.id);});
	$('#mapPropertyDots .dot').click(function() {self.clickDot(this.id);});
	
	$('#soundPlayerButton').click(function() {
		if (this.soundIsPlaying) {
			$('#soundPlayerButton').attr('src', 'assets/images/sound_off.gif');
			Util.createCookie('musicAutoPlay', 'no', 31);
			
			soundManager.pause('musicElement');
		} else {
			$('#soundPlayerButton').attr('src', 'assets/images/sound_on.gif');
			Util.createCookie('musicAutoPlay', 'yes', 31);
			
			soundManager.play('musicElement');
		}
		
		this.soundIsPlaying = !this.soundIsPlaying;
	}.bind(this));
}

Application.prototype.navButtonClicked = function() {
	var hm = Application.HashManager.getInstance();
	
	switch(this.id) {
		case 'buttonLivingAt':hm.setHash('livingAtAW');break;
		case 'buttonBenefits':hm.setHash('benefitsOfOwnership');break;
		case 'buttonGuest':hm.setHash('starwoodPreferredGuest');break;
	}
}

Application.prototype.hashChanged = function(data) {
	this.unsetContent();
	
	switch(data.hash) {
		case 'livingAtAW':
			this.setContent('contentLivingAt', 'buttonLivingAt');
		break;
		
		case 'benefitsOfOwnership':
			this.setContent('contentBenefits', 'buttonBenefits');
		break;
		
		case 'starwoodPreferredGuest':
			this.setContent('contentGuest', 'buttonGuest');
		break;
	}
}

Application.prototype.unsetContent = function() {
	$('#contentLivingAt').css('display', 'none');
	$('#contentBenefits').css('display', 'none');
	$('#contentGuest').css('display', 'none');
	
	$('#buttonLivingAt').removeClass('selected');
	$('#buttonBenefits').removeClass('selected');
	$('#buttonGuest').removeClass('selected');
}

Application.prototype.setContent = function(divId, buttonId) {
	$('#' + divId).css('display', 'block');
	$('#contentFooter').css('display', 'block');
	
	$('#' + buttonId).addClass('selected');
}

Application.prototype.showMap = function() {
	$.each(this.elementsList, function(index, item) {
		$('#' + item).css('display', 'none');
	});
	
	$.each(this.elementsMap, function(index, item) {
		$('#' + item).css('display', 'block');
	});
	
	$('#buttonMap').css('display', 'inline');
	$('#buttonList').css('display', 'none');
	$('#buttonMapLight').css('display', 'none');
	$('#buttonListLight').css('display', 'inline');
}

Application.prototype.showList = function() {
	$.each(this.elementsList, function(index, item) {
		$('#' + item).css('display', 'block');
	});
	
	$.each(this.elementsMap, function(index, item) {
		$('#' + item).css('display', 'none');
	});
	
	$('#buttonMap').css('display', 'none');
	$('#buttonList').css('display', 'inline');
	$('#buttonMapLight').css('display', 'inline');
	$('#buttonListLight').css('display', 'none');
	
	if (this.selectedProperty === false) {
		$('#propertySplash').css('display', 'none');
	}
}

Application.prototype.menuItemClicked = function(elementId) {
	var propertyId = elementId.split('_')[1];
	
	this.showSelectedProperty(propertyId);
}

Application.prototype.showSelectedProperty = function(propertyId) {
	if (this.selectedProperty !== false) {
		$('#propertyButton_' + this.selectedProperty + ' .normal').css('display', 'block');
		$('#propertyButton_' + this.selectedProperty + ' .hover').css('display', 'none');
	}
	
	this.selectedProperty = propertyId;
	
	$('#propertySplash .text').css('display', 'none');
	$('#propertySplash').css('display', 'block');
	$('#propertySplashText_' + propertyId).css('display', 'block');
	
	var slider = ImageSlider.SliderManager.getInstance().getSlider('imageSliderHomepage');
	slider.getDataSource().setProperty(propertyId);
	slider.reset();
	
	$('#propertyButton_' + this.selectedProperty + ' .hover').css('display', 'block');
	$('#propertyButton_' + this.selectedProperty + ' .normal').css('display', 'none');
}

Application.prototype.mouseOverDot = function(elementId) {
	var propertyId = elementId.split('_')[1];
	
	$('#propertyName_' + propertyId).css('display', 'block');
}

Application.prototype.mouseOutDot = function(elementId) {
	var propertyId = elementId.split('_')[1];
	
	$('#propertyName_' + propertyId).css('display', 'none');
}

Application.prototype.clickDot = function(elementId) {
	var propertyId = elementId.split('_')[1];
	
	$('#propertyName_' + propertyId).css('display', 'none');
	
	this.showList();
	this.showSelectedProperty(propertyId);
}

var application = new Application();

$(function() {
	application.init();
	$("#navAboutWResidences").bind("click", function(){
			$("#aboutWResidences").toggle(function(){
				$('.close').toggle();
			});
	});
	$(".close").bind("click", function(){
			$("#aboutWResidences").hide();
			$(this).hide();
	});
});

