Site = {
  BASE_URL : 'http://www.tangeroutlet.com./',
    
  Url : {
		base  : 'http://www.tangeroutlet.com./'
	}
};

/**
 * Site.Constants
 */
Site.Constants = {
    NORTH_AMERICAN_PHONE_FORMAT : /\(\d{3}\)\s\d{3}\-\d{4}/
};

/**
 * Site.Functions
 */

Site.Functions = {
  hideShow : function(id) {
    var obj = document.getElementById(id); // set the object to be changed
    if (obj.style.display == "none"){
      obj.style.display = "block";
    }else{
      obj.style.display = "none";
    }
  },
  
  switchFocus : function(id) {
    document.getElementById(id).focus();
  },
  
  openWin : function(page, windowName, windowFeatures) {
    return window.open(page, "", windowFeatures);
  },
  
  /***********
  * Dropdown change location
  * selector  select element
  * path    a path string representing the url path to take the user to
  *         the path should contain the string '<VALUE>' which will
  *         be replaced by the selected value retrieved from the
  *         selector element
  * target  target window or frame to have its location changed
  *
  *   Example:
  *
  *   A call to:
  *   changeLocation(this, '/center/<VALUE>/coupons', window)
  *
  *   where selector.options[selector.selectedIndex].value equals 'HIL'
  *   will change window.document.location to
  *   '/center/HIL/coupons'
  ************************************/
  changeLocation : function(selector, path, target) {
    if (selector != "void"){
      var ident = selector.options[selector.selectedIndex].value;
    
      path = path.replace(/\<VALUE\>/, ident);
      var page  = Site.BASE_URL + path;
      selector.selectedIndex = 0;
      target.document.location = page;
    }
  },
  
  /************
   * Phone number formatter
   * 
   * <input name="phone" onchange="Site.Functions.formatPhone(this)" />
   */
  applyPhoneFormat : function(field) {
    phone = field.value;
    
    if ( phone.replace(/\D/g,'').length == 3 ) {
      phone = phone.replace(/\D/g,'');
      phone = phone.replace(/^(\d{3})/, '($1) ');
      field.value = phone;
    }
    else if ( phone.replace(/\D/g,'').length == 6 ) {
      
      phone = phone.replace(/\D/g,'');
      phone = phone.replace(/^(\d{3})(\d{3})/,'($1) $2-');
      field.value = phone;
    }
    else if (   phone.replace(/\D/g,'').length > 6 
             && phone.replace(/\D/g,'').length <= 10
             && !phone.match(/\s$/) ) {
      
      phone = phone.replace(/\D/g,'');
      phone = phone.replace(/^(\d{3})(\d{3})(\d{1,4})/,'($1) $2-$3');
      field.value = phone;
    }
    else if (   !phone.match(/\s$/) 
             && phone.replace(/\D/g,'').length > 10 ) {
     
     phone = phone.replace(/\D/g,'');
     phone = phone.replace(/^(\d{3})(\d{3})(\d{4})(\d+)/,'($1) $2-$3 x$4');
     field.value = phone;
    }
    // Allow for extension input
    else if ( phone.match(/\s$/) ) {
      phone = phone.replace(/$/, 'x');
      field.value = phone;
    }
  },
  
  removePhoneFormat : function(field) {
    phone = field.value;
    field.value = phone.replace(/\D/g,'');
  }
};

/**
 * Site.PageAlerts
 * 
 * REQUIRES jQuery
 */
Site.PageAlerts = {
  hasAlerts           : false,
  pageAlertId         : '#pageAlerts',
  pageAlertClose      : '#pageAlertClose',
  pageAlertsShown     : false,

  alertEvents : function () {
    $(Site.PageAlerts.pageAlertClose).click(Site.PageAlerts.hideAlerts);
  },

  hideAlerts : function () {
    $(Site.PageAlerts.pageAlertId).hide();
  },

  showAlerts : function () {
    if (Site.PageAlerts.hasAlerts && !Site.PageAlerts.pageAlertsShown) {
      Site.PageAlerts.pageAlertsShown = true;
      
      $(Site.PageAlerts.pageAlertId).show('slow');
    }
  }
};

// $(document).ready(Site.PageAlerts.alertEvents);
//$(document).ready(Site.PageAlerts.hideAlerts);
$(document).ready(Site.PageAlerts.showAlerts);

/**
 * Site.Cart
 */
Site.Cart = {
  showFormName: 'cart',
  shipmentFormName: 'edit',
  paymentFormName: 'payment',
  orderFormName: 'finalreview',
  itemFieldId: '#itemqty_',

  remove_item: function(id) {
    $(this.itemFieldId + id)[0].value = 0;
    this.submitShowForm();
  },

  submitShowForm : function() {
    document.forms[this.showFormName].submit();
  }
};

/**
 * 
 * Site.ContactUs
 */
Site.ContactUs = {
  formDialog  : null,
  baseUrl     : 'contactus/', 
  
  initFormDialog : function() {
    Site.ContactUs.formDialog = $("#contactUsDialog");
    Site.ContactUs.formDialog.dialog({
      autoOpen: false,
      bgiframe: true,
      width: 400,
      height: 475,
      modal: true,
      resizable: false
    });
  },
  
  showForm: function (moniker, section, context, params) {
    var url = '/' + Site.ContactUs.baseUrl + moniker + '?';
    
    if (params == undefined) {
      params = {};
    }
    
    params['section'] = section;
    params['context'] = context;
    
      // Include params
    for ( var key in params ) { 
       url = url + escape(key) + '=' + escape(params[key]) + '&';
    }
    
    // Remove last char - either '?' or '&'
    url = url.slice(0, -1);
    
    Site.ContactUs.formDialog.load(
       url
    ).dialog("open");
  },
  
  submitForm : function(options) {
    Site.ActivitySpinner.startSpinner($(options.spinner));
    $('#contactUsForm').ajaxSubmit(
      {
        target: Site.ContactUs.formDialog
      });
  }
};
$(document).ready(Site.ContactUs.initFormDialog);


