$(document).ready(function() {
	// set up hover dropdown for main menu
	$('.effect').hover(
		function() {
			$(this).children('.drop').stop(true, true).fadeIn(300);
		},
		function() {
			$(this).children('.drop').stop(true, true).fadeOut(200);
		}
	);
	
	// set up hover text switching for main menu links
	/*
	$('a span.sample').hover(
		function() {
			var rel = $(this).attr('rel');
			var old = $(this).text();
			$(this).empty().text(rel);
			$(this).attr('rel', old);
		},
		function() {
			var rel = $(this).attr('rel');
			var old = $(this).text();
			$(this).empty().text(rel);
			$(this).attr('rel', old);
		}
	);
	*/
	$('li.textswitch').hover(
		function() {
			var oThis = $(this);
			var oSpan = oThis.find('span.sample:first');
			var rel = oSpan.attr('rel');
			var old = oSpan.text();
			oSpan.empty().text(rel);
			oSpan.attr('rel', old);
		},
		function() {
			var oThis = $(this);
			var oSpan = oThis.find('span.sample:first');		
			var rel = oSpan.attr('rel');
			var old = oSpan.text();
			oSpan.empty().text(rel);
			oSpan.attr('rel', old);
		}
	);	

	$('#black_box').mouseenter(function(){
		$('#sname').fadeOut("medium");
		$('#shadow').fadeOut("medium");
	});

	$('#black_box').mouseleave(function(){
		$('#sname').fadeIn("medium");
		$('#shadow').fadeIn("medium");
	});

	$('#login_password').keyup(function(e){
		
		if(e.keyCode == 13) {
		
			tryLogin();
		
		}
	});

	$('#sub_login_password').keyup(function(e){
		
		if(e.keyCode == 13) {
		
			trySubLogin();
		
		}
	});

});

$(function() {
	$('a[rel*=external]').click( function() {
		window.open(this.href);
		return false;
	});
});

// ### Maestro Gallery ###

var iMaestroGalleryItemCount = 0;
var iMaestroGalleryCurrentItem = 1;
var iMaestroGalleryItemWidth = 175; // should match CSS combination of item width and side margin
var iMaestroGalleryItemMarginWidth = 17; // should match CSS item side margin
var iMaestroGalleryItemsShown = 5;
var iMaestroGalleryTotalSlideWidth = (iMaestroGalleryItemWidth * iMaestroGalleryItemsShown);
var iMaestroGalleryContentFadeSpeedInMS = 400;
var bMaestroGalleryClickLock = false;
var iMaestroGallerySlideDurationInMS = 500;

function maestroGalleryMove(oEvent, iMoveIncrement) {
	oEvent.preventDefault();
	
	if (bMaestroGalleryClickLock === false) {
		bMaestroGalleryClickLock = true;
		iMaestroGalleryCurrentItem = iMaestroGalleryCurrentItem + iMoveIncrement;
		maestroGalleryAnimate();
	}
}

function maestroGalleryAnimate() {
	var iNewLeftPosition = maestroGalleryCalcLeftPos();
	$('.maestrogallery_slidearea').animate({ left: '-'+iNewLeftPosition+'px' }, iMaestroGallerySlideDurationInMS, function(){
		if (iMaestroGalleryCurrentItem > iMaestroGalleryItemCount) {
			iMaestroGalleryCurrentItem = 1;
			var iResetLeftPosition = maestroGalleryCalcLeftPos();
			$('.maestrogallery_slidearea').css('left', '-'+iResetLeftPosition+'px');
		} else if (iMaestroGalleryCurrentItem < 1) {
			iMaestroGalleryCurrentItem = iMaestroGalleryItemCount;
			var iResetLeftPosition = maestroGalleryCalcLeftPos();
			$('.maestrogallery_slidearea').css('left', '-'+iResetLeftPosition+'px');
		}
		bMaestroGalleryClickLock = false;
	});
}

	function maestroGalleryCalcLeftPos() {
		return (((iMaestroGalleryCurrentItem * iMaestroGalleryItemWidth) - iMaestroGalleryItemWidth + iMaestroGalleryItemMarginWidth) + (1 * iMaestroGalleryItemWidth));
	}

function maestroGalleryClick(oEvent, oThis) {
	oEvent.preventDefault();
	var oLink = oThis.find('a.meastrogallery_link');
	if (oLink.length !== 0)	{
		document.location = oLink.attr('href');
	}
}

