
// make sure jQuery doesn't conflict with other libraries
jQuery.noConflict();

// global variables
var MPServicesData = new Array();

//--------------------------------------------------------------------------------------
// STUFF TO DO AFTER THE DOCUMENT LOADS (JavaScript event overrides, etc.)
//--------------------------------------------------------------------------------------

// stuff to do when the page loads
jQuery(document).ready(function(){
	// temporarily hide the video player(s)
	jQuery('#vPlayer1').empty();
	// load image sets
	MPLoadImgSets();
	// update services content for tab-like browsing
	jQuery('.services-header').addClass('hiding');
	jQuery('#servicesNav').addClass('services-left-top');
	jQuery('.services-images').addClass('services-right');
	jQuery('.services-text').addClass('services-left-btm');
	MPLoadServicesContent(jQuery('#servicesPressLink'));
	jQuery('.services-link a', jQuery('#servicesNav')).each(function(){
		jQuery(this).attr('href', 'javascript:void(0);');
		jQuery(this).bind('click', function(e){
			e.preventDefault();
			MPLoadServicesContent(this);
		});
	});
	// add scrolling effect to anchor links, using panels
	var $panels = jQuery('#slider .scrollContainer > div');
	var $container = jQuery('#slider .scrollContainer');
	var horizontal = true;
	if (horizontal) {
	  $panels.css({
	    'float' : 'left',
	    'position' : 'relative' // IE fix to ensure overflow is hidden
	  });
	  $container.css('width', $panels[0].offsetWidth * $panels.length);
	}
	var $scroll = jQuery('#slider .scroll').css('overflow', 'hidden');
	function selectNav() {
	  jQuery('#vPlayer1').empty();
	  jQuery(this)
	    .parents('ul:first')
	      .find('a')
	        .removeClass('selected')
	      .end()
	    .end()
	    .addClass('selected');
	  jQuery('#topHomeLink').removeClass('hidden');
	  if (jQuery(this).attr('href') == '#home') jQuery('#topHomeLink').addClass('hidden');
	  if (jQuery(this).attr('href') == '#videos') setTimeout('MPShowVPlayer1()', 300);
	}
	// jQuery('#slider .navigation').find('a').click(selectNav);
	jQuery('#slider .navigation').find('a').each(function(){
		if (jQuery(this).attr('id') != 'topPriceLink') jQuery(this).bind('click', selectNav);
	});
	function trigger(data) {
	  var el = jQuery('#slider .navigation').find('a[href$="' + data.id + '"]').get(0);
	  selectNav.call(el);
	}
	if (window.location.hash) trigger({ id : window.location.hash.substr(1) });
		else jQuery('#topHomeLink a:first').click();
		/* else jQuery('ul.navigation a:first').click(); */
	var offset = parseInt((horizontal ? 
	  $container.css('paddingTop') : 
	  $container.css('paddingLeft')) 
	  || 0) * -1;
	var scrollOptions = {
	  target: $scroll,
	  items: $panels,
	  navigation: 'a.scrollme',
	  axis: 'xy',
	  onAfter: trigger,
	  offset: offset,
	  duration: 500,
	  easing: 'swing'
	};
	jQuery('#slider').serialScroll(scrollOptions);
	jQuery.localScroll(scrollOptions);
	scrollOptions.duration = 1;
	jQuery.localScroll.hash(scrollOptions);
	// attach people browser link events
	jQuery('#peopleLinks li a').each(function(){
		jQuery(this).bind('mouseover', function(e){
			// reset all links (clear active class)
			MPResetPeople();
			// set current to active
			var thisID = jQuery(this).parent().attr('id');
			MPSetPeopleActive(thisID);
		});
	});
	// attach people browser image events
	jQuery('#peoplePhotos img').each(function(){
		var photoID = jQuery(this).parents('div:first').attr('id');
		if (photoID != 'lacy3Photo')
		{
			jQuery(this).bind('mouseover', function(e){
				var photoID = jQuery(this).parents('div:first').attr('id');
				var linkID = MPGetPeopleLinkID(photoID);
				// reset all links (clear active class)
				MPResetPeople();
				// set current to active
				MPSetPeopleActive(linkID);
			});
		}
	});
	// attach people browser map events
	jQuery('area', jQuery('#lacyMap')).each(function(){
		jQuery(this).bind('mouseover', function(e){
			var linkID = MPGetPeopleLinkIDMap(jQuery(this).attr('id'));
			// reset all links (clear active class)
			MPResetPeople();
			// set current to active
			MPSetPeopleActive(linkID);
		});
	});
	// preload images
	var plList = [
		'resources/images/people/carrie_stone_active.jpg',
		'resources/images/people/gary_james_active.jpg',
		'resources/images/people/justin_hiott_active.jpg',
		'resources/images/people/kim_hiott_active.jpg',
		'resources/images/people/lacy_3_active_bernie.jpg',
		'resources/images/people/lacy_3_active_jack.jpg',
		'resources/images/people/lacy_3_active_john.jpg',
		'resources/images/people/randy_miller_active.jpg',
		'resources/images/people/scott_carlson_active.jpg',
		'resources/images/people/molly_mercer_active.jpg',
		'resources/images/people/brandy_west_active.jpg',
		'resources/images/people/rob_beckom_active.jpg'
		];
	var plImgList = new Array();
	for (var x=0; x<plList.length; x++)
	{
		plImgList[x] = new Image();
		jQuery(plImgList[x]).attr('src', plList[x]);
	}
	jQuery('a', jQuery('#sampleImagesNav')).each(function(){
		var thisImg = new Image();
		jQuery(thisImg).attr('src', jQuery(this).attr('href'));
	});
	jQuery('a', jQuery('#pressImagesLinks')).each(function(){
		var thisImg = new Image();
		jQuery(thisImg).attr('src', jQuery(this).attr('href'));
	});
	jQuery('a', jQuery('#binderyImagesLinks')).each(function(){
		var thisImg = new Image();
		jQuery(thisImg).attr('src', jQuery(this).attr('href'));
	});
	// handle auto-play of slideshows
	jQuery('.topnav-samples a:first').bind('click', function(e){
		MPImgSets['sampleImages'].reset();
		MPImgSets['sampleImages'].play();
	});
	// handle auto-play of slideshows
	jQuery('.topnav-services a:first').bind('click', function(e){
		MPLoadServicesContent(jQuery('#servicesPressLink'));
	});
});

