// Globally Accessed Variables -------------------------
var slidePosition = 1;
var t;
var eventSlidePosition = 1;



// Functions -------------------------------------------

function clearEmail(phrase) {
	if ($('#headeremail').val()==phrase) $('#headeremail').val('');
}

function clearSearch(phrase) {
	if ($('#headersearch').val()==phrase) $('#headersearch').val('');
}

function clearInput(input,phrase) {
	if ($('#'+input).val()==phrase) $('#'+input).val('');
}

function validate_email(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
      return false;
   }
   else return true;
}

function eventPopRSVP(email) {
	if (!email || email=='Enter email address to RSVP') alert("Please enter an email address before trying to RSVP.");
	else if (!validate_email(email)) alert("Please enter a valid email address.");
	else {
		$('#rsvpemail-form').val(email);
		popupToggle('poprsvp');
	}
}

function mailinglistPop(email) {
	if (!email || email=='Enter email address!') alert("Please enter an email address before trying to sign up.");
	else if (!validate_email(email)) alert("Please enter a valid email address.");
	else {
		$('#mlemail-form').val(email);
		popupToggle('popmailinglist');
	}
}

function mailinglistSubmit() {
	$.ajax({
		type: "POST",
		url: "/includes/mailinglist-save.php",
		data: $("#mailinglistform").serialize(),
		dataType: "json",
		success: function(data) {
			if (data.success==1) {
				$('#popmailinglistcontent').html(data.html);
			}
			else {
				$('#ml-error').html(data.html);
				$('#ml-error').slideDown();
			}
		}
	});
}

function eventRSVP() {
	$.ajax({
		type: "POST",
		url: "/includes/event-rsvp-save.php",
		data: $("#rsvpform").serialize(),
		dataType: "json",
		success: function(data) {
			if (data.success==1) {
				$('#poprsvpcontent').html(data.html);
			}
			else {
				$('#rsvp-error').html(data.html);
				$('#rsvp-error').slideDown();
			}
		}
	});
}

function referralSubmit() {
	$.ajax({
		type: "POST",
		url: "/includes/referral-save.php",
		data: $("#referralform").serialize(),
		dataType: "json",
		success: function(data) {
			if (data.success==1) {
				$('#referral-content').html(data.html);
			}
			else {
				$('#referral-error').html(data.html);
				$('#referral-error').slideDown();
			}
		}
	});
}

function eventRSVPAddGuests(i) {
	$('#rsvpguestdiv').html('');
	if (i>0) {
		for(g=1;g<=i;g++) {
			var html="<div class='guestdiv'><div class='formfield'><label>Guest Name:</label><input type='text' name='rsvp_guest["+g+"][name]' class='textinput' /></div><div class='formfield'><label>Guest Email Address:</label><input type='text' name='rsvp_guest["+g+"][email]' class='textinput' /></div></div>";
			$('#rsvpguestdiv').append(html);
		}
	}
}

function featureSlideInit(effectDuration,rotateDuration) {
	t=setTimeout("featuredAutoRotate("+effectDuration+","+rotateDuration+")",rotateDuration);
	var slideCount = $("#featuredslides li").size();
}

function featuredAutoRotate(effectDuration,rotateDuration)
{
	featuredSlide('next',effectDuration);
	t=setTimeout("featuredAutoRotate("+effectDuration+","+rotateDuration+")",rotateDuration);
}

function featuredSlide(direction,effectDuration) {
	clearTimeout(t);
	var targetSlide = '#featureslide_';
	var currentSlide = '#featureslide_'+slidePosition;
	var slideCount = $("#featuredslides li").size();
	if (direction=='previous'){
		if(slidePosition==1) targetSlideID = slideCount;
		else targetSlideID = slidePosition - 1;
	} else if (direction=='next') {
		if(slidePosition==slideCount) targetSlideID = 1;
		else targetSlideID = slidePosition + 1;
	}
	slidePosition = targetSlideID;
	targetSlide = targetSlide + targetSlideID;
	$("#featuredslides li").hide();
	$(currentSlide).show();
	$(targetSlide).show();
	$(currentSlide).css({zIndex:800,display:'block'});
	$(targetSlide).css({zIndex:1,display:'block'});
	$(currentSlide).fadeOut(effectDuration);
}

/*
function featureSlideInit() {
	t=setTimeout("featuredAutoRotate()",5000);
	var slideCount = $("#featuredslides li").size();
	var slideWidth = $("#featuredslides li").width();
	var firstSlide = $("#featuredslides").children("li").first();
	var lastSlide = $("#featuredslides").children("li").last();
	$(firstSlide).clone().appendTo("#featuredslides");
	$(lastSlide).clone().prependTo("#featuredslides");
	$("#featuredslides").width(slideWidth*(slideCount+2));
	$("#featuredslides").css({ marginLeft:slideWidth*-1 });
}

function featuredSlide(direction) {
	clearTimeout(t);
	var slideCount = $("#featuredslides li").size() - 2;
	var slideWidth = $("#featuredslides li").width();
	if (direction=='previous'){
		if(slidePosition==1){
			slidePosition--;
			$("#featuredslides").animate({ marginLeft: slideWidth*(slidePosition)*-1 }, 500,
				function() {
					$("#featuredslides").css({ marginLeft:slideWidth*slideCount*-1});
					slidePosition = slideCount;
				}
			);			
		} else {
			slidePosition--;
			$("#featuredslides").animate({ marginLeft: slideWidth*(slidePosition)*-1 }, 500 );
		}
	} else if (direction=='next') {
		if(slidePosition==slideCount) {
			slidePosition++;
			$("#featuredslides").animate({ marginLeft: slideWidth*(slidePosition)*-1 }, 500,
				function() {
					$("#featuredslides").css({ marginLeft:slideWidth*-1});
					slidePosition = 1;
				}
			);	
		} else {
			slidePosition++;
			$("#featuredslides").animate({ marginLeft: slideWidth*(slidePosition)*-1 }, 500 );
		}
	}
}
*/

