function createCookie( name,value,days ) {
	if (days) {
		var date = new Date();
		if(days == 'insta') {
			date.setTime(date.getTime()+50000);
		} else {
			date.setTime(date.getTime()+(days*24*60*60*1000));
		}
		
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
};

function grabCookie( check_name ) {
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ ) {
		
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		if ( cookie_name == check_name ) {
			b_cookie_found = true;
		
			if ( a_temp_cookie.length > 1 ) {
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	
	if ( !b_cookie_found ) {
		return null;
	}
}

jQuery(function() {
	if(location.pathname == '/') {
		if(grabCookie('lang') != null) {
			switch(grabCookie('lang')) {
				case 'ita':
					$(".eng").addClass('hide');
					$(".ita").removeClass('hide');
					break;
				
				case 'eng':
					$("#pulsantiera a:not(#active)").each(function(){
						var path = $(this).attr('href').substr(3);
						$(this).attr('href','/en'+path);
					});
					$(".ita").addClass('hide');
					$(".eng").removeClass('hide');
					break;
			}
		} else {
			$(".eng").addClass('hide');
			$(".ita").removeClass('hide');
		}
	}
	
	$(".flag").click(function(){
		var lang = $(this).attr('title');
		
		if(lang == 'Italiano') {
			if(location.pathname == '/') {
				$("#pulsantiera a:not(#active)").each(function(){
					var path = $(this).attr('href').substr(3);
					$(this).attr('href','/it'+path);
				});
				$(".eng").addClass('hide');
				$(".ita").removeClass('hide');
			}
			createCookie('lang','ita',10000);
		};
		
		if(lang == 'English') {
			if(location.pathname == '/') {
				$("#pulsantiera a:not(#active)").each(function(){
					var path = $(this).attr('href').substr(3);
					$(this).attr('href','/en'+path);
				});
				$(".ita").addClass('hide');
				$(".eng").removeClass('hide');
			}
			createCookie('lang','eng',10000);
		};
		
		if(location.pathname == '/') { return false; } else { return true; };
		
		
	});
	
	$(".category").click(function(){
			
		$(this).parent("dt").next('dd').toggle("slow", function() {
			var dlist	= $(this).prev('dt');
			var plus	= dlist.find(".plus");
			
			if(dlist.hasClass("open")) {
				plus.html('+');
				dlist.toggleClass("open");	
			} else {
				plus.html('-');
				dlist.toggleClass("open");
			}
			
		});
		
		return false;
	});
});