var tabNames = new Array;
var tabContainers = new Array;

$(function() {
	
//Set up tabs
	$('.tab').children().each(function() {
		tabNames.push($(this).attr('href'));
	});

	$('.tab-content').each(function() {
		tabContainers.push($(this));
	});
	
	if ($('#tabs')) {removeHash(); showTabContent();}
	
	
//hover for mainnav
	var dropDownLinks = $('#mainnav li').children('ul').children('li').children();
	dropDownLinks.hover(
		function(){
			$(this).css('color', '#999');
		},
		function(){
			$(this).css('color', '#fff');
		}
	);	
	dropDowns();
	
// Click for info-expand
	$('#home #photo-info').show();
	$('#info-expand').click(function() {
		var getWidth = $('#home #photo-info').css('width');
		if(getWidth != '740px'){
			$('#home #photo-info').animate( { width:"740px", margin:"0"}, { queue:false, duration:1000 } );
			$('#info-expand').addClass('open');
		}else{
			$('#home #photo-info').animate( { width:"100px", margin:"0"}, { queue:false, duration:1000 } );
			$('#info-expand').removeClass('open');
		}
	});
	

// Toggle read more
$('#toggle').click(function () {
	$("#more").slideToggle('slow');
	$('#toggle').toggleClass('closed');
	return false;
});
	
	
if($('.read-more')){  //adding padding to Read More button
	$('.read-more').after('<div style=\"margin:20px 0px 10px 15px; overflow: hidden; height: 0px;\"></div>');
}
		
	
//Distributiors

//hide all regions
$('div.location').css('visibility','visible');
	
if($('#getIt')){  //distributors location select
			showRegion();
		$("#getIt").change(function(){
			showRegion();
		});
	}
	
	

//distributors page information
	var regions = $('.regions');
	$('.distributor').css('height', '250px');
	$('.mootabs_panel').css('display', 'block');
	for (var i=0; i<regions.length; i++) {
		var current = regions[i].getAttribute("id");
		var name = "myTabs" + i;
	//	name = new mootabs(current);
}


$('.mootabs_title').each(function() { //Highlight and show first tab on page load
    var firstTab = $(this).children().eq(0)
	firstTab.addClass('active');
	regionInfo = firstTab.attr('title');
	$('#'+ regionInfo).show().siblings('div').hide();
});


$('.mootabs_title').children().click(function() { //on distributor tab click
	$(this).addClass('active').siblings().removeClass('active');
	regionInfo = $(this).attr('title');
	$('#' + regionInfo).show().siblings('div').hide();
});	

if ($('table').hasClass('striped')) {
	stripeRows();
}

//Adding drop shadows to background
$('#drop-shadow-l').show().css('bottom','211px');
$('#drop-shadow-r').show().css('bottom', '211px');


	
}); //End on document load

function stripeRows (){ //create alternating colors for table rows
$('table.striped').children().children().each(function(index) {
    if($(this).index() % 2 == 1){
        $(this).addClass('alt');
    }
  });
}

function showRegion(){
	// hides all regions
	$('.location').css('display','none');
	//finds value of selected item
	var region = '#' + $('#getIt').val();
	//shows region that matches selected item
	$(region).css('display','block');
}

function dropDowns(){
$('#mainnav').children().hover(
	function(){
		$(this).addClass('over');
	},
	function(){
		$(this).removeClass('over');
	}
);
}

//Tabs - 1:1 tab to container ratio.

if (!Array.prototype.indexOf)  // to make .indexOf() work in IE
{
  Array.prototype.indexOf = function(searchElement /*, fromIndex */)
  {
    "use strict";

    if (this === void 0 || this === null)
      throw new TypeError();

    var t = Object(this);
    var len = t.length >>> 0;
    if (len === 0)
      return -1;

    var n = 0;
    if (arguments.length > 0)
    {
      n = Number(arguments[1]);
      if (n !== n) // shortcut for verifying if it's NaN
        n = 0;
      else if (n !== 0 && n !== (1 / 0) && n !== -(1 / 0))
        n = (n > 0 || -1) * Math.floor(Math.abs(n));
    }

    if (n >= len)
      return -1;

    var k = n >= 0
          ? n
          : Math.max(len - Math.abs(n), 0);

    for (; k < len; k++)
    {
      if (k in t && t[k] === searchElement)
        return k;
    }
    return -1;
  };
}

function showTabContent(){
	for (var i=0; i < tabContainers.length; i++) {
		$(tabContainers[i]).hide();  //hide all
		$('.tab').eq(i).removeClass('selected'); //clear tabs
	}  
	var x = window.location.hash;	
	var y = tabNames.indexOf(x);
	
	if (!window.location.hash || window.location.hash == "#"){
		y = 0;
	}
	$(tabContainers[y]).show();
	$('.tab').eq(y).addClass('selected');
	return false;	
}

<!-- On url change check hash tag (back-button fix)-->
var hash = location.hash;

window.setInterval(function(){

    if (location.hash != hash){
		currentHash = window.location.hash;
		showTabContent();
		hash = location.hash;
    }
}, 100);

function removeHash(){
	for (var i=0; i < tabNames.length; i++) { 
		var x = tabNames[i].substring(1);
		var removeTag = document.getElementsByName(x)[0];
		var d = removeTag.parentNode;
		d.removeChild(removeTag);
	}
}