function pressSlideInit() {
	var slideCount = $("#pressthumbs .page").size();
	var slideWidth = $("#pressthumbs .page").width();
	$("#thumbs").width(slideWidth*slideCount);
	
	//$('#pressthumbs .thumbs .page li').mouseenter(function(){ thumbOverlayShow(this); });
	//$('#pressthumbs .thumbs .page li').mouseleave(function(){ thumbOverlayHide(this); });
/*
	if(slidePosition!=1){
		$("#thumbs").css({ marginLeft: slideWidth*(slidePosition-1)*-1 });
	}
*/
}

function pressSlide(direction) {
	var slideCount = $("#pressthumbs .page").size();
	var slideWidth = $("#pressthumbs .page").width();
	if (direction=='previous'){
		if(slidePosition==1) slidePosition = slideCount;
		else slidePosition--;
		$("#thumbs").animate({ marginLeft: slideWidth*(slidePosition-1)*-1 }, 500, function(){
			var titleID = '#pressdate-'+slidePosition
			$('.pressdate').hide();
			$(titleID).show();
		} );
	} else if (direction=='next') {
		if(slidePosition==slideCount) slidePosition = 1;
		else slidePosition++;
		$("#thumbs").animate({ marginLeft: slideWidth*(slidePosition-1)*-1 }, 500, function() {
			var titleID = '#pressdate-'+slidePosition
			$('.pressdate').hide();
			$(titleID).show();
		} );
	}
}

function loadPressThumbs(publication) {
	if (publication) {
		$.ajax({
			type: "GET",
			url: "/includes/press-thumbs.php",
			data: ({pub : publication}),
			dataType: "html",
			success: function(data) {
				$('#pressthumbs').html(data);
			}
		});
		loadPressExpanded('',publication);
	}
	else {
		$.ajax({
			type: "GET",
			url: "/includes/press-thumbs.php",
			dataType: "html",
			success: function(data) {
				$('#pressthumbs').html(data);
			}
		});
	}
}

function loadPressExpanded(id,publication) {
	$.ajax({
		type: "GET",
		url: "/includes/press-expanded.php",
		data: ({id:id, pub:publication}),
		dataType: "html",
		success: function(data) {
			$('#expandedthumb').html(data);
			pressSlideInit();
			pressExpandedInit();
		}
	});
}

function pressExpandedInit() {
/*
	$('#expandedthumb .image').mouseenter(function(){
		$('#zoombutton').fadeIn();
	});
	$('#expandedthumb .image').mouseleave(function(){
		$('#zoombutton').fadeOut();
	});
*/
	$('.lightbox').lightBox({
		imageLoading: '/images/lightbox-ico-loading.gif',
		imageBtnClose: '/images/lightbox-btn-close.gif',
		imageBtnPrev: '/images/lightbox-btn-prev.gif',
		imageBtnNext: '/images/lightbox-btn-next.gif',
		imageBlank: '/images/lightbox-blank.gif'
	});
}

function customerserviceLocation(id) {
	$.ajax({
		type: "GET",
		url: "/includes/customerservice-map.php",
		data: ({id:id}),
		dataType: "html",
		success: function(data) {
			$('#see-us').html(data);
		}
	});
}

function loadEventPhoto(event_id,photo_id,go) {
	var currentPhotoHeight = $('#eventphotolarge img').height();
	if(!currentPhotoHeight) var currentPhotoHeight = 360;
	$('#eventphotolarge').height(currentPhotoHeight);
	$.ajax({
		type: "GET",
		url: "/includes/event-photo.php",
		data: ({id:event_id,photo:photo_id,go:go}),
		dataType: "html",
		success: function(data) {
			$('#eventphotolarge').html(data);
			if($('#eventphotolarge embed') || $('#eventphotolarge object')){
				$('#eventphotolarge').animate({ height: 360 }, 500 );
			}
			$('#eventphotolarge img').load(function(){
				var currentPhotoHeight = $('#eventphotolarge img').height();
				$('#eventphotolarge').animate({ height: currentPhotoHeight }, 500 );
			});
		}
	});
}

