/*~~~~~~~~~~~~~~~~~Start Document Ready~~~~~~~~~~~~~~~~*/
$(document).ready(function($) {

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*                 Footer Ticker                    */
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

$("div#scrollingText").smoothDivScroll({
autoScroll: "always", 
autoScrollDirection: "endlessloopright", 
autoScrollStep: 1, 
autoScrollInterval: 25
});

$("div#scrollingText").bind("mouseover", function(){
	$("div#scrollingText").smoothDivScroll("stopAutoScroll");
});

$("div#scrollingText").bind("mouseout", function(){
	$("div#scrollingText").smoothDivScroll("startAutoScroll");
});

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*                 HIGHLIGHT CURRENT                */
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
$(function(){
   var path = location.pathname.substring(1);
   if ( path )
     $('.sub-nav a[href$="' + path + '"]').attr('class', 'active');
 });
 
//$(function(){
//   var path = location.pathname.substring(1);
//   if ( path )
//     $('.sub-nav a[href$="' + path + '"]').attr('class', 'active');
// });
 

//OR THIS WAY
//$("ul.sub-nav").find("a[href='"+window.location.pathname+"']").each(function(){
//$(this).addClass("current")
//})
 
    $(".sub-nav li").find("a[href='"+window.location.href+"']").each(function() {
        $(this).addClass("here");
    });

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*                 WRAP # ITEMS IN DIV              */
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

$('div.items > div.listing').each(function(i) {
    if( i % 3 == 0 ) {$(this).nextAll().andSelf().slice(0,3).wrapAll('<div class="slide-wrap"></div>');}
});


$('div#gallery > a').each(function(i) {
    if( i % 4 == 0 ) {$(this).nextAll().andSelf().slice(0,4).wrapAll('<div class="set-wrap"></div>');}
});


$('div.properties > div.listing').each(function(i) {
    if( i % 3 == 0 ) {$(this).nextAll().andSelf().slice(0,3).wrapAll('<div class="set-wrap"></div>');}
});

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*                  NTH ITEM WRAPS                  */
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

//controls the styles of list items
//$("#main-content ul.list-item li:nth-child(3n)").addClass("third")

//controls the clearing of every three product list items
//$('#main-content > ul.list-item > li').each(function(i) {
//    if( i % 3 == 0 ) {
//        $(this).nextAll().andSelf().slice(0,3).wrapAll('<div></div>');
//    }
//});

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*                 Add Class "Last"                 */
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

//$("div#gallery a:nth-child(4n)").addClass("last")

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*                  FADE BUTTONS                    */
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

//$(".followus a, .footer a img, .button").fadeTo("slow", 1); 
// This sets the opacity when the page loads
//$(".followus a, .footer a img, .button").hover(function(){
//	$(this).fadeTo("slow", 0.5); // This should set the opacity on hover
//	},function(){
//	$(this).fadeTo("slow", 1); // This should set the opacity back on mouseout
//});

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*                   Page Scroll                    */
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

function filterPath(string) {
return string
.replace(/^\//,'')
.replace(/(index|default).[a-zA-Z]{3,4}$/,'')
.replace(/\/$/,'');
}
var locationPath = filterPath(location.pathname);
var scrollElem = scrollableElement('html', 'body');

$('a[href*=#]').each(function() {
var thisPath = filterPath(this.pathname) || locationPath;
if (  locationPath == thisPath
&& (location.hostname == this.hostname || !this.hostname)
&& this.hash.replace(/#/,'') ) {
  var $target = $(this.hash), target = this.hash;
  if (target) {
    var targetOffset = $target.offset().top;
    $(this).click(function(event) {
      event.preventDefault();
      $(scrollElem).animate({scrollTop: targetOffset}, 400, function() {
        location.hash = target;
      });
    });
  }
}
});

// use the first element that is "scrollable"
function scrollableElement(els) {
for (var i = 0, argLength = arguments.length; i <argLength; i++) {
  var el = arguments[i],
      $scrollElement = $(el);
  if ($scrollElement.scrollTop()> 0) {
    return el;
  } else {
    $scrollElement.scrollTop(1);
    var isScrollable = $scrollElement.scrollTop()> 0;
    $scrollElement.scrollTop(0);
    if (isScrollable) {
      return el;
    }
  }
}
return [];
}

/*~~~~~~~~~~~~~~~~~End Document Ready~~~~~~~~~~~~~~~~*/
});

