// ATUNN js
function open_site(site_id) {	
	jQuery.post('function_update_on_fly.php', {'action': 'update_statistics', 'site_id': site_id}); 
}
function change_search_engine(is_sites_url) {	
    var url = is_sites_url;
	var domain = extract_domain(url);
	if( url.indexOf( "_search_keyword_" ) !== -1 ) {
		$('#current_search_engine_visible_name').attr("src",'http://www.google.com/s2/favicons?domain='+domain);
		document.searchform.service_available.value=url;
	}
	else {
		window.open(url.replace('_search_keyword_',document.searchform.q.value));
	}
	$("#search_popup").hide();
	document.searchform.q.focus();
}
function turn_off_buttons() {
	$('.sidebar_category_button').removeClass('active');
	$('.searchbar_button').removeClass('active'); 
	$('#header ul li a').removeClass('active'); //dockbar button
}
function prepare_load() {
	turn_off_buttons();
	$("#logo_homepage").show();
    $('#loading').show();
	$('#popup').hide();
	$('#homepage_content').show();
	$("#search_popup").hide();
    scroll(0,0);
}
function loadonfly_cat_insearchpopup (category_url) {
	prepare_load();	
	$('#search_popup').load(is_url+'show_sites_in_a_searchcategory.php', {'search_category': category_url}, function() { 
	    show_search_popup(); 
	});
}
function show_main_popup() {
	$('#loading').hide();
	$('#popup').show();
	$('#homepage_content').hide();
	$("#search_popup").hide();
	$("#logo_homepage").hide();
	$("#logo_sidebar").fadeIn();
}
function close_main_popup()  {
	$('#popup').hide();
	$('#homepage_content').show();
	turn_off_buttons();
	document.searchform.q.focus();
	$("#logo_homepage").fadeIn();
	$("#logo_sidebar").fadeOut();
}
function show_search_popup() {
	$('#loading').hide();
	$('#popup').hide();
	$("#search_popup").show();
	document.searchform.q.focus();
	$("#logo_homepage").fadeIn();
	$("#logo_sidebar").fadeOut();
}
function load_on_fly(page) {
    prepare_load();
	$('#popup').load(is_url+'load_on_fly.php?'+page, function() { 
	    show_main_popup(); 
	});
	return false;
}
function load_into_popup(url) {
    prepare_load();
	$('#popup').load(url, function() {
        show_main_popup();
	});
	return false;
}
//comments
function accept_comment(comment_id) {
	jQuery.post(is_url+'function_update_on_fly.php', {'action': 'accept_comment','comment_id': comment_id}); 
	$('#comment_'+comment_id).hide();
}
function remove_comment(comment_id) {
	jQuery.post(is_url+'function_update_on_fly.php', {'action': 'remove_comment', 'comment_id': comment_id}); 
	$('#comment_'+comment_id).hide();
}
function delete_comment(comment_id) {
	jQuery.post('function_update_on_fly.php', {'action': 'delete_comment', 'comment_id': comment_id}); 
	$('#comment_'+comment_id).hide();
}
//streaming link
function accept_other_informations(other_informations_id) {
	jQuery.post(is_url+'function_update_on_fly.php', {'action': 'accept_other_informations', 'other_informations_id': other_informations_id}); 
	$('#other_informations_'+other_informations_id).hide();
}
function remove_other_informations(other_informations_id) {
	jQuery.post(is_url+'function_update_on_fly.php', {'action': 'remove_other_informations','other_informations_id': other_informations_id}); 
	$('#other_informations_'+other_informations_id).hide();
}
//edit info
function edit_site_info(site_id) {
	prepare_load();
	$('#popup').load(is_url+'pages/edit_site_info.php', {'site_id': site_id}, function() { show_main_popup(); });
}
function edit_site_info_by_user(site_id) {
	prepare_load();
	$('#popup').load(is_url+'pages/edit_site_info_by_user.php', {'site_id': site_id}, function() { show_main_popup(); });
}
//favourites
function add_to_my_desktop (site_id) {
   if (site_id!=''){ 
	   $('#myfaves_content').load(is_url+'myfaves.php?', {'action': 'addto_mydesktop', 'site_id': site_id}); 
   }
}
function remove_from_my_desktop (site_id) {
   if (site_id!=''){  
	   $('#myfaves_content').load(is_url+'myfaves.php', {'action': 'removefrom_mydesktop', 'site_id': site_id});
   }
}
function add_to_my_personal_websites (my_personal_websites_link) {
   if (my_personal_websites_link!=''){  
	   $('#myfaves_content').load(is_url+'myfaves.php', {'action': 'add_to_my_personal_websites', 'my_personal_websites_link': my_personal_websites_link});
   }
}
function remove_from_my_personal_websites (my_personal_websites_link) {
   if (my_personal_websites_link!=''){  
	   $('#myfaves_content').load(is_url+'myfaves.php', {'action': 'remove_from_my_personal_websites', 'my_personal_websites_link': my_personal_websites_link});
   }
}
//for movies version (gia visti)
function remember_just_watched (site_id) {
   if (site_id!=''){ 
	   $('#myfaves_content').load(is_url+'myfaves.php', {'action': 'remember_just_watched', 'site_id': site_id}); 
   }
}
function forget_just_watched (site_id) {
   if (site_id!=''){  
	   $('#myfaves_content').load(is_url+'myfaves.php', {'action': 'forget_just_watched', 'site_id': site_id});
   }
}

