formManager = function(name_var) {
  this.init(name_var, this);
};

$.extend(formManager.prototype, {
	instance_name: null,
	instance: null,
	
	init: function(instance_name, instance) {
		this.instance_name = instance_name;
		this.instance = instance;
	},
	
	value_toggle: function(element) {
		element.each( function() {
			if(this.type!='submit' && this.type!='hidden') {
				this.onfocus = function() {
					if(this.value == this.defaultValue) {
						this.value = '';
					}
				};
				this.onblur = function() {
					if(this.value == '') {
						this.value = this.defaultValue;
					};
				};

			};
		});
	},
	
	stripDefaultValues: function(elements){
		elements.each( function() {
			if(this.type!='submit' && this.type!='hidden') {
				if(this.value == this.defaultValue) {
					this.value = '';
				}
			}
		});
	},
	
	setDefaultValues: function(elements){
		elements.each( function() {
			if(this.type!='submit' && this.type!='hidden') {
				if(this.value == '') {
					this.value = this.defaultValue;
				}
			}
		});
	}

});

$(document).ready(function(){

	var FormManager = new formManager('logo');
	FormManager.value_toggle($("form.default input"));
	FormManager.value_toggle($("form.default textarea"));
	
	
	
	$("a[href$=jpg], a[href$=png], a[href$=gif]").fancybox({	
			'zoomOpacity'			: true,
			'overlayShow'			: true,
			'zoomSpeedIn'			: 500,
			'zoomSpeedOut'			: 500,
			'overlayOpacity'		: 0.9
	});
	
  // Subscribe form
	$('#new_subscriber').submit(function() {
	
		FormManager.stripDefaultValues($("form#new_subscriber.default input"));
		FormManager.stripDefaultValues($("form#new_subscriber.default textarea"));
		
    $('#subscribe_form .flash').remove();
    // do validation.. if you want and then post it. 
    $.ajax({
      type: "POST",
      url: $(this).attr('action'),
      data: $(this).serialize(),
      success: function(data, textStatus){
        $('#new_subscriber').before('<div class="flash notice" id="sub-success" style="display:none;">Thank you for subscribing.</div>');
        $('#new_subscriber').fadeOut('fast', function(){ $('#sub-success').fadeIn(); });
      },
      error: function(request, textStatus){
      	FormManager.setDefaultValues($("form#new_subscriber.default input"));
      	FormManager.setDefaultValues($("form#new_subscriber.default textarea"));

        $('#new_subscriber').before('<div class="flash error">'+eval(request.responseText)+'</div>');
      },
      dataType: 'json'
    });
    return false; 
  });
  
  // contact form
	$('#new_contact').submit(function() {

		FormManager.stripDefaultValues($("form#new_contact.default input"));
		FormManager.stripDefaultValues($("form#new_contact.default textarea"));

    $('div.flash').remove();
    // do validation.. if you want and then post it. 
    $.ajax({
      type: "POST",
      url: $(this).attr('action'),
      data: $(this).serialize(),
      success: function(data, textStatus){
        $('#new_contact').before('<div class="flash notice" id="sub-success" style="display:none;">Thanks for getting in contact!</div>');
        $('#new_contact').fadeOut('fast', function(){ $('#sub-success').fadeIn(); });
      },
      error: function(request, textStatus){
      	FormManager.setDefaultValues($("form#new_contact.default input"));
      	FormManager.setDefaultValues($("form#new_contact.default textarea"));

        $('#new_contact').before('<div class="flash error">'+eval(request.responseText)+'</div>');
      },
      dataType: 'json'
    });
    return false; 
  });
  
  
  // comment form
	$('#comment-form').submit(function() {

		FormManager.stripDefaultValues($("form#comment-form.default input"));
		FormManager.stripDefaultValues($("form#comment-form.default textarea"));

    // do validation.. if you want and then post it. 
    $.ajax({
      type: "POST",
      url: $(this).attr('action'),
      data: $(this).serialize(),
      success: function(data, textStatus){
        $('#comment-form').before('<div class="flash notice" id="sub-success" style="display:none;">Thank you for your comment.</div>');
        $('#comment-form').fadeOut('fast', function(){ $('#sub-success').fadeIn(); });
        
        display_comment(data);
      },
      error: function(request, textStatus){
      	FormManager.setDefaultValues($("form#comment-form.default input"));
      	FormManager.setDefaultValues($("form#comment-form.default textarea"));
        $('#comment-form').before('<div class="flash error">'+eval(request.responseText)+'</div>');
      },
      dataType: 'json'
    });
    return false; 
  });

  var display_comment = function(comment){
  	//first attribute
  	for(key in comment) {
  		comment = comment[key];
  		break;
	}
	
  	 li = $(document.createElement('li'));
  	 
  	 li.append('<p>' + comment.body + '</p>');

	 d = comment.created_at.match(/^([0-9]{4})\-([0-9]{2})\-([0-9]{2})T.*$/);
		dl = document.location;
  	 li.append('<a title="Permalink to this comment" href="' + dl.protocol + '//' + dl.host + dl.pathname + '#comment:' + comment.id + '">' + d[3] + '-' + d[2] + '-' + d[1] + '</a>');
  	 
  	 li.append(' | posted by ');
  	 if(comment.url.length >0){
	  	li.append('<a title="Created by ' + comment.author + ', ip recorded." href="' + comment.url + '">' + comment.author + '</a>');
	 }else{
	  	li.append(comment.author);
	 }

  	 if(!$('#comment-list').length){
  	 	
  	 	$('div#comments h2:first').after('<ul id="comment-list"></ul>');
  	 	$('div#comments p.no-comments').remove();
  	 }
  	 
  	 $('#comment-list').append(li);
  };

	//SWFOBJECT
	swfobject.embedSWF("/flash/ident/ident.swf", "site-title", "140", "140", "10.0.0", null, null, {menu: "false", wmode: 'transparent', bgcolor: "#292929"});  
	

});


// Cufon Replacement
	Cufon.replace('p.home-intro', { fontFamily: 'Gotham Book' });
	Cufon.replace('h1', { fontFamily: 'Gotham Book' });
	Cufon.replace('h3', { fontFamily: 'Gotham Bold' });


	$(document).ready(function() {
		
		// Clicky links
		var link;
		$("div.subpage").hover(function(){ 
			$(this).css("cursor","pointer");	
		});

		$("div.subpage").click(function(){
				link = $(this).find('h2 a').attr("href");
			 window.location.href = link;
		});
		
		$("div.driver").hover(function(){ 
			$(this).css("cursor","pointer");	
		});

		$("div.driver").click(function(){
				link = $(this).find('h3 a').attr("href");
			 window.location.href = link;
		});

		// ie6 fix
		
		$(".driver, .subpage, #help-menu, ul.main-nav li, div.share").hover(
      function () {
        $(this).addClass("hover");
      }, 
      function () {
        $(this).removeClass("hover");
      }
    );	
			
		$("div.subpage:first").css("margin-left","0");
		$("div.subpage:eq(3),div.subpage:eq(6) ").css("margin-left","0");
		
		//clear values
			$("#top-q, #q, #location, .resource-list dd input").each( function() {
				this.initial = this.value;
				if(this.type!='submit') {
					this.onfocus = function() {
						if(this.value == this.initial) {
							this.value = '';
						}
					};
				}
			});
		
		
	});

