/*
---------------------------------------------------------------------

MANUXONLINE JS

Script type: General
Author: Emanuele Pelliccione* 
Last Update: 03-05-2009

*except "smooth_scroll" and "hasCSS" functions

---------------------------------------------------------------------
*/

/*
--------------------------------------------------------
INITIALIZATION
--------------------------------------------------------
*/ 

function init() {
	
	if( document.getElementById ) {
		
		create_navigation();
		
		create_sub_navigation();
		
		create_header_buttons();
		
		manage_navigation();
		
		manage_sub_navigation();
		
		manage_header_buttons();
		
		manage_contact_form();
		
		emails();
		
		smooth_scroll();
			
	}
	else {
		
		return false;
		
	}
	
}

window.onload = init;

/*
--------------------------------------------------------
INITIALIZATION > AJAX
--------------------------------------------------------
*/

function make_xhr() {

	if( window.ActiveXObject ) {
	
		xml_http = new ActiveXObject( "Microsoft.XMLHTTP" );
		
	}
	else if ( window.XMLHttpRequest ) {
	
		xml_http = new XMLHttpRequest();
		
	}
	
	return xml_http;

}

/*
--------------------------------------------------------
NAVIGATION
--------------------------------------------------------
*/

function create_navigation() {
	
	if( hasCSS() ) {
	
		var logo = document.getElementsByTagName( "h1" ).item( 0 );
		
		if( logo ) {
			
			ul_navigation = document.createElement( "ul" );
			
			ul_navigation.setAttribute( "id" , "navigation" );
			
			ul_navigation.innerHTML = '<li id="navigation-profile"><a href="#profile">Profilo</a></li><li id="navigation-portfolio"><a href="#portfolio">Portfolio</a></li><li id="navigation-links"><a href="#links">Links</a></li><li id="navigation-contacts"><a href="#contacts">Contatti</a></li>';
				
			insertAfter( logo , ul_navigation );
			
		}
	
	}

}

function manage_navigation() {
	
	if( hasCSS() ) {
	
		page = document.getElementsByTagName( "body" ).item( 0 );
		
		page.className = "page-profile";
			
		var button_profile = document.getElementById( "navigation-profile" ).firstChild;
			
		var button_portfolio = document.getElementById( "navigation-portfolio" ).firstChild;
			
		var button_links = document.getElementById( "navigation-links" ).firstChild;
			
		var button_contacts = document.getElementById( "navigation-contacts" ).firstChild;
			
		if( button_profile ) {
				
			button_profile.onclick = function() {
					
				page.className = "page-profile";
					
				return false;
					
			}
				
		}
			
		if( button_portfolio ) {
				
			button_portfolio.onclick = function() {
					
				page.className = "page-portfolio";
					
				return false;
					
			}
				
		}
			
		if( button_links ) {
				
			button_links.onclick = function() {
					
				page.className = "page-links";
					
				return false;
					
			}
				
		}
			
		if( button_contacts ) {
				
			button_contacts.onclick = function() {
					
				page.className = "page-contacts";
					
				return false;
					
			}
				
		}
		
	}
	
}

/*
--------------------------------------------------------
SUBNAVIGATION
--------------------------------------------------------
*/ 