function initializeMaestroGallery() {
	iMaestroGalleryItemCount = parseInt($('.maestrogallery_item').size());

	//$('.maestrogallery_content').hide();
	
	if (iMaestroGalleryItemCount >= iMaestroGalleryItemsShown) {
		var oFirstItems = $('.maestrogallery_item').slice(0, 5).clone();
		var oLastItem = $('.maestrogallery_item').slice(-1).clone();
		oFirstItems.appendTo('.maestrogallery_slidearea');
		oLastItem.prependTo('.maestrogallery_slidearea');
		
		iMaestroGalleryTotalSlideWidth = (iMaestroGalleryItemCount * iMaestroGalleryItemWidth) + (6 * iMaestroGalleryItemWidth);
		var iNewLeftPosition = maestroGalleryCalcLeftPos();
		$('.maestrogallery_slidearea').css('width', iMaestroGalleryTotalSlideWidth + 'px');
		$('.maestrogallery_slidearea').css('left', '-'+iNewLeftPosition + 'px');
		
		$('.maestrogallery_nav').hover(function() {
				var oOnImg = $(this).find('img.maestrogallery_nav_on');
				oOnImg.stop(true, true).fadeIn(300);
			}, function() {
				var oOnImg = $(this).find('img.maestrogallery_nav_on');
				oOnImg.stop(true, true).fadeOut(300);
			});
					
		$('.maestrogallery_back').click(function(event){
			maestroGalleryMove(event, -1);
		});
		
		$('.maestrogallery_next').click(function(event){
			maestroGalleryMove(event, 1);
		});
	} else {
		$('.maestrogallery_nav').fadeTo(1000, 0.3);
	}

	$('.maestrogallery_item').css('cursor', 'pointer');
	$('.maestrogallery_item').click(function(event) {
		//var oThis = $(this);
		//maestroGalleryClick(event, oThis);
		//alert('seriously, why go through this rewrite just to call the href? come on guys.');
	});	
	$('.maestrogallery_item').hover(function() {
			var oContent = $(this).find('.maestrogallery_content');
			//oContent.stop(true, true).fadeOut(iMaestroGalleryContentFadeSpeedInMS);
			oContent.stop(true, true).slideUp(iMaestroGalleryContentFadeSpeedInMS);
		}, function() {
			var oContent = $(this).find('.maestrogallery_content');
			//oContent.stop(true, true).fadeIn(iMaestroGalleryContentFadeSpeedInMS);
			oContent.stop(true, true).slideDown(iMaestroGalleryContentFadeSpeedInMS);
		});
}







// ### Featured Gallery ###

var iFeaturedGalleryItemCount = 0;
var iFeaturedGalleryCurrentItem = 1;
var iFeaturedGalleryItemWidth = 965; // should match CSS
var iFeaturedGalleryTotalInnerWidth = iFeaturedGalleryItemWidth;
var iFeaturedGalleryNavFadeSpeedInMS = 500;
var iFeaturedGallerySlideDurationInMS = 1200;
var bFeaturedGalleryClickLock = false;
var oFeaturedGalleryTimeOutInstance = null;
var iFeaturedGalleryRotationTimeInMS = 5400;
var iFeaturedGalleryRotationTimeAfterClickInMS = 12000;

function featuredGalleryMove(oEvent, iMoveIncrement) {
	clearTimeout(oFeaturedGalleryTimeOutInstance);
	oFeaturedGalleryTimeOutInstance = setTimeout(featuredGalleryRotate, iFeaturedGalleryRotationTimeAfterClickInMS);
	
	oEvent.preventDefault();
	
	if (bFeaturedGalleryClickLock === false)
	{
		bFeaturedGalleryClickLock = true;
		
		iFeaturedGalleryCurrentItem = iFeaturedGalleryCurrentItem + iMoveIncrement;
		if (iFeaturedGalleryCurrentItem < 1) {
			iFeaturedGalleryCurrentItem = iFeaturedGalleryItemCount;
		}
		else if (iFeaturedGalleryCurrentItem > iFeaturedGalleryItemCount) {
			iFeaturedGalleryCurrentItem = 1;
		}
	
		featuredGalleryAnimate();
	}
}

function featuredGalleryAnimate() {
	var iNewLeftPosition = ((iFeaturedGalleryCurrentItem * iFeaturedGalleryItemWidth) - iFeaturedGalleryItemWidth);
	$('.featuredgallery_inner').animate({ left: '-'+iNewLeftPosition+'px' }, iFeaturedGallerySlideDurationInMS, 'easeOutCubic', function(){
		bFeaturedGalleryClickLock = false;
	});
}

