$(document).ready(function(){
						   
	 //Style Radio Buttons on Homepage
	if($('body').attr('id')=='homepage'){
		$('input').customInput();
	}
	
	// Product Slider
	if ($(".products-scroll-wrap").length > 0) $(".products-scroll-wrap").easySlider();
	
	// Corners
	$("#menu ul li.current").corner("top 8px");
	
	//  Accordion
	heightArray = new Array();
	$("dl.v_show_hide dd").each(function(i) {
	 theHeight = $(this).height();
	 heightArray[i] = theHeight;
	});

	$("dl.v_show_hide dd").hide();
	$("dl.v_show_hide dt").click(function () {
	 $(this).next("dd").css({height: heightArray[$("dl.v_show_hide dt").index(this)]});
	 $(this).next("dd").slideToggle("slow")
		.siblings("dd").slideUp("slow");
	});
		
	// Load News
	$('#loadnews').load('/classes/rssreader.php', function(){
	  $('#news').newsTicker();
	});
	
	// Tooltip
	(function(){
		var
		$tool = $('<div id="tooltip"><div id="toolarrow"></div><div id="toolcontent"></div></div>').appendTo('body'),
		$content = $('#toolcontent'),
		offset,
		timeout,
		delay = 500;
		$('.btn-help').hover(function(){
			var left = 'auto', right = 'auto', leftOffset;
			
			$tool.hide();
			clearTimeout(timeout);
			offset = $(this).offset();
			$content.html($(this).find('.tipinfo').html());
			
			leftOffset = offset.left + 30 + $(this).width()/2;
			
			if(leftOffset + $tool.outerWidth() > $(window).width()){
				$tool.addClass('inverted');
				right = $(document).width() - offset.left + 10;
			} else {
				$tool.removeClass('inverted');
				left = leftOffset;
			}
			
			$tool.css({top:offset.top, left:left, right:right}).show();
		}, function(){
			timeout = setTimeout(function(){ $tool.hide(); }, delay);
		});
		
		$tool.hover(function(){
			clearTimeout(timeout);
		}, function(){
			timeout = setTimeout(function(){ $tool.hide(); }, delay);
		});
	}());
	
	
	//instant search
	
	var runningRequest = false;
    var request;

   //Identify the typing action
    $('input#q').keyup(function(e){
		//if( this.value.length < 3 ) return;
        e.preventDefault();
        var $q = $(this);
		

        if($q.val() == ''){
            $('div#results').html('').hide();
            return false;
        }

        //Abort opened requests to speed it up
        if(runningRequest){
          //request.abort();
		  return false;
        }

        runningRequest=true;

		$.post('/js/search.php', { q: $q.val() }, function(data) {
			showResults(data,$q.val());
			runningRequest=false;
		}, 'json');

		//Create HTML structure for the results and insert it on the result div
		function showResults(data, highlight){
           var resultHtml = '';
            $.each(data, function(i,item){
                if ( item.title ) resultHtml += '<div class="result">' + item.title + '</div>';
            });
			if ( resultHtml )
            	$('div#results').html(resultHtml).css("display","block");
			else
				$('div#results').css("display","none");
        }

        //$('form').submit(function(e){
            //e.preventDefault();
        //});

        $('div.result').live('click', function() {
			$('input#q').val($(this).text());
			$('div#results').css("display","none");
			//$('form').submit();
			$('form[name=search]').submit();
        });
	
    });
	
	if ( $('#_country').val() == 'IE' )
	{
		$('#_postcode').hide().prev('label').hide().next().hide();
	}
		
    $('#_country').change(function() {
		if ( $('#_country').val() == 'IE' )
		{
			$('#_postcode').val('0000');
			$('#_postcode').hide().prev('label').hide().next().hide();
		}
		else
		{
			if ( $('#_postcode').val() == '0000' ) $('#_postcode').val('');
			$('#_postcode').show().prev('label').show().next().show();
		}
    });
	
});
	
	 
//Initiate Menus
ddsmoothmenu.init({
 mainmenuid: "menu",
 orientation: 'h',
 classname: 'dropdown',
 contentsource: "markup"
})
	
// Google Analytics
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-3881401-27']);
  _gaq.push(['_setDomainName', '.nutrigold.co.uk']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