function create_sub_navigation() {

	if( hasCSS() ) {
		
		menu_profile_intro = document.getElementById( "menu-profile-intro" );
		
		menu_profile_skills = document.getElementById( "menu-profile-skills" );
		
		menu_profile_experiences = document.getElementById( "menu-profile-experiences" );
		
		menu_profile_colophon = document.getElementById( "menu-profile-colophon" );
		
		
		menu_links_resources = document.getElementById( "menu-links-resources" );
		
		menu_links_javascript = document.getElementById( "menu-links-javascript" );
		
		menu_links_utilities = document.getElementById( "menu-links-utilities" );
		
		menu_links_inspiration = document.getElementById( "menu-links-inspiration" );
		
		menu_links_usability = document.getElementById( "menu-links-usability" );
		
		
		if( menu_profile_intro || menu_profile_skills || menu_profile_experiences || menu_profile_colophon ) {
			
			var title_side_profile = document.getElementById( "title-side-profile" );
			
			if( title_side_profile ) {
			
				title_side_profile.style.background = "transparent url(img/title-side-profile.png) 0 0 no-repeat";
				
			}
			
		}
		
		if( menu_profile_intro ) {
			
			menu_profile_intro.style.padding = "0";
			
			menu_profile_intro.innerHTML = '<a href="">In breve</a>';
			
		}
		
		if( menu_profile_skills ) {
			
			menu_profile_skills.style.padding = "0";
			
			menu_profile_skills.innerHTML = '<a href="">Skills</a>';
			
		}
		
		if( menu_profile_experiences ) {
			
			menu_profile_experiences.style.padding = "0";
			
			menu_profile_experiences.innerHTML = '<a href="">Esperienze professionali</a>';
			
		}
		
		if( menu_profile_colophon ) {
			
			menu_profile_colophon.style.padding = "0";
			
			menu_profile_colophon.innerHTML = '<a href="">Colophon</a>';
			
		}
		
		
		if( menu_links_resources || menu_links_javascript || menu_links_utilities || menu_links_inspiration || menu_links_usability ) {
			
			var title_side_links = document.getElementById( "title-side-links" );
			
			if( title_side_links ) {
			
				title_side_links.style.background = "transparent url(img/title-side-links.png) 0 0 no-repeat";
				
			}
			
		}
		
		if( menu_links_resources ) {
			
			menu_links_resources.style.padding = "0";
			
			menu_links_resources.innerHTML = '<a href="">Risorse</a>';
			
		}
		
		if( menu_links_javascript ) {
			
			menu_links_javascript.style.padding = "0";
			
			menu_links_javascript.innerHTML = '<a href="">Javascript &amp; Ajax</a>';
			
		}
		
		if( menu_links_utilities ) {
			
			menu_links_utilities.style.padding = "0";
			
			menu_links_utilities.innerHTML = '<a href="">Utilities</a>';
			
		}
		
		if( menu_links_inspiration ) {
			
			menu_links_inspiration.style.padding = "0";
			
			menu_links_inspiration.innerHTML = '<a href="">Inspiration</a>';
			
		}
		
		if( menu_links_usability ) {
			
			menu_links_usability.style.padding = "0";
			
			menu_links_usability.innerHTML = '<a href="">Usability &amp; Accessibility</a>';
			
		}
	
	}
	
}

