// product image zoom
var ImageZoom = {
	zoom: function(elm) {
		ImageZoom.showLoading();
		var preloader = new Image();
		preloader.onload = function() {
			$('#product_view .product_image img').attr('src', $(elm).attr('href'));
			preloader.onload=function(){};
			ImageZoom.hideLoading();
		};
		preloader.src = $(elm).attr('href');
		return false;		
	},
	showLoading: function() {
		$('#product_view .product_image img').hide();
		$('#product_view .product_image').append('<div id="image-preview-loading"><img src="/images/ico-loading.gif" /></div>');
	},
	hideLoading: function() {
		$('#image-preview-loading').remove();
		$('#product_view .product_image img').show();
	}
};

// main js
$(document).ready(function() {
	var search = $('#global_search_field');
	var search_default = 'Search Jim\'s Firearms';
	if (search.val() == '') {
		search.val(search_default);
	}
	search.focus(function(){
		if (search.val() == search_default) {
			search.val('');
		}
	});
	search.blur(function(){
		if (search.val() == '') {
			search.val(search_default);
		}
	});
	
	if ($.fn.innerfade) {
		$('ul.rotation.about').innerfade({
			speed: 3000,
			timeout: 6000,
			type: 'sequence',
			containerheight: '189px'
		});
		$('ul.rotation.shop').innerfade({
			speed: 3000,
			timeout: 6000,
			type: 'sequence',
			containerheight: '223px'
		});
		$('ul.rotation.home').innerfade({
			speed: 3000,
			timeout: 6000,
			type: 'sequence',
			containerheight: '298px'
		});
	}
	
	if($('#order_by').length > 0) {
		$('#order_by').change(function() {
			$(this).parent().parent().submit();
		});
	}
	
	if ($.fn.positionFooter) {
		$("#main_content").positionFooter();
	}	
	
	// ------------------------------------------------
	// admin
	// ------------------------------------------------
	
	if ($.fn.equalHeights) {
		// equal heights is only in the admin, so put the admin specific js here
		
		$.facebox.settings.opacity = 0.7;
		$('a[rel*=facebox]').facebox();
		
		if ($('#content').length > 0 && $('#main_wrapper').length > 0) {
			$('#content').equalHeights();
		}
		
		// per-page dropdown for products
		if($('#admin_option_per_page').length > 0) {
			$('#admin_option_per_page').change(function() {
				$('#admin-listing-options').submit();
			});
		}
		
		// - creates auto striping tables
		function stripeTable() {
			$('table.zebra').each(function() {
				$(this.getElementsByTagName("tbody")[0].getElementsByTagName("tr")).each(function(i) {
					$(this).mouseover(function(){$(this).addClass('ruled')});
					$(this).mouseout(function(){$(this).removeClass('ruled')});
					if(i%2==1) { 
						$(this).addClass('even');
					} else {
						$(this).removeClass('even');
					}
				});
			});
		}
		stripeTable();
		
		$("#categories_sortable tbody").sortable({
			revert: true,
			axis: 'y',
			update: function() {
				serial = $('#categories_sortable tbody').sortable('serialize');
				stripeTable();
				$.ajax({
					url: '/admin/categories/update_order',
					type: "post",
					data: serial,
					error: function(){
						alert("There was an error saving your changes.");
					}
				});
			}
		});
	}
	
});