/* Wray Bros JS */

var hovr_a = [];
var hovr_b = [];
var box;
var dbox;

var searchtext = "Product Live Search...";
var searchpoptext = "Enter a product code, name or description...";

function check_hover() {
	if (!hovr_a[box] && !hovr_b[box]) {
		$('.nav-pop').hide();
		//$('#suggestions').hide();
		$('#menu ul li').removeClass('hilite');
		$('#menu ul li a.sub').removeClass('hov-link');
		$('#dkbox').hide();
		// This next bit is necessary due to cufon retaining colours even after
		// the class has been removed.
		Cufon.replace('#menu ul li a', {
			hover: '#fff'
		});
	}
}

function trigger (carousel, state) {
	var frame = carousel.first;
	$('.jcarousel-control a img').attr('src','img/bullet.png');
	$('.jcarousel-control a[rel='+frame+'] img').attr('src','img/bullet-sel.png');

}

function mycarousel_initCallback(carousel) {
    jQuery('.jcarousel-control a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).attr('rel')));
				$('.jcarousel-control a img').attr('src','img/bullet.png');
				jQuery('img',this).attr('src','img/bullet-sel.png');

				
        return false;
    });
};

function mycarousel_initCallback_news(carousel) {
    jQuery('#mycarousel-next').bind('click', function() {
        carousel.next();
        return false;
    });

    jQuery('#mycarousel-prev').bind('click', function() {
        carousel.prev();
        return false;
    });
};

var qs_results = function(txt, start)
	{
		$.post("/ajax/", {'method':'search', 'q': txt, 'start':start}, function(data)
		{ // Do an AJAX call
			$('#suggestions').html('');
			//$('#suggestions').fadeIn(); // Show the suggestions box
			$('#suggestions').html(data); // Fill the suggestions box
			//alert('data='+data)
		});
	}

$(function() {

$('a.qs_next, a.qs_prev').live('click', function()
	{
		qs_results($('#search').val(), parseInt($(this).attr('rel')));
		return false;
	});

	// Search box
	if ($('#search').val() == "") {
		$('#search').val(searchtext);
	}

	$('#search').focus(function() {
		if ($('#search').val() == searchtext) {
			$('#search').val('');
		}
	});

	$('#search').blur(function() {
		if ($('#search').val() == "") {
			$('#search').val(searchtext);
		}
	});
	// -- Search box



	$('.cloud-zoom, .cloud-zoom-gallery').CloudZoom()

	

	$('#search').live('keyup', function() {
		var kw = $(this).val();
		if (kw.length >= 3)
		{
			$.ajax(
			{
				url:'/ajax/index.php',
				type:'post',
				data:{'method':'search','q':kw},
				dataType:'html',
				success:function(d)
				{
					var sbleft = $('#search').offset().left;
					var sbtop = $('#search').offset().top+25;

					$('#suggestions').html('');
					$('#suggestions').show();
					$('#suggestions').offset({left:sbleft,top:sbtop});
					$('#suggestions').append(d);
					//$('#suggestions').css('zIndex','999999999999');
					$('#suggestions').css('z-index', '99999999');
					$('#suggestions').addClass('ontop');

				}
			});
		} else {
			$('#suggestions').hide();
		}
	});

	$('.search-pop').live('keyup', function() {
		var kw = $(this).val();
		var that = $(this);
		if (kw.length >= 3)
		{
			$.ajax(
			{
				url:'/ajax/index.php',
				type:'post',
				data:{'method':'search','q':kw},
				dataType:'html',
				success:function(d)
				{
					var sbleft = $(that).offset().left;
					var sbtop = $(that).offset().top-390;

					$('#suggestions').html('');
					$('#suggestions').show();
					$('#suggestions').offset({left:sbleft,top:sbtop});
					$('#suggestions').append(d);
					//$('#suggestions').css('zIndex','999999999999');
					$('#suggestions').css('z-index', '99999999');
					$('#suggestions').addClass('ontop');

				}
			});
		} else {
			$('#suggestions').hide();
		}
	});
	// -- Search box

	// Search box
	if ($('.search-pop').val() == "") {
		$('.search-pop').val(searchpoptext);
	}

	$('.search-pop').focus(function() {
		if ($('.search-pop').val() == searchpoptext) {
			$('.search-pop').val('');
		}
	});

	$('.search-pop').blur(function() {
		if ($('.search-pop').val() == "") {
			$('.search-pop').val(searchpoptext);
		}
	});
	// -- Search box

	$("#home-scroll").jcarousel({
        scroll: 1,
				auto: 20,
        initCallback: mycarousel_initCallback,
				itemLoadCallback: trigger,
        buttonNextHTML: null,
        buttonPrevHTML: null
    });

		$("#news-box").jcarousel({
        scroll: 1,
        initCallback: mycarousel_initCallback_news,
        buttonNextHTML: null,
        buttonPrevHTML: null
    });

		

	$.localScroll();

	/*$('.filter-head a').click(function() {
			var td = $(this).parent().parent().find('ul');
			$(td).slideToggle();
			return false;
		});*/

		$('#perpage').change(function() {
			var v = $(this).val();
			$('#pp').val(v);
			$("#filter_form").submit();
		})

		$('#sortby').change(function() {
			var v = $(this).val();
			$('#sb').val(v);
			$("#filter_form").submit();
		})

		$('.filter-head-main a').click(function() {
			var hdiv = $(this).attr('rel');
			$('#hidden_filter_'+hdiv).slideToggle();

			var im = $('img', this).attr('alt');
			if (im == "minus") {
				$('img', this).attr('src', '/img/expand.png');
				$('img', this).attr('alt', 'expand');
			} else {
				var xx = $('img', this).attr('src');
				$('img', this).attr('src', '/img/minus.png');
				$('img', this).attr('alt', 'minus');
			}
			

			return false;
		});

		$('.filter-head-main-top a').click(function() {
			var hdiv = $(this).attr('rel');
			$('#outer_filter_'+hdiv).slideToggle();

			var im = $('img', this).attr('alt');
			if (im == "expand") {
				$('img', this).attr('src', '/img/expand.png');
				$('img', this).attr('alt', 'expand');
			} else {
				var xx = $('img', this).attr('src');
				$('img', this).attr('src', '/img/minus.png');
				$('img', this).attr('alt', 'minus');
			}
			return false;
		});

		$('#next_link').click(function() {
			var st = $(this).attr('rel');
			$('#st').val(st);
			$("#filter_form").submit();
			return false;
		});

		$('#prev_link').click(function() {
			var st = $(this).attr('rel');
			$('#st').val(st);
			$("#filter_form").submit();
			return false;
		});

		$('.filter input[type=checkbox]').click(function() {
			//var td = $(this).parent().parent().parent().find('ul');
			//$(this).parent().parent().parent().find(':checkbox').attr('checked', this.checked);
			$("#filter_form").submit();
		});

		$('.xbox a').click(function() {
			var td = $(this).attr('rel');
			$('#rad_'+td).removeAttr('checked');
			$("#filter_form").submit();
		});
		
		/*$('.filter-head-main a').click(function() {
			var im = $('img', this).attr('alt');
			if (im == "minus") {
				$('img', this).attr('src', '/img/expand.png');
				$('img', this).attr('alt', 'expand');
			} else {
				$('img', this).attr('src', '/img/minus.png');
				$('img', this).attr('alt', 'minus');
			}
		});		*/

	//

	$('#menu ul li a').hover(
		function () {
			$('.nav-pop').hide();
			$('#menu ul li a.sub').removeClass('hov-link');
			// This next bit is necessary due to cufon retaining colours even after
		// the class has been removed.
		Cufon.replace('#menu ul li a', {
			hover: '#fff'
		});
			box = $('a.sub',$(this).parent()).attr('rel');
			var mpos = $('.inner-header').offset().left;
			var mpost = $('#menu').offset().top + 49;

			$('.'+box).addClass('hilite');
			$('#'+box).show();
			$('#'+box).offset({left:0}); // fixes jumpiness with FF
			$('#'+box).offset({left:mpos,top:mpost});

			$('a.sub',this).addClass('hov-link');
			$('#dkbox').show();

			hovr_a[box] = true;
		},
		function () {
			hovr_a[box] = false;
			setTimeout("check_hover()",300);
		}
	);

	$('.nav-pop').hover(
		function() {
			dbox = $(this).attr('id');
			hovr_b[dbox] = true;
		},
		function() {
			hovr_b[dbox] = false;
			setTimeout("check_hover()",300);
		}
	);

	//

});

