/*
 * Theme specific scripts
 *
 * Additional third party scripts live in scripts/ sub-directory
 */
$(document).ready(function() {
  // Auto striped tables
  $('.striped tr').mouseover(function() {$(this).addClass("over");}).mouseout(function() {$(this).removeClass("over");});
  $('.striped tr:even').addClass("alt");

  // Open link in new window
  $('a[rel="external"]').click( function() {
      window.open( $(this).attr('href') );
      return false;
  });

  $('#block-menu-primary-links .menu li').hover(function() {
    $(this).addClass('hover');
  }, function() {
    $(this).removeClass('hover');
  });

   // Slide toggle
   //
   // Hides the slide-box as soon as the DOM is ready.
   // (a little sooner than page load)
    $('.slide-box').hide();

   // Toggle the slidebox on clicking the noted link.
    $('a.slide-toggle').click(function() {
      $('.slide-box').slideToggle(400);
      return false;
    });

    // Add class="current" to current page links in the Top Nav.
    var currentpath = location.pathname;
    $('#foo a[@href="' + currentpath + '"]').parent().addClass('current');
    $('a[@href*=".pdf"]').attr("target", "_blank");
    $('a[@href*=".PDF"]').attr("target", "_blank");

    $("#contact_form").bind('submit', function() {
        if ($('input[name="items[Name]"]').val() == '') {
			alert("You didn't enter a value for your Name.");
			return false;
        }

        var email = $('input[name="email"]').val();
        if (email == '' || !is_email_valid(email)) {
			alert("You didn't enter a valid value for your email.");
			return false;
        }

        return true;
    });
    $("#volunteer_form").bind('submit', function() {
        if ($('input[name="items[Name]"]').val() == '') {
			alert("You didn't enter a value for your Name.");
			return false;
        }
        if ($('input[name="items[Address]"]').val() == '') {
			alert("You didn't enter a value for your Address.");
			return false;
        }
        if ($('input[name="items[State]"]').val() == '') {
			alert("You didn't enter a value for your State.");
			return false;
        }
        if ($('input[name="items[Zip]"]').val() == '') {
			alert("You didn't enter a value for your Zip Code.");
			return false;
        }
        if ($('input[name="items[Primary_Phone]"]').val() == '') {
			alert("You didn't enter a value for your Primary Phone.");
			return false;
        }

        var email = $('input[name="email"]').val();
        if (email == '' || !is_email_valid(email)) {
			alert("You didn't enter a valid value for your email.");
			return false;
        }

        return true;
    });

    // Take all divs that match class .admin-links and move them inside of
    // the next adjacent sibling div with class .block.
    var $blocks = $(".block");
    $blocks.each(function() {
      var $block = $(this);
      var $admin = $(this).prev();

      if (!$admin.hasClass("admin-links"))
        return $block;

      var $copy = $admin.clone(true);
      $admin.remove();
      $copy.prependTo($block);

      return this;
    });

    if ($.browser.safari) {
        if ($("#marquee").length == 0) {
            $("body.sidebar-right #main").css("margin-left", "-40px");
        } else {
            $("#marquee").css("margin-left", "-41px");
        }
    }
    if ($.browser.msie && $.browser.version.substr(0, 1) == '7') {
        $("h1.page-title").css("height", "40px");
    }
});

function flashPutHref(href) { location.href = href; }

function add_flash(src, container, width, height, flashvars, params, attributes) {
	var flashvars = jQuery.extend(flashvars, {});
	var params = jQuery.extend(params, {
	    "base": ".",
		"bgcolor": "#121212",
		"allowfullscreen": "false",
		"wmode": "transparent"
	});
	var attributes = jQuery.extend(attributes, {});

	if ($.browser.safari) {
		attributes["style"] = "margin-top: -34px";
	}

	swfobject.embedSWF("/sites/default/themes/buckhead-heritage/flash/"+src+".swf", container, width, height , "9.0.0", false, flashvars, params, attributes);
}

function is_email_valid(inemail){
	invalidChars = "/:,;";

	for (i=0; i < invalidChars.length; i++) {
		badChar = invalidChars.charAt(i);
		if (inemail.indexOf(badChar,0) != -1) {
			return false;
		}
	}

	atPos = inemail.indexOf("@", 1);
	if (atPos == -1) {
		return false;
	}

	if (inemail.indexOf("@", atPos+1) != -1) {
		return false;
	}

	periodPos = inemail.indexOf(".", atPos);
	if (periodPos == -1) {
		return false;
	}

	if (periodPos + 3 > inemail.length) {
		return false;
	}

	return true;
}