function manage_sub_navigation() {
	
	if( hasCSS() ) {
	
		manage_sub_sections_profile( 0 );
		
		manage_sub_sections_links( 0 );
		
		
		var button_profile_intro = menu_profile_intro.firstChild;
		
		var button_profile_skills = menu_profile_skills.firstChild;
		
		var button_profile_experiences = menu_profile_experiences.firstChild;
		
		var button_profile_colophon = menu_profile_colophon.firstChild;
		
		
		var button_links_resources = menu_links_resources.firstChild;
		
		var button_links_javascript = menu_links_javascript.firstChild;
		
		var button_links_utilities = menu_links_utilities.firstChild;
		
		var button_links_inspiration = menu_links_inspiration.firstChild;
		
		var button_links_usability = menu_links_usability.firstChild;
		
		
		if( button_profile_intro ) {
			
			button_profile_intro.onclick = function() {
				
				manage_sub_sections_profile( 0 );
				
				page.className = "page-profile";
				
				return false;
				
			}
			
		}
		
		if( button_profile_skills ) {
			
			button_profile_skills.onclick = function() {
				
				manage_sub_sections_profile( 1 );
				
				page.className = "page-profile";
				
				return false;
				
			}
			
		}
		
		if( button_profile_experiences ) {
			
			button_profile_experiences.onclick = function() {
				
				manage_sub_sections_profile( 2 );
				
				page.className = "page-profile";
				
				return false;
				
			}
			
		}
		
		if( button_profile_colophon ) {
			
			button_profile_colophon.onclick = function() {
				
				manage_sub_sections_profile( 3 );
				
				page.className = "page-profile";
				
				return false;
				
			}
			
		}
		
		if( button_links_resources ) {
			
			button_links_resources.onclick = function() {
				
				manage_sub_sections_links( 0 );
				
				page.className = "page-links";
				
				return false;
				
			}
			
		}
		
		if( button_links_javascript ) {
			
			button_links_javascript.onclick = function() {
				
				manage_sub_sections_links( 1 );
				
				page.className = "page-links";
				
				return false;
				
			}
			
		}
		
		if( button_links_utilities ) {
			
			button_links_utilities.onclick = function() {
				
				manage_sub_sections_links( 2 );
				
				page.className = "page-links";
				
				return false;
				
			}
			
		}
		
		if( button_links_inspiration ) {
			
			button_links_inspiration.onclick = function() {
				
				manage_sub_sections_links( 3 );
				
				page.className = "page-links";
				
				return false;
				
			}
			
		}
		
		if( button_links_usability ) {
			
			button_links_usability.onclick = function() {
				
				manage_sub_sections_links( 4 );
				
				page.className = "page-links";
				
				return false;
				
			}
			
		}
		
	}
	
}

function manage_sub_sections_profile( section_show ) {
	
	var sub_sections_profile = new Array();
	
	sub_sections_profile[0] = document.getElementById( "intro" );
	
	sub_sections_profile[1] = document.getElementById( "skills" );
	
	sub_sections_profile[2] = document.getElementById( "experiences" );
	
	sub_sections_profile[3] = document.getElementById( "colophon" );
	
	
	var sub_menu_profile = new Array();
	
	sub_menu_profile[0] = menu_profile_intro;
	
	sub_menu_profile[1] = menu_profile_skills;
	
	sub_menu_profile[2] = menu_profile_experiences;
	
	sub_menu_profile[3] = menu_profile_colophon;
	
	
	if( sub_sections_profile[0] && sub_sections_profile[1] && sub_sections_profile[2] && sub_sections_profile[3] ) {
	
		for( var i = 0 ; i < sub_sections_profile.length ; i++ ) {
			
			sub_sections_profile[i].className = "hide";
			
		}
		
		sub_sections_profile[section_show].removeAttribute( "class" );
		
		sub_sections_profile[section_show].removeAttribute( "className" ); // IE
		
	}
	
	if( sub_menu_profile[0] && sub_menu_profile[1] && sub_menu_profile[2] && sub_menu_profile[3] ) {
		
		for( var j = 0 ; j < sub_menu_profile.length ; j++ ) {
			
			sub_menu_profile[j].removeAttribute( "class" );
			
			sub_menu_profile[j].removeAttribute( "className" ); // IE
			
		}
		
		sub_menu_profile[section_show].className = "menu-link-on";
		
	}
	
}