$(function() {

	$('.more-cat a').click(function() {
		$('.hidden-cat').slideToggle('slow');
		if (more_cat) {
			more_cat = false;
			$('.more-cat a').text('More');
		} else {
			more_cat = true;
			$('.more-cat a').text('Less');
		}
		return false;
	});
	
	
		$("#prod-thumbs").jcarousel({
        scroll: 1,
        initCallback: mycarousel_initCallback_thumb,
        buttonNextHTML: null,
        buttonPrevHTML: null
    });	
    
 function mycarousel_initCallback_thumb(carousel) {
		
    jQuery('#thumbcarousel-next').bind('click', function() {
        carousel.next();
        return false;
    });

    jQuery('#thumbcarousel-prev').bind('click', function() {
        carousel.prev();
        return false;
    });
};

$('.prod-tabs a.prod-tab').click(function() {
			var tab = $(this).attr('rel');

			$('.ptab').hide();
			$('.prod-tabs a').removeClass('p-act-tab');

			$('#ptab-'+tab).show();
			$(this).addClass('p-act-tab');

			return false;
		});

	var t;
	var dhov;
	/*$('.sub-mimg').hover(function() {
		$('#dkbox').show();
		dhov = true;
		//t = setTimeout(function() { $('#dkbox').hide(); dhov = false; } ,1000);
	});
	$('.sub-mimg').mousemove(function() {
		clearTimeout(t);
		dhov = true;
		$('#dkbox').show();
		//t = setTimeout(function() { $('#dkbox').hide(); dhov = false; } ,1000);
	});
	$('*').not('.sub-mimg').hover(function() {
		if (dhov) {
			//t = setTimeout(function() { $('#dkbox').hide(); dhov = false; } ,1000);
			$('#dkbox').hide(); dhov = false;
		}
	});*/

});



/* Fonts */

Cufon.replace('#header .header-rtop .top-menu li a', {
	hover: '#cc9900'
});
Cufon.replace('#menu ul li a', {
	hover: '#fff'
});

Cufon.replace('#header .header-rtop .top-menu li.basket');
Cufon.replace('#menu .tel');
Cufon.replace('.whcuf');
Cufon.replace('.whcuf2');
Cufon.replace('.orcuf');