function eventSlideInit() {
	var slideCount = $("#eventphotothumbs .page").size();
	var slideWidth = $("#eventphotothumbs .page").width();
	$("#eventphotothumbs .thumbs").width(slideWidth*slideCount);
	
	slideCount = $("#eventthumbs li").size();
	slideWidth = $("#eventthumbs li").width() + 16;
	$("#eventthumbs .thumbs").width(slideWidth*slideCount);
}

function eventSlide(direction) {
	var slidesPerPage = 6;
	var slideCount = Math.ceil($("#eventthumbs .thumbs li").size() / slidesPerPage);
	if(slideCount > 1) {
		var slideWidth = $("#eventthumbs li").width() + 16;
		if (direction=='previous'){
			if(eventSlidePosition==1) eventSlidePosition = slideCount;
			else eventSlidePosition--;
			$("#eventthumbs .thumbs").animate({ marginLeft: slideWidth*(eventSlidePosition-1)*-1*slidesPerPage }, 500 );
		} else if (direction=='next') {
			if(eventSlidePosition==slideCount) eventSlidePosition = 1;
			else eventSlidePosition++;
			$("#eventthumbs .thumbs").animate({ marginLeft: slideWidth*(eventSlidePosition-1)*-1*slidesPerPage }, 500 );
		}
	}
}

function gotoEventSlide(slideNumber) {
	var slidesPerPage = 6;
	var slideCount = Math.ceil($("#eventthumbs .thumbs li").size() / slidesPerPage);
	var slideTarget = Math.ceil(slideNumber / slidesPerPage);
	if(slideTarget > 1) {
		var slideWidth = $("#eventthumbs li").width() + 16;
		eventSlidePosition = slideTarget;
		$("#eventthumbs .thumbs").css({ marginLeft: slideWidth*(eventSlidePosition-1)*-1*slidesPerPage });
	}
}

function eventPhotoSlide(direction) {
	var slideCount = $("#eventphotothumbs .page").size();
	var slideWidth = $("#eventphotothumbs .page").width();
	if (direction=='previous'){
		if(slidePosition==1) slidePosition = slideCount;
		else slidePosition--;
		$("#eventphotothumbs .thumbs").animate({ marginLeft: slideWidth*(slidePosition-1)*-1 }, 500 );
	} else if (direction=='next') {
		if(slidePosition==slideCount) slidePosition = 1;
		else slidePosition++;
		$("#eventphotothumbs .thumbs").animate({ marginLeft: slideWidth*(slidePosition-1)*-1 }, 500 );
	}
}

function eventsInit() {
	//$('#eventthumbs .thumbs li').mouseenter(function(){ thumbOverlayShow(this); });
	//$('#eventthumbs .thumbs li').mouseleave(function(){ thumbOverlayHide(this); });
}

function thumbOverlayShow(thumb){
	$(thumb).children('.overlay').fadeIn(500);
}

function thumbOverlayHide(thumb){
	$(thumb).children('.overlay').fadeOut(500);
}

function referralsToggle(){
	var windowHeight = $(window).height();
	var windowScroll = $(window).scrollTop();
	var referralsHeight = $("#referrals").height();
	var referralsMargin = ((windowHeight - referralsHeight) / 2) + windowScroll;
	$("#referrals").css({marginTop:referralsMargin});
	$("#referrals").fadeToggle(500);
}

function popupToggle(popupID){
	popupID = '#'+popupID;
	if(!$(popupID).is(':visible')){
		var windowHeight = $(window).height();
		var windowScroll = $(window).scrollTop();
		var referralsHeight = $(popupID).height();
		var referralsMargin = ((windowHeight - referralsHeight) / 2) + windowScroll;
		$(popupID).css({marginTop:referralsMargin});
	}
	$(popupID).fadeToggle(500);
}

function scrollbarInit() {
	$('.scroll-pane').jScrollPane(
		{
			verticalDragMinHeight: 18,
			verticalDragMaxHeight: 18
		}
	);
}

function collectionMenuInit() {
	var paneHeight = $('.scroll-pane').height();
	var itemHeight = $('#collectionmenu li').height();
	var catHeight = $('#categorymenu li').length * itemHeight;
	var desHeight = $('#designermenu li').length * itemHeight;
	if(catHeight < paneHeight) $('#categorymenu .scroll-pane').height(catHeight);
	if(desHeight < paneHeight) $('#designermenu .scroll-pane').height(desHeight);
	
	scrollbarInit();
}

function collectionMenu(target) {
	var targetID;
	var hideID;
	if(target=='categories') {
		targetID = '#categorymenu';
		hideID = '#designermenu';
	} else {
		hideID = '#categorymenu';
		targetID = '#designermenu';
	}
	if (jQuery.browser.msie && (parseInt(jQuery.browser.version)==6 || parseInt(jQuery.browser.version)==7)) {
		$(targetID).show();
		$(hideID).hide();
	} else {
		$(targetID).slideDown();
		$(hideID).slideUp();
	}
}

function onYouTubePlayerReady() {
	//ytplayer = document.getElementById(playerId);
	//ytplayer.addEventListener("onStateChange", "playerStateChange");
	$('.ytplayer').each(function(index,element){
		element.addEventListener("onStateChange", "playerStateChange");
	});
}

function playerStateChange(newState){
	if (newState == 1) clearTimeout(t);
}