//otherthings
function extract_domain(url_to_extract) {
	var domain = url_to_extract.match(/(\w+):\/\/([^/:]+)(:\d*)?([^# ]*)/);
	domain = RegExp.$2;
	//domain = domain.replace('www.','');
	return domain;
}
//SCRIPT COOKIE
function getCookie(c_name) {
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
		{
		c_start=c_start + c_name.length+1;
		c_end=document.cookie.indexOf(";",c_start);
		if (c_end==-1) c_end=document.cookie.length;
		return unescape(document.cookie.substring(c_start,c_end));
		}
	  }
	return "";
}
function DeleteCookie (name) {
	var exp = new Date();
	exp.setTime (exp.getTime() - 1);
	var cval = GetCookie (name);
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
function setCookie ( name, value, expires, path, domain, secure ) {
    name=name.replace(/ /g,''); //aggiunto da me
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	/*
	if the expires variable is set, make the correct
	expires time, the current script below will set
	it for x number of days, to make it for hours,
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
	/*expires = expires * 1000 * 60 * 60 * 24;*/ //originale
	expires = expires * 1000 * 60; //fatto in minuti
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}
//per aggiornare database 
function attach_file( p_script_url ) {
      // create new script element, set its relative URL, and load it
      var script = document.createElement( 'script' );
      script.src = p_script_url;
      document.getElementsByTagName( 'head' )[0].appendChild( script );
}
function remove_file( p_script_url ) {
      // create new script element, set its relative URL, and load it
      script = document.createElement( 'script' );
      script.src = p_script_url;
      document.getElementsByTagName( 'head' )[0].removeChild( script );
}


function urlencode(str) {
    return escape(str).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
}
function urldecode(str) {
	str = str.replace(/'+'/g, ' ');
	str = unescape(str);
	return str;
}

function clean_string(string) { //uguale a php
	string=string.toLowerCase();
	string= string.replace(/'/g,'');
	string= string.replace(/,/g,'');
	string= string.replace(/- /g,'');
	string= string.replace(/ -/g,'');
	string= string.replace(/  /g,'-');
	string= string.replace(/ /g,'-');
	string= string.replace(/'\'/g,'-');
	return string;
}


function change_translation() {
	if(event.button==2) {
		alert('to develop the change');
	}
}

