

jQuery.fn.hint = function (blurClass) {
  if (!blurClass) {
    blurClass = 'blur';
  }

  return this.each(function () {
    // get jQuery 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
    }
  });
};


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

  var title = document.title;

  $.address.init(function(event) {
    $('#tabs').tabs();

    $('input[title!=""]').hint();
    $('a[rel=external]').attr('target', '_blank');

    $('#followContainer').fadeIn('slow', function() {
      if($.browser.msie) {$(this).get(0).style.removeAttribute('filter');}
    });

    $('#optionsContainer').slideDown('slow');

    $('a[rel=help]').bind('click', function(e) {
      var contentHeight = ($('#content').height() > 1800) ? $('#content').height()+375 : 1800 ;

      $('#helpContainer').css('height', contentHeight).toggle('slide', 'slow');
      e.preventDefault();
    });

    $('a[rel=about]').bind('click', function(e) {
      var contentHeight = ($('#content').height() > 1800) ? $('#content').height()+375 : 1800 ;

      $(this).toggleClass('selected');
      $("body").append('<div id="curtain"></div>');
      $('#curtain').fadeToggle();
      $('#helpContainer:visible').hide('slide', 'slow');
      $('#miscContainer').css('height', contentHeight).toggle('slide', 'slow');
      e.preventDefault();
    });

    $('#btnCloseMisc').bind('click', function(e) {
      $('a[rel=about]').toggleClass('selected');
      $('#miscContainer').hide('slide', 'slow');
      $('#curtain').fadeOut('slow', function() {
        $(this).remove();
      });
      e.preventDefault();
    });

    $('#btnCloseHelp').bind('click', function(e) {
      $('#helpContainer').hide('slide', 'slow');
      e.preventDefault();
    });

    $('#btnSubscribe').bind('click', function(e) {
      $(this).toggleClass('selected');
      $('#subscribeContainer').slideToggle("slow");
      e.preventDefault();
    });



    $('#bulletin').anythingSlider({
          easing: "easeInOutExpo",        // Anything other than "linear" or "swing" requires the easing plugin
          autoPlay: true,                 // This turns off the entire FUNCTIONALY, not just if it starts running or not.
          delay: 5000,                    // How long between slide transitions in AutoPlay mode
          startStopped: false,            // If autoPlay is on, this can force it to start stopped
          animationTime: 600,             // How long the slide transition takes
          hashTags: false,                 // Should links change the hashtag in the URL?
          buildNavigation: false,          // If true, builds and list of anchor links to link to each slide
          pauseOnHover: true,             // If true, and autoPlay is enabled, the show will pause on hover
          startText: "Go",             // Start text
           stopText: "Stop" //,               // Stop text
          //   navigationFormatter: formatText       // Details at the top of the file on this use (advanced use)
            });
  }).change(function(evt) {
    var section = evt.value;
    var path = evt.pathNames[0];
    var selection = $('a[rel=address:' + section + ']');

    $('#content:visible').fadeOut('slow');

     $('a[rel^=address]').each(function() {
             $(this).toggleClass('selected', $(this).attr('title') == selection.attr('title'));
          });



    if (path == 'projects') {
      $('#otherContainer').animate({"left": "550px"}, "slow");
    } else {
      $('#otherContainer').animate({"left": "430px"}, "slow");
    }

    // console.log(path);

    $('#miscContainer:visible').hide('slide', 'slow', function() {
      $('a[rel=about]').toggleClass('selected');
      $('#curtain').fadeOut('slow', function() {
        $(this).remove();
      });
    });

    $('#helpContainer:visible').hide('slide', 'slow');

      $.address.title(title + ' | ' + selection.attr("title"));

    $.scrollTo({top: 0, left: 0}, 800);

    if (typeof path == 'undefined') {

      path = 'shop';

      $('a[href=shop]').toggleClass('selected');
    }

    $.ajax({
      url: 'page/' + path,
      beforeSend : function() {
          $("body").append('<div id="loading"></div>');
    //	  $("#contentContainer").fadeOut('slow');
      },
        success: function(html){

        $("#contentContainer").fadeOut('slow', function() {
        $(this).hide().html(html).fadeIn('slow', function() {
          $("#loading").remove();
          $('a[rel=external]').attr('target', '_blank');
          $('a[rel^=address]').address();
          if($.browser.msie) {$(this).get(0).style.removeAttribute('filter');}

          imagePreview();
          filterGallery();
          viewInfo();

          $('img.thumb').bind('mouseover', function() {
            $(this).stop().fadeTo("slow", 0.5);
          }).bind('mouseout', function() {
            $(this).stop().fadeTo("slow", 1);
          });
        });
      })

    //	$("#footer").css({position:"absolute", top: $('#content').height()});
      }
    });

  });

  this.viewInfo = function() {
    $('a.btnInfo').click(function(e) {
      var id = $(this).attr('rel');
      var target = $('.'+id);


      $('.projectDetail:visible').slideUp('slow');


      if ($(this).text() == 'View Info') {
        $('.btnInfo.selected').removeClass('selected').text('View Info');
            $(this).addClass('selected').text('Hide Info');

        target.slideDown('slow', function() {
          $.scrollTo( target, 800 );
        });
        } else {
          //	$(this).text('View Info');

        $(this).removeClass('selected').text('View Info');
        target.slideUp('slow');
        }


      e.preventDefault();
    });

  }


  this.filterGallery = function() {
    $('a.filter').click(function(e) {
      $('a.current').removeClass('current');
      $(this).addClass('current');


      var filterText = $(this).text();
      var filterVal = $(this).text().toLowerCase();

      $('.itemCategory').text(filterText);

      if(filterVal == 'all') {
        $('.itemContainer:hidden').show(); // .fadeIn('slow');
        $('.itemCount').text($('.itemContainer').length);
      } else {

        $('.itemContainer').each(function() {
          if(!$(this).hasClass(filterVal)) {
            $(this).hide(); //.fadeOut('normal');
          } else {
            $(this).show(); // .fadeIn('slow');
          }
        });

        $('.itemCount').text($('.'+filterVal).length);
      }



      e.preventDefault();
    });
  }

  this.imagePreview = function(){
    /* CONFIG */

    xOffset = -222;
    yOffset = 15;

      // these 2 variable determine popup's distance from the cursor
      // you might want to adjust to get the right result

    /* END CONFIG */
    $("a.preview").hover(function(e){
    //	this.t = this.title;
    //	this.t = this.rel;
    //	this.title = "";
    //	var c = (this.t != "") ? "<br/>" + this.t : "";
      myFlash = $.flash.create({
          swf: 'images/slideshows/'+this.rel+'.swf',
          height: 440,
          width: 400

        }
      );

      $("body").append("<div id='preview'></div>");



      $("#preview")
        .html(myFlash)
        .css("top",(e.pageY + xOffset) + "px")
        .css("left",(e.pageX + yOffset) + "px")
        .fadeIn("fast")
        .animate({opacity: 1.0}, 3000)
        .animate({
               height: "440px"
            }, 'normal' );
        },
      function(){
      //	this.title = this.t;
        $("#preview").remove();
        });

    $("a.preview").click(function(e) {
      e.preventDefault();
    });

    $("a.preview").mousemove(function(e){
      $("#preview")
        .css("top",(e.pageY + xOffset) + "px")
        .css("left",(e.pageX + yOffset) + "px");
    });
  };


