Cufon.replace('span.project-info')('#main-nav.consultations a',{hover:true})('span.project-title')('h2')('#main-content h1')('.post h3')('.post h4')('dl.project-info dd')('dl.project-info dt')('#supplementary-content h3')('p.title')('span.footer-title')('.list-projects li .client');

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

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;
				}
			}
		});
	}

});

$.extend(jbLogo.prototype, {
   	instance_name: null,
	instance: null,
	selected_input: false,
 	load_status: 0,
    latest_articles: [],
	latest_projects: [],
	els: [],
	rows: [],

   init: function(instance_name, instance) {
	 this.instance_name = instance_name;
	 this.instance = instance;
	
	 $('input, textarea, select').focus(function() {
		instance.selected_input = true;
	 });
	
	 this.registerKeyPresses();
   },

   registerKeyPresses: function() {
	    var obj = this;
		$(document).keypress(function(e) {
			//rows
			switch (e.which) {
				case 49:
					obj.transition(0,"row");
					break;
				case 50:
					obj.transition(1,"row");
					break;
				case 51:
					obj.transition(2,"row");
					break;
				case 52:
					obj.transition(3,"row");
					break;
				case 53:
					obj.transition(4,"row");
					break;
				case 54:
					obj.transition(5,"row");
					break;
				case 55:
					obj.transition(6,"row");
					break;
				case 97:
					obj.transition(0,"col");
					break;
				case 98:
					obj.transition(1,"col");
					break;
				case 99:
					obj.transition(2,"col");
					break;
				case 100:
					obj.transition(3,"col");
					break;
				case 101:
					obj.transition(4,"col");
					break;
				case 102:
					obj.transition(5,"col");
					break;
				case 103:
					obj.transition(6,"col");
					break;
			}
		});
   },

   loadAjax: function(urls) {
		obj = this;
     	$.each(urls, function(i,url){
			$.getJSON(url, function(data){
				$.each(data, function(i,item){
					if(url.match("articles")) {
						if(item.article!=undefined) {
							obj.latest_articles.push(item.article.permalink);
						}
					} else {
						if(item.project!=undefined) {
							obj.latest_projects.push(item.project["permalink"]);
						}
					}
				});
				obj.load_status++;
				obj.construct();
			});
		});
   },

   construct: function() {
		if(this.load_status == 2) {
			//create list, populate
			//var els = new Array();
			for(i=0;i<(7*7);i++) {
				this.els.push($(document.createElement("li")));
			}
	
	 		$("h1#site-title").replaceWith('<ul id="logo-fun"></ul>');
			$("#logo-fun").click(function(){ document.location = "/"; });
			$("#logo-fun").css("cursor","pointer");
			
			var j=0;
			$.each(this.els, function(i, el) {
				$("#logo-fun").append(el);                              
				if (i % 7 == 0) {
					el.addClass("last");
				}
			});
	
			//set up the grid, we dont have multi dimesional arrays in JS so nested will have to do
			columns1 = new Array(7);      
			columns2 = new Array(7);   
			columns3 = new Array(7);
			columns4 = new Array(7);
			columns5 = new Array(7);
			columns6 = new Array(7);
			columns7 = new Array(7);

			this.rows = new Array(7);
			this.rows[0] = columns1;
			this.rows[1] = columns2;
			this.rows[2] = columns3;
			this.rows[3] = columns4;
			this.rows[4] = columns5;
			this.rows[5] = columns6;
			this.rows[6] = columns7;

			for (i=0; i < 7; i++) { columns1[i] = i; }           // number the columns 1 through 5
			for (i=0; i < 7; i++) { columns2[i] = i + 7; }     // number the next row of columns 6 through 10
			for (i=0; i < 7; i++) { columns3[i] = i + 14; }   // and so on
			for (i=0; i < 7; i++) { columns4[i] = i + 21; }
			for (i=0; i < 7; i++) { columns5[i] = i + 28; }
			for (i=0; i < 7; i++) { columns6[i] = i + 35; }
			for (i=0; i < 7; i++) { columns7[i] = i + 42; }
		}
		
		$("#logo-fun li").each(function(){
		this.onmouseover = function() {
				$(this).animate({opacity: 0}, 500 );
			};
			this.onmouseout = function() {
				$(this).animate({opacity: 1}, 500 );
			};
		});
   },

   transition: function(elm, r_or_c) {
		var obj = this;
		if(this.selected_input == false) {
			for(i=0;i<7;i++) {
				//if the 7th one then trigger page change
				if(r_or_c == "row") {
					if(i != 6) {
						this.els[this.rows[elm][i]].animate({opacity: 0}, (500 + i*100));
					} else {
						this.els[this.rows[elm][i]].animate({opacity: 0}, (500 + i*100));
						setTimeout(function() { obj.gotoLink(elm, r_or_c); }, 1000);
					}
				} else {
					if(i != 6) {
						this.els[this.rows[i][elm]].animate({opacity: 0}, (500 + i*100));
					} else {
						this.els[this.rows[i][elm]].animate({opacity: 0}, (500 + i*100));
						setTimeout(function() { obj.gotoLink(elm, r_or_c); }, 1000);
					}
				}
			}
		}
   },

   gotoLink: function(which, r_or_c) {
		if(r_or_c == "row") {
			//if there aren't enough articles then randomly pick one
			if(this.latest_articles[which]!=undefined) {
				document.location = this.latest_articles[which];
			} else {
				document.location = this.latest_articles[Math.floor(Math.random()*this.latest_articles.length)];
			}
		} else if(r_or_c == "col") {
			//if there aren't enough projects then randomly pick one
			if(this.latest_projects[which]!=undefined) {
			document.location = this.latest_projects[which];
			} else {
			document.location = this.latest_projects[Math.floor(Math.random()*this.latest_projects.length)];
			}
		}
   }
   


});

$(document).ready(function(){
	
	if($('body.consultations').length <= 0) {
		var Logo = new jbLogo('logo');
		Logo.loadAjax(['/articles.json?per_page=7','/projects.json?per_page=7']);
	}
	
	var FormManager = new formManager('logo');
	FormManager.value_toggle($("form.default input"));
	FormManager.value_toggle($("form.default textarea"));
	
  // 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"));

    $('#contact_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_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"));

    $('#comments .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){
        $('#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
	if($('body.consultations').length <= 0) {
		swfobject.embedSWF("/flash/ident/ident.swf", "site-title", "140", "140", "10.0.0", null, null, {menu: "false", wmode: 'transparent', bgcolor: "#292929"});  
	}

  	if($.browser.msie && $.browser.version.substr(0,3)=="6.0") {
		$("#recent-articles dt:first").addClass("first");
	}


	//SWFOBJECT
	swfobject.embedSWF("/flash/banner/hero_banner.swf", "slides", "750", "422", "9.0.0", null, null, {menu: "false", bgcolor: "#0093D1"});



	
});