function featuredGalleryClick(oEvent, oThis) {
	oEvent.preventDefault();
	var oLink = oThis.find('.featuredgallery_content a.watchnowlink');
	if (oLink.length !== 0)	{
		document.location = oLink.attr('href');
	}
}	

function featuredGalleryRotate()
{
	iFeaturedGalleryCurrentItem = iFeaturedGalleryCurrentItem + 1;
	if (iFeaturedGalleryCurrentItem < 1) {
		iFeaturedGalleryCurrentItem = iFeaturedGalleryItemCount;
	}
	else if (iFeaturedGalleryCurrentItem > iFeaturedGalleryItemCount) {
		iFeaturedGalleryCurrentItem = 1;
	}

	featuredGalleryAnimate();
	
	featuredGalleryRotateRecurse();
}

	function featuredGalleryRotateRecurse()	{
		oFeaturedGalleryTimeOutInstance = setTimeout(featuredGalleryRotate, iFeaturedGalleryRotationTimeInMS);
	}
	
function initializeFeaturedGallery() {
	iFeaturedGalleryItemCount = parseInt($('.featuredgallery_item').size());
	iFeaturedGalleryTotalInnerWidth = iFeaturedGalleryItemCount * iFeaturedGalleryItemWidth;
	$('.featuredgallery_inner').css('width', iFeaturedGalleryTotalInnerWidth + 'px');
	
	$('.featuredgallery_nav').hover(function() {
			var oOnImg = $(this).find('img.featuredgallery_nav_on');
			oOnImg.stop(true, true).fadeIn(300);
		}, function() {
			var oOnImg = $(this).find('img.featuredgallery_nav_on');
			oOnImg.stop(true, true).fadeOut(300);
		});
	
	$('.featuredgallery_item').css('cursor', 'pointer');
	$('.featuredgallery_item').click(function(event) {
		var oThis = $(this);
		featuredGalleryClick(event, oThis);
	});
	$('.featuredgallery_item').hover(function() {
			var oLink = $(this).find('a.watchnowlink');
			oLink.addClass('on');
		}, function() {
			var oLink = $(this).find('a.watchnowlink');
			oLink.removeClass('on');
		});
	
	$('.featuredgallery').hover(function() {
			$('.featuredgallery_nav').stop(true, true).fadeIn(iFeaturedGalleryNavFadeSpeedInMS);
		}, function() {
			$('.featuredgallery_nav').stop(true, true).fadeOut(iFeaturedGalleryNavFadeSpeedInMS);
		});
	
	$('.featuredgallery_back').click(function(event){
		featuredGalleryMove(event, -1);
	});
	
	$('.featuredgallery_next').click(function(event){
		featuredGalleryMove(event, 1);
	});
	
	featuredGalleryRotateRecurse();	
}


function highFive(scoop_id, total)
{
	$.ajax({
		  type: "POST",
		  url: '/latestScoop/highFive',
		  data: ({scoop_id:scoop_id, total:total}),
		  success: function(data) {
			if(data != "false")
				$('#h_count').html(data);
			else
			{
				alert('Please log in or register to high five');
			}
		  }
	});
}

function indexHighFive(scoop_id, total, i)
{
	$.ajax({
		  type: "POST",
		  url: '/latestScoop/highFive',
		  data: ({scoop_id:scoop_id, total:total}),
		  success: function(data) {
			if(data != "false")
				$('#h_count_' + i).html(data);
			else
			{
				alert('Please log in or register to high five');
			}
		  }
	});
}

function addComment()
{
	var scoop_id = $('#scoop_id').val();
	var comment = $('#comment').val();
	var maestro_id = $('#maestro_id').val();

	if(scoop_id > 0 && comment != '')
	{
		$.ajax({
			  type: "POST",
			  url: '/latestScoop/addComment',
			  data: ({scoop_id:scoop_id, maestro_id:maestro_id, comment:encodeURIComponent(comment)}),
			  success: function(data) {
				 $('#comments_create').slideUp("fast");
				 setTimeout(function(){
					 $('#comments_create').html(data);
				 }, 500);
				 setTimeout(function(){
					 $('#comments_create').slideDown("fast");
				 }, 800);
			  }
		});
	}
}

function comment()
{
	$('html,body').animate({scrollTop: $("#comments_create").offset().top - 30}, 1000);
}




