main = function() {
	return {

		// Vars
		
		links               : {},      // Links set by Symfony
		city                : null,    // Current city data
		
		// Methods
		
		init : function() {
		  
      // Notice box close
      
      $('#noticeBox span.x img').click(function(e) {
        $('#noticeBox').animate({ 'opacity' : 0}, 333, function(e) {
          $('#noticeBox').remove();
        });
      });
      
      // Profile Popup
      
      main.profilePopupInit();
      
      // Question submit
      
      main.initQuestion();
      
      // City Switch init
      
      main.initCitySwitch();
		  
		},
		
		initFormButton : function(formId, buttonName, buttonMessage) {
			$('#' + formId).submit(function(e) {
				$('#' + formId + ' input[name=' + buttonName + ']').attr('disabled', true).val(buttonMessage).blur();
			});
		},

		addCommasToInt : function(nStr) {
			nStr += '';
			var x = nStr.split('.');
			var x1 = x[0];
			var x2 = x.length > 1 ? '.' + x[1] : '';
			var rgx = /(\d+)(\d{3})/;
			while (rgx.test(x1)) {
				x1 = x1.replace(rgx, '$1' + ',' + '$2');
			}
			return x1 + x2;
		},

		randomString : function() {
			var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ';
			var string_length = 4;
			var randomstring = '', rnum;
			for (var i=0; i<string_length; i++) {
				rnum = Math.floor(Math.random() * chars.length);
				randomstring += chars.substring(rnum,rnum+1);
			}

			return randomstring;
		},
		
		initSwitchCity : function() {
		  $('a.citySelect').click(function(e) {
		    e.preventDefault();
		    main.switchCity();
		  });
		},
		
		switchCity : function() {
		  
		  if ($('#switchCityDialog').size() > 0) {
		    $('#switchCityDialog').dialog('open');
		    return;
		  }
		  
		  var div = $('<div></div>').attr('id', 'switchCityDialog');
		  div.append($('<p></p>').html('<strong>Pick your new city:</strong>'));
		  
		  var select = $('<select></select>');
		  for (var i=0; i<main.cities.length; i++) {
		    select.append($('<option></option>').val(main.cities[i].slug).html(main.cities[i].name));
		  }
		  select.val(main.city.slug);
		  
		  div.append($('<p></p>').append(select));
		  div.dialog({
        'modal' : true,
        'title' : 'Switch to a different city',
        'dialogClass' : 'dialogCreateAccount',
        'width' : '450px',
        'buttons' : {
          'Switch your city' : function() {
            $(this).dialog("close");
            var url = main.links.switchCityLink.replace('CITYID', $('#switchCityDialog select').val());
            window.location = url;
          },
          'Cancel' : function() {
            $(this).dialog("close");
          }
        }
		  });
		},
		
		fb_login : function() {
		  window.location.reload();
		},
		
		fb_login_photo : function() {
		  window.location = main.links.fbPhotoLink;
		},
		
		profilePopupInit : function() {
		  
		  $('#profilePopup').mouseout(function(e) {
        var parents = $(e.relatedTarget).parents();
        for (var i=0; i<parents.length; i++) {
          if (parents[i] == this) {
            return;
          }
        }
        $(this).css('display', 'none');
		  });
		  
		  $('a.profileSmallLink img').live('mouseover', function(e) {
		    
        // Content
        
        var name = $(this.parentNode).attr('username');
        var href = $(this.parentNode).attr('href');
        var src = $(this).attr('src');
        $('#profilePopup h6 a').text(name).attr('href', href);
        $('#profilePopup .photo img').attr('src', src);
		    
        // Position
		    
		    var offset = $(this).offset();
		    
		    $('#profilePopup').css({
		      'top' : (offset.top - 4) + 'px',
		      'left' : (offset.left - 4) + 'px',
		      'display' : 'block'
		    });
		  });
		},
		
		initQuestion : function() {
		  $('div.wrap-right div.question a.answer').click(function(e) {
		    e.preventDefault();
		    $(this).blur();

		    var form = $(this).parents('form');
		    var answerContainer = $(this).parents('div.answer-container');
		    if (form.length === 0 || answerContainer.length === 0) {
		      return;
		    }
		    
		    $('#question_answerValue').val($(this).attr('value'));
		    answerContainer.empty().append($('<p/>').html('<strong>Saving...</strong>'));
        form.submit();
		  });
		},
		
		initCitySwitch : function() {
		  
		  $('#citySelect').change(function(e) {
		    if ($(this).val() == '-1') {
		      return;
		    }
		    
		    window.location = main.links.switchCityLink.replace('CITYID', $(this).val());
		  });
		  
		}
		
	};
}();
$.ajaxSetup({'cache' : false}); // No cache for IE on Ajax requests