// JavaScript Document
$(document).ready(function(){
	var searchmode = false;
	var topc = 197;
	var vtop;
	
	resizeBG();
	
	$(document).bind('keyup', function(e) {
		// if esc is pressed
		if (e.keyCode == 27 && searchmode == true) {
			showAll();
		}
	});
	
	$(document).bind('keypress', function(e) {
		var ival = $('#search input').val();
		if(searchmode == true && ival == "Search here ..."){
			$('#search input').val('');
		}
	});
	
	$('section#search').click(function(e) {
		if(e.target.nodeName == 'SECTION'){
			showAll();
		}
	});
		
	$('#s').click(function() {
		$(this).val('');
	});
		
	$('#social li:first-child').click(function() {
		if(!searchmode){
			hideAll();
		}
	});
	
	// check if element is there then assign onclick
	if ($("section#search-result h3").length > 0){
	  $('section#search-result h3').click(function() {
			hideAll();
		});
	}
	
	
	$(window).scroll(function() {
		resizeBG();
	});
	$(window).bind('resize', function() { 
		resizeBG();
	});
	
	function resizeBG(){
		if(searchmode == true){
			vtop = topc - $(window).scrollTop();
			var bgPos = topc - $(window).scrollTop();
			if(vtop < 30){
				$('section#search').css('top',0);
				$('section#search h2').css('margin-top',topc);
			}else{
				$('section#search').css('top',bgPos);
				$('section#search h2').css('margin-top',topc - bgPos);
			}
		}
	}
	
	function hideAll(){
		searchmode = true;
		var w = $(window).width();
		var h = $(window).height();
		$('#search').css({width: w + "px", height : h + "px"});
		$('#search').fadeIn('fast');
		
		$("#search input").focus();
		$('#search input').val('Search here ...');
		
		resizeBG();
		
		
	}
	function showAll(){
		searchmode = false;
		$('#search').fadeOut('fast');
	}
	
});

