$(document).ready(function() {

/* Add Tweet This Button */
/* This feature was removed at the request of Katherine
	$('div#latest-from p').each(function() {
		var original = $(this).html().replace(/ /g,'+');
		var tweetLink = '<div class="share-box"><a class="share" href="http://www.twitter.com/?status=RT+@medeavodka+' + original + '" target="_blank">Tweet This</a></div>';
				
		$(tweetLink).prependTo(this).css('display', 'none');
	});
	
	$('div#latest-from p').hover(function() {
		$(this).find('div.share-box').show(400);
	}, function() {
		$(this).find('div.share-box').hide(400);
	});
*/

	// Video Lightbox
	$("a[rel='lightbox'], .wall .video").prettyPhoto({
		autoplay: true,
		default_height: 510,
		default_width: 853,
		social_tools: ''
	});
	
	$('.wall').find('.image').prettyPhoto({
		social_tools: ''
	});

	/* Logic for "flag as inappropriate" button on aspirations */
	// $('#latest-from div.items div.aspiration form').hide(0);
	// $('#latest-from div.aspiration').hover(function() {
	// 	$(this).find('form').show('slow');
	// }, function() {
	// 	$(this).find('form').hide('slow');
	// });
		
	// for age verification (no longer in use)
	// if (getCookie('ageverified') != 'verified') {
	// 	$('#age-verification').show(0);
	// }
	
	// $('#age-yes').click(function() {
	// 	$('#age-verification').fadeOut('slow');
	// 	setCookie('ageverified', 'verified', 1600000);
	// 	return false;
	// });
	
	$('#latest-from div.items div.aspiration form input.flag').click(function() {
		var answer = confirm ("Are you sure you want to flag this content?  This action can't be undone.");
		if (answer) {
			return true;
		} else {
			return false;
		}
	});

	// Toggle Newsletter Form
	$('#social #newsletter').hover(function() {
		$('#newsletter-form').slideDown(500);
	}, function() {
		$('#newsletter-form').slideUp(500);
	});


	// Open links in new tab/window
	$('a[rel*=blank]').click(function(){
		this.target = "_blank";
	});


	// Insert Browser Specific Stylesheets */
	if ($.browser.opera) {
		$('head').append('<link rel="stylesheet" href="/css/opera.css" type="text/css" />');
	}


	// Age Verification Stuff
	$('div#age-verification #no, div#age-verification #yes').fadeTo(0, .25);

	$('div#age-verification #no, div#age-verification #yes').hover(function() {
		$(this).stop().fadeTo(500, 1);
	}, function() {
		$(this).stop().fadeTo(500, .25);		
	});
	
	
	// Homepage Stuff	
	$('#inner-ticker').css('left', '660px');

	$('#inner-ticker img').load(scrollWords());

	function scrollWords(){
		$('#inner-ticker').css('left', '640px');
		$('#inner-ticker').animate({left: '-27000'}, 147000, 'linear', scrollWords2);
	}
	
	function scrollWords2(){
		$('#inner-ticker').css('left', '640px');
		$('#inner-ticker').animate({left: '-27000px'}, 147000, 'linear', scrollWords);
	}
	
	// Aspirations Stuff 
	$('div#latest-from p:odd').removeClass('am').addClass('want');

	// initialize scrollable  
	$('div.scrollable').scrollable({ 
		vertical:true,  
		size: 10 
		 
	// use mousewheel plugin 
	}).mousewheel();   
	
	
	// Tastings Stuff
	$('div.tastings table tr:nth-child(odd)').addClass('even'); 
	
	$('div.tastings tr td:nth-child(1)').css('width', '215px');
	$('div.tastings tr td:nth-child(2)').css('width', '155px');
	$('div.tastings tr td:nth-child(3)').css('width', '258px');
	
	
	// How To Tick Stuff
	if (!($.browser.msie && $.browser.version=="6.0")) {
		$('span.diagram2').hover(function(){
			$('img.diagram1, img.diagram3, img.diagram4, img.diagram5').css('z-index', '0').css('display', 'none');
			$('img.diagram2').css('z-index', '10').css('display', 'inline');
		}, function(){
			$('img.diagram2').css('z-index', '0').css('display', 'none');
			$('img.diagram1').css('z-index', '10').css('display', 'inline');
		});
		
		$('span.diagram3').hover(function(){
			$('img.diagram1, img.diagram2, img.diagram4, img.diagram5').css('z-index', '0').css('display', 'none');
			$('img.diagram3').css('z-index', '10').css('display', 'inline');
		}, function(){
			$('img.diagram3').css('z-index', '0').css('display', 'none');
			$('img.diagram1').css('z-index', '10').css('display', 'inline');
		});
		
		$('span.diagram4').hover(function(){
			$('img.diagram1, img.diagram3, img.diagram2, img.diagram5').css('z-index', '0').css('display', 'none');
			$('img.diagram4').css('z-index', '10').css('display', 'inline');
		}, function(){
			$('img.diagram4').css('z-index', '0').css('display', 'none');
			$('img.diagram1').css('z-index', '10').css('display', 'inline');
		});
		
		$('span.diagram5').hover(function(){
			$('img.diagram1, img.diagram3, img.diagram2, img.diagram5').css('z-index', '0').css('display', 'none');
			$('img.diagram5').css('z-index', '10').css('display', 'inline');
		}, function(){
			$('img.diagram5').css('z-index', '0').css('display', 'none');
			$('img.diagram1').css('z-index', '10').css('display', 'inline');
		});
	}
	
	// Contact Us Stuff
	$('#contactform').submit(function(){
	
		var action = $(this).attr('action');
		
		$('#message').slideUp(750,function() {
		$('#message').hide();
		
 		$('#submit')
			.after('<img src="/images/ajax-loader.gif" class="loader" />')
			.attr('disabled','disabled');
		
		$.post(action, { 
			name: $('#name').val(),
			email: $('#email').val(),
			phone: $('#phone').val(),
			subject: $('#subject').val(),
			comments: $('#comments').val(),
			verify: $('#verify').val()
		},
			function(data){
				document.getElementById('message').innerHTML = data;
				$('#message').slideDown('slow');
				$('#contactform img.loader').fadeOut('slow',function(){$(this).remove()});
				$('#contactform #submit').attr('disabled',''); 
				if(data.match('success') != null) $('#contactform').slideUp('slow');
			});
		});
		return false;
	});
	
	// Fix sidebar height problem in IE6
	if ($.browser.msie && $.browser.version == 6.0) {
		if ($(document).height() > $(window).height()) {
			var documentHeight = $(document).height() + 'px';
			
			$('#sidebar').css('height', documentHeight);
			
			// Take care of an image sizing and loading problem
		}
	}
});

function setCookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

/*
	if the expires variable is set, make the correct
	expires time, the current script below will set
	it for x number of days, to make it for hours,
	delete * 24, for minutes, delete * 60 * 24
*/
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}

	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

function getCookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ ) {
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name ) {
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}

	if ( ! b_cookie_found ) {
		return null;
	}
}
