// Javascript for the CMS CSS Menu Module
// Copyright (c) 2005 Alexander Endresen
// Released under General Public Licence
// This script will emulate the css :hover effect on the menu elements in IE
/*
// The variables
var cssid = "nav"; // CSS ID for the menuwrapper
var menuadd = "h";  // Character to be added to the specific classes upon hovering. So for example, if this is set to "h", class "menuparent" will become "menuparenth" when hovered over.
var menuh = "menuh"; // Classname for hovering over all other menu items

if (window.attachEvent){
//alert("mike");
window.attachEvent("onload", cssHover);
}

function cssHover() {
	var sfEls = document.getElementById(cssid).getElementsByTagName("li");
	
	for (var i=0; i<sfEls.length; i++) {

		sfEls[i].onmouseover=function() {
			if (this.className != "") {
				this.className = this.className + menuadd;
				
			}
			else {	
				this.className = menuh;
			}
		}

		sfEls[i].onmouseout=function() {
			if (this.className == menuh) {
				this.className = "";
			}
			else {
				this.className = this.className.replace(new RegExp(menuadd + "$"), "");
			}
		}
	}
	
	

}
*/




// -------------------------------------------------------------------------------------------
// The Image preloader
// -------------------------------------------------------------------------------------------


(function($)
{
	$.fn.kriesi_image_preloader = function(variables) 
	{
		var defaults = 
		{
			fadeInSpeed: 600,
			delay:500,
			callback: ''
		};
		
		var options = $.extend(defaults, variables);
		
		return this.each(function()
		{	
			var imageContainer = jQuery(this),
				images = imageContainer.find('img').css({opacity:0, visibility:'hidden',display:'block'}),
				imagesToLoad = images.length;				
				
				
				imageContainer.operations =
				{	
					preload: function()
					{	
						var stopPreloading = true;
												
						images.each(function(i, event)
						{	
							var image = $(this);							
							if(event.complete == true)
							{	
								imageContainer.operations.showImage(image);
							}
							else
							{	
								image.bind('error load',{currentImage: image}, imageContainer.operations.showImage);
							}
							
						});
						
						return this;
					},
					
					showImage: function(image)
					{	
						imagesToLoad --;
						if(image.data.currentImage != undefined) { image = image.data.currentImage;}
													
						if (options.delay <= 0) image.css('visibility','visible').animate({opacity:1}, options.fadeInSpeed);
											 
						if(imagesToLoad == 0)
						{
							if(options.delay > 0)
							{
								images.each(function(i, event)
								{	
									var image = $(this);
									setTimeout(function()
									{	
										image.css('visibility','visible').animate({opacity:1}, options.fadeInSpeed, function()
										{
											$(this).parent().removeClass('preloading');
										});
									},
									options.delay*(i+1));
								});
								
								if(options.callback != '')
								{
									setTimeout(options.callback, options.delay*images.length);
								}
							}
							else if(options.callback != '')
							{
								(options.callback)();
							}
							
						}
						
					}

				};
				
				imageContainer.operations.preload();
		});
		
	};

})(jQuery);

jQuery.fn.mailme = function() {
    var at = / at /;
    var dot = / dot /g;
    this.each( function() {
        var addr = jQuery(this).text().replace(at,"@").replace(dot,".");
        var title = jQuery(this).attr('title')
        $(this)
            .after('<a href="mailto:'+addr+'" title="'+title+'">'+ addr +'</a>')
            .remove();
    });
};