//--------------------------------------------------------------------------------------
// CUSTOM "SERVICES" BROWSER FUNCTIONS (implements jQuery)
//--------------------------------------------------------------------------------------

// load image browsers
function MPLoadImgSets()
{
	// build slideshow image sets
	iKey = 'pressImages';
	MPImgSets[iKey] = new MPImgSet(iKey, iKey+'Links', iKey+'Stage');
	MPImgSets[iKey].captTemplate = '<div class="services-image-caption"><p>[caption]</p></div>';
	MPImgSets[iKey].load();
	iKey = 'binderyImages';
	MPImgSets[iKey] = new MPImgSet(iKey, iKey+'Links', iKey+'Stage');
	MPImgSets[iKey].captTemplate = '<div class="services-image-caption"><p>[caption]</p></div>';
	MPImgSets[iKey].load();
	// handle sample image links (change stage image)
	iKey = 'sampleImages';
	MPImgSets[iKey] = new MPImgSet(iKey, iKey+'Nav', iKey+'Stage');
	MPImgSets[iKey].layoutTemplate = "[caption]\n[img]\n[playpause]";
	MPImgSets[iKey].load();
}

// get services block ID from link ID
function MPGetServicesBlockID(linkID)
{
	var results = linkID.replace('Link', '');
	return results;
}

// update services content based on clicked (or default) link
function MPLoadServicesContent(linkObject)
{
	jQuery('.services-link', jQuery('#servicesNav')).removeClass('active');
	jQuery(linkObject).parents('div:first').addClass('active');
	jQuery('.services-section').removeClass('hiding').addClass('hiding');
	var divID = MPGetServicesBlockID(jQuery(linkObject).attr('id'));
	jQuery('#'+divID).removeClass('hiding');
	///*
	if (divID == 'servicesPress')
	{
		MPImgSets['pressImages'].reset();
		MPImgSets['pressImages'].play();
	}
	else if (divID == 'servicesBindery')
	{
		MPImgSets['binderyImages'].reset();
		MPImgSets['binderyImages'].play();
	}
	//*/
}

