$.fn.betterTooltip = function(options){

		/* Setup the options for the tooltip that can be 
		   accessed from outside the plugin              */
		var defaults = {
			speed: 200,
			delay: 300
		};

		var options = $.extend(defaults, options);

		/* Create a function that builds the tooltip 
		   markup. Then, prepend the tooltip to the body */
		getTip = function() {
			var tTip = 
			"<div class='tip'>" +
				"<div class='tipMid'>"	+
				"</div>" +
				"<div class='tipBtm'></div>" +
			"</div>";
			return tTip;
		}
		$("body").prepend(getTip());

		/* Give each item with the class associated with 
		   the plugin the ability to call the tooltip    */
		$(this).each(function(){

			var $this = $(this);
			var tip = $('.tip');
			var tipInner = $('.tip .tipMid');
			
			var tTitle = (this.title);
			this.title = "";
			
			var offset = $(this).offset();
			var tLeft = offset.left;
			var tTop = offset.top;
			var tWidth = $this.width();
			var tHeight = $this.height();
	
			/* Mouse over and out functions*/
			$this.hover(function() {
				tipInner.html(tTitle);
				setTip(tTop, tLeft);
				setTimer();
			}, 
			function() {
				stopTimer();
				tip.hide();
			}
		);		   
	
		/* Delay the fade-in animation of the tooltip */
		setTimer = function() {
			$this.showTipTimer = setInterval("showTip()", defaults.delay);
		}
	
		stopTimer = function() {
			clearInterval($this.showTipTimer);
		}
	
		/* Position the tooltip relative to the class 
		   associated with the tooltip                */
		setTip = function(top, left){
			var topOffset = tip.height();
			var xTip = (left-30)+"px";
			var yTip = (top-topOffset-60)+"px";
			tip.css({'top' : yTip, 'left' : xTip});
		}
	
		/* This function stops the timer and creates the
		   fade-in animation                          */
		showTip = function(){
			stopTimer();
			tip.animate({"top": "+=20px", "opacity": "toggle"}, defaults.speed);
		}
	});
};


