function goTo(address, timeout)
{
  if(timeout > 0)
  {
    window.setTimeout(function(){document.location.href = address;}, timeout);
  }
  else
  {
    document.location.href = address;
  }
}

function confirmQuestion(question, address)
{
  if(confirm(question))
  {
    goTo(address);
  }
}

function showEmail(name, domain, cy, text)
{
  return '<a href="mailto:'+name+'@'+domain+'.'+cy+'">'+text+'</a>';
}


function popUp(url, p)
{
  popup = window.open(url, 'popup', 'width='+p.width+',height='+p.height+',status='+p.status+',location='+p.location+',scrollbars='+p.scrollbars+',resizable='+p.resizable+',menubar='+p.menubar+',toolbar='+p.toolbar+',top='+p.top+',left='+p.left);
  popup.focus();
}

function toggleLayer(open_id, close_class)
{
  $('.'+close_class).hide();

  if($('#'+open_id).css('display') == 'none')
  {
    $('#'+open_id).fadeIn();
  }
  else
  {
    $('#'+open_id).fadeOut();
  }
}

function ajax_viewVote(mode, survey_question_id)
{
  $('div[name=answers]').slideUp('slow');
  $('div[name=options]').slideDown('slow');

  $.ajax({
    url: 'ajax/request-viewVote.php',
    data: {p_mode: mode, p_survey_question_id: survey_question_id},
    type: 'POST',
    async: false,
    cache: false,
    success: function(val) {
      $('#answers_'+survey_question_id).slideUp('slow', function(){$(this).html(val).slideDown('slow');});
      $('#options_'+survey_question_id).slideUp('slow');
    }
  });
}

function ajax_insertVote(survey_question_id, survey_answer_id)
{
  $.ajax({
    url: 'ajax/request-insertVote.php',
    data: {p_survey_question_id: survey_question_id, p_survey_answer_id: survey_answer_id},
    type: 'POST',
    async: false,
    cache: false,
    success: function(val) {
      if(val != "true")
      {
        alert(val);
      }

      ajax_viewVote('view', survey_question_id);
    }
  });
}

function ajax_setUserState(field, user_id, ustate)
{
  $.ajax({
    url: '../ajax/request-setUserState.php',
    data: {p_field: field, p_user_id: user_id, p_state: ustate},
    type: 'POST',
    async: false,
    cache: false
  });
}

function checkVideo(video_id)
{
  $('#'+video_id+'_error').hide();

  $.ajax({
    url: 'ajax/request-checkVideo.php',
    data: {p_video_url: $('#'+video_id).val()},
    type: 'POST',
    async: false,
    cache: false,
    success: function(val) {
      if(val == "false")
      {
        $('#'+video_id+'_error').slideDown('slow');
      }
    }
  });
}

function showTwitterPosts(site_id)
{
  $('#twitter-posts').html('<img src="images/ajax-loader.gif">');

  $.ajax({
    url: 'ajax/request-showTwitterPosts.php',
    data: {site: site_id},
    type: 'POST',
    async: true,
    cache: false,
    success: function(val) {
      $('#twitter-posts').hide().html(val).fadeIn('slow');
    }
  });
}

function showComments(site_id, blog_id, table)
{
  $.ajax({
    url: 'ajax/request-showComments.php',
    data: {p_table: table, p_blog_id: blog_id, site: site_id},
    type: 'POST',
    async: false,
    cache: false,
    success: function(val) {
      refreshCommentsNumber(blog_id, table);
      $('#all_comments_'+blog_id).html(val).slideDown('slow');
    }
  });
}

function refreshCommentsNumber(blog_id, table)
{
  $.ajax({
    url: 'ajax/request-refreshCommentsNumber.php',
    data: {p_table: table, p_blog_id: blog_id},
    type: 'POST',
    async: false,
    cache: false,
    success: function(val) {
      $('#balloon_number_'+blog_id).html(val);
    }
  });
}

function writeComment(blog_id, table)
{
  $.ajax({
    url: 'ajax/request-writeComment.php',
    data: {p_table: table, p_blog_id: blog_id, p_comment: $('#write_comment_'+blog_id).val()},
    type: 'POST',
    async: false,
    cache: false,
    success: function(val) {
      if(val) {
        alert(val);
      } else {
        $('#write_comment_'+blog_id).val('');
        showComments(1, blog_id, table);
      }
    }
  });
}

