/*************
 * FUNCTIONS
 *************/

function getMovieName(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
   }
   else {
	   return document[movieName];
   }
}

function sendFlashMessage(msg) {
	getMovieName('imagecontent').gotoSubTopic(msg);
}


function activatePopupLink() {

	jQuery('a.popup-link').click(function(){
		var myWindow = $(window)
		var windowHeight = myWindow.height();
		var windowWidth = myWindow.width();
		
		var that = jQuery(this);
		that.blur();
		window.open(that.attr('href') + '?w=' + windowWidth + '&h=' + windowHeight, 
					'mapWindow', 
					'height=' + windowHeight + ',width=' + windowWidth + ',scrollbars=1').focus();		
		return false;
	});
}

function updateLastImage(src) {
	$.cookie('last_image', src, {path: '/'});
}

function visitUrl( url ) {
	location.href = url;
}

function updateTitle( subtext ){
	var pieces = document.title.split(":", 2); // just get the first two pieces: "Battery Wharf" and the main category (e.g. "Residences")
	
	/* Fix for IE.
	 *
	 * IE 6/7/8 adds a hash param to the document.title, so we trim it off if it's there:
	 */	
	pieces[1] = pieces[1].split('#')[0]; // pieces[1] is the main category. pieces[1].split('#')[0] is everything before the first '#'
		
	pieces.push( " " + subtext);
	document.title = pieces.join(':');
}


var urlParts = location.href.split("/"); 
var section = urlParts[ urlParts.length - 2 ];

var subSection = location.hash.slice(1); // slice(1) trims off the first character (which is a # if there's a hash)


/*****************
 * on DOM Ready:
 *****************/


var updatePhotoCaption, currentTopic, currentSubTopic;

(function ($) {
	$( function () { // DOM is ready:
				 
		var current_sub_nav = null;
		
		var topicLinks = $('#nav li > a');
		currentTopic = topicLinks.filter('.' + section).addClass('selected');
		currentSubTopic = currentTopic.next('.subtopic');
	
		topicLinks.each( function () {
			var that = $(this);
			
			//that.next('.subtopic').css('top', that.position().top + 'px');
						
			that.click(function () {
				that.blur();
				
				if (that.hasClass('selected')) {
					// we're already on the clicked topic. do nothing.
					return false;
				} else if (that.get(0) == currentTopic.get(0)) {
					// we're on a subtopic of the main topic that was clicked.
					// go back to Main topic
					currentSubTopic.find('.selected').removeClass('selected');
					currentTopic.addClass('selected');
					sendFlashMessage('main');
					location.hash = '#';
					return false;
				}
				
				
				
				if (current_sub_nav) {
					
					
					current_sub_nav.hide(	"slide", 
											{ direction: 'left', easing: 'easeInQuint' }, 
											'fast', 
											function(){
												//setTimeout(visitUrl, 500, that.attr('href'));
												location.href = that.attr('href');
											}
										);
					//getMovieName('imagecontent').pageExit();
					
					return false;
					
				} else {
					return true;	
				}
			
			
			});						 
		});
		

		
		if ( currentSubTopic.length ) {
			// there are subtopics
			currentSubTopic.show("slide", { direction: 'left', easing: 'easeOutCubic' }, 'slow');
			current_sub_nav = currentSubTopic;
		}
				
		if (subSection != '') {
			currentTopic.removeClass('selected');
			var new_subtopic = currentSubTopic.find('a[rel='+ subSection +']');
			new_subtopic.addClass('selected');
			updateTitle( new_subtopic.text() );
		}
		
		// attach the click() functionality to .subtopic's:
		$('#nav .subtopic:visible a').not('.normal-link').click( function(){
			var that = $(this);

			if (! that.hasClass('selected') ) {
				currentTopic.removeClass('selected');
				$('#nav .subtopic .selected').removeClass('selected');
				that.addClass('selected');
				updateTitle( that.text() );
				sendFlashMessage(that.attr('rel'));
			}
			
			that.blur();
		});

		var photoCaption = $('#photo-caption');
		photoCaption.data('previousValue', '');

		updatePhotoCaption = function(html_text) {
			if (html_text !== photoCaption.data('previousValue')) {
				photoCaption.fadeOut('slow', function() {
					photoCaption.html(html_text).fadeIn('slow');
					activatePopupLink();
				});
				
				photoCaption.data('previousValue', html_text);
			}
		}
		
		//activatePopupLink();
		
	});
})(jQuery);