// Javascript for Mark Schimmel by Evan Bartlett (www.evanbartlett.com)

$(document).ready(documentReady);
function documentReady() {
	// Initial variables
	var History = window.History;
	var State = History.getState();
	var _state = window.location.pathname;
	var _title = document.title;
	var _slug = window.location.href;
	var _filter_highlight;
	var _bodyclass;
	
	// Initial functions
	$('.email_fix').map(function (e) { $(this).html('<a href="mailto:'+$(this).html().replace(' [ at ] ','@').replace('/','').replace(':','.')+'" title="Email Mark">'+$(this).html().replace(' [ at ] ','@').replace('/','')+'</a>'); });
	
	// History.js
    if (!History.enabled) {
		return false;
    } else {
		// Main navigation
		$('.nav_btn').click(function (e) {
			e.preventDefault();
			_state = $(this).attr("href");
			_title = $(this).attr("title");
			_slug = $(this).attr("href");
			History.pushState({state:_state}, 'Mark Schimmel - '+_title, _slug);
		});
		// Work filter
		$('#work_filter').change(function() {
			_state = $(this).val();
			_title = $("option:selected").attr("data-title");
			_slug = $(this).val();
			if (_state != "#") {
				History.pushState({state:_state}, 'Mark Schimmel - '+_title, _slug);
			}
		});
		// Work thumbnail
		$('.work_thumb_holder a').click(function (e) {
			e.preventDefault();
			_state = $(this).attr("href");
			_title = $(this).attr("data-title");
			_slug = $(this).attr("href");
			History.pushState({state:_state}, 'Mark Schimmel - '+_title, _slug);
		});
		// Work back
		$('#work_back').click(function (e) {
			e.preventDefault();
			_state = $(this).attr("href");
			_title = $(this).attr("title");
			_slug = $(this).attr("href");
			History.pushState({state:_state}, 'Mark Schimmel - '+_title, _slug);
		});
		History.pushState({state:_state}, _title, _slug);
	}
	
    History.Adapter.bind(window,'statechange',function() {
		_page_targets = '';
		_page = '';
		_section = '';
		_track_page = '';
		_track_section = '';
		_track_url = '';
        var State = History.getState();
		_page_targets = State.data['state'].split('/');
		_page = _page_targets[1];
		_section = _page_targets[2];
        History.log(State.data, State.title, State.url);
		_change_page(_page, _section);
		// Analytics
		_track_page = _page;
		if (_section != '') {
			_track_section = _section+'/';
		} else {
			_track_section = '';
		}
		_track_url = '/'+_track_page+'/'+_track_section;
		_gaq.push(['_setAccount', 'UA-26353761-1']);
		_gaq.push(['_trackPageview', _track_url]);
    });
	
	// Change page
	function _change_page(_1, _2) {
		if (_1 == 'information') {
			// Clear project detail
			_clear_project_detail(_1);
			// Set body class variable
			_bodyclass = 'information';
		} else if (_1 == 'project') {
			// Hide details right quick
			$('.video_copy').removeClass('show');
			// Load project data
			_load_project_detail(_2);
			// Set body class variable
			_bodyclass = 'project';
		} else {
			// Ensure it wasn't coming from the root
			if (typeof _2 == 'undefined') {
				if (_1 != 'information' && _1 != 'project' && _1 != 'work') {
					_2 = 'home';
				} else {
					_2 = '';
				}
			}
			// Set section
			if (_2 != '' && _2 != 'undefined' && _2 != 'home') {
				// Filter thumbs
				$('.work_thumb_holder').each(function() {
					if ($(this).attr("data-category") == _2) {
						$(this).addClass('work_thumb_show').removeClass('work_thumb_hide');
					} else {
						$(this).addClass('work_thumb_hide').removeClass('work_thumb_show');
					}
				} );
				// Set highlight value
				_filter_highlight = document.title;
				_filter_highlight = _filter_highlight.replace('Mark Schimmel - ', '');
				// Set highlight text
				$('.highlight').text('"'+_filter_highlight+'"');
				// Set work back button
				$("#work_back").attr("href", "/work/"+_2+"/");
				$("#work_back").attr("title", _filter_highlight);
			} else if (_2 == '' || _2 == 'undefined' || _2 == 'home') {
				// Display all thumbs
				$('.work_thumb_holder').addClass('work_thumb_show').removeClass('work_thumb_hide');
				// Set highlight value
				_filter_highlight = "All";
				// Set highlight text
				$('.highlight').text('"'+_filter_highlight+'"');
				// Set work back button
				$("#work_back").attr("href", "/work/");
				$("#work_back").attr("title", "Work");
			}
			// Clear project detail
			_clear_project_detail(_1);
			// Set body class variable
			_bodyclass = 'work';
		}
		// Reset work filter
		$('#work_filter option[value="#"]').attr('selected', 'selected');
		// Reset scrollbars for all pages
		$('.page_wrapper').scrollTop(0);
		// Set body class
		$("body").attr('class', _bodyclass);
		// Set share links
		_curr_share_link = window.location;
		_curr_share_title = document.title.replace(/\s+/g, '+');
		$('#share_twitter').attr('href', "http://twitter.com/intent/tweet?text="+_curr_share_title+"&url="+_curr_share_link);
		$('#share_facebook').attr('href', "http://www.facebook.com/sharer/sharer.php?u="+_curr_share_link+"&t="+_curr_share_title);
	}
	
	// Load project detail
	function _load_project_detail(_target) {
		// Show loader
		$('#ajax_loader').addClass('show');
		// Load in specified project
		$.ajax( {
			type:'POST',
			url:'/data.php',
			dataType:'json',
			data: {
				project_id:_target
			},
			success:function(data) {
				// Hide loader
				$('#ajax_loader').removeClass('show');
				if (data.error) {
					// Display error message
					$('.video_copy .project_video').html('Error Loading Project.');
					$('.video_copy .name').text('');
					$('.video_copy .year').text('');
					$('.video_copy .project_descr').html('');
				} else {
					// Display project data
					$('.video_copy .project_video').html('<iframe src="http://player.vimeo.com/video/'+data.vimeo_id+'?title=0&amp;byline=0&amp;portrait=0" width="640" height="360" frameborder="0" webkitAllowFullScreen allowFullScreen></iframe>');
					$('.video_copy .name').text(data.title);
					$('.video_copy .year').text(data.year);
					$('.video_copy .project_descr').html(data.descr);
				}
				// Show display area
				$('.video_copy').addClass('show');
			},
			error:function(XMLHttpRequest, textStatus, errorThrown) {
				// Hide loader
				$('#ajax_loader').removeClass('show');
				// Display error message
				$('.video_copy .project_video').html('Error Loading Project.');
				$('.video_copy .name').text('');
				$('.video_copy .year').text('');
				$('.video_copy .project_descr').html('');
				// Show display area
				$('.video_copy').addClass('show');
			}
		} );
	}
	
	// Clear project detail
	function _clear_project_detail(_target) {
		if (_target != "project") {
			// Hide display area
			$('.video_copy').removeClass('show');
			// Clear project detail display
			setTimeout(function() {
				$('.video_copy .project_video').text('');
				$('.video_copy .project_video').html('');
				$('.video_copy .name').text('');
				$('.video_copy .year').text('');
				$('.video_copy .project_descr').html('');
			}, 800);
		}
	}
	
	// Naviation dropdown code
	function MM_jumpMenu(targ,selObj,restore) {
		if (!History.enabled) {
			eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
			if (restore) selObj.selectedIndex=0;
		}
	}
}
