var marker = [];
var map;
var addthis_config = {
	services_compact: 'facebook, favorites, twitter, print, email, more',
	ui_offset_top: '-15',
	username: 'bradfordmercantile'
}

$(function () {
// ------------------------------------------------------------------------------------------------------------ INITIATE GMAP?		
  $("body").removeClass('noscript');

// ------------------------------------------------------------------------------------------------------------ AUTO SUBMIT JQUERY SELECT BOX		  
  $(".list-filter .newList li").click(function() {
  	window.location=($(this).parents('.jqselect').children().val());
  });

// ------------------------------------------------------------------------------------------------------------ FAQ TOGGLE
	$('dl dt').click(function () {
  	$(this).next().toggle();
	});
	
// ------------------------------------------------------------------------------------------------------------ NAV GOODIES
	var nav_act
	$('#nav-content .sub-nav').each(function(i) {
		if ($(this).hasClass ("active")) {
			nav_act=i;
		}
	});
  $('#nav-content').accordion({
  	header: 'h3',
  	active: nav_act,
  	alwaysOpen: true,
  	autoHeight: false
  });

  $("#nav-content h3 a").hover(
    function () {
      $(this).stop().animate({backgroundPosition:"(0 -3px)"}, 300);
    }, 
    function () {
      $(this).stop().animate({backgroundPosition:"(0 -300px)"}, 300);
    }
  );

// ------------------------------------------------------------------------------------------------------------ SET FORM INPUT VALUES  
  if ($("#search-txt").val()=="") {
    $("#search-txt").val("Search the site");
  }
  $("#search-txt").bind("click",function() {
    if ($(this).val()=="Search the site") {
      $(this).val("");
    }
  }).bind("blur",function(){
    if ($(this).val()=="") {$(this).val("Search the site");}
  });
// ------------------------------------------------------------------------------------------------------------ PRODUCT DETAIL TABS
	$("#product-tabs a, #prod-lng-desc").click(function () {
    $(".tab-content").hide();
    $("#product-tabs a").removeClass('active');
    $('#'+$(this).attr('rel')).show();
    var active=$(this).attr('rel');
    $("#product-tabs a[rel="+active+"]").addClass('active');
    return false;
  });

// ------------------------------------------------------------------------------------------------------------ TWITTER ROTATION
  $('#twitter ul').cycle({
  	pause:1, 
  	cleartype: true, 
  	cleartypeNoBg: true
  });
  
// ------------------------------------------------------------------------------------------------------------ ADD TO CART VALIDATION
	if ($(".prod-buttons .foxycart").length > 0 ) {
		var errors = 0;
		$("select.validate").each(function() {
        if ($(this).val() == "") {
    			errors ++;
      	}
    });
    if (errors > 0) {
			$("#product-notice").html("Please select your option(s)");
			$('#submit-product').attr('disabled', 'disabled');
		}
	}
	$("select.validate").change(function () {
  	var errors = 0;
  	$("select.validate").each(function() {
        if ($(this).val() == "") {
    			errors ++;
      	}
    });
    if (errors > 0) {
			$("#product-notice").html("Please select your option(s)");
			$('#submit-product').attr('disabled', 'disabled');
		} else {
			$("#product-notice").html("");
			$('#submit-product').attr('disabled', '');
		}
  });
  $("#submit-product").click(function() {
  	$("select.validate").each(function() {
        var option = $(this).val();
        if (option == "") {
    			$("#product-notice").html("Please select your option(s)");
        	return false;
      	}
    });
  });
// ------------------------------------------------------------------------------------------------------------ BRAND WINDOW
    $('#bw').cycle({
	  	fx:'fade', 
	  	speed:'fast',
	  	pause:1,
	  	timeout:6000,
	  	next:'#bw-next', 
	  	prev:'#bw-prev'
	  });
		$('#bw-pause').click(function() {
			if ($(this).hasClass("resume")) {
				$('#bw').cycle('resume');
			} else {
				$('#bw').cycle('pause');
			}
		  $(this).toggleClass("resume");
		  return false;
		});
    
    $('#bw li.bottom-hover .overlay').stop().animate({bottom:"-80px"},200);
		$('#bw li.right-hover .overlay').stop().animate({right:"-225px"},200);
		$('#bw li.left-hover .overlay').stop().animate({left:"-235px"},200);

		$("#bw li.bottom-hover").hover(function() {
			$(this).children(".overlay").stop().animate({bottom:"0px"},500);
		}, function() {
			$(this).children(".overlay").stop().animate({bottom:"-80px"},500);
		});

		$("#bw li.right-hover").hover(function() {
			$(this).children(".overlay").stop().animate({right:"0px"},500);
		}, function() {
			$(this).children(".overlay").stop().animate({right:"-225px"},500);
		});

		$("#bw li.left-hover").hover(function() {
			$(this).children(".overlay").stop().animate({left:"0px"},500);
		}, function() {
			$(this).children(".overlay").stop().animate({left:"-235px"},500);
		});

	if ( $('#map').length > 0 ) {MapData();}
	if ( $('#store-map').length > 0 ) {StoreMap();}
	if ( $('#maillist').length > 0 ) {
		$("#maillist").validate({
			errorLabelContainer: $("#maillist #error"),
			rules: {
				first_name: "required",
				email: {
					required: true,
					email: true
				}
			},
			messages: {
				first_name: "Please enter your firs tname",
				email: "Please enter a valid email address"
			}
		});
	}
			
});

