// Get t'font replacement out.
Cufon.replace('.cufon');
Cufon.replace('#container h4');

// get GET params in JS
function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

$(function(){

	//$("ul.subnav").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)

	$("ul.topnav li a").mouseover(function() { //When trigger is clicked...
		//Following events are applied to the subnav itself (moving subnav up and down)
		  $(this).parent().find("ul.subnav").show(); //Drop down the subnav on click
    
		$(this).parent().hover(function() {
		}, function(){
			$(this).parent().find("ul.subnav").hide(); //When the mouse hovers out of the subnav, move it back up
		});

		//Following events are applied to the trigger (Hover events for the trigger)
		}).hover(function() {
			$(this).addClass("subhover"); //On hover over, add class "subhover"
		}, function(){	//On Hover Out
			$(this).removeClass("subhover"); //On hover out, remove class "subhover"
	});

  // Accordion
  if($("#accordion").length > 0) {
    $("#accordion").accordion({ collapsible: true, header: "h3", autoHeight: false, active: false, navigation: true });
    id = gup('id');
    var panel = (id != "") ? parseInt(id) : -1;
    $("#accordion").accordion("activate", panel);
  }
  
  // Clear text on input focus
  $("input").focus(function()
	{
		var name = $(this).attr("rel");
		if($(this).attr("value") == name)
		{
			$(this).attr("value","");
		}
	});

	$("input").blur(function()
	{
		var name = $(this).attr("rel");
		if($(this).attr("value").length < 1)
		{
			$(this).attr("value", name)
		}
	});
});

if($('#slideshow').length > 0) {
  /*** 
      Simple jQuery Slideshow Script
      Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, not responsible for anything, etc.  Please link out to me if you like it :)
  ***/

  function slideSwitch() {
      var $active = $('#slideshow IMG.active');

      if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

      // use this to pull the images in the order they appear in the markup
      var $next =  $active.next().length ? $active.next()
          : $('#slideshow IMG:first');

      // uncomment the 3 lines below to pull the images in random order

       var $sibs  = $active.siblings();
       var rndNum = Math.floor(Math.random() * $sibs.length );
       var $next  = $( $sibs[ rndNum ] );


      $active.addClass('last-active');

      $next.css({opacity: 0.0})
          .addClass('active')
          .animate({opacity: 1.0}, 1000, function() {
              $active.removeClass('active last-active');
          });
  }

  $(function() {
      setInterval( "slideSwitch()", 5000 );
  });
}

// if($('.infiniteCarousel').length > 0) {
  (function () {
      $.fn.infiniteCarousel = function () {
          function repeat(str, n) {
              return new Array( n + 1 ).join(str);
          }
        
          return this.each(function () {
              // magic!
              var $wrapper = $('> div', this).css('overflow', 'hidden'),
                  $slider = $wrapper.find('> ul').width(9999),
                  $items = $slider.find('> li'),
                  $single = $items.filter(':first')
                
                  singleWidth = $single.outerWidth(),
                  visible = Math.ceil($wrapper.innerWidth() / singleWidth),
                  currentPage = 1,
                  pages = Math.ceil($items.length / visible);
                
              /* TASKS */
            
              // 1. pad the pages with empty element if required
              if ($items.length % visible != 0) {
                  // pad
                  $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
                  $items = $slider.find('> li');
              }
            
              // 2. create the carousel padding on left and right (cloned)
              $items.filter(':first').before($items.slice(-visible).clone().addClass('cloned'));
              $items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
              $items = $slider.find('> li');
            
              // 3. reset scroll
              $wrapper.scrollLeft(singleWidth * visible);
            
              // 4. paging function
              function gotoPage(page) {
                  var dir = page < currentPage ? -1 : 1,
                      n = Math.abs(currentPage - page),
                      left = singleWidth * dir * visible * n;
                
                  $wrapper.filter(':not(:animated)').animate({
                      scrollLeft : '+=' + left
                  }, 500, function () {
                      // if page == last page - then reset position
                      if (page > pages) {
                          $wrapper.scrollLeft(singleWidth * visible);
                          page = 1;
                      } else if (page == 0) {
                          page = pages;
                          $wrapper.scrollLeft(singleWidth * visible * pages);
                      }
                    
                      currentPage = page;
                  });
              }
            
              // 5. insert the back and forward link
              $wrapper.after('<a href="#" class="arrow back">&lt;</a><a href="#" class="arrow forward">&gt;</a>');
            
              // 6. bind the back and forward links
              $('a.back', this).click(function () {
                  gotoPage(currentPage - 1);
                  return false;
              });
            
              $('a.forward', this).click(function () {
                  gotoPage(currentPage + 1);
                  return false;
              });
            
              $(this).bind('goto', function (event, page) {
                  gotoPage(page);
              });
            
              // THIS IS NEW CODE FOR THE AUTOMATIC INFINITE CAROUSEL
              $(this).bind('next', function () {
                  gotoPage(currentPage + 1);
              });
          });
      };
  })(jQuery);

  $(function () {
      // THIS IS NEW CODE FOR THE AUTOMATIC INFINITE CAROUSEL
      var autoscrolling = true;
    
      $('.infiniteCarousel').infiniteCarousel().mouseover(function () {
          autoscrolling = false;
      }).mouseout(function () {
          autoscrolling = true;
      });
    
      setInterval(function () {
          if (autoscrolling) {
              $('.infiniteCarousel').trigger('next');
          }
      }, 4000);
      
      // Create the tooltips only on document load
      $('.infiniteCarousel ul li a').qtip({ 
        style: { 
          name: 'dark', 
          tip: true,
          border: {
             width: 2,
             radius: 8
          },
          width: 350
        },
        position: {
          corner: {
            target: 'topMiddle',
            tooltip: 'bottomMiddle'
          }
        }  
      });
  });
