$(document).ready(function() {


/* ************************ CAPTION POSITION RESET ************************* */

var captionSpan = $('span.current');

function stray () {
	if (captionSpan)
		$('span.current').attr('top','500px');
}

stray();

/* ************************ CAPTION FADE ************************* */
var captionBlock = $('div.caption-container');
var vidCaptions = $('.gall p + p');

// onload caption fade functions

function onLoad_hidePhotoCaption() {
	if('div.caption-container:visible')
		$(captionBlock).delay(5000).fadeOut();
}
function onLoad_hideVideoCaption() {
	if('.gall p + p')
		$(vidCaptions).delay(5000).fadeOut();
}

// runtime

onLoad_hidePhotoCaption();
onLoad_hideVideoCaption();

// event triggered fade functions

var timerPhoto;
var timerVideo;

function photoCaption_fader () {
	if (timerPhoto) {
        clearTimeout(timerPhoto);
        timerPhoto = 0;
    }

    $(captionBlock).fadeIn();
    timerPhoto = setTimeout(function() {
        $(captionBlock).fadeOut();
    }, 3000)
}
function videoCaption_fader () {
	if (timerVideo) {
        clearTimeout(timerVideo);
        timerVideo = 0;
    }

    $(vidCaptions).fadeIn();
    timerVideo = setTimeout(function() {
        $(vidCaptions).fadeOut();
    }, 3000)
}

// runtime photo

$('html').mousemove(function() {
    photoCaption_fader();
});
$('html').keyup(function() {
   photoCaption_fader();
});
$('html').click(function() {
   photoCaption_fader();
});

// runtime video

$('html').mousemove(function() {
    videoCaption_fader();
});
$('html').keyup(function() {
    videoCaption_fader();
});
$('html').click(function() {
    videoCaption_fader();
});



/* ************** VIDEO TOGGLE "a" = video plus photo  ********** */

var toggleAnchor_a = $('div#videoPlusPhoto a#toggle_anchor');
var photos_a = $('div#videoPlusPhoto div.photospace');
var video_a = $('div#videoPlusPhoto p');

var photoPlusVideo = $('div#photoPlusVideo');
var videoPlusPhoto = $('div#videoPlusPhoto');

// swap functions

var detachedPhotos_a;
var detachedVideo_a;

function swap_videoToPhoto_a () {
	detachedVideo_a = video_a.detach();
    detachedPhotos_a.prependTo(".entry-content");
    detachedPhotos_a = null;
    
    toggleAnchor_a.text("Project Film").stop();
}
function swap_photoToVideo_a () {
	detachedPhotos_a = photos_a.detach();
    detachedVideo_a.prependTo(".entry-content");
    detachedVideo_a = null;
    toggleAnchor_a.text("Project Stills").stop();
}	

// page-load detach functions

function pageLoad_photoDetach_a () {
       detachedPhotos_a = photos_a.detach();
    }
function pageLoad_videoDetach_a () {
       detachedVideo_a = video_a.detach();
    }

// toggles

function toggleVideoToPhoto_a() {
        toggleAnchor_a.toggle(function (){
            swap_videoToPhoto_a();
            photoCaption_fader();
        }, function(){
        	swap_photoToVideo_a();
        	videoCaption_fader();
        });
    }
function togglePhotoToVideo_a() {
        toggleAnchor_a.toggle(function (){
        	swap_photoToVideo_a();
        	videoCaption_fader();
        }, function(){
        	swap_videoToPhoto_a();
        	photoCaption_fader();
        });
    }

// runtime


$(function() {
	
        	pageLoad_photoDetach_a();
        	toggleVideoToPhoto_a();
    });

/*  ************* VIDEO TOGGLE "b" = photo plus video  ************  */

var toggleAnchor_b = $('div#photoPlusVideo a#toggle_anchor');
var photos_b = $('div#photoPlusVideo div.photospace');
var video_b = $('div#photoPlusVideo p');

// swap functions

var detachedPhotos_b;
var detachedVideo_b;

function swap_videoToPhoto_b () {
	detachedVideo_b = video_b.detach();
    detachedPhotos_b.prependTo(".entry-content");
    detachedPhotos_b = null;
    toggleAnchor_b.text("Project Film").stop();
}
function swap_photoToVideo_b () {
	detachedPhotos_b = photos_b.detach();
    detachedVideo_b.prependTo(".entry-content");
    detachedVideo_b = null;
    toggleAnchor_b.text("Project Stills").stop();
}	


// page-load video detach

function pageLoad_videoDetach_b () {
       detachedVideo_b = video_b.detach();
    }

// toggles

function toggleVideoToPhoto_b() {
        toggleAnchor_b.toggle(function (){
            swap_videoToPhoto_b();
            photoCaption_fader();
        }, function(){
        	swap_photoToVideo_b();
        	videoCaption_fader();
        });
    }
function togglePhotoToVideo_b() {
        toggleAnchor_b.toggle(function (){
        	swap_photoToVideo_b();
        	videoCaption_fader();
        }, function(){
        	swap_videoToPhoto_b();
        	photoCaption_fader();
        });
    }

// runtime


$(function() {
        	pageLoad_videoDetach_b();
        	togglePhotoToVideo_b();
    });


/* ************************ initial hide ************************* */

var sidebarSurplus = $('.gallery-one h2.siteNav, .gallery-one p#copy');

function sidebarHide () {
		
		sidebarSurplus.hide();
}

function surplusShow() {
  	var ca = document.cookie.split(';');
  	for (var i = 0; i < ca.length; i++) {
     		if (ca) {
     		sidebarSurplus.show();
   	 	}
  	}
}


sidebarHide();


/* ************************ #galleryNav HOVER STATES ************************* */

/* ************ responding to external navigation (routed from ext. link) ********** */


/* ************ responding to internal navigation (use of site navigation) ********** */

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

var galleryLink = $('#galleryNav li a');

galleryLink.click(function(){
  var href = $(this).attr('href');
  
  /* (figure out which anchor was clicked and get the href) */
  // (you might need to escape the href)

  createCookie(href, "visited"); // no expire: session cookie ?
});


markVisited();


function markVisited() {
  	var anchors = $('#galleryNav li a');
  	for (var i = 0; i < anchors.length; i++) {
    	if (readCookie(anchors[i].href) == "visited") {
     		$(anchors[i]).addClass('visited');
     		sidebarSurplus.show();
   	 	}
  	}
}


var homeLink = $('h1 a');

function killCookies() {
  	var anchors = $('#galleryNav li a');
  	
  	for (var i = 0; i < anchors.length; i++) {
    	if (readCookie(anchors[i].href) == "visited") {
     		eraseCookie(anchors[i].href);
   	 	}
  	}
}


function endCookies() {
  	var ca = document.cookie.split(';');
  	for (var i = 0; i < ca.length; i++) {
     		eraseCookie(ca[i]);
  	}
}

homeLink.click( function() {
	endCookies();
});


/*  ************  .postNav HOVER STATES  ************/

// test for current post

var currPost = $('.newsletter .entry-content #postTitle').text();

// get post titles from postNav

var postTitles = $('.postNav #galleryNav a');

// loop over titles


function postTitles_navCheck() {
	$(postTitles).each(function() {
		var navTitle = $(this).text();
		if (navTitle == currPost) {
			$(this).parent().addClass('current_page_item');
    	};
	});
}

// add current class

var postListItem = $('.postNav #galleryNav li');



// runtime

postTitles_navCheck();


/*
function deleteAllCookies() {
    var cookies = document.cookie.split(";");

    for (var i = 0; i < cookies.length; i++) {
        var cookie = cookies[i];
        var eqPos = cookie.indexOf("=");
        var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
        document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
    }
}
*/

/* 110920

var galleryLinks = $('#galleryNav li');
var allHrefs = new Array();

// get all hrefs from #galleryNav links
	galleryLinks.each(function(){
 		 allHrefs.push($(this).find('a').attr('href'));
	})

alert(hrefs);

var linkHref = $('#galleryNav li a').attr('href');

link.click() { function setCookie(linkHref,'')
	{
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
	}

}





function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}

function checkCookie()
{
//var url = $(this).attr('href'); ??
var username=getCookie("username");
  if (username!=null && username!="")
  {
  alert("Welcome again " + username);
  }
else 
  {
  username=prompt("Please enter your name:","");
  if (username!=null && username!="")
    {
    setCookie("username",username,365);
    }
  }
}

*/





/*   ------- add floater to login page ---------- */

function addFloater() {
	$('<div id="floater"></div>').prependTo('div#login');
}


});