function manage_sub_sections_links( section_show ) {
	
	var sub_sections_links = new Array();
	
	sub_sections_links[0] = document.getElementById( "resources" );
	
	sub_sections_links[1] = document.getElementById( "javascript" );
	
	sub_sections_links[2] = document.getElementById( "utilities" );
	
	sub_sections_links[3] = document.getElementById( "inspiration" );
	
	sub_sections_links[4] = document.getElementById( "usability" );
	
	
	var sub_menu_links = new Array();
	
	sub_menu_links[0] = menu_links_resources;
	
	sub_menu_links[1] = menu_links_javascript;
	
	sub_menu_links[2] = menu_links_utilities;
	
	sub_menu_links[3] = menu_links_inspiration;
	
	sub_menu_links[4] = menu_links_usability;
	
	
	if( sub_sections_links[0] && sub_sections_links[1] && sub_sections_links[2] && sub_sections_links[3] && sub_sections_links[4] ) {
		
		for( var i = 0 ; i < sub_sections_links.length ; i++ ) {
			
			sub_sections_links[i].className = "hide";
			
		}
		
		sub_sections_links[section_show].removeAttribute( "class" );
		
		sub_sections_links[section_show].removeAttribute( "className" ); // IE
		
	}
	
	if( sub_menu_links[0] && sub_menu_links[1] && sub_menu_links[2] && sub_menu_links[3] && sub_menu_links[4] ) {
		
		for( var j = 0 ; j < sub_menu_links.length ; j++ ) {
			
			sub_menu_links[j].removeAttribute( "class" );
			
			sub_menu_links[j].removeAttribute( "className" ); // IE
			
		}
		
		sub_menu_links[section_show].className = "menu-link-on";
		
	}
	
}

/*
--------------------------------------------------------
HEADER
--------------------------------------------------------
*/ 

var header_top = 46;

function create_header_buttons() {
	
	if( hasCSS() ) {
	
		var contents = document.getElementById( "contents" );
		
		if( contents ) {
		
			var button_minimize = document.createElement( "button" );
			
			var button_maximize = document.createElement( "button" );
			
			var button_minimize_text = document.createTextNode( "comprimi" );
			
			var button_maximize_text = document.createTextNode( "espandi" );
			
			button_minimize.setAttribute( "id" , "minimize" );
			
			button_maximize.setAttribute( "id" , "maximize" );
			
			button_minimize.appendChild( button_minimize_text );
			
			button_maximize.appendChild( button_maximize_text );
			
			insertAfter( contents , button_minimize );
			
			var minimize = document.getElementById( "minimize" );
			
			insertAfter( minimize , button_maximize );
			
		}
		
	}

}

function manage_header_buttons() {

	if( hasCSS() ) {

		$( document ).ready( function() {
									  
			$( "#maximize" ).addClass( "hide" );
			
			$( "#minimize" ).click( function() {
										  
				$( "#header" ).animate( { "top" : "-46px" } , 1500 );
				
				$( "#contents" ).animate( { "paddingTop" : "0" } , 1500 );
				
				header_top = 0;
				
				$( this ).addClass( "hide" );
				
				$( "#maximize" ).removeAttr( "class" );
				
				$( this ).animate( { "top" : "0" } , 1500 );
				
				$( "#maximize" ).animate( { "top" : "0" } , 1500 );
			
			});
		
			
			$( "#maximize" ).click( function() {
			
				$( "#header" ).animate( { "top" : "0" } , 1500 );
				
				$( "#contents" ).animate( { "paddingTop" : "46px" } , 1500 );
				
				header_top = 46;
				
				$( this ).addClass( "hide" );
				
				$( "#minimize" ).removeAttr( "class" );
				
				$( "#minimize" ).animate( { "top" : "46px" } , 1500 );
				
				$( this ).animate( { "top" : "46px" } , 1500 );
			
			});
		
		});
		
	}
	
}

/*
--------------------------------------------------------
EMAILS
--------------------------------------------------------
*/ 

function emails() {

	var mail_info = document.getElementById( "mail-info" );
	
	var mail_msn = document.getElementById( "mail-msn" );
    
	if( mail_info ) {
      		
		mail_info.innerHTML = '<a href="mailto:info' + '@' + 'manuxonline' + '.' + 'com">info' + '@' + 'manuxonline' + '.' + 'com</a>';
    
	}
	
	if( mail_msn ) {
			
		mail_msn.innerHTML = '<a href="mailto:manumode' + '@' + 'hotmail' + '.' + 'com">manumode' + '@' + 'hotmail' + '.' + 'com</a>';
		
	}
	
}

/*
--------------------------------------------------------
CONTACT FORM
--------------------------------------------------------
*/

