var currentPage = ( ie4 ) ? location.pathname.split(/\//)[0] : location.pathname.split(/\//)[1], gallery;

var siteData =
[
	{ href: "/motion",			name: "motion" },
	{ href: "/web",				name: "web" },
// 	{ href: "/print",			name: "print" },
	{ href: "/photography",		name: "photography" },
	{ href: "/events"	,		name: "events" },
	{ href: "/bookmarklets.html",name: "bookmarklets" },
	{ href: "/contact"		,	name: "contact" }
];

function buildHeader()
{
	var out = '<div id="page">'
			+ '\n		<div id="header_outter">'
			+ '\n			<div id="header">'
			+ '\n				<a id="ear" href="/">samrod</a>'
			+ '\n				<div class="nav">' + buildLinks() + '<br clear="all" />'
			+ '\n				</div><br clear="all" />'
			+ '\n			</div>'
			+ '\n		</div>'
			+ '\n	<div class="sandbox_outter">';
	document.write( out );
}

function buildFooter()
{
	var out = '\n	</div>'
			+ '\n	<div id="footer_outter">'
			+ '\n		<div id="footer">'
			+ '\n			<div class="column">&copy; 2011 Samrod Shenassa</div>'
			+ '\n			<div id="social" class="right"><a class="fb" href="http://facebook.com/samrod" title="Samrod on Facebook" target="_new">&nbsp;</a><a class="mm" href="http://modelmayhem.com/samrod" title="Samrod on ModelMayhem" target="_new">&nbsp;</a><a class="imdb" href="http://www.imdb.com/name/nm0791503/" title="Samrod on IMDb" target="_new">&nbsp;</a></div><br clear="all" />'
			+ '\n		</div>'
			+ '\n	</div><br clear="all" />'
			+ '\n</div>';
	document.write( out );
}

function buildLinks()
{
	var out = '',
		x;
	for ( x = siteData.length-1; x>=0; x-- )
	{
		var thisLink = siteData[x];
		if ( currentPage == thisLink.href.replace(/\//gi, '' ) )
		{
			out += '\n			<a class="current" href="' + thisLink.href + '">' + thisLink.name+ '</a>';
			document.body.className += thisLink.name;
		}
		else
				out += '\n			<a href="' + thisLink.href + '">' + thisLink.name+ '</a>';
	}
	return out;
}



// WEB
function populateWebSlideshows()
{
	var x = sr.Slideshows.length,
		y,
		thisSite;

	thisSite = webPort[x];
	var tempSlides = [];
	for ( y = 0; y < thisSite.slides; y++ )
		tempSlides.push( { file: thisSite.siteName + parseInt(y+1) + '.jpg', caption: '' } );

	sr.Slideshow(
		{
			container:	thisSite.siteName,
			imgDir:		"/img/portfolio/websites/",
			counter:	'bullets',
			fadeRamp:	sr.Faders.quickFade,
			transition:	'dissolve',
			draggable:	false,
			arrows: 	[ '&lt;', '&gt;' ],
			slides:		tempSlides
		}
	);
	x++;
	if ( x != webPort.length )
		setTimeout( populateWebSlideshows, 250 );
}



// PHOTOGRAPHY
function populatePhotoSlideshows()
{
	sr.Slideshow(
		{
			container:	'photography',
			imgDir:		"/img/portfolio/photography/",
			fadeRamp:	sr.Faders.smoothFade,
			arrows: 	[ '&lt;', '&gt;' ],
			counter:	'digits',
			displayOrder: [ 'caption', 'pic', 'nav' ],
 			draggable:	false,
 			syncURL:	true,
			shortcuts:	true,
			slides:		slides
		}
	);
}



// MOTION
var lastMovie;
function play( src, w, t )
{
	var thisMovie = src.replace(/\..*/gi, ''),
		out = d( thisMovie ).getElementsByTagName("span")[0].innerHTML;
	d( 'link_' + lastMovie ).className = "";
	d( 'link_' + thisMovie ).className = "active";
	lastMovie = thisMovie;

	sr.Video(
	{
		vidDir: '/av/',
		container: 'motion',
		src: src,
		width: w,
		caption: d( thisMovie ).innerHTML.replace(/<span>.*<\/span>/gi, '<span>' + out + '</span>')
	})
}


// EVENTS
function handleEvents()
{
	if ( urlValueOf[ 'e' ] )
		showEvent();
	else
		listEvents();

	document.body.className = ( getCookie("eventsView") ) ? getCookie("eventsView") : 'events grid';
	bindEvent( d( 'galleryLink' ), 'click', function() { window.location = "/events" } );
	bindEvent( d("linkMe"), 'click', function() { window.location.search = '?e=' + urlValueOf[ 'e' ] + '&s=' + (parseInt( sr.Slideshows['events'].currentSlide )+1); } );

	function listEvents()
	{
		var x,
			out = '',
			eventContainer,
			eventThumb,
			eventTitle,
			eventDate,
			thisDate,
			eventLink,
			statusTimer,
			link;

		for ( x=0; x < Events.length; x++ )
		{
			var thisEvent = Events[x];
			thisDate = eval( 'new Date(' + thisEvent.date + ')' );

			eventContainer = sr.CreateElement({	tagName:	'div',	className:	'eventContainer' });
			eventTitle = sr.CreateElement({	tagName:	'h2', innerHTML: thisEvent.title });
			eventDate = sr.CreateElement({	tagName:	'span',		className:	'eventContainer', innerHTML: thisDate.getMonth() + ' / ' + thisDate.getFullYear() });
			eventThumb = sr.CreateElement(
			{
				tagName:	'img',
				className:	'thumb',
				id:	thisEvent.id,
				src: '/img/events/' + thisEvent.id + '/thumb.jpg',
				events:
				[
					{ trigger: 'mouseover', func: function(e) { eventInfo(e) } },
					{ trigger: 'mouseout', func: function(e) { clearInfo(e) } },
					{ trigger: 'click', func: function(e) { showAlbum(e) } }
				]
			});

			sr.Fader( { element: eventThumb, ramp: sr.Faders.halfRamp.reverse() } );
			bindEvent( d("listView"), 'click', function() { document.body.className = 'events listView';setCookie("eventsView", "events listView") } );
			bindEvent( d("gridView"), 'click', function() { document.body.className = 'events grid';	setCookie("eventsView" ,"events grid ") } );

			eventContainer.appendChild( eventThumb );
			eventContainer.appendChild( eventTitle );
			eventContainer.appendChild( eventDate );
			d( "events" ).appendChild( eventContainer );

			Events[ thisEvent.id ] = thisEvent;
			Events.initialTitle = d( "eventTitle" ).innerHTML;
			Events.initialDate = d( "eventDate" ).innerHTML;
		}
	
		function eventInfo(e)
		{
			var target = e.target || e.srcElement;
			clearTimeout( statusTimer );
			sr.Faders[ target.id ].fade();
			d( "eventTitle" ).innerHTML = Events[ target.id ].title;
		
			var eventDate = eval( 'new Date(' + Events[ target.id ].date + ')' );
			d( "eventDate" ).innerHTML = eventDate.getMonth() + ' / ' + eventDate.getFullYear();
			link = '/events/?e=' + target.id;
			window.status = 'Go to "http://' + window.location.host + link;
		}
	
		function clearInfo(e)
		{
			var target = e.target || e.srcElement;
			sr.Faders[ target.id ].fade();
			d( "eventTitle" ).innerHTML = d( "eventDate" ).innerHTML = '';
			statusTimer = setTimeout( function()
			{
				d( "eventTitle" ).innerHTML = Events.initialTitle;
				d( "eventDate" ).innerHTML = Events.initialDate;
				window.status = ''
			}, 1000 );
		}
	
		function showAlbum()
		{
			window.location = link;
			return false;
		}
	}
	
	function showEvent()
	{
		var x, currentEvent, menuItem;
		for ( x=0; x < Events.length; x++ )
		{
			menuItem = sr.CreateElement({ tagName: 'option', value: Events[x].id, innerHTML: Events[x].title });
			if ( Events[x].id == urlValueOf[ 'e' ] )
			{
				menuItem.setAttribute( "selected", "selected" );
				document.title = 'samrod photography: ' + Events[x].title;

				d( "eventTitle" ).innerHTML = Events[ x ].title;
				var eventDate = eval( 'new Date(' + Events[ x ].date + ')' );
				d( "eventDate" ).innerHTML = eventDate.getMonth() + ' / ' + eventDate.getFullYear();

				var eventData =  document.createElement( "script" );
				eventData.setAttribute( "type", "text/javascript" );
				eventData.setAttribute( "src", "/js/events/" + urlValueOf[ 'e' ] + ".js" );
				bindEvent( eventData, 'load', buildEventsSlideshow );
				bindEvent( eventData, 'readystatechange', buildEventsSlideshow );
				document.getElementsByTagName( "head" )[0].appendChild( eventData );
			}
	
			bindEvent( d("eventsList"), 'change', function(e)
			{
				var target = e.target || e.srcElement;
				window.location = '/events?e=' + target.value;
			});
			d("eventsList").appendChild( menuItem );
			Show( "eventTools" );
			Hide( "galleryTools" );
		}
	}
}

function buildEventsSlideshow()
{
	if ( typeof slides != "undefined" )
	{
		sr.Slideshow(
			{
				container:	'events',
				imgDir:		'/img/events/' + urlValueOf[ 'e' ] + '/',
				thumbsDir:	'/img/events/' + urlValueOf[ 'e' ] + '/thumbs/',
				transition:	'crossdissolve',
				counter:	'counter',
				shortcuts:	true,
				syncURL:	true,
				arrows: 	[ '&lt;', '&gt;' ],
				slides:		slides
			}
		);
	}
}



/*  C O N T A C T    F O R M */
function submitContact()
{
	var contactForm = document.forms.contactForm,
		emailField = contactForm.email.value;

	if ( contactForm.message.value.replace(/ /gi, '').length == 0 || contactForm.message.value == initialLabel )
	{
		alert( "Hey Einstein, you forgot to write a comment.");
		contactForm.message.select();
		return false;
	}
	else if ( emailField.length == 0 )
	{
		alert( "Please provide an email address." );
		contactForm.email.focus();
		return false;
	}
	else if ( emailField.search(/[0-9a-z\.\-]{1,64}@([a-z\d]\.|\-?[a-z\d])+\.[a-z]{2,4}$/gi) != 0 )
	{
		alert( "Invalid email address. Please check your entry.");
		contactForm.email.select();
		return false;
	}
	else contactForm.submit();
}

var initialLabel;
function initForm()
{
	var contactForm = document.forms.contactForm;
	initialLabel = contactForm.message.value + '';

	contactForm.message.select();
	bindEvent( contactForm.message, 'blur', function()
		{
			if ( contactForm.message.value.replace(/ /gi, '').length == 0 )
				contactForm.message.value = initialLabel;
		});
	bindEvent( contactForm.message, 'focus', function()
		{
			if ( contactForm.message.value = initialLabel )
				contactForm.message.select();
		});
}


// Google Analytics Tracking Code
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1481334-3']);
_gaq.push(['_trackPageview']);

(function()
{
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();


