// OPENBUSINESS

// GUIDE
	
// 1 - spans
// 2 - first
// 3 - last
// 4 - add other classes
// 5 - forms
// 6 - tabbed menu



jQuery(document).ready(function ($) {

// $("div:hidden").show().addClass("spam");

// 1 - spans -------------------------------------------------------------------

	$("#logo, #open100").wrapInner(document.createElement("span"));
	
	$("div#leader").wrapInner(document.createElement("div"));
	
	$("#leader div:first").attr("id" , "inner").addClass("clearfix");
	$("#leader").addClass("clearfix");

$.fn.wrapMe = function(options) {

		var defaults = { myElm: "<span></span>" };
		var options = $.extend(defaults, options);
		
		$(this).each(function() {
			if (this.firstChild && this.firstChild.nodeType == 3) {				//look for list elements which contain only text and wrap a span inside
			$(this.firstChild).wrap(options.myElm)
		}});

		// allow jQuery chaining

		return this;

	};

$(".postMeta li").wrapMe();


// 2 - first -------------------------------------------------------------------

	$(".hMenu li:first-child, .directory tr td:first-child, .directory tr th:first-child, #openMenu li:first-child, #sideBar ul li:first-child").addClass("first");
	$("#leader div.col8padded:first-child, #mainCol div.col3:first-child, #mainCol div.col4:first-child, feeds div:first-child").addClass("firstCol");

// 3 - last --------------------------------------------------------------------

	$("#openMenu li:last-child, #sideBar ul li:last-child, .companyInfo li:last-child, #description .imgLeft:last").addClass("last");

// 4 - add other classes -------------------------------------------------------

	// clearing
	
	$("#featured, .hMenu, #leader, #leaderContribute, .navigation").addClass("clearfix");
		
	// link styles

	$("#footer p a, #viewAll a").addClass("linkStyle1");

	$("#featured .hMenu li a,p.linklove a, .postMeta li a, #footerMenu li a, .catLink, #sideBar h2 a, #sideBar h3 a, #sideBar ul a, .comment-author a, .comment-meta a, .tabbed li a, .nakedList li a").addClass("linkStyle2");
	
	$("#sideBar .more-link, .comment-reply-link,#leader p a, .license_block a").addClass("linkStyle3");
	
	$("h1 a, #mainCol h3 a, #openMenu a").addClass("linkStyle4");
	$("#leader h2 a").addClass("linkStyle2");

//	$("#mainCol h2 a, #leaderContribute h2 a").addClass("linkStyle2");
//		$(".homeTitle a").removeClass("linkStyle4").addClass("linkStyle2");
//		$("#leader h1 a").removeClass("linkStyle4").addClass("linkStyle2");
//		$("#leader h2 a").removeClass("linkStyle4").addClass("linkStyle2");
//		$("#contribute h2 a").removeClass("linkStyle4").addClass("linkStyle2");
//		$("#feeds h2 a").removeClass("linkStyle4").addClass("linkStyle2");


	$("#mainCol h2 a").each(function() {

		var $this = $(this);
		var parentId = $this.parents("div").attr("class");
	
		if (parentId != "summary" ) {
			$this.addClass("linkStyle2");
			} else {
			$this.addClass("linkStyle4");
		}
	});

	$(".directory h2 a").removeClass("linkStyle2").addClass("linkStyle4");

	$(".tabbed li").addClass("tab");
	
	// menu types

	$("ul.postMeta").addClass("discreet");
	$("#sideBar ul, .companyInfo").addClass("vMenu");
	
	// table styling
	
	$(".directory tr th:odd").addClass("altCol");
	
	$(".directory tr td:nth-child(even)").addClass("altCol");

	$(".directory tr td p").addClass("w3");
	
	$(".dirColFirst").addClass("w2");
	
	$(".directory .lastCol").addClass("w4");
	
	// other stuff
	
	$(".feedList ul").addClass("newsFeed");
	$("#commentform fieldset ul li:nth-child(2)").addClass("marginTop");
	$(".neatForm textarea, .cform textarea").addClass("w6");
	//$("#leader").prepend("<span></span>");
	$(".wp-pagenavi").addClass("w12");
	$("#commentForm h2, #comments h2, .neatForm legend").addClass("decorated");
	$(".neatForm label, .cform label").addClass("w2");
	$("#comments .linkStyle3, .catLink, .moreLink .last a").append(" &raquo;");
	$(".comment-author img").addClass("imgLeft");
	

// 5 - forms -------------------------------------------------------
	
	// http://remysharp.com/2007/01/25/jquery-tutorial-text-box-hints/
	
	$.fn.hint = function (blurClass) {
  if (!blurClass) { 
    blurClass = 'blur';
  }

  return this.each(function () {
    // get $ version of 'this'
    var $input = $(this),

    // capture the rest of the variable to allow for reuse
      title = $input.attr('title'),
      $form = $(this.form),
      $win = $(window);

    function remove() {
      if ($input.val() === title && $input.hasClass(blurClass)) {
        $input.val('').removeClass(blurClass);
      }
    }

    // only apply logic if the element has the attribute
    if (title) { 
      // on blur, set value to title attr if text is blank
      $input.blur(function () {
        if (this.value === '') {
          $input.val(title).addClass(blurClass);
        }
      }).focus(remove).blur(); // now change all inputs to title

      // clear the pre-defined text when form is submitted
      $form.submit(remove);
      $win.unload(remove); // handles Firefox's autocomplete
    }
  });
};

$('input:text').hint();

// text area hint

	// http://blog.amnuts.com/2009/02/17/text-box-hint-values-with-jquery/
	
    $.fn.textboxhint = function(userOptions) {
        var options = $.extend({}, $.fn.textboxhint.defaults, userOptions);
        var $this = $(this);
        return $this.filter(':text,textarea').each(function(){
            if ($this.val() == '') {
                $this.focus(function(){
                    if ($this.attr('typedValue') == '') {
                        $this.removeClass(options.classname).val('');
                    }
                }).blur(function(){
                    $this.attr('typedValue', $(this).val());
                    if ($this.val() == '') {
                        $this.addClass(options.classname).val(options.hint);
                    }
                }).blur();
            }
        });
    };
 
    $.fn.textboxhint.defaults = {
        // hint: 'Please enter a value',
        classname: 'hint'
    };


	$(".neatForm label:contains('Your comment')").next("textarea").textboxhint({
		hint: 'Your comment'
	});

	$(".neatForm label:contains('What they do')").next("textarea").textboxhint({
		hint: 'What they do'
	});

	$(".neatForm label:contains('How they demonstrate openness')").next("textarea").textboxhint({
		hint: 'How they demonstrate openness'
	});


	$("label:contains('Name'), label:contains('Email'), label:contains('Message'), label:contains('Comment'), label:contains('Your Comment'), label:contains('Captcha'), label:contains('URL'), label:contains('Category'), label:contains('What they do'), label:contains('How they demonstrate openness'), label:contains('Company logo'), label:contains('Email address')").append(" <abbr title=\"Required field\">*</abbr>");


// 6 - Tabbed menu effects  ----------------------------------------------------
		
	// When a link is clicked change class of parent list item
	$(".tab").click(function () {
			
		// switch all tabs off
		$(".current-cat").removeClass("current-cat");
			
		// switch this tab on
		$(this).addClass("current-cat");

	});
			
	// When a link is clicked slide content in and out
	$("a.tabbedLink").click(function () {

		// slide all elements with the class 'content' up
		$(".tabbedContent").hide();
	
		// Find the ID of the link and pass it as a class to the pageBody and wrapper. Display the div and slide down the content.
		var content_show = $(this).attr("id");
		$("."+content_show).show();

	});
	

});