jQuery.fn.random = function() {
    var randomIndex = Math.floor(Math.random() * this.length);  
    return jQuery(this[randomIndex]);
};

jQuery(document).ready(function() {

  //toTop Button
  jQuery("#toTop").click(function(event){
    event.preventDefault();
    jQuery('html, body').animate({ scrollTop: 0 }, 'slow');
  });
  
  jQuery('#cta a').hover(function() {
    jQuery(this).stop().animate({backgroundPosition: '0 -2px'},150);
  }, function() {
    jQuery(this).stop().animate({backgroundPosition: '0 -5px'},150);
  });

  jQuery(".tab_content").hide(); //Hide all content
  
  /*var randomele = jQuery("ul.tabs li").random();*/
	var randomele = jQuery("ul.tabs li").last();
  randomele.addClass("active").show(); //Activate first tab
  
  var activeTab = jQuery(randomele).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
  jQuery(activeTab).show(); //Fade in the active ID content
  
  //On Click Event
  jQuery("ul.tabs li").click(function() {
    if(!jQuery(this).hasClass("active")) {
      jQuery("ul.tabs li").removeClass("active"); //Remove any "active" class
      jQuery(this).addClass("active"); //Add "active" class to selected tab
      jQuery(".tab_content").hide(); //Hide all tab content
  
      var activeTab = jQuery(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
      jQuery(activeTab).fadeIn(); //Fade in the active ID content
    }
    return false;
  });
   
  //Set default open/close settings
  jQuery('.acc_container').hide(); //Hide/close all containers
  jQuery('.acc_trigger:first').addClass('active').next().show(); //Add "active" class to first trigger, then show/open the immediate next container
  
  //On Click
  jQuery('.acc_trigger').click(function(){
    if( jQuery(this).next().is(':hidden') ) { //If immediate next container is closed...
      jQuery('.acc_trigger').removeClass('active').next().slideUp(); //Remove all "active" state and slide up the immediate next container
      jQuery(this).toggleClass('active').next().slideDown(); //Add "active" state to clicked trigger and slide down the immediate next container
    }
    return false; //Prevent the browser jump to the link anchor
  });
  
  var hash = window.location.hash;
  if (hash) jQuery(hash).trigger('click');
  
  jQuery('.accClick').click(function(event){
   var linkhash = jQuery(this).attr("href").split('#');
   event.preventDefault();
   jQuery('#'+linkhash[1]).trigger('click');
  });
});

  function fbg (vars)
  {
	  jQuery.fancybox(vars, {
			'padding'			: 10,
			'type'              : 'image'
		});
  }
	
jQuery(window).load(function() {
	if (document.getElementById("slider"))
	{
		jQuery('#slider').nivoSlider({pauseTime:5000});
	}
});


jQuery(window).load(function() {
			var carousel, positionc, position, finpos, carheight, active, carouselinstance, nivo, moveit = true;
    	nivo = jQuery('#portslider').nivoSlider({
				pauseTime:5000,
        pauseOnHover:false,
        controlNavThumbs:true, // Use thumbnails for Control Nav
        controlNavThumbsFromRel:true,
				afterLoad: function(){
					carousel = jQuery('#mycarousel').jcarousel({
						vertical: true,
						scroll: 3,
						initCallback: function (carouselini) {
							carouselinstance = carouselini;
						}
					});
				},
				afterChange: function(){
					if( moveit ) {
						active = jQuery('#mycarousel').find('.active');
						position = active.position();
						positionc = jQuery('#mycarousel').position();
						finpos = positionc.top + position.top;
						carheight = jQuery('.jcarousel-clip').height();
						if(finpos < 0 || finpos > carheight) {
							console.log(active.attr('rel'));
							carouselinstance.scroll(jQuery.jcarousel.intval(parseFloat(active.attr('rel'))+1));
						}
					}
				}
			});
			
			jQuery('.nivo-control').bind('click', function(){
				nivo.data('nivo:vars').stop = true;
			});
			
			jQuery('.jcarousel-prev, .jcarousel-next').bind('click', function(){
				moveit = false;
			});
    });