$(document).ready(function() {

   $("#nav li").hover(function(){
   //alert("mike");
        $('ul:first',this).css({'display':'block'});
      //   $('ul:first',this).hide();
        // $('ul:first',this).fadeIn();

    }, function(){
            $('ul:first',this).css('display', 'none');
    
    });


// convert email addresses 
if ($('span.mailme').length) { // implies *not* zero
$('span.mailme').mailme();
}


if ($('#content img').length) { // implies *not* zero
$('#content img').jcaption();
}
	
if ($('a.fancybox').length) { // implies *not* zero
$("a.fancybox").fancybox();
}

$("#enews").click(function() {
	$.fancybox({
			'padding'		: 0,
			'autoScale'		: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'title'			: this.title,
			'width'		: 650,
			'height'		: 640,
			'href'			: "http://eepurl.com/dwG9P",
			'type'			: 'iframe',
			'swf'			: {
			   	 'wmode'		: 'transparent',
				'allowfullscreen'	: 'true'
			}
		});

	return false;
});


$("#cellardor").click(function() {

$.fancybox({
			'padding'		: 0,
			'autoScale'		: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'title'			: this.title,
			'width'		: 650,
			'height'		: 640,
			'href'			: "http://eepurl.com/dD5Ms",
			'type'			: 'iframe',
			'swf'			: {
			   	 'wmode'		: 'transparent',
				'allowfullscreen'	: 'true'
			}
		});
	return false;
});




// FORM TRIGGERS

$("#enews-form").click(function() {

//alert($("#cellardor").is(':checked'));
 if( $("#enews-form").is(':checked') == true){

$.fancybox({
			'padding'		: 0,
			'autoScale'		: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'title'			: this.title,
			'width'		: 650,
			'height'		: 640,
			'href'			: "http://eepurl.com/dwG9P",
			'type'			: 'iframe',
			'swf'			: {
			   	 'wmode'		: 'transparent',
				'allowfullscreen'	: 'true'
			}
		});
 }


//if($("#cellardor").is(':checked') == true){};


	//return false;
});



$("#cellardor-form").click(function() {

//alert($("#cellardor").is(':checked'));
 if( $("#cellardor-form").is(':checked') == true){

$.fancybox({
			'padding'		: 0,
			'autoScale'		: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'title'			: this.title,
			'width'		: 650,
			'height'		: 640,
			'href'			: "http://eepurl.com/dD5Ms",
			'type'			: 'iframe',
			'swf'			: {
			   	 'wmode'		: 'transparent',
				'allowfullscreen'	: 'true'
			}
		});
 }


//if($("#cellardor").is(':checked') == true){};


	//return false;
});








/* TABLE */
 $("td:first-child").addClass("stockists");
  $("td:nth-child(2)").addClass("stockists_address");
  $("td:nth-child(3)").addClass("stockists_phone");
if($('#gallery')){		
	$('#slider').kriesi_image_preloader({callback:add_functionality});
	$('#quicklinkholder').kriesi_image_preloader({delay:200});
}
$("#logo").hide().fadeIn("slow");

//$(".home").kriesi_image_preloader({delay:200});
function add_functionality()
			{
$('#slider').nivoSlider({
					effect:'fade', //Specify sets like: 'fold,fade,sliceDown'
					slices:12,
					animSpeed:500, 
					pauseTime:4000,
					directionNav:true,
					controlNav:true,
					controlNavThumbs:false, 
					keyboardNav:true, 
					pauseOnHover:true, 
					captionOpacity:1
				});
				// $('#slider').animate({ 'height','370px' }, 1000);
				//$('#slider').css('height','370px');
				 $("#slider").animate({
    height: "370px"
  }, 1500 );
		}			
		
		


if ($('#demo').length) { // implies *not* zero

$('#demo').masonry({
 // columnWidth: 20, 
  itemSelector: '.box' 
});
}


$("#content").kriesi_image_preloader({delay:200});

$("#footer ul").hide();
$('.grid_4').hover(function(){
				$(this).find('ul').fadeIn();
			}, function(){
				$(this).find('ul').fadeOut();
			});
$('.grid_7').hover(function(){
				$(this).find('ul').fadeIn();
			}, function(){
				$(this).find('ul').fadeOut();
			});

        
        slide("#sub-nav", 35, 25, 800, .8);
        topslide("#nav", 13, 10, 600, .8);
        

});


$.fn.delay = function( time, name ) {
    return this.queue( ( name || "fx" ), function() {
        var self = this;
        setTimeout(function() { $.dequeue(self); } , time );
    } );

};

function topslide(navigation_id, pad_out, pad_in, time, multiplier)
{

	// creates the target paths
	var list_elements = navigation_id;//+ " li._1";
	
	var link_elements = list_elements + " li li a";
	
	// initiates the timer used for the sliding animation
	var timer = 0;
	
	/* creates the slide animation for all list elements 
	$(list_elements).children().each(function(i)
	{
	//alert (list_elements);
	
		// margin left = - ([width of element] + [total vertical padding of element])
		$(this).hide();
		timer = (timer*multiplier + time);
		//$(this).fadeIn("slow", timer);
		//setTimeout(function(){ $(this).fadeIn();}, 1000);
		$(this).delay(timer).fadeIn(500);
		//$(this).animate({ paddingLeft: pad_out }, 250);
	});
	*/
	
	$(link_elements).each(function(i)
	{
		$(this).hover(
		function()
		{
			$(this).animate({ paddingLeft: pad_out }, 250);
		},		
		function()
		{
			$(this).animate({ paddingLeft: pad_in }, 250);
		});
	});

	
	
	
}
function slide(navigation_id, pad_out, pad_in, time, multiplier)
{

	// creates the target paths
	var list_elements = navigation_id + " li";
	
	var link_elements = list_elements + " a";
	
	// initiates the timer used for the sliding animation
	var timer = 0;
	
	// creates the slide animation for all list elements 
	$(list_elements).each(function(i)
	{
	//alert (list_elements);
	
		// margin left = - ([width of element] + [total vertical padding of element])
		$(this).hide();
		timer = (timer*multiplier + time);
		//$(this).fadeIn("slow", timer);
		//setTimeout(function(){ $(this).fadeIn();}, 1000);
		$(this).delay(timer).fadeIn('slow');
	});

	// creates the hover-slide effect for all link elements 		
	$(link_elements).each(function(i)
	{
		$(this).hover(
		function()
		{
			$(this).animate({ paddingLeft: pad_out }, 250);
		},		
		function()
		{
			$(this).animate({ paddingLeft: pad_in }, 250);
		});
	});
}
