function join_mailing_list(name,email){
	$("#mailing_list_submit span").html('Sending...');
	$.ajax({
		type: "POST",
		url: "/home/join",
		data: "name="+name+"&email="+email,
		success: function(msg){
			if(msg=='1'){
				$("#join_form").html(' ').prepend('<p>Thank you! Your email address has been added to the our mailing list.</p>');
			}else{
			};
		}
	});
};

var hash = false;
function checkHash(){ 
    if(window.location.hash != hash) { 
        hash = window.location.hash; 
    	return hash;
    }else{
    	return false;
    }
}

function verifyEmail(emailAddress){
	var status = false;     
	var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
     if (emailAddress.search(emailRegEx) == -1) {

     }else{
          status = true;
     }
     return status;
}

$(document).ready(function() {
	
	// Newsletter Slide Box
	$('div.join a.tab, #newsletter a.close').click(
		function(){
			$('#newsletter').slideToggle('slow');
		}
	);

	// Newsletter form
	$('#join_form').submit(function(){ $('#mailing_list_submit').click(); });
	$('#mailing_list_submit').click(function() {
		if(!$("#ml_name").val() || ! $("#ml_email").val() || !verifyEmail($("#ml_email").val())){
			alert("Please provide your name and email address");
		}else{
			join_mailing_list($("#ml_name").val(),$("#ml_email").val());
		}
		return false;
	});

	// Homepage Heroes
	if($('#heroes').length){
		$('#heroes').innerfade({
			speed: 'slow',
			timeout: 5000,
			type: 'sequence',
			containerheight: 	'370px',
	        slide_timer_on: 	'yes',
	        slide_ui_parent: 	'heroes',
	        slide_ui_text:		null,
	       	pause_button_id: 	null,
	       	slide_nav_id:		'hero_nav'
		});
		$.setOptionsButtonEvent();
	}
		
	// Home Announcement Slider
	if($('#announcements').length){
		$('#announcements #view').carousel('#right_btn', '#left_btn');
		function nextAnnouncement(){
			$('#right_btn').click();
		}
		setTimeout(nextAnnouncement,11000);
	}
		
	// FAQ
	if($('ul.faq').length){
		$('ul.faq div.toggle').hide();
		$('ul.faq li').click(
			function(){
				$(this).find('div.toggle').slideToggle('1000');
				$(this).find('a').toggleClass('minus');
			
				return false;
			}
		);
	}
		
	//Tooltip
	$("div.item_container a.item, div.item_container a.grey_btn").tooltip({
		bodyHandler: function() {
			return $('.' + $(this).attr("rel")).html();
		},
		showURL: false,
		bottom: 10,
		left: 10,
		track: true,
		delay: 0
	});
	
	// Product Button (more info)
	$("div.item_container").mouseover(function() {
		$(this).children("a.grey_btn").show();
	}).mouseout(function(){
		$(this).children("a.grey_btn").hide();
	});
	
	// Product detail div slider
	$('div.product_item_container a').click(function(){
			
		var h = checkHash();
			
		var toExpand = $('#' + $(this).attr('name'));
		var targetOffset = $(this).parents('div.product_item_container').children('a').eq(0).offset().top;
		var clickedToClose = ($(toExpand).hasClass('expanded')) ? true : false;
		
		if($('.expanded').length){
		
			var currentRow = $('.expanded').attr('dir');
			var nextRow = $(toExpand).attr('dir');
			
			if(nextRow>currentRow){
				targetOffset -= 545;
			}else if(nextRow!=currentRow){
				targetOffset -= 10;
			}else{
				targetOffset = false;
			}
			
			$('.expanded').removeClass('expanded').slideUp('slow',function(){  
				
				if(targetOffset){
					
				$('html,body').animate({scrollTop: targetOffset}, 400, function(){
					if(!clickedToClose){
						$(toExpand).slideDown('slow',function(){ }).addClass('expanded');
					}
				}); 
			
				}else{
					if(!clickedToClose){
						$(toExpand).slideDown('slow',function(){ }).addClass('expanded');
					}
				}
			});
			
		}else{
			
			 $('html,body').animate({scrollTop: targetOffset-10}, 400, function(){ 
			 	$(toExpand).slideDown('slow',function(){ }).addClass('expanded');
			 });
		
		}

	});
	
	// check for product hashes (externally linked
	if($('div.product_item_container a').length){
		if(h=checkHash()){
			$('a[name='+h.slice(1) +']').click();
		}
	}
	
	$('div.item_show div.closer a.close').click(
		function(){
			$(this).removeClass('expanded');
			$(this).parents('div.item_show').slideUp('slow');
			return false;
		}
	);
	
	// product multiple imagizer
	$('div.img_box a').click(function(){
		
		var imageId = $(this).attr('rel');
		var maxIndex = $('img.img'+imageId).length;
		var current = $('img.current.img'+imageId);
		var currentIndex = $(current).attr('rel');
		
		if($(this).hasClass('left_btn')){
			
			// Backward
			var toShow = (currentIndex == 1) ? $('img.img'+imageId).eq(maxIndex-1) : $('img.img'+imageId).eq(currentIndex-2);
			
			$(current).fadeOut('fast',function(){ 
				$(toShow).fadeIn('fast',function(){ 
					$(toShow).addClass('current'); 
					$(current).removeClass('current'); 
				}) 
			});

		}else{
			
			// Forward
			var toShow = (currentIndex < maxIndex) ? $('img.img'+imageId).eq(currentIndex) : $('img.img'+imageId).eq(0);
			
			$(current).fadeOut('fast',function(){ 
				$(toShow).fadeIn('fast',function(){ 
					$(toShow).addClass('current'); 
					$(current).removeClass('current'); 
				}) 
			});

		}
		
		return false;
		
	});

	// Item Image Slider
	//$('div.item_show div.img_box').carousel('a.right_btn', 'a.left_btn');

	/* Subnav Rollover */
	$('#nav li').hover(function() {
		$(this).find('.subnav').fadeIn(500);
	}, function() {
		$(this).find('.subnav').delay(2000).fadeOut();
	});
	
	$('#nav li a').click(function(e) {
		var $here = $(e.target);
		if($here.text() === 'Featured Jewelry') {
			e.preventDefault();
		}
	});
	
	$('a.hours').hover(function() {
		$(this).parent().find('.subnav').fadeIn(500);
	}, function() {
		$(this).parent().find('.subnav').delay(1000).fadeOut();
	});
	
	$('a.hours').click(function(e) {
		e.preventDefault();
	});
	
	// Anchor Form Submit
	$('.form_submit').live("click",function(){
		$(this).parents("form").submit();
		return true;
	});		
	
	// Sliding Side-Menu
	// Wuori
	if($('#sidenav a').length){
		if (window.XMLHttpRequest && parseFloat($(window).height()) >= 630){	
			
			// avoid IE6
			var top = $('#sidenav').offset().top - parseFloat($('#sidenav').css('marginTop').replace(/auto/, 0));
			var menu_height = $('#sidenav').height();
			var max_height = $('#footer').offset().top - menu_height;
			
			top -= 20;
			
			$(window).scroll(function(event)
			{
				var y = $(this).scrollTop();
				if (y >= top)
				{
					$('#sidenav').addClass('sn_fixed');
					$('#sidenav a.back').slideDown('fast').css('display','block');
				}
				else
				{
					$('#sidenav').removeClass('sn_fixed');
					$('#sidenav a.back').slideUp('fast');
				}
			})
		}

	};
		
	$('#sidenav ul a').click(function(){
		
		var link = $(this);
		var targetOffset = 0;
		var linkOffset = $('#sidenav').offset().top - $(this).offset().top;
		targetOffset = $($(this).attr('href')).offset().top + linkOffset - 5;
		
		if(!$('#sidenav').hasClass('activated')){
			//targetOffset -= 20;
			$('#sidenav').addClass('activated');
		} 
		
		$('html,body').animate({scrollTop: targetOffset}, 500, function(){
				$('#sidenav ul li').removeClass('current');
				$(link).parent().addClass('current');
		});
		
		return false;
	
	});
		
		
/*
		$('#sidenav ul li a').smoothScroll({
			offset: -30,
			speed: 500,
			afterScroll: function(){
				$('#sidenav ul li').removeClass('current');
				$(this).parent().addClass('current');
			}
		});
*/
		
	
	//back to top
	$('a.back').click(
		function(){	
			$('#sidenav ul li.current').removeClass('current');
			$('#sidenav ul li').eq(0).addClass('current');
		
			$('html, body').animate({scrollTop:0}, '500', function(){
			});
			return false;
		}
	);

});