// ------------------------------------------------------------------------------------------------------------ DECLARE LIGHTBOX
		$('#prod-img-thumbs a').lightBox();
		$('a[rel*=lightbox]').lightBox();
// ------------------------------------------------------------------------------------------------------------ DECLARE SELECT STYLES
		$('.jqselect select').sSelect();

// ----------------------------------------------------------------------------------------------------------- FoxyCart Validation
/* function fc_PreProcess() { // First let's define the function
	var errors = 0;
	$("select.validate").each(function() {
		if ($(this).val() == "") {
			errors ++;
		}
	});
	if (errors > 0) {
			$("#product-notice").html("Please select your option(s)");
			return false;
		} else {
			return true;
		}

}
*/
// ----------------------------------------------------------------------------------------------------------- CREATE STORE MAP
function StoreMap(center, zoom) {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("store-map"));
    map.setCenter(new GLatLng(41.751457,-71.271301), 13);
    map.setUIToDefault();
    map.removeMapType(G_SATELLITE_MAP);
    map.removeMapType(G_NORMAL_MAP);
    map.removeMapType(G_PHYSICAL_MAP);
    map.setMapType(G_HYBRID_MAP);
    map.disableScrollWheelZoom();

	  var home_marker = new GIcon();
	  home_marker.image = "/images/site/gmap-home.png";
	  home_marker.iconSize = new GSize(22, 30);
	  home_marker.iconAnchor = new GPoint(11, 15);
  
		var store = new GLatLng(41.751457,-71.271301);
		map.addOverlay(new GMarker(store, home_marker));
  } else {
    alert("Sorry, the Google Maps API is not compatible with this browser");
  }    
}
// ----------------------------------------------------------------------------------------------------------- CREATE MAP
function LoadMap(center, zoom) {
	var lat = center.split(',')[0];
	var lon = center.split(',')[1];
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
    map.setCenter(new GLatLng(lat, lon), zoom);
    map.setUIToDefault();
    map.removeMapType(G_SATELLITE_MAP);
    map.removeMapType(G_NORMAL_MAP);
    map.removeMapType(G_PHYSICAL_MAP);
    map.setMapType(G_HYBRID_MAP);
    map.disableScrollWheelZoom();
  } else {
    alert("Sorry, the Google Maps API is not compatible with this browser");
  }
}
// ----------------------------------------------------------------------------------------------------------- LOOP THROUGH DYNAMIC MARKERS
function addMarkers() {
  if (marker_array.length) {
    for (n=0; n < marker_array.length; n++ ) {
      var point = new GLatLng(marker_array[n].lat, marker_array[n].lng);
      marker[n] = createMarker(point, marker_array[n].name, marker_array[n].url, marker_array[n].type, marker_array[n].street, marker_array[n].city, marker_array[n].state);
      map.addOverlay(marker[n]);
    }
  }
}
// ----------------------------------------------------------------------------------------------------------- CREATE DYNAMIC POINTS
function createMarker(point, title, url, icon, street, city, state) {
  var disp_marker = new GIcon();
  disp_marker.image = "/images/site/gmap-"+icon+".png";
  disp_marker.iconSize = new GSize(22, 30);
  disp_marker.iconAnchor = new GPoint(11, 15);
  var marker = new GMarker(point, disp_marker);
  var displayString = "<div class='map-info'>\
  	<h3>"+title+"</h3>\
  	<p>"+street+"<br/>"+city+", "+state+"</p>\
  	<p><a href='/mia/"+url+"/'>View Profile</a> | <a href='/shop/mia/"+url+"'/>Shop "+title+"</a></p><br/>\
  	</div>";
	GEvent.addListener(marker, 'click', function() {map.openInfoWindowHtml(point, displayString);});
	return marker;
}