//equal heights function - matches heights of a group of elements to the largest 
function equalHeight(group) {
    tallest = 0;
    group.each(function() {
        thisHeight = $(this).height();
        if(thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
    group.height(tallest);
}

$.fn.fadeToggle = function(speed, easing, callback) {
return this.animate({opacity: 'toggle'}, speed, easing, callback);
};

// Define: Linkify plugin
(function($){

  var url1 = /(^|&lt;|\s)(www\..+?\..+?)(\s|&gt;|$)/g,
      url2 = /(^|&lt;|\s)(((https?|ftp):\/\/|mailto:).+?)(\s|&gt;|$)/g,

      linkifyThis = function () {
        var childNodes = this.childNodes,
            i = childNodes.length;
        while(i--)
        {
          var n = childNodes[i];
          if (n.nodeType == 3) {
            var html = $.trim(n.nodeValue);
            if (html)
            {
              html = html.replace(/&/g, '&amp;')
                         .replace(/</g, '&lt;')
                         .replace(/>/g, '&gt;')
                         .replace(url1, '$1<a href="http://$2" target=\"_blank\">$2</a>$3')
                         .replace(url2, '$1<a href="$2" target=\"_blank\">$2</a>$5');
              $(n).after(html).remove();
            }
          }
          else if (n.nodeType == 1  &&  !/^(a|button|textarea)$/i.test(n.tagName)) {
            linkifyThis.call(n);
          }
        }
      };

  $.fn.linkify = function () {
    return this.each(linkifyThis);
  };

})(jQuery);

$(document).ready(function() {

    $('img.caption').jcaption();

    /**************************************
    set equalHeights on content and sidebar
    ***************************************/
    // set equal heights on sidebar and content
    //equalHeight($('#sidebar, #content'));

    /***************
    set last classes
    ****************/
    //$('#news .row:last').addClass('last');
    
    /************
    lavalamp nav
    *************/
	//$("#primary-nav ul").addClass('lavaLampWithImage');
    //$("#primary-nav ul li a.current").parent('li').addClass('current');
    //$("#primary-nav ul").lavaLamp({
    //    speed: 350,
    //    click: function(event, menuItem) {
    //        //return false;
    //    }
    //});

    /**************
    input help text
    ***************/
    // set value to title text
    $('form .hashelptext').each(function(){
        // set original value to title
        $(this).val($(this).attr('title'));

        // set value to null on focus
        $(this).focus(function(){
            $(this).val('');
        });

        // reset help text to title if no user input
         $(this).blur(function(){
            if ( $(this).val() == '' )
                $(this).val($(this).attr('title'));
        });
    });

    $('#emaillink').hide();

    $('#email, input.close').click(function(event) {
       event.preventDefault;
       $('#emaillink').fadeToggle('slow');
       return false;
    });
    $("a#track-shipment,a#trade-alert").fancybox({
			'width'				: '50%',
			'height'			: '40%',
			'autoScale'     	: false,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'type'				: 'iframe'
		});

    $('#track_my_shipment').submit(function() {
       var s = $.trim($('#track_my_shipment input[name=trackNo]').val());

       var msg = '<h3>Please enter a Tracking/PAPS Number</h3>' +
                 '<a href="#" class="close">OK</a>';
       if (s.length == 0 || s == 'Enter Tracking/PAPS #') {
             //blockUI style confirmation window
             $.blockUI.defaults.css = { };
             $.blockUI({
                message: msg,
                overlayCSS: { opacity: '0.5' }
             });
             $('.blockOverlay').click($.unblockUI);
             $('.blockUI a.close').click(function(){
                $.unblockUI();
                return false;
             });
             return false;
       }

       $('#track_my_shipment input[name=trackNo]').val(s);
       return true;
    });

    // TWITTER
        var username='anderinger'; // set user name
        var format='json'; // set format, you really don't have an option on this one
        var url='http://api.twitter.com/1/statuses/user_timeline/'+username+'.'+format+'?callback=?'; // make the url
        $.getJSON(url,function(tweet){ // get the tweets
                $("#tweet").html(tweet[0].text); // get the first tweet in the response and place it inside the div
                $("#tweet").linkify();
                $("#tweet").fadeIn(500);

                $("#tweet1").html(tweet[0].text); // get the first tweet in the response and place it inside the div
                $("#tweet1").linkify();
                $("#tweet2").html(tweet[1].text); // get the first tweet in the response and place it inside the div
                $("#tweet2").linkify();
                $("#tweet3").html(tweet[2].text); // get the first tweet in the response and place it inside the div
                $("#tweet3").linkify();
                $("#tweet4").html(tweet[3].text); // get the first tweet in the response and place it inside the div
                $("#tweet4").linkify();
                $("#tweet5").html(tweet[4].text); // get the first tweet in the response and place it inside the div
                $("#tweet5").linkify();
                $(".tweet").fadeIn(800);
        });

    $("a#employment-policy").fancybox({'width'         : '50%',
                                       'height'        : '40%',
                                       'autoScale'     : false,
                                       'transitionIn'  : 'none',
                                       'transitionOut' : 'none'});
});

