$(function() {
    $('#change').click(function() {
        $('#user-client').hide();
        $('#user-client-select').show();
        return false;
    });
    
    $('#user-client-select select').change(function() {
        document.cookie = 'client=' + this.value + '; expires=Wed, 1 Aug 2020 00:00:00 UTC; path=/';
        document.location = document.location;
    });

	$("#more-tmail").click(function () {
		  $(".column-2").toggle();
		  return false;
	    });

});

function ajax_json(url, data, callback) {
	$.ajax({ type: 'POST', url: url, data: data, dataType: 'json', success: function(json) {
		if (json.alert) {
			alert(json.alert);
		} else if (json.error) {
			alert(json.error);
		} else if (json.confirm) {
			if (confirm(json.confirm)) {
				data.confirm = 1;
				ajax_json(url, data, callback);
			}
		} else {
			callback(json);
		}
	}, error: function(a, b, c) {
		alert('There has been an unexpected error: ' + c);
	} })
}


function ajaxSubmitButton(formitForm, submittingMessage, submittedMessage) {
    $(function() {
        formitForm.setup({
            submit: function(val) {
                $('input[@type=submit]').val(submittingMessage);
                $.post(document.location, val, function() {
                    $('input[@type=submit]').val(submittedMessage);
                });
            }
        });
    });
}

ajax = {
    post: function(element, action, params) {
        var widget = $(element).parents('.widget');
        params.widget = widget.attr('id');
        params.action = action;
        $.post(ajax.getRelativeURI(), params, function(json) {
            if (json.confirm) {
                if (confirm(json.confirm)) {
                    params.confirm = 1;
                    ajax.post(element, action, params);
                }
            }
            if (json.error) {
                alert(json.error);
            }
            if (json.html) {
                widget.html(json.html);
                ajax.setHandlers(widget);
            }
        }, 'json');
    },
    
    callback: function(json) {
        
    },
    
    getRelativeURI: function(location) {
        location = location ? location : document.location;
    	var regex = new RegExp('http://[^/]+(/?.+)');
    	var match = regex.exec(location);
    	return match ? match[1] : location;
    },
    
    getParam: function(element, param) {
        var idElement = $(element);
        var regexp = new RegExp(param+'-([a-z0-9_]+)');
        while (true) {
        	if ($(idElement).is('html') || !element) {
        	    return null;
        	}
        	var cssClass = idElement.attr('class');
        	if (cssClass) {
        	    var match = regexp.exec(cssClass);
        	    if (match) {
        		return match[1];
        	    }
        	}
        	idElement = idElement.parent();
        }
    },
    
    setHandlers: function(element) {
        $(element).find('a.ajax').click(function() {
            var action = ajax.actions[ajax.getParam(this, 'action')];
            if (action) {
                action(this);
            } else {
                alert('No action assigned for ' + ajax.getParam(this, 'action'));
            }
            return false;
        });
        $(element).find('input[@type=checkbox]').click(function() {
            var action = ajax.getParam(this, 'action');
            ajax.post(this, action, { id: ajax.getParam(this, 'id'), checked: $(this).attr('checked') ? 1 : 0 });
        });
    },
    
    actions: {
        paginate: function(element) {
            ajax.post(element, 'paginate', { start: ajax.getParam(element, 'start') });
        },
        
        cancel: function(element) {
            ajax.post(element, 'cancel', { id: ajax.getParam(element, 'id'), start: ajax.getParam(element, 'start') });
        },
        
        remove: function(element) {
            ajax.post(element, 'remove', { id: ajax.getParam(element, 'id'), start: ajax.getParam(element, 'start') });
        }
    }
}
$(function() {
    ajax.setHandlers($('body'));
});

function get_property(element, property) {
  var idElement = $(element);
  var regexp1 = new RegExp('^' + property+'-([a-z0-9_]+)');
  var regexp2 = new RegExp(' ' + property+'-([a-z0-9_]+)');
  for (var i = 0; i < 20; i++) {
    if ($(idElement).is('html')) {
      return null;
    }
    var cssClass = idElement.attr('class');
    if (cssClass) {
      var match = regexp1.exec(cssClass);
      if (match) {
		return match[1];
      }
      match = regexp2.exec(cssClass);
      if (match) {
		return match[1];
      }
    }
    idElement = idElement.parent();
  }
  alert('ran into endless loop trying to get ' + property + ' from ' + $(element).html());
  return null;
}

function string_to_tag(s) {
  if (!s) {
    return '';
  }
  s = s.toLowerCase();
  s = s.replace(/ & /g, ' and ');
  s = s.replace(/^\s+|\s+$/g, ''); // trim
  s = s.replace(/[ ]+/g, '-');
  s = s.replace(/[-]+/g, '-');
  s = s.replace(/[^a-z0-9-]+/g, '');
  return s;
}

