$(document).ready(function() {
	// z-index fix
	// var zIndexNumber = 1000; 
	// $('div').each(function() { $(this).css('zIndex', zIndexNumber); zIndexNumber -= 10; });

	last_gallery_item();

	// home page 
	$('#carousel ul').cycle({
		fx		: 'fade',
		speed	: 1200,
		timeout : 5000
	});
	

	// gallery image changer
	$('#gallery_images').cycle({
		fx		: 'fade',
		timeout: 0, 
		pager:  '#pager',
		pagerAnchorBuilder: function(idx, slide) {
			return '<div class="pager_thumb"><a href="#"><img src="' + $(slide).attr('src') +'" height="90" width="90" /></a></div>';
		}
	});
	
	
	// Contact form Submit
	$("label").inFieldLabels();
	$(".contact_form form").validate();
	
	$('.contact_form form').submit(function(event) {
		if($(this).valid()) {
			$.get('/contact_form_send.php', {name: $('#name').val(), email: $('#email').val(), message: $('#message').val() }, function(data, textStatus, xhr) {
				$('.form_complete').slideDown(500);
			});
		} else {
			// do nothing
		}
		return false;
	});
	
	
	// gallery show hide items
	// check if narrow items
	// if(window.location.hash != "") {
	// 	$('.gallery_cat_button').each(function(index) {
	// 		$(this).removeClass('selected');
	// 		if(window.location.hash == $(this).attr('href')) {
	// 			$(this).addClass('selected');
	// 			var cat_name = window.location.hash.substr(1);
	// 
	// 			// show hide products based on hash value 
	// 			// change urls also
	// 			if(cat_name == 'all') {
	// 				$('.product_block').show(0, function() {
	// 					changeUrls(cat_name);
	// 				});
	// 			} else {
	// 				$('.product_block').hide(0);
	// 				$('.product_block.' + cat_name).show(0, function() {
	// 					changeUrls(cat_name);
	// 				});
	// 			}
	// 			
	// 
	// 		}
	// 	});
	// 	last_gallery_item();
	// }

	$('.gallery_cat_button').click(function(event) {
		$('.gallery_cat_button').removeClass('selected');
		$(this).addClass('selected');
	});
	
	$("a[rel=craftmanship_gallery]").fancybox({
		'titlePosition' 	: 'inside',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
				var heading = title.split('|')[0];
				var text = title.split('|')[1];
				return '<div class="fancy_title"><a href="#close" class="fancy_close" title="close">CLOSE</a><div class="inner"><h2>' + heading + '</h2><br>' + text + '</div></div>';
		},
		'onComplete'		: function () {
			$('.fancy_close').click(function(event) {
				$.fancybox.close();
				return false;
			});
		}
	});
	
	
	// show hide gallery products
	// $('.gallery_cat_button').click(function(event) {
	// 	var cat_name = $(this).attr('href').substr(1);
	// 	if (cat_name == 'all') {
	// 		$('.product_block').fadeIn(100, function() {
	// 			last_gallery_item();
	// 		});
	// 	} else {
	// 		$('.product_block').fadeOut(100);
	// 		$('.product_block.' + cat_name).fadeIn(100, function() {
	// 			last_gallery_item();
	// 		});
	// 	}
	// 	changeUrls(cat_name);
	// 
	// });
	// 
	// 
	// function changeUrls(cat_url) {
	// 
	// 	if(cat_url == "") {
	// 		cat_url = 'all';
	// 	}
	// 	
	// 	$('.product_block').each(function(index) {
	// 		var page_url = $(this).children('a').attr('href').split('/')[2];
	// 		$(this).children('a').attr('href', '/cms-' + cat_url + '/' + page_url);
	// 		$(this).children('p').children('a').attr('href', '/cms-' + cat_url + '/' + page_url);
	// 	});
	// }
	// 
	// 
	function last_gallery_item() {
		var num = 1;
		$('.product_block').removeClass('end')
		$('.product_block').each(function(index) {
			if((num % 5) == 0 && $(this).is(':visible')) {
				$(this).addClass('end');
			}
			if($(this).is(':visible')) {
				num++;
				console.log($(this).attr('class'));
			}
	
		});
	}
	
});
