/* ===========================================================
 *
 *  Cathy George Design - has modified elements of this site - I hope that's ok.
 *  
 *  
 *  Nerd on a bike? Nice!
 *  We'd like to be more nerdy than we are, but as you
 *  probalby can tell by looking at the code in this .js
 *  file you'll notice that we're not that good at this.
 *
 *
 *
 *  Name:          damp.js
 *  Created:       2010-12-23
 *  Updated:       2011-05-08
 *  Updated by:    Me myself and I, and De La Soul
 *  Produced by:   Shotguns & Hat-Tricks
 *
 *  Function:      Make the site work
 *
 *  _we are vem. | Polly 44 | D.A.M.P. Bicycles | Wilcoxson
 * =========================================================== */

	$(document).ready(function()
		{


		// ===========================================================
		//  Clear all radio buttons on load
		// ===========================================================

		$(":input").removeAttr("checked").removeAttr("selected");

		//- /Clear all radio buttons on load


		// ===========================================================
		//  Fix the fixed menu if it's too long
		// ===========================================================


		function staticNav()
			{
				var sidenavHeight = $("#menu").height(); //Get height of sidenav
				var winHeight = $(window).height(); //Get height of viewport
				var browserIE6 = (navigator.userAgent.indexOf("MSIE 6")>=0) ? true : false; //Check for IE6

				if (browserIE6)  //if IE6...
					{
						$("#menu").css({"position" : "absolute"});  //reset the sidenav to be absolute
					}
				else //if not IE6...
					{
						$("#menu").css({"position" : "fixed"}); //reset the sidenav to be fixed
					}

				if (sidenavHeight > winHeight) //If sidenav is taller than viewport...
					{
						$("#menu").css({"position" : "static"}); //switch the fixed positioning to static. Say good bye to sticky nav!
					}
			}

			staticNav(); //Execute function on load

			$(window).resize(function () { //Each time the viewport is adjusted/resized, execute the function
				staticNav();
			});

		//- /Fix the fixed menu if it's too long


		// ===========================================================
		//  Img slider
		// ===========================================================

		$(window).load(function()
			{
				$("#prod_welcome, #prod_barb, #prod_palt, #prod_ear, #prod_mud, #prod_bean, #prod_beast, #prod_del, #prod_tee").nivoSlider(
					{
						effect: "fade", //Specify sets like: "fold,fade,sliceDown"
						animSpeed: 200, //Slide transition speed
						pauseTime: 900000,
						directionNav: true, //Next & Prev
						controlNav: false, //1,2,3...
						keyboardNav: true //Use left & right arrows
					});
				$("#prod_welcome, #prod_barb, #prod_palt, #prod_ear, #prod_mud, #prod_bean, #prod_beast, #prod_del, #prod_tee").data("nivo:vars").stop();
			});

		//-- /Img slider


		// ===========================================================
		//  Tooltip
		// ===========================================================

		// Show a small tooltip on specific "links"
		$(function()
			{
				$("a.top, a.geo, a.buy").tipTip({maxWidth: "auto", edgeOffset: 10, fadeIn: 0, defaultPosition: "top", delay: 0});
				$("label").tipTip({maxWidth: "auto", edgeOffset: 10, fadeIn: 0, defaultPosition: "bottom", delay: 0});
				$(".info_bubble_geo").tipTip({maxWidth: "auto", edgeOffset: 15, fadeIn: 0, defaultPosition: "right", delay: 0});
				$(".info_bubble_links").tipTip({maxWidth: "auto", edgeOffset: 15, fadeIn: 0, defaultPosition: "right", delay: 0});
			});

		//-- /Tooltip


		// ===========================================================
		//  Toggle
		// ===========================================================

		$("a.ie").click(function()
			{
				$("#ie").slideUp("fast");
			});

		// Hide (Collapse) the toggle containers on load
		$(".product_buy").hide();
		$(".product_geo").hide();
		$("#contact_map").hide();
		$("#contact_dealers").hide();

		// Toggle product geo/buy containers
/*
		$("a.main").click(function()
			{
				var itemName = $(this).attr("href");
				//alert(itemName);

				$("#geo_" + itemName).hide();
				$("#buy_" + itemName).hide();

				if (itemType == "geo")
					{
						$("#info_" + itemName).slideToggle("fast");
					}

				return false;
			});
*/

		$("a.buy, a.geo").click(function()
			{
				var itemName = $(this).attr("rel");
				var itemType = itemName.split("_");

				$("#" + itemName).slideToggle("fast");
				$(this).toggleClass("item_active");

				if (itemType[0] == "geo")
					{
						$("#info_" + itemType[1]).slideToggle("fast");
					}
				return false;
			});
			// /Toggle product geo/buy containers


		// Toggle contact form/dealers/map containers
		$("a.dealers, a.map").click(function()
			{
				var itemType = $(this).attr("rev");

				$("#contact_" + itemType).slideToggle("fast");
				$(this).toggleClass("item_active");
				

				if (itemType == "dealers")
					{
						$("#contact_map").slideUp("fast");
						$("#contact_map_link").removeClass("item_active");
						if ($(this).hasClass("item_active"))
							{
								$("#contact_private").slideUp("fast");
							}
						else
							{
								$("#contact_private").slideDown("fast");
							}
					}
				else if (itemType == "map")
					{
						$("#contact_dealers").slideUp("fast");
						$("#contact_dealers_link").removeClass("item_active");
						if ($(this).hasClass("item_active"))
							{
								$("#contact_private").slideUp("fast");
							}
						else
							{
								$("#contact_private").slideDown("fast");
							}
					}
				return false;
			});
			// /Toggle contact form/dealers/map containers

		//-- /Toggle


		// ===========================================================
		//  Up, up and away!
		// ===========================================================

		$("a.top, a.menu_logo").click(function()
			{
				$("html, body").animate({scrollTop: "0px"}, 500);
			});

		//-- /Up, up and away!


		// ===========================================================
		//  Change product color / texture
		// ===========================================================

		$(".color_option span, label.radio_color").mouseover(function()
			{
				var itemProductlID = "#color_" + $(this).attr("rel");
				var itemTextureID = itemProductlID + "_texture";
				var itemColorName = $(this).attr("alt");
				var itemColorCode = $(this).attr("rev");

				if (itemColorCode != "custom") // If the color isn't "custom"
					{
						$(itemProductlID).css({"background" : "#" + itemColorCode, "display" : "block"});
						$(itemProductlID + "_name").css({"display" : "block"});
						$(itemProductlID + "_name").html(itemColorName);

						if (itemColorCode == "ffffff")
							{
								$(itemTextureID).attr("src", "!pix/color_texture_white.jpg");
								$(itemProductlID + "_name").css({"color" : "#000000"});
							}
					}
				else  // If the color is "custom" then show "custom" color image as BG
					{
						$(itemProductlID).css({"background" : "url(!pix/color_custom.png)", "display" : "block"});
						$(itemProductlID + "_name").css({"display" : "block"});
						$(itemProductlID + "_name").html(itemColorName);
					}
			});
		$(".color_option span, label.radio_color").mouseout(function ()
			{
				var itemProductlID = "#color_" + $(this).attr("rel");
				var itemTextureID = itemProductlID + "_texture";

				$(itemProductlID).css({"background" : "", "display" : "none"});
				$(itemProductlID + "_name").css({"display" : "none"});
				$(itemProductlID + "_name").css({"color" : "#ffffff"});
				$(itemProductlID + "_name").html("");
				$(itemTextureID).attr("src", "!pix/color_texture.png");
			});

		//-- /Change product color / texture


		// ===========================================================
		//  Highlight menu on scroll
		// ===========================================================

		$("#menu").visualNav({
				link              : "a",         // add a link class, as necessary (e.g. 'a.menu')
				targetAttr        : "href",      // added in case you have link = "div" and attribute something like
				selectedClass     : "menu_active",  // css class applied to menu when a link is selected (highlighted)
				selectedAppliedTo : "a",        // to only apply to the link, use the same value as is in the link option
				contentClass      : "content_block",   // content class to get height of the section
				contentLinks      : "internal",
				bottomMargin      : 500,         // margin from the end of the page where the last menu item is used (in case the target is short)
				animationTime     : 500         // time in milliseconds
			});

		//-- /Highlight menu on scroll


		// ===========================================================
		//  $$$$$$$$
		// ===========================================================

		// Toggle selected form elements
		$("label[itemprop=buy]").click(function()
			{
				var itemType = $(this).attr("class");
				var itemName = $(this).attr("rel");

				$("label." + itemType + "[rel=" + itemName + "]").removeClass("radio_active");
				$(this).addClass("radio_active");
			});
			// /Toggle selected form elements

		// Calculate price
		$("input[type=radio]").change(function()
			{
				var itemPrice = parseInt($(this).closest("form").find("input[name=product_price]").val());
				var itemTmp = $(this).closest("form").attr("id");
				var itemType = itemTmp.split("_")[1];

				$("#" + itemTmp + " input[type=radio]:checked").each(function()
					{
						itemPrice += parseFloat($(this).attr("rel"));
					});
				//$(this).parent().parent().find(".totalSum").html(itemPrice);
				$(this).closest("form").find(".product_total").html("- Total cost: $" + itemPrice + " -");
				$("#total_" + itemType).val(itemPrice);
			});
			// /Calculate price

		//-- /$$$$$$$$


		// ===========================================================
		//  Resize textarea
		// ===========================================================

		$("#contact_msg, #contact_d_msg").elastic();

		//-- /Resize textarea


		});
		

