function openSelect(element, num)
{
	var val = $('select#'+element).val();
	if (val.indexOf("|") != -1)
	{
		if (!num) num = 0;
		val = val.split('|');
		val = val[num];
	} // indexOf
	if (val == "") alert('Sorry the file you requested is not available.');
	else 
	{
		var text = $('select#'+element+' :selected').text();
		if (confirm('View ' + text + '?'))
		{
			window.open(val, 'pdf');
		} // confirm
	} // else
} // openSelect



$(function() {


/*	$('.animator-content').cycle({ 
	//        fx:      'fade', 
	easing: 'easeInOutExpo',
	fit: true,
	speed:    1000, 
	timeout:  5000,
	next:    '.advance-1'
	});


	function killCycle(width,offset) {
		var imageWidth = width - offset;
		$('.animator-stretch-content').cycle('stop');
		$('.animator-stretch-content').cycle('destroy');
		$('.animator-stretch-content').cycle({ 
		//        fx:      'fade', 
		easing: 'easeInOutExpo',
		fit: true,
		width: imageWidth,
		speed:    1000, 
		timeout:  5000,
		next:    '.advance'
		});
	};
	
	

	var resizeTimer;
	
	$(window).resize(function() {
		var elem = $(this);
	    $('#window-info').text( 'window width: ' + elem.width() + ', height: ' + elem.height() );
	    clearTimeout(resizeTimer);

		if ($('body').hasClass('home') || $('body').hasClass('wines')){
			$offset = 745;
		} else {
			$offset = 945;	
		}
		
	    resizeTimer = setTimeout(killCycle(elem.width(),$offset), 300);
	});
	
	$(window).resize();
	
	
*/

	

	$('#expander-click').click(function(){
		
		var expanderLink = $(this);
		var expanderLinkWords = $(this).text().replace( "Hide", "View" );
		var expanderLinkwordsExpanded = $(this).text().replace( "View", "Hide" );

		//alert(linkwordsExpanded);
		
		var $widgets = $('.arrow-top-2, .arrow-bottom-2, .advance');

		if ($('.expander').hasClass('active')) {
			$('.expander').animate({
			  width: '530px'
			}, 500, function() {
			  // Animation complete.
			expanderLink.text(expanderLinkWords);
			}).removeClass('active');

			$widgets.animate({
			  left: '736px'
			}, 500, function() {
			  // Animation complete.
			});
					
		} else {
			$('.expander').animate({
			  width: '735px'
			}, 500, function() {
			  // Animation complete.
			expanderLink.text(expanderLinkwordsExpanded);
			}).addClass('active');

			$widgets.animate({
			  left: '942px'
			}, 500, function() {
			  // Animation complete.
			});
		
		}
	});

/* read more read less widget */

	$('.text-expanded').hide();

	$('.text-summary').each(function(){
		$(this).after('&nbsp;&nbsp;<a href="#" class="read-more">Read More</a>');
	});
	
	$('.text-expanded').each(function(){
		$(this).append('&nbsp;&nbsp;<a href="#" class="read-less">Read Less</a>');
	});

	$('.read-more').click(function(){
		$(this).toggle().next().show();
	});
	
	$('.read-less').click(function(){
		$(this).parent().hide();
		$(this).parent().parent().children('.read-more').show();
	});

/* tabber func */

function tabber(e,active) {

/* 
target function must have a 1 table in it somewhere with two cells:
1.) title 2.) data and as many rows as necessary 	
*/

	var dataTable = e+" table";
	var dataTitle = e+" td:nth-child(1)";
	var dataCell = e+" td:nth-child(2)";
	var photoCell = e+" td:nth-child(3)";
	
	$(e).append('<div class="tabber"><div class="tabber-wrapper"><div class="tabber-nav"></div><div class="tabber-content"><div class="tabber-title"></div><div class="tabber-text"></div></div></div></div>');
	
	$(dataTable).hide();
	
	$(dataTitle).each(function(){
		$(e+' .tabber-nav').append('<span class="tabber-link">'+$(this).html()+'</span>');
	});

	$(dataCell).each(function(){
		$(e+' .tabber-content .tabber-text').append('<div class="tabber-text-node">'+$(this).html()+'</div>');
	});
	
	$(photoCell).each(function(){
		$('.col-1').append('<div class="tabber-image">'+$(this).html()+'</div>');
	});

	$('.tabber-image').hide();
	$('.col-1 .tabber-image:nth-child(9)').show();			

	$(e+' .tabber-content .tabber-title-node, .tabber-content .tabber-text-node, .tabber-image').hide();
	
	// initialize the "active" one
	if (active) {
	 $(e+' .tabber-nav a:nth-child('+active+')').addClass('active');
	 $(e+' .tabber-content .tabber-title .tabber-title-node:nth-child('+active+')').toggle();
	 $(e+' .tabber-content .tabber-text .tabber-text-node:nth-child('+active+')').toggle();					
	}

	$(e+' .tabber-nav .tabber-link').each(function(index) {
		var tabNav = $(this);
		tabNav.click(function(nav) {
			var tabInc = index + 1;
			$(e+' .tabber-nav .tabber-link').removeClass('active');		
	//		alert(tabInc);
			$(e+' .tabber-content .tabber-title-node, '+e+' .tabber-content .tabber-text-node, .tabber-image').hide();
			$(e+' .tabber-content .tabber-title .tabber-title-node:nth-child('+tabInc+')').toggle();
			$(e+' .tabber-content .tabber-text .tabber-text-node:nth-child('+tabInc+')').toggle();			
			$('.col-1 .tabber-image:nth-child('+tabInc+')').show();			
			$('.col-1 .defaultImage').hide();
			$(e+' .tabber-nav .tabber-link:nth-child('+tabInc+')').addClass('active');		
			return false;
		});
	});
	
}

tabber("#vineyard-listing",0);
tabber("#grape-listing",0);
tabber("#people-listing",0);
tabber("#centralcoast-listing",0);

$('#vineyard-listing .tabber-nav .tabber-link:nth-child(3), #vineyard-listing .tabber-nav .tabber-link:nth-child(6), #vineyard-listing .tabber-nav .tabber-link:nth-child(9), #grape-listing .tabber-nav .tabber-link:nth-child(3), #grape-listing .tabber-nav .tabber-link:nth-child(6), #grape-listing .tabber-nav .tabber-link:nth-child(9), ').width('90px');

$('.two-cols .col, .three-cols .col').append('<div style="width:100%"><!-- hey --></div>');

$('.v65-club').each(function(){
	var title = $(this).children('.v65-clubTitle').find('a').html();
	
	$(this).children('.v65-clubTeaser').children('.small-text').prepend('<span style="float:left;margin-right:1em" class="all-caps purple">'+title+'</span>');
});	

$('.v65-product3Up h2 a').addClass('all-caps');
$('.v65-product3Up h2 a').addClass('purple');
$('legend').addClass('all-caps');
$('legend').addClass('purple');
$('.v65-cartItemDescription a strong').addClass('all-caps');
$('.v65-cartItemDescription a strong').addClass('purple');
$('#v65-productDescription h3').addClass('all-caps');
$('#v65-productDescription h3').addClass('purple');



$('.wineclubs').each(function(){
	var title = $('.wineclubs h1').html();
	$('.v65-clubSignUp-Teaser').children('.small-text').prepend('<span style="float:left;margin-right:1em" class="all-caps purple">'+title+'</span>');
});	


var invaderContent = $('#LimeKilnText').html();
var invaderContent1 = $('#LimeKilnText1').html();
var invaderContent2 = $('#LimeKilnText2').html();

$('#invader .content').html(invaderContent);
$('#invader1 .content').html(invaderContent1);
$('#invader2 .content').html(invaderContent2);


$('.advance, #invader .content').hover(
	function(){
		$('#invader').show();
	},
	function(){
		$('#invader').hide();	
	}
);

$('.advance-1, #invader1 .content').hover(
	function(){
		$('#invader1').show();
	},
	function(){
		$('#invader1').hide();	
	}
);

$('.advance-2, #invader2 .content').hover(
	function(){
		$('#invader2').show();
	},
	function(){
		$('#invader2').hide();	
	}
);

$('.nav li:nth-child(7)').addClass('li-stripes');
$('.nav li:nth-child(1)').addClass('li-stripes');
$('.nav li:nth-child(3)').addClass('li-stripes');

//$('#v65-checkoutCartSummaryWrapper').append($('#v65-checkoutFooter'));

$('#gravity-flow-click').click(function(){
	$('#gravity-flow').modal();
});

$('#emailSignupClick, #emailSignupClick2').click(function(){
	$('#emailSignupForm').modal();
});

$('#shippingClick').click(function(){
	$('#shippingPop').modal();
});


$('.bottleshots .all-caps').each(function(){
	$(this).text().replace("_"," ");
});

//$('#v65-productSpecs').addClass('lines');

/* force second image to occupy vin65's gallery widget*/
if ($('#v65-productImagesOther').length) {
	var imageName = $('#v65-productImagesOther a:nth-child(2)').attr('href').replace('javascript:vin65.product.switchProductImage','').replace("('","").replace("','lrg');","");
	var imageUrl = '/assets/images/products/large/'+imageName;
	$('#v65-productImage').html('<img src="'+imageUrl+'" />');	
}

$('.v65-product3UpTeaser h2').each(function(){
	var productLink = $(this).children().attr('href');
	var productData = $(this).text().split('::');
	var productTitle = productData[0];
	var productSubTitle = productData[1];
	$(this).hide();
	if (productSubTitle){
		$(this).parent().prepend('<h3><a href="'+productLink+'">'+productSubTitle+'</a></h3>');		
	}
	if (productTitle){
		$(this).parent().prepend('<h2><a class="purple all-caps" href="'+productLink+'">'+productTitle+'</a></h2>');
	}
	
	$(this).parent().parent().children('.v65-product3UpAddToCart').prepend('<a href="'+productLink+'">View Details</a>');

});

$('#v65-productDescription h1').each(function(){
	var productData = $(this).text().split('::');
	var productTitle = productData[0];
	var productSubTitle = productData[1];
	$(this).hide();
	if (productSubTitle){
		$(this).parent().prepend('<h2>'+productSubTitle+'</h2>');
	}
	if (productTitle){
		$(this).parent().prepend('<h1>'+productTitle+'</h1>');
	}
});

/* random homepage images */
var randomNum = Math.floor((5 - 2 + 1) * Math.random() + 2);
$('.home .container .content').css('background-image','url(/assets/images/home-photo-right-'+randomNum+'.jpg)');
$('.home .container .content .col-1').html('<img src="/assets/images/home-photo-left-'+randomNum+'.jpg" />')

$('.members .subNav').append($('#v65-modalLoginLink-Logout'));

if ($('#v65-modalLoginLink-Login').length) {
	$('#login').html($('#v65-modalLoginLink-Login'));	
} else {
	$('#login').html($('#v65-modalLoginLink-Name'));
	$('#v65-modalLoginLink-Name').text('My Account');	
}

	
});