//--------------------------------------------------------------------------------------
// CUSTOM "PEOPLE" BROWSER FUNCTIONS (implements jQuery)
//--------------------------------------------------------------------------------------

// set active version of current people photo ID
function MPSetPeopleActive(linkID)
{
	var photoID = MPGetPeoplePhotoID(linkID);
	if (photoID && linkID)
	{
		// set new image source
		var thisImg = jQuery('#'+photoID+' img:first');
		if (linkID == 'jackLacyLink') var newSrc = 'lacy_3_active_jack.jpg';
		else if (linkID == 'johnLacyLink') var newSrc = 'lacy_3_active_john.jpg';
		else if (linkID == 'bernieLacyLink') var newSrc = 'lacy_3_active_bernie.jpg';
		else
		{
			var thisImg = jQuery('#'+photoID+' img:first');
			var oldSrc = jQuery(thisImg).attr('src');
			var pData = MPGetImgFileData(oldSrc);
			var newSrc = pData.basename+'_active.'+pData.extension;
		}
		newSrc = 'resources/images/people/'+newSrc;
		jQuery(thisImg).attr('src', newSrc);
		// set new photo div class
		var photoDiv = jQuery('#'+photoID);
		jQuery(photoDiv).attr('class', 'active');
		// add email link to photo div
		var thisLI = jQuery('#'+linkID);
		var thisLinkURL = jQuery('a:first', thisLI).attr('href');
		jQuery(photoDiv).append('<p class="people-email-link"><a href="'+thisLinkURL+'">EMAIL</a></p>');
		// set new link class
		jQuery(thisLI).attr('class', 'active');
	}
}

// set inactive version of current people photo ID
function MPSetPeopleInactive(thisImg)
{
	var imgDir = 'resources/images/people/';
	var tdID = jQuery(thisImg).parent().attr('id');
	if (tdID == 'lacy3Photo')
	{
		var newSrc = imgDir+'lacy_3.jpg';
	} else {
		var oldSrc = jQuery(thisImg).attr('src');
		var newSrc = oldSrc.replace('_active', '');
	}
	jQuery(thisImg).attr('src', newSrc);
}

// function to clear "active" class assignments from people links and photos
function MPResetPeople()
{
	// reset link li classes
	jQuery('#peopleLinks li').removeClass('active');
	// reset photo td classes
	jQuery('#peoplePhotos td div').removeClass('active');
	// reset photo sources (remove _active)
	jQuery('#peoplePhotos td img').each(function(){
		MPSetPeopleInactive(this);
	});
	// hide all email links
	jQuery('#peoplePhotos td div p').remove();
}

// get matching photo ID for people link ID
function MPGetPeoplePhotoID(linkID)
{
	if (linkID == 'jackLacyLink' || linkID == 'johnLacyLink' || linkID == 'bernieLacyLink')
		var results = 'lacy3Photo';
		else var results = linkID.replace('Link', 'Photo');
	return results;
}

// get matching link ID for people photo ID
function MPGetPeopleLinkID(photoID)
{
	var results = photoID.replace('Photo', 'Link');
	return results;
}

// get matching link ID for people from map ID
function MPGetPeopleLinkIDMap(mapID)
{
	var results = mapID.replace('Map', 'Link');
	return results;
}

// get image name from full path
function MPGetImgFileData(path)
{
	if (typeof path != 'undefined')
	{
		var dirArray = path.split('/');
		var currentSrc = dirArray[dirArray.length - 1];
		var srcArray = currentSrc.split('.');
		var thisExtension = srcArray[srcArray.length - 1];
		var x = srcArray.slice(0, srcArray.length - 1);
		var thisBasename = x.join('.');
		return { filename: currentSrc, basename: thisBasename, extension: thisExtension }
	} else return false;
}

//--------------------------------------------------------------------------------------
// CUSTOM FUNCTION to MAKE VIDEO PLAYER VISIBLE (implements jQuery)
//--------------------------------------------------------------------------------------

function MPShowVPlayer1()
{
	var content = '<iframe src="resources/videos/LithoWeb/index.html" width="364" height="246" frameborder="0" scrolling="no" name="LithoVF"></iframe>';
	jQuery('#vPlayer1').html(content);
}