var output;

function manage_contact_form() {
	
	button_send = document.getElementsByTagName( "button" ).item( 0 );
	
	var contact_form = document.getElementsByTagName( "form" ).item( 0 ); 
	
	if( button_send && contact_form ) {
		
		var div_output = document.createElement( "div" );
		
		div_output.setAttribute( "id" , "output" );
		
		insertAfter( contact_form , div_output );
	
		output = document.getElementById( "output" );
	
		button_send.onclick = function() {
			
			make_request_post();
			
			button_send.disabled = true;
			
			return false;
		
		}
	
	}
	
}

function make_query_string() {

	var name = document.getElementById( "name" ).value;
	
	var website = document.getElementById( "website" ).value;
	
	var email = document.getElementById( "email" ).value;
	
	var subject = document.getElementById( "subject" ).value;
	
	var message = document.getElementById( "message" ).value;
	
	if( name && email && subject && message ) {

		var query_string = "name=" + name + "&website=" + website + "&email=" + email + "&subject=" + subject + "&message=" + message;
		
		return query_string;
		
	}

}

function make_request_post() {

	var ajax = make_xhr();
	
	loaded = false;
	
	if( ajax ) {
		
		if( !loaded ) {
			
			if( output ) {
			
				output.innerHTML = "<img src='img/loader.gif' />";
				
			}
			
		}

		var url = "scripts/php/send-form.php?" + new Date().getTime();
		
		var query_string = make_query_string();
		
		xml_http.open( "post" , url , true );
		
		xml_http.onreadystatechange = make_state_change;
		
		xml_http.setRequestHeader( "content-type" , "application/x-www-form-urlencoded" );
		
		xml_http.send( query_string );
	
	}
	
}

function make_state_change() {

	if( xml_http.readyState == 4 ) {
	
		if( xml_http.status == 200 ) {
		
			parse_results();
			
		}
		
	}
	
}

function parse_results() {
	
	if( output && button_send ) {
	
		output.innerHTML = xml_http.responseText;
		
		loaded = true;
		
		button_send.disabled = false;
		
	}

}

/*
--------------------------------------------------------
PAGE SMOOTH SCROLL
--------------------------------------------------------
*/ 

/*
smooth_scroll
by Karl Swedberg
http://www.learningjquery.com/2007/09/animated-scrolling-with-jquery-12
*/

function smooth_scroll() {

	$(document).ready(function(){
	  $('a[href*=#]').click(function() {
	 if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
	 && location.hostname == this.hostname) {
	   var $target = $(this.hash);
	   $target = $target.length && $target
	   || $('[name=' + this.hash.slice(1) +']');
	   if ($target.length) {
	  //var targetOffset = $target.offset().top;
	  var targetOffset = $target.offset().top - header_top;
	  $('html,body')
	  .animate({scrollTop: targetOffset}, 1000);
		return false;
	   }
	 }
	  });
	});

}

/*
--------------------------------------------------------
FUNCTIONS
--------------------------------------------------------
*/

function insertAfter( referenceNode , newNode ) {
	
    referenceNode.parentNode.insertBefore( newNode , referenceNode.nextSibling );

}

function get_first_child( element ) {
  
	if( element ) {
  
		var first_child = element.firstChild;
	
	}
  
	while( first_child && first_child.nodeType != 1 ) {
	
		first_child = first_child.nextSibling;
	
	}
	
	return first_child;

}

/*
hasCSS
by David Hellsing
http://monc.se/kitchen/144/detect-css-with-jquery
*/

function hasCSS() {
	
	var _d = document.createElement('div')
 	
	_d.id = 'css_test'
 	
	$('body').append(_d)
	
	$('#css_test').css({width:'1px',height:'1px',display:'none'})
 	
	var _v = ($('#css_test').width() != 1) ? false : true
 	
	$('#css_test').remove()
	
	return _v

}