var MapWindow = function() {
	function _close(e) {
		if (_isEscapeKey(e.keyCode)) {
			pub.close();
		}
	}
	
	/**
	 * Checks whether the key pressed is the escape key
	 * @param Int keycode
	 * @return Boolean
	 */
	function _isEscapeKey(keycode) {
		return (keycode == 27) ? true : false;
	}
	
	/**
	 * Adds event handlers
	 */
	function _init() {
		$('#closewindow').click(pub.close);
		$(document).click(pub.close);
		// this is to stop the click event bubbling up to the document
		$('#mapwindow-content').click(function(e) { e.stopPropagation(); });
		$('#leftarrow').click(function(e) {
		  var url = $(this).attr('href');
  		
		  var hash = location.hash;
  	  var parts = hash.split('/');
      
      if (hash == '' || parts[1] != 'phones') {
		    var hash = '#/phones/' + _selectedPhone + '/features/' + url;       
      }
      else {
		    var hash = '#/' + parts[1] + '/' + parts[2] + '/features/' + url;       
		  }

  		hash = hash.replace(/^.*#/, '');
  		
  		$.history.load(hash);
  		
  		return false;
		});
		$('#rightarrow').click(function(e) {
		  var url = $(this).attr('href');
  		
		  var hash = location.hash;
  	  var parts = hash.split('/');
      
      if (hash == '' || parts[1] != 'phones') {
		    var hash = '#/phones/' + _selectedPhone + '/features/' + url;       
      }
      else {
		    var hash = '#/' + parts[1] + '/' + parts[2] + '/features/' + url;       
		  }

  		hash = hash.replace(/^.*#/, '');
  		
  		TrackEvent.addLabel({action: 'creds', label: $(this).children(0).attr('alt')});
  		
  		$.history.load(hash);
  		
  		return false;
		});
		$('a', '#thumbnails-placeholder').click(function(e) {
		  var url = $(this).attr('href');
  		
		  var hash = location.hash;
  	  var parts = hash.split('/');
      
      if (hash == '' || parts[1] != 'phones') {
		    var hash = '#/phones/' + _selectedPhone + '/features/' + url;       
      }
      else {
		    var hash = '#/' + parts[1] + '/' + parts[2] + '/features/' + url;       
		  }

  		hash = hash.replace(/^.*#/, '');
  		
  		$.history.load(hash);
  		
  		return false;
		});
		$(document).keypress(_close);
	}
	
	/**
	 * Unbinds event handlers
	 */
	function _shutDown() {
		$('#closewindow').unbind('click', pub.close);
		$(document).unbind('click', pub.close);
		$('#leftarrow').unbind('click', _open);
		$('#rightarrow').unbind('click', _open);
		$('a', '#thumbnails-placeholder').unbind('click', _open);
		$(document).unbind('keypress', _close);
	}
	
	/**
	 * Loads a url into the window
	 * @param Event
	 * @return false
	 */
	function _open(e) {
		var url = $(this).attr('href');
		pub.open(url);
		
		return false;
	}
	
	// Public API
	var pub = {};
	
	pub.open = function(url) {
		var el = $('#mapwindow');
		var init = _init; // store reference to the private _init fn
				
		$("#mapwindow-content").load(url, {}, function() {
			if (el.css('display') != 'block') {
		    	el.css('display', 'block');

		    	document.getElementById("mapwindow-content").style.display = 'block';    
		  	}
		  	
		  // If contains video, bind events
		  $('#videourl', '#mapwindow-content').each(function() {
		    var url = $(this).attr('value');
		    
		    if (url.charAt(url.length - 1) != '&') {
		      url = url + '&';
		    }
		    
		    url = url + 'enablejsapi=1&playerapiid=ytplayer';
		    		    
		    var params = { allowScriptAccess: "always" };
        var atts = { id: "myytplayer" };
        swfobject.embedSWF(url, "ytapiplayer", "501", "302", "8", null, null, params, atts);
		  });
		
			init();
		});
	};
		
	pub.close  = function() {
		document.getElementById("mapwindow-content").style.display = 'none';  
		document.getElementById("mapwindow").style.display = 'none';

		_shutDown();
	};
	
	return pub;
}();