function h(s) {
  if (s == null) {
    return '';
  }
  s = s.toString();
  s = s.replace(/&/g, '&amp;');
  s = s.replace(/\"/g, '&quot;');
  s = s.replace(/</g, '&lt;');
  s = s.replace(/>/g, '&gt;');
  return s;
}

function u(s) {
  return encodeURIComponent(s);
}

function classattr(attrs) {
  var a = [];
  for (var k in attrs) {
    if (attrs[k]) {
      a.push(k);
    }
  }
  return a.length ? ' class="' + a.join(' ') + '"' : '';
}

function uri(uri, params) {
  if (!params || !uri) {
    return uri;
  }
  var parts = uri.split(/\?/);
  var path = parts[0];
  var args = {};

  if (parts.length > 1) {
    var args_list = parts[1].split(/\&/);
    for (var i=0; i < args_list.length; i++) {
      parts = args_list[i].split(/=/);
      args[parts[0]] = decodeURIComponent(parts[1]);
    }
  }

  for (var k in params) {
    if (params[k] === null) {
      delete args[k];
    } else {
      args[k] = params[k];
    }
  }

  var query = '';
  for (var k in args) {
    query += '&' + k + '=' + encodeURIComponent(args[k]);
  }
  if (!query) {
    return path;
  } else {
    return path + '?' + query.substring(1);
  }
}

function truncate(string, length) {
  if (string === null) {
    return '';
  }
  string = string.toString();
  if (!length || string.length <= length) {
    return string;
  }
  return string.substring(0, length - 2) + '...';
}

function element(name, attribs, is_open) {
  var html = '<' + name;
  if (attribs) {
    for (var k in attribs) {
      if (attribs[k] !== null) {
        html += ' ' + k + '="' + h(attribs[k]) + '"';
      }
    }
  }
  if (!is_open) {
    html += ' /';
  }
  html += '>';
  return html;
}

function link(href, name, attribs, length) {
  name = name !== null ? name : href;

  // shortcut: if third param is number, treat it as length param
  if (!length && typeof(attribs) == 'number') {
    length = attribs;
    attribs = {};
  } else {
    attribs = attribs ? attribs : {};
  }
  attribs.href = href;
  var text = length ? truncate(name, length) : name;
  return element('a', attribs, true) + h(text) + '</a>';
}

function link_image(href, attribs, iattribs) {
  // shortcut: if first param is string, shift values
  if ( !iattribs ) {
	  iattribs = attribs;
    attribs = {};
  }
  attribs.href = href;
  var image = element('img', iattribs);
  return element('a', attribs, true) + image + '</a>';
}

function input_text(name, value, attribs) {
  attribs = attribs ? attribs : {};
  attribs.name = name;
  attribs.type = attribs.type ? attribs.type : 'text';
  if (typeof(attribs.value) == 'undefined') {
    attribs.value = request[name] ? request[name] : value;
  }
  return element('input', attribs);
}

function input_file(name, attribs) {
  attribs = attribs ? attribs : {};
  attribs.name = name;
  attribs.type = 'file';
  return element('input', attribs);
}

function input_hidden(name, value, attribs) {
  attribs = attribs ? attribs : {};
  attribs.name = name;
  attribs.value = value;
  attribs.type = 'hidden';
  return element('input', attribs);
}

function input_submit(value, attribs) {
  attribs = attribs ? attribs : {};
  attribs.value = value;
  attribs.type = 'submit';
  return element('input', attribs);
}

function input_radio(name, value, attribs) {
  attribs = attribs ? attribs : {};
  attribs.type = 'radio';
  attribs.name = name;
  attribs.value = value;
  if (request && request[name] == value) {
    attribs.checked = 'checked';
  }
  return element('input', attribs);
}

function input_checkbox(name, checked, attribs) {
  attribs = attribs ? attribs : {};
  attribs.name = name;
  attribs.type = 'checkbox';
  if (checked) {
    attribs.checked = 'checked';
  }
  return element('input', attribs);
}

function textarea(name, value, attribs) {
  attribs = attribs ? attribs : {};
  attribs.name = name;
  return element('textarea', attribs, true) + h(value) + '</textarea>';
}

function button(label, attribs) {
  return element('button', attribs, true) + h(label) + '</button>';
}

function select(name, options, value, attribs, options_attribs) {
  attribs = attribs ? attribs : {};
  attribs.name = name;
  var html = element('select', attribs, true);
  html += select_options(options, value, options_attribs);
  html += '</select>';
  return html;
}

function select_options(options, value, attribs) {
  var html = '';
  if (options instanceof Array) {
    for (var i=0; i<options.length; i++) {
      var k = options[i];
      var v = options[i];
      if (typeof(v) == 'object') {
        k = v.getName();
        v = v._id;
      }
      var options_attribs = { value: v };
      if (value == v) {
        options_attribs.selected = 'selected';
      }
      html += element('option', options_attribs, true) + h(k) + '</option>';
    }
  } else {
    for (var k in options) {
      var options_attribs = { value: options[k] };
      if (value == options[k]) {
        options_attribs.selected = 'selected';
      }
      html += element('option', options_attribs, true) + h(k) + '</option>';
    }
  }
  return html;
}












/* network rollovers */
$(function() {
        $('#network-map a img').hover(
            function() {
                this.src = this.src.replace( /\.gif$/, '_over.gif' );
            },
            function() {
                this.src = this.src.replace( /_over\.gif$/, '.gif' );
            }
        );
    }); 


/* I'll check the docs for this to make it more efficient */

/* network rollovers */
$(function() {
        $('#network-map-1').hover(
            function() {
				$("#network-map-copy-1").show();
            },
            function() {
                $("#network-map-copy-1").hide();
            }
        );
    }); 

$(function() {
        $('#network-map-2').hover(
            function() {
				$("#network-map-copy-2").show();
            },
            function() {
                $("#network-map-copy-2").hide();
            }
        );
    }); 

$(function() {
        $('#network-map-3').hover(
            function() {
				$("#network-map-copy-3").show();
            },
            function() {
                $("#network-map-copy-3").hide();
            }
        );
    }); 