function setActive(table, row_id, active, user_id)
{
  $.ajax({
    url: '../ajax/request-setActive.php',
    data: {p_table: table, p_row_id: row_id, p_active: active, p_user_id: user_id},
    type: 'POST',
    async: false,
    cache: false
  });
}

function setVarDB(table, row_id, field, value)
{
  $.ajax({
    url: '../ajax/request-setVarDB.php',
    data: {p_table: table, p_row_id: row_id, p_var: field, p_val: value},
    type: 'POST',
    async: false,
    cache: false
  });
}

function contentReport(table, row_name, row_id)
{
  if(table && row_name && row_id)
  {
    $.ajax({
      url: 'ajax/request-contentReport.php',
      data: {p_table: table, p_row_name: row_name, p_row_id: row_id},
      type: 'POST',
      async: false,
      cache: false,
      success: function(val) {
        $('#contentReport').css({top: ((document.documentElement.clientHeight / 2) - ($('#contentReport').height() / 2))+'px', left: (($(document).width() / 2) - ($('#contentReport').width() / 2))+'px'}).html(val).fadeIn('slow');
      }
    });
  }
}

function sendContentReport()
{
  var ajax_msg = $('#cr_msg').val();
  var ajax_table = $('#cr_table').val();
  var ajax_row_name = $('#cr_row_name').val();
  var ajax_row_id = $('#cr_row_id').val();

  if(ajax_msg && ajax_table && ajax_row_name && ajax_row_id)
  {
    $.ajax({
      url: 'ajax/request-sendContentReport.php',
      data: {p_table: ajax_table, p_row_name: ajax_row_name, p_row_id: ajax_row_id, p_msg: ajax_msg},
      type: 'POST',
      async: false,
      cache: false,
      success: function(val) {
        $('.ui-widget').fadeIn(2000);
        $('#contentReport #cr_layer_1').hide();
        $('#contentReport #cr_layer_2').fadeIn('slow');
      }
    });
  }
  else
  {
    $('#cr_msg').css({borderColor: '#f00'});
  }
}

function deleteQuizSession()
{
  $.ajax({
    url: 'ajax/request-deleteQuizSession.php',
    type: 'POST',
    async: false,
    cache: false
  });
}

function openGameLayer(game, top_game, left_game)
{
  top_game = (top_game > 0) ? top_game : 0;
  left_game = (left_game > 0) ? left_game : 0;

  var flash_width = 0;
  var flash_height = 0;

  switch(game)
  {
    case 'infotour':
      flash_width = 640;
      flash_height = 365;
    break;
    case 'game_association':
      flash_width = 1000;
      flash_height = 350;
    break;
    case 'game_searchgame':
      flash_width = 800;
      flash_height = 600;
    break;
  }

  if(flash_width > 0 && flash_height > 0)
  {
    $.ajax({
      url: 'ajax/request-gameLayer.php',
      data: {p_game: game, p_flash_width: flash_width, p_flash_height: flash_height},
      type: 'POST',
      async: false,
      cache: false,
      success: function(val) {
        $('#site-shadow').fadeIn('slow');
        $('#game-layer').css({width: flash_width+'px', height: flash_height+'px', top: ((document.documentElement.clientHeight / 2) - top_game)+'px', left: ((document.documentElement.clientWidth / 2) - left_game)+'px'}).fadeIn('slow');
        $('#game-layer #gcontent').html(val);
      }
    });
  }
}

function closeGameLayer()
{
  $('#site-shadow').fadeOut('slow');
  $('#game-layer').hide();
  $('#game-layer #gcontent').html('');
}

function openQuiz(quiz_id)
{
  $('#site-shadow').fadeIn('slow');
  $('#quiz-layer').css({top: ((document.documentElement.clientHeight / 2) - 250)+'px', left: ((document.documentElement.clientWidth / 2) - 249)+'px'}).fadeIn('slow');
  playQuiz(quiz_id);
}

