
$(document).ready(function() {

	//Show the paging and activate its first link
$(".paging").show();
$(".paging a:first").addClass("active");


//Get size of the image, how many images there are, then determin the size of the image reel.
var imageWidth = $(".window").width();
var imageHeight = $(".window").height();
var imageSum = $(".image_reel img").size();
var imageReelWidth = imageWidth * imageSum;
var imageReelHeight = imageHeight * imageSum;

//Adjust the image reel to its new size
$(".image_reel").css({'width' : imageReelWidth});

//Paging  and Slider Function
rotate = function(){
    var triggerID = $active.attr("rel") - 1; //Get number of times to slide
    var image_reelPosition = triggerID * imageHeight; //Determines the distance the image reel needs to slide

    $(".paging a").removeClass('active'); //Remove all active class
    $active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)

    //Slider Animation
    $(".image_reel").animate({
        top: -image_reelPosition
    }, 350 );

};

doChange = true;

//Rotation  and Timing Event
rotateSwitch = function(){
	if (doChange == true) {
    play = setInterval(function(){ //Set timer - this will repeat itself every 7 seconds
        $active = $('.paging a.active').next(); //Move to the next paging
        if ( $active.length === 0) { //If paging reaches the end...
            $active = $('.paging a:first'); //go back to first
        }
        rotate(); //Trigger the paging and slider function
    }, 5000); //Timer speed in milliseconds (7 seconds)
    };
};

rotateSwitch(); //Run function on launch


//On Hover
$(".image_reel a").hover(function() {
    clearInterval(play); //Stop the rotation
}, function() {
    rotateSwitch(); //Resume rotation timer
});

//On Click
$(".paging a").click(function() {
    $active = $(this); //Activate the clicked paging
    //Reset Timer
    clearInterval(play); //Stop the rotation
    rotate(); //Trigger rotation immediately
    rotateSwitch(); // Resume rotation timer
    doChange = false;
    return false; //Prevent browser jump to link anchor
});

$("#stripe2").css('z-index' , 1);
$("#sidebar").css('z-index' , 50);
$("#pageNav").css('z-index' , 150);
$("#map-info").css('z-index' , 9999);
$("#main-nav a").css('z-index' , 99999);
$('#locator-main').css('z-index' , 9500);
//$("#header img.titleBar").css('z-index' , 20);

$('#locator-click').click(function() {
  // $('#overlay').css('display', 'block');
  // //$('#locator-main').css('display', 'block');
  // $('#locator-main').slideDown('slow', function() {
  //  	initialize();
  // });

  location.href = 'http://dunnbros.com/find-a-shop';

});

$('#find-another-shop').click(function() {
  $('#overlay').css('display', 'block');
  //$('#locator-main').css('display', 'block');
  $('#locator-main').slideDown('slow', function() {
   	initialize();
  });
});

$('#overlay').click(function() {
  $('#overlay').css('display', 'none');
  $('#locator-main').css('display', 'none');
});

// // mapInit = function() {
// // 	initialize();
// // 	$('#state-select').find('option:first').attr('selected', 'selected').parent('select');
// // 	$('#search_city').children().remove().end().append('<option value="" selected>City</option>') ;
// // 	$("#map-zip").attr("value", $("#map-zip-side").attr("value"));
// // 	codeAddress("#map-zip-side");
// // };
// 
// $("#map-button-side").click(function() {
// 	$('#overlay').css('display', 'block');
// 	// $('#locator-main').css('display', 'block');
// 
// 	$('#locator-main').slideDown('slow', function() {
// 		// mapInit();
// 		initialize();
// 		$('#state-select').find('option:first').attr('selected', 'selected').parent('select');
// 		$('#search_city').children().remove().end().append('<option value="" selected>City</option>') ;
// 		$("#map-zip").attr("value", $("#map-zip-side").attr("value"));
// 		codeAddress("#map-zip-side");
// 	});
// 
// 	return false;
// 
// 	/* $('#locator-main').slideDown('slow', function() {
// 	initialize();
// 	$('#state-select').find('option:first').attr('selected', 'selected').parent('select');
// 	$('#search_city').children().remove().end().append('<option value="" selected>City</option>') ;
// 	codeAddress("#map-zip-side");
// 	});*/
// });

$("#map-zip-side").click(function() {
	if($("#map-zip-side").attr("value") == "Zip") $("#map-zip-side").attr("value", "");
  return false;
});

$("#newsletter-input").click(function() {
	if(this.value == "Email") this.value = "";
  return false;
});

$("#bro-form").click(function() {
	if(this.value == "Email") this.value = "";
  return false;
});




var groups = new Array("centralAmerica","southAmerica","africa","indonesia","specials","blends");
var groups_len=groups.length;

function toggle_groups(grp_name, grp_list){
	for ( var j=0, len=grp_list.length; j<len; ++j ){
		if (grp_list[j] != grp_name && $('ul.sub' + grp_list[j]).is(':visible')) {
			$('ul.sub' + grp_list[j]).slideToggle(50);
		}
	}
	$('ul.sub' + grp_name).slideToggle(50);
}


for ( var i=0, len=groups_len; i<len; ++i ){
  $('li.dh' + groups[i] + ' a').bind("click", { grp: groups[i], grps: groups }, function(event){
		toggle_groups(event.data.grp, event.data.grps);
	});
}





}); /*END doc ready */






























