$j = jQuery.noConflict();

$j(document).ready(function(){
  stylePage();
});

function attachSelectStyle(select) {
  var div = $j(select).parent();
  var span = $j('span', div);
  span.text($j(select).find(':selected').text());
  
  $j(select).focus(function() {
    div.addClass('focus');
  }).blur(function(){
    div.removeClass('focus');
    div.removeClass('active');
  }).change(function(){
    span.text($j(this).find(':selected').text());
    div.removeClass('active');
  });
}

function stylePage() {
  $j('input[title],textarea').each(label_input);
  $j('form').submit(clear_labelled_fields);
  make_clickable('.clickable');
  $j("input:checkbox,input:radio,input:file,textarea").uniform();
  $j('select').each(function(){
    attachSelectStyle(this);
  });
}


// ---
function label_input() {
  var me = $j(this);
  console.log("Labelling: " + me.id);
  me.focus(function() {
    if (me.val() == me.attr('title')) {
      me.val('');
    }
    me.removeClass('labelled');
    return true;
  }).blur(function() {
    var val = $j.trim(me.val());
    if (val == me.attr('title') || val == '') {
      me.val(me.attr('title'));
      me.addClass('labelled');
    }
    return true;
  });
  me.blur();
}
  
function clear_labelled_fields() {
  var me = $j(this);
  var fields = me.find('input.labelled');
  fields.each(function() {
    var f = $j(this);
    f.val('');
    f.removeClass('labelled');
  });
}
  
function make_clickable(selector) {
  $j(selector).live('click', function() {
  window.location = $j(this).find('a').eq(0).attr('href');
  return false;
  }).live('mouseover', function() {
    $j(this).addClass('hover');
  }).live('mouseout', function() {
    $j(this).removeClass('hover');
  }).addClass('clickable');
}

