$site_url = 'http://markomasnjak.com/';

$('document').ready(function(){
	
	//socialFeeds();
	attachHoverListeners('.project');
	stickySidebar($('div.widget'), 202);
	
	if(window.location.hash){
		var $catChoice = window.location.hash.split('#')[1];
		showCategoryProjects($catChoice);
		}//end if
		
	attachCategoryPicker();
	
	$('.close-button').click(function(e){
		e.preventDefault();
		showHiddenPanel($(this).attr('href'),300,false);
		});
	
	$('.inline-content').click(function(e){
		
		e.preventDefault();
		
		var $link = $(this);
		var $temp = $link.attr('href').replace('#','#inline-').toLowerCase();
		
		if($($temp).css('display') == "block"){
			showHiddenPanel($link.attr('href'),300,false);
			}//end if
		
		else{
			$('.close-button').fadeOut(150,function(){
				$('.hidden-panel').slideUp(300);
				showHiddenPanel($link.attr('href'),300,true);
				});
			}//end else
		
		});
	
	$('.back-to-top').click(function(e){
		e.preventDefault();
		$('html, body').animate({scrollTop: '0px'}, 300);
		});
	
	});

function attachCategoryPicker(){
	
	$('a.category-trigger').unbind().click(function(e){
		
		if(!$(this).hasClass('on-page')){
			e.preventDefault();
			var $catChoice = $(this).attr('href').split('#')[1];
			window.location.hash=$catChoice;
			showCategoryProjects($catChoice);
			}//end if
			
		});
		
	}//end function

function createProjectList($catName, $catChoice, $url){
	
	var $returnHtml = '';
	
	$returnHtml='<div id="project-category-list-'+$catChoice+'" class="project-category-list">'+
		
		'<header>'+
			'<h3>Category: '+$catName+'</h3>'+
			'<a class="close-category" href="'+$url+'">View all</a>'+
			'</header>'+
		
		'<section class="projects">';
		
	$('#project-list .'+$catChoice).each(function(i){
		
		if(i%4 == 3){
			$returnHtml+='<article class="project last">'+$(this).html()+'</article>';
			}//end if
			
		else{
			$returnHtml+='<article class="project">'+$(this).html()+'</article>';
			}//end else
		
		});
		
	$returnHtml+='</section></div>';
	
	return $returnHtml;
	
	}//end function

function showCategoryProjects($catChoice){
	
	var $catName = toTitleCase($catChoice.replace('-', ' '));
	var $insertPos = '#project-list';
	var $html = createProjectList($catName, $catChoice, $site_url);
	
	if($('.project-category-list').length>0){
		$insertPos = '#'+$('.project-category-list').attr('id');
		}//end if
	
	$($html).insertBefore($insertPos);
	
	$($insertPos).slideUp(function(){
		
		if($insertPos != '#project-list'){
			$(this).remove();
			}//end if
		
		});

	$('#project-category-list-'+$catChoice).slideDown(function(){
		attachHoverListeners('.project');
		attachCategoryPicker();
		});
	
	}//end function

function toTitleCase($str){
    return $str.replace(/\w\S*/g, function($txt){return $txt.charAt(0).toUpperCase() + $txt.substr(1).toLowerCase();});
	}

function attachHoverListeners($selector){
	
	if(!$('body').hasClass('mobile')){
	
		$($selector).unbind().hover(
		
			function(){
				$($selector).css('opacity','.25');
				$(this).css('opacity','1');
				},
		
			function(){
				$($selector).css('opacity','1');
				});
			
			}//end mobile device check
	
	$('.close-category').unbind().click(function(e){
				
		e.preventDefault();
		window.location.hash='';
		$('#project-list').slideDown();
				
		$('.project-category-list').slideUp(function(){
			$('.project-category-list').remove();
			});
				
		});
	
	}//end function


function showHiddenPanel($url, $speed, $bool){
	
	var $target = $url.split('#');
	
	if($target.length>1){
		$target = $target[1];
		}//end if
	
	else{
		$target = $target[0];
		}//end else
	
	if($bool == true){
		
		$('#inline-'+$target).slideDown($speed,function(){
			$('.close-button').fadeIn($speed/2);
			});
			
		}//end if
		
	else{
		
		$('.close-button').fadeOut(($speed/2),function(){
			$('.hidden-panel').slideUp($speed);
			});
		
		}//end else
	
	}//end function

function stickySidebar($item, $count){
	
	if($item.length>0){
		
		if($('html').scrollTop()>$count || $('body').scrollTop()>$count){
			$item.addClass('sticky');
			}//end if
		
		$(window).scroll(function(){
			
			if($('html').scrollTop() > $count || $('body').scrollTop() > $count){
				$item.addClass('sticky');
				}//end if
		
			else{
				$item.removeClass('sticky');
				}//end else
			
			});
		
		}//end if
	
	}//end function
