$(document).ready(function() {

	$('.not_delivered').click(function(e) {
		alert('This feature has not been delivered yet.');
		e.stopImmediatePropagation();
		return false;
	});

	// hover for the menu
	$('li.nav_tab').hover(
		function()  {
			$(this).addClass('active'); // for IE z-index support & hover support
			$(this).children('span').addClass('active').siblings('div').show();
			$(this).children('a').addClass('active').siblings('div').show();
		},
		function()  {
			$(this).removeClass('active'); // for IE z-index support & hover support
			$(this).children('span').removeClass('active').siblings('div').hide();
			$(this).children('a').removeClass('active').siblings('div').hide();
		}
	);

	// as soon as page loads, make the flash message gradually dissapear
	$(".top_flash").fadeTo(7000, 0, function() { $(this).hide('slow'); }).removeClass("top_flash");

	// initialize the datepicker used in the forms
 	$(".datepicker").datepicker();
	$(".datepicker2").datepicker({dateFormat: 'mm/yy'});  

	// in products/get, when you click on an additional image on right side of page, the big picture will change to the additional image
	$('div.views li a').click(function() {
		$('div.views li.active').removeClass('active');
		$(this).parent().addClass("active");
		$('#featured_image').attr('src', $(this).attr('name'));
	});

	function showFinish(finish_code, isFirstTime) {
		// Only show the additional images of the active color
		$('div.views li').hide();
		$('div.views li[finish~="' + finish_code + '"]').show();
	
		// active will cause the additional image to have a background behind it 
		//remove all the existing additonal images that have the class "active", since we starting a new color now
		$('div.views li.active').removeClass('active');
		
		if (isFirstTime) {
			// make the featured image be the active additional image when the page first loads
			var active = $('div.views li.featured_image a:visible');
		} else {
			// after the user clicks new finish colors, then get the first additional image of the current active color
			var active = $('div.views li a:visible:first');
		}
		// change the big image to the first additional image
		$('#featured_image').attr('src', active.attr('name'));
	
		// make the li of that additional image "active"
		active.parent().addClass("active");
	}

	$('#finishes li').click(function() {
		$('#finishes li.active').removeClass('active');
		finish_code = $(this).attr('finish');
		$(this).addClass("active");
	    showFinish(finish_code);
	});

	var letter_code_regex = /\d{4}(GR|QW|BC|CB|DA|DC|DP|EC|ECMO|LA|LC|LP|MO|MOEC|SL|SLEC|CA|BG|BGC|[A-Z])/;

	// Get the initial finish code
	if($('#featured_image').attr('src')) {
	
		var finish_code;
		var regexresult = $('#featured_image').attr('src').match(letter_code_regex);
	
//		alert(regexresult);	
		if (regexresult) {
			finish_code = regexresult[1];

			// for the current featured image, highlight the correct finish color icon on the right side
			$('#finishes li[finish~="' + finish_code + '"]').addClass('active');
			showFinish(finish_code, true);
		} else {
			
		}
		
	

	}

	$('.js_tab a').click(function() {
		activate_tab(this);
	});

	$('.product_info a').click(function() {
		activate_tab($('li#product_info_tab a')[0]);
	});

	function activate_tab(el) {
		$('.js_tab.active').removeClass('active');
		$(el).parent('li').addClass('active');
		$('.left_col li.active').removeClass('active').addClass('inactive');
		$('li#' + $(el).parent('li').attr('id').replace(/_tab$/, "")).addClass('active').removeClass('inactive');
	}

	$("#read_more").click(function() {
		$('.js_tab.active').removeClass('active');
		$('li#reviews_tab').addClass('active');
		$('.left_col li.active').removeClass('active').addClass('inactive');
		$('li#reviews').addClass('active').removeClass('inactive');
	});

	$("a.rate_star").click(function() {
		n = parseInt($(this).text());
		$("#comment_rating").val(n);
		$(".rate_star.active").removeClass("active");
		for (var i=1; i <= n; i++) $("a#rate_star_" + i).addClass("active");
	});
	
	$(".rate_star").hover(function() {
		n = parseInt($(this).text());
		for (var i=1; i <= n; i++) $("a#rate_star_" + i).addClass("hover");
	}, function() {
		$(".rate_star.hover").removeClass("hover");
	});

	$("select#state").change(function() {
		$.get($(this).parent().parent().attr('action'), $(this).serialize(), null, "script");
	});

	// search prefill
	if (($('#search_container input').val() == '' ) || ($('#search_container input').val() == 'Search Site')) {
		$('#search_container input').addClass('instructions').val('Search Site');
	}
	
	$('#search_container input').focus(function () {
		if ($(this).val() == 'Search Site') {
			$(this).removeClass('instructions').val('');
		}
	});
	
	$('#search_container input').blur(function () {
		if ($(this).val() == '') {
			$(this).addClass('instructions').val('Search Site');
		}
	});
  
	$('.finish_swatch').click(function () {
		show_finish = $(this).attr('id').split("_")[1];
		$('.views').children().each(function () {
			finish = $(this).children(":first").attr('name').match(letter_code_regex)[1];
			if (show_finish == finish) {
				$(this).show();
			} else {
				$(this).hide();
			}
		})
	})
});


// function validates that at least one site is checked
// and that the current site is checked
// note that it expects a 'global' variable to have been set
// by the page itself (because this file isn't erb)
function validate_site_choice() {
	if ($(".site_choice:checked").length == 0) {
		alert ('You must have one site selected.');
		return false;
	} else if (! $("#" + current_site_checkbox_id).attr('checked') ) {   
		alert ('You must have the current site selected.');
		return false;
	} else {
		return true
	}
}









//*****************************
// MIMICS RUBY ON RAILS RESTFUL PATTERN FOR DELETE
//********************************
/*
if (
	confirm('Are you sure you want to delete this address?')
) {
 var f = document.createElement('form'); 
 f.style.display = 'none'; 
 this.parentNode.appendChild(f); 
 f.method = 'POST';
  f.action = this.href;
  var m = document.createElement('input'); 
  m.setAttribute('type', 'hidden'); 
  m.setAttribute('name', '_method');
   m.setAttribute('value', 'DELETE');
    f.appendChild(m);
    var s = document.createElement('input'); 
    s.setAttribute('type', 'hidden'); 
    s.setAttribute('name', 'authenticity_token'); 
    s.setAttribute('value', 'gXSqsYwsi3AVr5qTvIJioSduNKk17+kDdgKe+alP7+I='); 
    f.appendChild(s);
    f.submit(); 
 };
*/

var RESTFULMDB = {};
RESTFULMDB.restfulDELETE = function(el, msg, token) {
	if ( confirm(msg)) {
		var s = $(el).attr('href');
		var f = $('<form method="POST" style="display:none"></form>').attr('action', s);
		$(el).parent().append(f);
		f.append(
			$('<input type="hidden" name="_method" value="DELETE">')
		)
		//.append(
		//	$('<input type="hidden" name="authenticity_token" value="' + token + '">')	
		//)
		f.submit()
	}
};




