$(document).ready(function() {
	
	equalHeight($(".eqh"));
	lastList($(".lastlist"));
	floatTheImage($('.floatimage'));
	convertPaging($('ul.convert-paging'));
	$(".home-section-col").height(parseFloat($('.home-section').height()) - 1);
	
	$("span.wo-title, span.title").each(function(){
		$(this).next().remove();
	})
	
	$('#q').labelify({ text: "label" });	
	//$("ul.paging").width($("ul.paging").innerWidth());

});

jQuery.fn.labelify = function(settings) {
  settings = jQuery.extend({
    text: "title",
    labelledClass: ""
  }, settings);
  var lookups = {
    title: function(input) {
      return $(input).attr("title");
    },
    label: function(input) {
      return $("label[for=" + input.id +"]").text();
    }
  };
  var lookup;
  var jQuery_labellified_elements = $(this);
  return $(this).each(function() {
    if (typeof settings.text === "string") {
      lookup = lookups[settings.text]; // what if not there?
    } else {
      lookup = settings.text; // what if not a fn?
    };
    // bail if lookup isn't a function or if it returns undefined
    if (typeof lookup !== "function") { return; }
    var lookupval = lookup(this);
    if (!lookupval) { return; }

    // need to strip newlines because the browser strips them
    // if you set textbox.value to a string containing them    
    $(this).data("label",lookup(this).replace(/\n/g,''));
    $(this).focus(function() {
      if (this.value === $(this).data("label")) {
        this.value = this.defaultValue;
        $(this).removeClass(settings.labelledClass);
      }
    }).blur(function(){
      if (this.value === this.defaultValue) {
        this.value = $(this).data("label");
        $(this).addClass(settings.labelledClass);
      }
    });
    
    var removeValuesOnExit = function() {
      jQuery_labellified_elements.each(function(){
        if (this.value === $(this).data("label")) {
          this.value = this.defaultValue;
          $(this).removeClass(settings.labelledClass);
        }
      })
    };
    
    $(this).parents("form").submit(removeValuesOnExit);
    $(window).unload(removeValuesOnExit);
    
    if (this.value !== this.defaultValue) {
      // user already started typing; don't overwrite their work!
      return;
    }
    
    // actually set the value
    this.value = $(this).data("label");
    $(this).addClass(settings.labelledClass);

  });
};


function equalHeight(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(parseFloat(tallest) + 50);
}

function lastList(group){
	group.each(function(){
		$(this).children('li:last').addClass('last');
	})
}

function floatTheImage(group){
	group.find("img[align='left'], img[align='right']").addClass('float-the-image');
	$('img.float-the-image').each(function(){
		var _w = $(this).width();
		var _align = $(this).attr('align');
		
		if(_align != ""){
			if(_w < 500){
				if(_align == 'left'){
					$(this).css('margin-right','10px').css('float','left');
				}else{
					$(this).css('margin-left','10px').css('float','right');
				}
			}
		}
	})
}

function convertPaging(group){
	group.find('strong, a').addClass('page-number');
	$('.page-number').each(function(){
		var _val = $(this).text();
		var _url = $(this).attr('href');
		if( _url ){
			$(this).parent().append("<li><a href='"+_url+"'>"+_val+"</a></li>");
			$(this).remove();
		}else{
			$(this).parent().append("<li class='active'><a href='#'>"+_val+"</a></li>");
			$(this).remove();
		}
	})
}

// Cufon
Cufon.replace(
	'#content .home-coverstory h1 a,' +
	'#content .home-coverstory h1 a,' +
	'#content .home-section ul li.home-section-col ul li h2,' +
	'#content .primary .page-section h1,' +
	'#content .primary ul.article-list li h2 a,' +
	'#content .primary ul.article-list li h2,' +
	'span.title,' +
	'div.blue-box540-inside form fieldset label',{ 
	hover: true
})