$(document).ready(function() {

//$("div#socialline ul#sociallinemenu li img").bubbleup({tooltip: true, scale:96});
$("#query").fieldtag();

$('.tTip').betterTooltip({speed: 150, delay: 300});

 $("#loading").bind("ajaxSend", function(){
   $(this).show()
		  .animate({
          opacity: 1
        }, 500, 'swing', function() {
          // once the animation is complete, set the tracker variables
          beingShown = false;
          shown = true;
        });
 }).bind("ajaxComplete", function(){
   $(this).animate({
          opacity: 0
        }, 1200, 'swing', function () {
          // once the animate is complete, set the tracker variables
          shown = false;
$("#loading").css('display', 'none');
        });
 });

 
  $('#subscribe').submit(function(e) {

    e.preventDefault();
    if ($("#subscribe :text").val() != '') {
      $('#subscribe').nyroModalManual({
      width: 360, height:100, minWidth:360, minHeight:100
    });
    } else {
      alert("Enter a valid email address");
    }
    return false;
  });



	$('.nyromodal').nyroModal();
	$(".accordian h3").live('click', function(){
		$(this).next("p").slideToggle("slow")
		.siblings("p:visible").slideUp("slow");
		$(this).toggleClass("active");
		$(this).siblings("h3").removeClass("active");
	});
	
		$(".list").live('click', function() {
$("#frontcontacts").load(this.href, function(){
	$(".pagination").hide(); });return false;
});


 $("#qtyleft , #qtyright").change(function  () {
 var qty = Number($('#qtyleft').attr('value')) + Number($('#qtyright').attr('value')); 
 var productid = $('#product_id').attr('value');
 var theone = "/ajax/calculatesavings/product_id/" + productid + "/qty/" + qty ;
		$("#producttotal").load(theone); return false; 
});

		$("#postageexpress").live('click', function() {
			var express = $("#postageexpress").attr('checked')?1:0;
	$("#checkoutsummary .checkoutsummarytable").load("/ajax/checkoutsubtotal/express/" + express); 
	});
		
	/*$("#promocodeform").livequery('submit', function() {
		var code = $("#promocode").val();
		if(code != '')
		$.post(
				'/ajax/checkoutsubtotal', {
					promocode: code
				}, function(responseText){$("#checkoutsummary .checkoutsummarytable").html(responseText);}, 'html');
		return false;
	});*/
		
	$("#prescription-element input").change(function(){
		if($(this).val() == "Netlens already has my prescription on file") {
			$("#optom_name").attr('disabled', 'disabled');
			$("#optom_phone").attr('disabled', 'disabled');
			$("#optom_address").attr('disabled', 'disabled');
		} else {
			$("#optom_name").removeAttr('disabled');
			$("#optom_phone").removeAttr('disabled');
			$("#optom_address").removeAttr('disabled');
		}
	});
	$("#promptDialog button").click(function(){
	    $('#promptDialog').fadeOut('fast');
	    $("#wrapper").fadeTo(0, 1);
	});
	$("#disclaimerLink").click(function(){
		$('#promptDialog').fadeIn();
	    $("#wrapper").fadeTo(0, 0.6);
		return false;
	});

 $(".qtyleftcheckout , .qtyrightcheckout").change(function  () 
 {	
	 var leftqty = $(this).parents(".checkoutitemdetails").find(".qtyleftcheckout").attr('value');
	 var rightqty = $(this).parents(".checkoutitemdetails").find(".qtyrightcheckout").attr('value');
	 var position = $(this).parents(".checkoutitem").find(".arrayid").attr('value');
	 var qty = Number($(this).parents(".checkoutitemdetails").find(".qtyleftcheckout").attr('value')) + Number($(this).parents(".checkoutitemdetails").find(".qtyrightcheckout").attr('value'));
	 var productid = $(this).parents(".checkoutitemdetails").find(".productid").attr('value');
	 var total = "/ajax/checkoutproducttotal/product_id/" + productid + "/leftqty/" + leftqty + "/rightqty/" + rightqty + "/position/" + position;
	 var theone = "/ajax/checkoutproductprice/product_id/" + productid + "/qty/" + qty ;
	 var lefteyeprice = "/ajax/checkouteyetotal/product_id/" + productid + "/qty/" + qty + "/eyeqty/" + leftqty;
	 var righteyeprice = "/ajax/checkouteyetotal/product_id/" + productid + "/qty/" + qty + "/eyeqty/" + rightqty;
	 var eyetotalfunction = 
	 function(){ 
	 	$(this).parents(".checkoutitem").find(".thistotalcost")
	 	.load(total,null, function(){
	 		$("#checkoutsummary .checkoutsummarytable").load("/ajax/checkoutsubtotal");
	 		
 		});
	 		};
	 var lefteyepricefunction = function(){$(this).parents(".checkoutitemdetails").find(".lefttotalprice").load(lefteyeprice,null ,eyetotalfunction); };
	 $(this).parents(".checkoutitemdetails").find(".priceperbox").load(theone); 
	 $(this).parents(".checkoutitemdetails").find(".righttotalprice").load(righteyeprice,null,lefteyepricefunction);
	
		return false; 
});
 
$(".qtycheckout").change(function  () {
	
	 var position = $(this).parents(".checkoutitem").find(".arrayid").attr('value');
	 var qty = Number($(this).parents(".checkoutitemdetails").find(".qtycheckout").attr('value')) ;
	 var productid = $(this).parents(".checkoutitemdetails").find(".productid").attr('value');
	 var theone = "/ajax/checkoutaccessoryprice/product_id/" + productid + "/qty/" + qty + "/position/" + position;
	
	 var checkoutsummary = function(){$("#checkoutsummary .checkoutsummarytable").load("/ajax/checkoutsubtotal");};
 	 
	 $(this).parents(".checkoutitemdetails").find(".lefttotalprice").load(theone,null,function(){
	 	$(this).parents(".checkoutitem").find(".thistotalcost").load(theone,null,checkoutsummary);}); 
	return false; 
});

$(".deleteitem").live('click', function() {
var deleteitem = this.href;
$.get(deleteitem, null, function(){
	$(this).parents(".checkoutitem").fadeOut(500);
$("#checkoutsummary .checkoutsummarytable").load("/ajax/checkoutsubtotal"); 
}
);

return false; });
$(".removeitem").live('click', function() {
$(this).parents(".cartcontact").fadeOut(500); 
$.get(this.href, null, function(){$(".items h1").load('/ajax/itemsincart', function(){
	});});
return false; });

$(".thumbs").live('click', function() {
$("#frontcontacts").load(this.href, function(){
	$(".pagination").show();
        });

;return false; });



$(".pagination a").live('click', function() { 
		var theone = "/index/pagination/page/" + this.pathname.substring(this.pathname.lastIndexOf('/')+1);
		$(".pagination").load(theone);
		$("#frontcontacts").load(this.href, function(){ 
        });
		return false;
});
	
		






var atcoptions = { target:'.cartcontents',url:'/ajax/addtocart',success: function() {
	$(".items h1").load('/ajax/itemsincart', function(){
	});}};   		
$("#productadd, .addtocart").submit(function(){
		
 $(this).ajaxSubmit(atcoptions); 
 
return false;
});


$('.trigger').live('mouseover', function()  
    {  
        if (!$(this).data('init'))  
         {  
             $(this).data('init', true);  
            $(this).hoverIntent  
             (  
                 function()  
                 {  
                     /* mouseover logic */  
                 },  
   
                 function()  
                 {  
                     /* mouseout logic */  
                 }  
             );  
             $(this).trigger('mouseover');  
         }  
     });  



});





   

jQuery(function()
{
    $('.cartitem').live('mouseover', function()
    {
        if (!$(this).data('init'))
        {
            $(this).data('init', true);
            $(this).hoverIntent
            (
                function()
                {
                   $(this).children(".cartcontenttooltip").animate({opacity: "show", top: "-65", left: "235"}, "5000");
                },

                function()
                {
                   $(this).children(".cartcontenttooltip").animate({opacity: "hide", top: "-65", left: "195"}, "slow");
                }
            );
            $(this).trigger('mouseover');
        }
    });
});





jQuery(function()
{
    $('.bubbleInfo').live('mouseover', function()
    {
        if (!$(this).data('init'))
        {
            $(this).data('init', true);
            $(this).hoverIntent
            (
                function()
                {
                   $(this).children(".popup").animate({opacity: "show", top: "-240", left: "-50"}, "slow");
                },

                function()
                {
                   $(this).children(".popup").animate({opacity: "hide", top: "-180"}, "fast");
                }
            );
            $(this).trigger('mouseover');
        }
    });
});


	
