/***
 * global.js
 * 
 * stuff what happens everywhere
 *
 **/

$(function(){

  // set target to _blank for all external links
  $('a[rel="external"]').each(function(index, that){
    $(this).attr('target', '_blank');
  });

  /****** mailing list ********/

  // scroll for top link
  $('#top-social .mail').click(function(){
    $(window).stop().scrollTo('#email-holder', 800);
    $('#mailing-list-name').focus();

    $('#mailing-list-arrow').show().delay(8000).fadeOut('slow');

    return false;
  });

  // field nasty for clicking

  $('#mailing-list-name, #mailing-list-email').
    //	click(function(){
    focus(function(){
      var name = $('#mailing-list-name').attr('value');
      var email = $('#mailing-list-email').attr('value');
      if(name == 'name' && email == 'e-mail address'){
	$('#mailing-list-name, #mailing-list-email').attr('value', '');
      }
    }).blur(function(){

      var name = $('#mailing-list-name').attr('value');
      var email = $('#mailing-list-email').attr('value');
      if(name == '' && email == ''){
	$('#mailing-list-name').attr('value', 'name');
	$('#mailing-list-email').attr('value', 'e-mail address');
      }
    });


  // jquery.cycle plugin
  // should work for all of our banners
  slideshows = $(".slideshow");
  slideshows.each( function(){
    slideshow = $(this);
    anext = slideshow.parent().find("a.nextarrow");
    aprev = slideshow.parent().find("a.prevarrow");
    slideshow.cycle({
      fx : 'fade',
	timeout : 5000,
	speed : 1000,
	sync : true,
	pause : 1,
	next : anext,
	prev : aprev
	});
  });

  // Splash Banners	
  if($("#splash-container").length) {
    var gal = $("#photo-gallery.splash").jqm({
			modal: false,
			trigger : '.tehModal',
			overlay : 90,
			onShow : openGallerySplash
			}).jqmShow();

			$("#photo-gallery .left a, #photo-gallery .right a").live('click', function(){
				openGallerySplash(this);
				return false;
			});

			$("#photo-gallery .close").live('click', function(){
				$("#photo-gallery").jqmHide();
			});

			$("#photo-gallery .close-image").live('click', function(){
				$("#photo-gallery").jqmHide();
			});
		}

		// unfocus links
		$("#main-nav li a").live( 'click', function(){
			$(this).blur();
			return true;
		});

		// photo galleries
		if( $("#photo-gallery").length > 0 && 
		!$("#photo-gallery").hasClass("splash") &&
		!$("#photo-gallery").hasClass("slide") ){

			$("#photo-gallery").jqm({ 
				modal: false,
				trigger : '.tehModal',
				overlay : 90,
				onShow : openGallery
			});

			$("#photo-gallery .left a, #photo-gallery .right a").live('click', function(){
				updateGallery(this);
				return false;
			});

			$("#photo-gallery .close").live('click', function(){
				$("#photo-gallery").jqmHide();
			});
		}
});


function openGallerySplash(hash){
		hash.t = $(".small_thumb")[0];

		img_id = $(hash.t).attr("data-id");
		modal = $("#photo-gallery");
		modal.html( $("#splash-container .modal-content[data-id=" + img_id + "]").html() );

		hash.w.fadeIn("slow");
}

	function openGallery(hash){
		updateGallery(hash.t);
		//  hash.w.css('top', Number($('html').scrollTop() - 523) + 'px');
		if( $.browser.webkit )
		hash.w.css('top', Number($('body').scrollTop()) + 'px');
		else
		hash.w.css('top', Number($('html').scrollTop()) + 'px');
		hash.w.fadeIn("slow");
	}

	function updateGallery(link){
		img_id = $(link).attr("data-id");
		modal = $("#photo-gallery");
		modal.html( $("div.modal-content[data-id=" + img_id + "]").html() );
	}



/** Modal Slideshows **/

$(function(){
	// photo gallery slideshows
	if( $("#photo-gallery.slide").length > 0 ){

		$("#photo-gallery.slide").jqm({ 
			modal: false,
			trigger : '.tehModal',
			overlay : 90,
			onShow : function( hash ){
				if( $.browser.webkit ){
					hash.w.css('top', Number($('body').scrollTop()) + 'px');
				}else{
					hash.w.css('top', Number($('html').scrollTop()) + 'px');
				}
				hash.w.fadeIn("slow");
			}
		});

		$("#photo-gallery.slide .close").live('click', function(){
			$("#photo-gallery.slide").jqmHide();
		});

		slideshow = $("#photo-gallery.slide");

		aprev = slideshow.find(".left a");
		anext = slideshow.find(".right a");
		slideshow.cycle({
			fx: 'fade',
			timeout : 3000,
			speed : 1000,
			sync : true,
			pause : 1,
			next : anext,
			prev : aprev,
			pager : '#photo-gallery-nav',
			pagerAnchorBuilder: function(idx, slide) {
				return '#photo-gallery-nav a:eq(' + idx + ')';
			}
		});
	}
});