/**
 * 
 * Site.ContactUs.SendFriend
 */
Site.ContactUs.SendFriend = {
  formDialog : null,
  
  initFormDialog : function() {
    Site.ContactUs.SendFriend.formDialog = $("#sendFriendDialog");
    Site.ContactUs.SendFriend.formDialog.dialog({
      autoOpen: false,
      bgiframe: true,
      width: 400,
      height: 400,
      modal: true,
      resizable: false
    });
  },
  
  showFormDialog : function() {
    Site.ContactUs.SendFriend.formDialog.load(
        "/contactus/send_to_a_friend", null, Site.ContactUs.SendFriend.formLoaded
    ).dialog("open");
  },
  
  formLoaded : function(responseText, textStatus, xmlHr) {
    $("#sendFriendPageTitleField")[0].value = document.title;
    $("#sendFriendPageUrlField")[0].value = window.location.href;
  },
  
  submitForm : function(options) {
    Site.ActivitySpinner.startSpinner($(options.spinner));
    $('#sendFriendForm').ajaxSubmit(
      {
        target: Site.ContactUs.SendFriend.formDialog
      });
  }
};

$(document).ready(Site.ContactUs.SendFriend.initFormDialog);

/**
 * 
 * Site.Club
 */
Site.Club = {
  infoForm : '#clubInfoForm',
  loginDialog : null,
  
  initLoginDialog : function() {
  	Site.Club.loginDialog = $("#clubLogin");
  	Site.Club.loginDialog.dialog({
  		autoOpen: false,
  		bgiframe: true,
  	    width: 400,
  	    modal: true,
  	    resizable: false
  	});
  },
  
  /**
   * showLoginDialog(data)
   * 
   * options - let's you pass in parameters to this form directly from JS
   *           currently supported params are:
   *           next_url - the url to which the user's browser will redirect if
   *                      login is successful, this is the most useful option
   *           member_number - the member number to use for login
   *           zip_code - the zip code to use for login
   */
  showLoginDialog : function(options) {
  	Site.Club.loginDialog.load(
  			"/club/account/login",
  			options
  	).dialog("open");
  },
  
  submitLoginForm : function(options) {
    Site.ActivitySpinner.startSpinner($(options.spinner));
  	$('#club_login').ajaxSubmit(
  		{
  			target: Site.Club.loginDialog
  		});
  },
  
  initInfoForm : function(options) {
    Site.Functions.applyPhoneFormat($(this.infoForm)[0].elements['phone']);
  },
  
  submitInfoForm : function(options) {
    if (options) {
      Site.ActivitySpinner.startSpinner($(options.spinner));
    }
    
//    Site.Functions.removePhoneFormat($(this.infoForm)[0].elements['phone']);
    $(this.infoForm).submit();
  }
};

$(document).ready(Site.Club.initLoginDialog);

/**
 * 
 * Site.Register
 */
Site.Register = {
  dialog : null,
  
  initDialog : function() {
    Site.Register.dialog = $("#registerDialog");
    Site.Register.dialog.dialog({
      autoOpen: false,
      bgiframe: true,
      width: 400,
      modal: true,
      resizable: false,
      position: 'top'
    });
  },
  
  showDialog : function(title, url, data) {
    Site.Register.dialog.load(
        url,
        data
    ).dialog('option', 'title', title);
    
    if ( ! Site.Register.dialog.dialog('isOpen') ) {
      Site.Register.dialog.dialog('open');
    }
  },
  
  showLoginDialog : function(data) {
    Site.Register.showDialog(
        'TangerStyle Community Login', 
        '/register/login',
        data);
  },
  
  submitLoginForm : function(options) {
    Site.ActivitySpinner.startSpinner($(options.spinner));
    $('#customerLoginForm').ajaxSubmit(
      {
        target: Site.Register.dialog
      });
  },
  
  showEmailSignupDialog : function() {
    Site.Register.showDialog('TangerStyle Email Signup', '/register/email');
  },
  
  submitEmailSignupForm : function(options) {
    Site.ActivitySpinner.startSpinner($(options.spinner));
    $('#emailSignupForm').ajaxSubmit(
      {
        target: Site.Register.dialog
      });
  },
  
  showCommunitySignupDialog : function() {
    Site.Register.showDialog('TangerStyle Community Signup', '/register/community');
  },
  
  submitCommunitySignupForm : function(options) {
    Site.ActivitySpinner.startSpinner($(options.spinner));
    $('#communitySignupForm').ajaxSubmit(
      {
        target: Site.Register.dialog
      });
  },
  
  showCommunityTermsDialog : function(options) {
    Site.Register.communityTermsFormSccuess = options.formSuccess;
    Site.Register.showDialog('TangerStyle Community Terms & Agreement', '/register/community_terms');
  },
  
  submitCommunityTermsForm : function(options) {
    Site.ActivitySpinner.startSpinner($(options.spinner));
    $('#communityTermsForm').ajaxSubmit(
      {
        target: Site.Register.dialog,
        success: Site.Register.communityTermsFormSccuess
      });
    
    Site.Register.communityTermsFormSccuess = undefined;
  }
};

$(document).ready(Site.Register.initDialog);