/***
 * Common
 */

$(function() {
	
	// Initiate Kakart Live Search
	$('#search').kakartLiveSearch({
		overallContainer: 'form.fm-search',
		source: '/ajax/search',
		resultsContainer: '.live-search-results',
		itemTemplate: function(index, item) {
			return '\
			<li class="clearfix">\
				<a href="/products/view/' + item.product_fname + '">\
					<div class="search-image-holder" style="background: url(/media/images/small/' + escape(item.image_url) + ') no-repeat top left; float: left;">\
						<img class="search-image" src="/skin/thejewellerystop/image/standard/frame-75x75.png" title="' + item.product_name + '" />\
					</div>\
					<div style="float: left; width: 190px; height: 75px; border; solid 1px green; position: relative;">\
						<span class="title" style="display: block; font-size: 1.5em;">' + item.product_name + '</span>\
						<span class="product_set">' + item.category_name + '</span>\
						<span class="price" style="color: #c41c70; position: absolute; bottom: 0; right: 0;">' + (item.variant_price_offer ? 'From ' : '') + '&pound;' + (item.variant_price_offer ? item.variant_price_offer : item.variant_price) + '</span>\
					</div>\
				</a>\
			</li>';
		}
	});
	
	// Initiate Kakart Lightbox
	$('.magnify a').fancybox({
		overlayOpacity			: 0.8,
		overlayColor			: '#000000',
		overlayShow				: true,
		zoomOpacity				: true,
		zoomSpeedIn				: 600,
		zoomSpeedOut			: 500,
		easingIn				: 'easeOutBack',
		easingOut				: 'easeInBack',
		enableEscapeButton		: true,
		showTitle				: true,
		itemArray	 			: $('a[rel=product_group]').slice(1)
	});
	
	// AJAX Help Lightbox
	$('.ajax-help').fancybox({ 
		overlayOpacity			: 0.8,
		overlayColor			: '#000000',
		overlayShow				: true,
		zoomOpacity				: true,
		zoomSpeedIn				: 600,
		zoomSpeedOut			: 500
	});
	
	// Parent Closer.
	$('a.close-parent').click(function() {
        $(this).parent().hide('slide', { easing: 'easeOutQuint', direction: 'down' }, 500, function() {
            $(this).remove();
        });
    });
	
	// Tabs
	var tab_contents = $('.tab-content');
	var tabs = $('ul.tabbed li a');
	if (tabs.length) {
		if (window.location.hash) {
			// find a tab with this id
			var tab_id = window.location.hash.substr(1);
			$('.tab-content:not(#' + tab_id + ')').hide();
			tabs.each(function() {
				var url = $(this).attr('href');
				var id = url.substr(url.indexOf('#') + 1);
				if (id != tab_id)
					$(this).parent().removeClass('active');
				else
					$(this).parent().addClass('active');
			});
		}
		else {
			$('.tab-content:not(.default)').hide();
		}
		
		tabs.click(function() {
			var url = $(this).attr('href');
			var id = url.substr(url.indexOf('#') + 1);
			tab_contents.hide();
			$('#' + id).show();

			tabs.each(function() { $(this).parent().removeClass('active'); });
			$(this).parent().addClass('active');
		})
	}
});

if(!window.console) {
	window.console = {
		log: function(text) {
			//alert(text);
		}
	}
}