/*
	new modal code
*/
var popupManager = {
	popupDiv: null,
	backgroundDiv: null,

	slideShowTimer: null,
	slideShowURLs: null,
	slideShowIndex: 0,
	slideShowInterval: 6000,
	
	popup: function(file, callBack) {
		if(popupManager.popupDiv) {
			//> remove current popup content
			popupManager.removePopup();
		}
		
		popupManager.setBackground();
		
		$.ajax({
			url: file,
			type: 'get',
			success: function(response) { 
				if(!response || !response.length) {
					return;
				}
				
				var viewportHeight = $(window).height();
				var scrollTop = $(window).scrollTop();
				
				popupManager.popupDiv = document.createElement('div');
				popupManager.popupDiv.id = "popup-container";
				popupManager.popupDiv.style.top = (scrollTop + 100) + "px"
				popupManager.popupDiv.innerHTML = response;
				document.body.appendChild(popupManager.popupDiv);
				$(popupManager.popupDiv).fadeIn(300);
				
				if($("#slideshow-controls")) {
					if(popupManager.slideShowTimer) {
						$("#slideshow-controls").addClass("playing");
					} else {
						$("#slideshow-controls").removeClass("playing");
					}
				}
				
				if(callBack) {
					callBack();
				}
			}
		});
	},
	
	/*
		Remove background and popup
	*/
	closePopup: function() {
		if(popupManager.popupDiv) {
			popupManager.slideShowIndex = 0;
			popupManager.stopSlideShow();
			popupManager.removePopup();
			popupManager.restoreBackground();
		}
	},
	
	/*
		Remove popup (leave background intact)
	*/
	removePopup: function() {
		$(popupManager.popupDiv).fadeOut(300, function() {
			popupManager.popupDiv.parentNode.removeChild(popupManager.popupDiv);
			popupManager.popupDiv = null;
		});
	},
	
	setBackground: function() {
		if(!popupManager.backgroundDiv) {
			popupManager.backgroundDiv = document.createElement('div');
			popupManager.backgroundDiv.className = "background";
			popupManager.backgroundDiv.onclick = popupManager.closePopup;
			
			document.body.appendChild(popupManager.backgroundDiv);
			$(popupManager.backgroundDiv).fadeIn(300);
		}
	},
	
	restoreBackground: function() {
		if(popupManager.backgroundDiv) {
			$(popupManager.backgroundDiv).fadeOut(300, function() {
				popupManager.backgroundDiv.parentNode.removeChild(popupManager.backgroundDiv);
				popupManager.backgroundDiv = null;
			});
		}
	},
	
	startSlideshow: function(aryUrls, startIndex, autoAdvance) {
		popupManager.slideShowURLs = aryUrls || popupManager.slideShowURLs;
		popupManager.slideShowIndex = parseInt(startIndex || popupManager.slideShowIndex);
		
		if(autoAdvance == undefined || autoAdvance == true) {
			if(popupManager.slideShowURLs && popupManager.slideShowURLs.length > 1) {
				popupManager.slideShowTimer = setInterval(function() {
					popupManager.incrementIndex();
					popupManager.popup(popupManager.slideShowURLs[popupManager.slideShowIndex]);
				}, popupManager.slideShowInterval);
				
				if($("#slideshow-controls")) {
					$("#slideshow-controls").css("display", "block");
					$("#slideshow-controls").addClass("playing");
				}
			} else {
				if($("#slideshow-controls")) {
					$("#slideshow-controls").css("display", "none");
					$("#slideshow-controls").removeClass("playing");
				}
			}
		}
	},
	
	stopSlideShow: function() {
		clearInterval(popupManager.slideShowTimer);
		popupManager.slideShowTimer = null;
		$("#slideshow-controls").removeClass("playing");
	},
	
	pauseSlideShow: function(forcePause) {
		if(forcePause || popupManager.slideShowTimer) {
			popupManager.stopSlideShow();
		} else {
			popupManager.startSlideshow(null, null, true);
		}
	},
	
	incrementIndex: function() {
		popupManager.slideShowIndex = (popupManager.slideShowIndex + 1) % popupManager.slideShowURLs.length;
	},
	
	decrementIndex: function() {
		popupManager.slideShowIndex = (popupManager.slideShowIndex - 1) % popupManager.slideShowURLs.length;
	}
};

function validateNewsletterForm() {
	var error = false;
	var errorMessage = "";
	
	$("#frmNewsletter input[type='text']").removeClass("error");
	
	if(!$("#email").attr("value")) {
		error = true;
		errorMessage += "<p>Email address is required.</p>";
		$("#email").addClass("error");
	}
	if(!$("#first_name").attr("value")) {
		error = true;
		errorMessage += "<p>First name is required.</p>";
		$("#first_name").addClass("error");
	}
	if(!$("#last_name").attr("value")) {
		error = true;
		errorMessage += "<p>Last name is required.</p>";
		$("#last_name").addClass("error");
	}
	
	$("#errorMessage-newsletter").css("display", "none");
	$("#errorMessage-newsletter").html(errorMessage);
	$("#errorMessage-newsletter").fadeIn();
	
	return !error;
}

function validateNewsletterFormSms() {
	var error = false;
	var errorMessage = "";
	
	$("#frmNewsletter-sms input[type='text']").removeClass("error");
	
	if(!$("#area_code").attr("value")) {
		error = true;
		errorMessage += "<p>Area code is required.</p>";
		$("#area_code").addClass("error");
	}
	if(!$("#prefix").attr("value")) {
		error = true;
		errorMessage += "<p>Prefix name is required.</p>";
		$("#prefix").addClass("error");
	}
	if(!$("#suffix").attr("value")) {
		error = true;
		errorMessage += "<p>Suffix is required.</p>";
		$("#suffix").addClass("error");
	}
	
	$("#errorMessage-sms").css("display", "none");
	$("#errorMessage-sms").html(errorMessage);
	$("#errorMessage-sms").fadeIn();
	
	return !error;
}


//});