function playQuiz(quiz_id, quiz_start, answer)
{
  $.ajax({
    url: 'ajax/request-playQuiz.php',
    data: {p_quiz_id: quiz_id, p_answer: answer, p_quiz_start: quiz_start},
    type: 'POST',
    async: false,
    cache: false,
    success: function(val) {
      $('#quiz-layer #qcontent').html(val);
    }
  });
}

function closeQuiz(address)
{
  $('#site-shadow').fadeOut('slow');
  $('#quiz-layer').fadeOut('slow');
  deleteQuizSession();

  if(address) {
    goTo(address);
  }
}

function openSurveyLayer()
{
  $.ajax({
    url: 'ajax/request-SurveyLayer.php',
    data: {},
    type: 'POST',
    async: false,
    cache: false,
    success: function(val) {
      if(val)
      {
        $('#site-shadow').fadeIn('slow');
        $('#survey-layer').css({top: ((document.documentElement.clientHeight / 2) - ($('#survey-layer').height() / 2))+'px', left: ((document.documentElement.clientWidth / 2) - ($('#survey-layer').width() / 2))+'px'}).fadeIn('slow');
      }
    }
  });
}

function closeSurveyLayer()
{
  $('#site-shadow').fadeOut('slow');
  $('#survey-layer').hide();
  $('#survey-layer #scontent').html('');
}

function setPercentPoints(p_event, p_percent)
{
  $.ajax({
    url: 'ajax/request-setPercentPoints.php',
    data: {p_percent: p_percent, p_event: p_event},
    type: 'POST',
    async: false,
    cache: false,
    success: function() {
      goTo('game-result.php?g='+p_event+'&c='+p_percent);
    }
  });
}

function setPoints(p_event)
{
  $.ajax({
    url: 'ajax/request-setPoints.php',
    data: {p_points: p_points, p_event: p_event},
    type: 'POST',
    async: false,
    cache: false
  });
}

var votedTimeOut = new Array();

function star_VoteOver(star_id, unique_id)
{
  votedTimeOut[unique_id] = clearTimeout(votedTimeOut[unique_id]);

  var s = 0;

  for(s = 1; s <= star_id; s++)
  {
    $('#star-'+s+'-'+unique_id).attr('src', $('input[name=starvote_src_hover]').val());
  }

  for(s = (star_id + 1); s <= 5; s++)
  {
    $('#star-'+s+'-'+unique_id).attr('src', $('input[name=starvote_src_standard]').val());
  }
}

function star_VoteOut(unique_id)
{
  votedTimeOut[unique_id] = window.setTimeout(function(){
    for(var s = 1; s <= 5; s++)
    {
      $('#star-'+s+'-'+unique_id).attr('src', $('#star-'+s+'-'+unique_id).attr('mainsrc'));
    }
  }, 250);
}

function star_VoteClick(star_id, unique_id)
{
  var s = 0;

  for(var s = 1; s <= star_id; s++)
  {
    $('#star-'+s+'-'+unique_id).attr({src: $('input[name=starvote_src_active]').val(), onClick: '', onMouseOver: '', onMouseOut: ''});
  }

  for(s = star_id; s <= 5; s++)
  {
    $('#star-'+s+'-'+unique_id).attr({onClick: '', onMouseOver: '', onMouseOut: ''});
  }

  $('#'+unique_id).effect("pulsate", { times: 1 }, 250);

  window.setTimeout(function(){
    $.ajax({
      url: 'ajax/request-star_VoteClick.php',
      data: {p_unique_id: unique_id, p_star_id: star_id},
      type: 'POST',
      async: false,
      cache: false,
      success: function(val) {
        $('#'+unique_id).html(val).effect("pulsate", { times: 1 }, 250);
      }
    });
  }, 1000);
}

function showEventCalender(calendar_id, type, month, year)
{
  $.ajax({
    url: 'ajax/request-eventCalendar.php',
    data: {p_calendar_id: calendar_id, p_type: type, p_month: month, p_year: year},
    type: 'POST',
    async: true,
    cache: false,
    success: function(val) {
      $('#'+calendar_id).html(val);
    }
  });
}

function setEventDescription(description)
{
  $('#event-description').html(description);
}
