//Menu Hover State

$(function() {

    //Preserves the mouse-over on top-level menu elements when hovering over children
    $(".menu li").each(function(i){
      $(this).hover(function(){
        $('.level1a', this).addClass("active");
      },function(){
        $('.level1a', this).removeClass("active");
      });
    });

  });


//Tabs

$(document).ready(function() {

	//When page loads...
	$(".tab_content").hide(); //Hide all content
	$("ul.tabs li:first").addClass("active").show(); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content

	//On Click Event
	$("ul.tabs li").click(function() {

		$("ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});

});

//Text in Form Elements
$(function(){ 
			    // find all the input elements with title attributes
				$('input[title!=""]').hint();
				$('textarea[title!=""]').hint();
			});
//SLIDE PANEL
$(document).ready(function() {
 
	// Expand Panel
	$(".open").click(function(){
		$("div.panel", $(this).parent().parent()).slideDown("slow");	
	});	
 
	// Collapse Panel
	$(".close").click(function(){
		$("div.panel", $(this).parent().parent()).slideUp("slow");	
	});		
 
	// Switch buttons from "Log In | Register" to "Close Panel" on click
	$(".toggle div").click(function () {
		$(".toggle div", $(this).parent().parent()).toggle();
	});		
 
});

//// VERTICALLY ALIGN IMAGE FUNCTION
//(function ($) {
//        $.fn.vAlign = function() {
//          return this.each(function(i){
//          var ah = $(this).height();
//          var ph = $(this).parent().height();
//          var mh = (ph - ah) / 2;
//          $(this).css('margin-top', mh);
//          });
//        };
//    })(jQuery);
//    
//    $(document).ready(function(){
//    $('.vaIMG p').vAlign();
//    });

////Menu

$('document').ready(function() {
    $('.vaIMG p').vAlign();
});

(function($) {
    $.fn.vAlign = function(container) {
        return this.each(function(i) {
            if (container == null) {
                container = 'div';
            }
            $(this).html("<" + container + ">" + $(this).html() + "</" + container + ">");
            var el = $(this).children(container + ":first");
            var elh = $(el).height(); //new element height
            var ph = $(this).height(); //parent height
            var nh = (ph - elh) / 2; //new height to apply
            $(el).css('margin-top', nh);
        });
    };
})(jQuery);


 function mainmenu(){
$(" div.submenulist ").css({display: "none"}); // Opera Fix
$("#menucontent ul.menu li div.submenulist").fadeTo(0, 0.97);
$(" li.level1").hover(function(){
		$(this).find('div.submenulist:hidden').css({visibility: "visible",display: "none"}).fadeIn(0);
		},function(){
		$(this).find('div.submenulist').css({visibility: "hidden",display: "none"});
		});
}

$(document).ready(function(){	
						   
	mainmenu();
	
		// FadeRoll
	$('a').hover(function(){
		$(this).find('.faderoll').stop().fadeTo(0, 0.80);
	}, function(){
		$(this).find('.faderoll').stop().fadeTo(500, 1.00);
 	});	
	
	// Rollovers (instant)
	rollover_triggered = 'no';
	$('.rollover img, img.rollover').parent('a').focus(function(){
		$(this).children('img').attr('src', $(this).children('img').attr('src').replace(/\_off\./gi, '_on\.'));
		rollover_triggered = 'yes';
	});
	$('.rollover input, input.rollover').focus(function(){
		$(this).attr('src', $(this).attr('src').replace(/\_off\./gi, '_on\.'));
		rollover_triggered = 'yes';
	});
	$('.rollover img, .rollover input, img.rollover, input.rollover').parent('a').blur(function(){
		$(this).children('img').attr('src', $(this).children('img').attr('src').replace(/\_on\./gi, '_off\.'));
		rollover_triggered = 'no';
	});
	$('.rollover input, input.rollover').blur(function(){
		$(this).attr('src', $(this).attr('src').replace(/\_on\./gi, '_off\.'));
		rollover_triggered = 'no';
	});
	$('.rollover img, .rollover input, img.rollover, input.rollover').hover(function(){
		$(this).attr('src', $(this).attr('src').replace(/\_off\./gi, '_on\.'));
	},
	function(){
		if ( rollover_triggered != 'yes' ) {
			$(this).attr('src', $(this).attr('src').replace(/\_on\./gi, '_off\.'));
		}
	});
	// Rollovers (preload)
	$('.rollover img, .rollover input, img.rollover, input.rollover').each(function(){
		var source_image = $(this).attr('src');
		var rollover_image = source_image.replace(/\_off\./gi, '_on\.');
		$('<img>').attr('src', rollover_image);
	});



});

function AddToBasket(productID, isCase, quantity) {

    $("#response" + productID).empty().html('<img src="/images/loading.gif" />');
    var url = "productID=" + productID + "&IsCase=" + isCase + "&quantity=" + $("#quantity" + productID).val() + "&rand=" + Math.random();

    $.ajax({
        type: "GET",
        url: "/Products/AddToBasket.aspx",
        data: url,
        success: function(msg) {
        $("#response" + productID).empty().text("Product Added");
        $("#basketinfo").load('/Ajax/QuickBasket.aspx?rand=' + Math.random());
        
        },
        error: function(msg) {        
        }
    });


    if (document.location.href == "http://99ppreview.evdesign.co.uk/Products/Basket.aspx") {
        location.href = "/Products/Basket.aspx";
    }
    return false;
}

function AddToBasket(productID, isCase, quantity, location) {
    $("#response" + location + productID).empty().html('<img src="/images/loader.gif" style=""  />');
    var url = "productID=" + productID + "&IsCase=" + isCase + "&quantity=" + $("#quantity" + location + productID).val() + "&rand=" + Math.random();

    $.ajax({
        type: "GET",
        url: "/Products/AddToBasket.aspx",
        data: url,
        success: function(msg) {

            $("#response" + location + productID).load('/Ajax/ProductCount.aspx?ProductID=' + productID + '&IsCase='+isCase+'&rand=' + Math.random());
            $("#basketinfo").load('/Ajax/QuickBasket.aspx?rand=' + Math.random());
            $("#quantity" + location + productID).val("Quantity");
        },
        error: function(msg) {
        }
    });

   if (document.location.href == "http://99ppreview.evdesign.co.uk/Products/Basket.aspx") {
    location.href = "http://99ppreview.evdesign.co.uk/Products/Basket.aspx";
    }
    return false;
}

function Clear(obj) {
    if (obj.value == "Quantity")
        obj.value = "";
}
function Refresh(obj) {
    if (obj.value == "")
        obj.value = "Quantity";
}

