var working = 0;

function check_if_cached(id)
{
  if (!tpl[id])
  {
    if (id == "i902")
      tpl[id] = ct("about");
    else
    if (id == "i903")
      tpl[id] = ct("terms");
    else
    if (id == "i904")                                                                                                            
      tpl[id] = ct("contact");
    else
    if (id == "i905")
      tpl[id] = ct("copyright");
  }
}

function inc_views(code, id)
{
  var url = hostname + "/updates/iu.php?code=" + code + "&" + id;

  if (!tpl[code + id])
    tpl[code + id] = sv(url);
}

function update(id, params, history)
{
  if (working == 1)
    return;
  else
    working = 1;

  cursor_wait();

  var url = hostname + "/updates/iu.php?code=" + id + "&" + params;

  switch(id)
  {
    case "login":
      eval(svs(hostname + '/updates/iu.php?code=' + id + '&username=' + document.getElementById("username_text").value + '&password=' +document.getElementById("password_text").value));
      finish(id);      
      break;
    case "i902":
    case "i903":
    case "i904":
    case "i905":
      set_popup_bounds();
      check_if_cached(id); 
      $('#popup_div').html(tpl[id]);
      $('#b1').css("margin-right", "17px");
      $('#b1').css('overflow', 'hidden');
      if (browser == 'msie')
        document.documentElement.scrollTop = 0 + "px";
      $('#popup_div').fadeIn('normal');
      finish(id, true);
      break;
    default:   
      if (!ajax)
      {
        if (params)
          window.location = hostname + "/index.php?page=" + id + "&" + params;
        else
          window.location = hostname + "/" + id;
          
        return false;
      }
                                                    
      if (!tpl[id + params])
        $('#cache_div').load(url, {}, function(){if (skip) skip = null; else func(id, params, document.getElementById('sub_center_column'));f2(id);});
      else
      {      
        setTimeout(function()
        {                             
          $('#center_topic').html(titles[id+params]);
          replaceHtml(document.getElementById('sub_center_column'), tpl[id+params]);     
          fix_btns_classes(id);
          document.documentElement.scrollTop = 0 + "px";
          finish(id); 
        }, 50); 
      }
  } 
}

function get_page_width()
{
  if (window.innerWidth)
    return window.innerWidth;
  else
  if (document.documentElement && document.documentElement.clientWidth)
    return document.documentElement.clientWidth;
  else
  if (document.body)
    return document.body.clientWidth;
}

function get_page_height()
{
  if (window.innerHeight)
    return window.innerHeight;
  else
  if (document.documentElement && document.documentElement.clientHeight)
    return document.documentElement.clientHeight;
  else
  if (document.body)
    return document.body.clientHeight;
}

function set_popup_bounds()
{           
  var newWidth = get_page_width();
  var newHeight = get_page_height();

  //Configure popup_div
  if (typeof(window.innerWidth) == 'number')
  {
    document.getElementById('popup_div').style.left = newWidth / 2 - 420 + "px";
    document.getElementById('popup_div').style.height = newHeight - 115 + "px";
  }
  else
  {
    document.getElementById('popup_div').style.left = newWidth / 2 - 420 + "px";
    document.getElementById('popup_div').style.height = newHeight - 120 + "px";
  }  
}

function news_more(id)
{               
  inc_views('inc_news_views', 'id='+id);   
  $('#news_link_'+id).hide();  
  $('#news_body_'+id).toggle('fast');
  $('#news_title_'+id).addClass('link');
}

function general_more(id)
{               
  inc_views('inc_general_views', 'id='+id);   
  $('#news_link_'+id).hide();  
  $('#news_body_'+id).toggle('fast');
  $('#news_title_'+id).addClass('link');
}
     
function news_title_click(id)                                       
{
  if (document.getElementById('news_title_'+id).className == 'link') 
    $('#news_c_'+id).toggle('fast');     
}
                                  
function online_users()
{
  var url = hostname + "/updates/iu.php?code=online_users&page=" + page;

  $('#cache_div').load(url, {}, function(){func('online_users', '', document.getElementById('online_div'));});
}

function vote_poll(poll_id, poll_aa, poll_div)
{
  var xs = (svs(hostname + "/updates/iu.php?code=poll_vote&id=" + poll_id + "&aa=" + poll_aa));
  eval(xs);

  xs = null;
}

function view_poll(poll_id, poll_div, code)
{
  if (working == 1)
    return;
  else
    working = 1;
    
  var url = hostname + "/updates/iu.php?code=" + code + "&id=" + poll_id;   
  cursor_wait();

  $('#' + poll_div).slideUp('normal', function(){$('#cache_div').load(url, {}, function(){if (skip) skip = null; else $('#' + poll_div).html(func2(code)); $('#' + poll_div).slideDown('normal', function(){finish(code);});});});
}
 

function view_video(id, par)
{
  if (par == 1)
  {
    inc_views('inc_video_views', 'id=' + id);
    $('#video_' + id + '_1').fadeToggle("normal", function(){$('#video_' + id + '_2').slideToggle("slow", function(){$('#embed_' + id).show();});});
  }
  else
  if (par == 2)
    $('#embed_' + id).hide("fast", function(){$('#video_' + id + '_2').fadeToggle("normal", function(){$('#video_' + id + '_1').slideToggle("slow");});});
}

function view_photos(b_id, page_id, limit, history)
{
  var url = hostname + "/updates/iu.php?code=get_photos&id=" + b_id + "&page_id=" + page_id + "&limit=" + limit;
  if (document.getElementById('photos_div'))
    $('#cache_div').load(url, {}, function(){func('get_photos', '', 'photos_div', true);});
  else
    $('#cache_div').load(url, {}, function(){func('get_photos', '', 'sub_center_column', true);});
  url = null;
}

function member_photos(u_id, page_id, limit)
{
  var url = hostname + "/updates/iu.php?code=member_photos&id=" + u_id + "&page_id=" + page_id + "&limit=" + limit;

  $('#cache_div').load(url, {}, function(){func('member_photos', '', 'photos_div', true);});

  url = null;
}

function func(id, params, el, classic)
{
  if (!tpl[id] || !cache)
  {
    $.ajax({
      url: hostname + "/updates/get_tpl.php?code=" + id + "&rsid=" + parseInt(Math.random()*99999999),
      cache: false,
      async: true,
      success: function(html){
        tpl[id] = html;
        tpl[id+params] = eval(tpl[id]);
        if(typeof(title) != 'undefined') 
          titles[id+params] = title;                 
        if (!classic)
          replaceHtml(el, tpl[id+params]); 
        else
          $('#'+el).html(tpl[id+params]);           
      }
    });
  }
  else
  {
    tpl[id+params] = eval(tpl[id]);
    if(typeof(title) != 'undefined') 
      titles[id+params] = title;     
    if (!classic)
      replaceHtml(el, tpl[id+params]);
    else
      $('#'+el).html(tpl[id+params]);    
  }
}

function func2(id, params)
{
  if (!tpl[id] || !cache)
    tpl[id] = cp(id);
  tpl[id+params] = eval(tpl[id]);
  if(typeof(title) != 'undefined') 
    title[id+params] = title;
  return tpl[id+params];
}
 
function finish(id)
{                         
  //$('#left_buttons').html(left_buttons);
 // fix_btns_classes(id);
        
  pngfix();
  parseStylesheets();

  cursor_clear();

  working = 0;
}

function f2(id)
{
  if(typeof(title) != 'undefined') 
    $('#center_topic').html(title); 
  document.documentElement.scrollTop = 0 + "px";
  finish(id);          
}

function fix_btns_classes(id)
{    
  o = 'button';
  n = 'button_with';
  document.getElementById("news").className = o;
  document.getElementById("bands").className = o;
  document.getElementById("interviews").className = o;
  document.getElementById("videos").className = o;
  document.getElementById("photos").className = o; 
  document.getElementById("mp3").className = o; 
  document.getElementById("demos").className = o; 
  document.getElementById("discography").className = o;
  document.getElementById("poems").className = o;
  document.getElementById("concerts").className = o;
  document.getElementById("general").className = o;
  document.getElementById("concerts").className = o;
  document.getElementById("contact").className = o;
           
  switch(id)
  {
    case 'news':
      document.getElementById("news").className = n;
      break;
    case 'bands':
    case 'view_band_page': 
      document.getElementById("bands").className = n;
      break;
    case 'interviews':
    case 'view_interview':
      document.getElementById("interviews").className = n;
      break;
    case 'videos':
      document.getElementById("videos").className = n;
      break;
    case 'photos':          
      document.getElementById("photos").className = n;
      break;
    case 'mp3':
      document.getElementById("mp3").className = n;
      break;
    case 'demos':
    case 'view_band_demos':
      document.getElementById("demos").className = n;
      break;
    case 'discography':
      document.getElementById("discography").className = n;
      break;
    case 'poems':
      document.getElementById("poems").className = n;
      break;
    case 'concerts':
      document.getElementById("concerts").className = n;
      break;
    case 'general':
      document.getElementById("general").className = n;
      break;
    case 'guestbook':
      document.getElementById("guestbook").className = n;
      break;
    case 'contact':
      document.getElementById("contact").className = n;
      break; 
  }
  o = null;
  n = null;
}

function cursor_wait()
{
  $('#loading_bar').show();
  $('#loading_mini').show();   
 // $('#load_div').show();
}

function cursor_clear()
{
  $('#loading_bar').hide();
  $('#loading_mini').hide();
//  $('#load_div').hide();
}

function toggle_comments(id)
{
  if (document.getElementById('comments_' + id).style.display == 'block')
    $('#view_comments_' + id).html('Προβολή Σχολίων');
  else
    $('#view_comments_' + id).html('Απόκρυψη Σχολίων');
  $('#comments_' + id).toggle('fast');
}

function add_comment(type, id)
{
  var com = 'coco_' + id;
  var user = document.getElementById('username_text_' + id).value;
  var text = document.getElementById('comments_textarea_' + id).value;

  if (user.length == 0)
  {
    jAlert('Εισάγετε Ψευδώνυμο', 'Ειδοποίηση');
    return;
  }
  
  if (text.length == 0)
  {
    jAlert('Εισάγετε Σχόλιο', 'Ειδοποίηση');
    return;
  }
  
  text = text.replace(/\n/g, '<br />');  
       
  var returned = svs(hostname + '/updates/iu.php?code=add_comment&type=' + type + '&id=' + id + '&user=' + user + '&text=' + text);
       
  if (returned.substring(0, 4) != '<div')
  {
    jAlert(returned, 'Ειδοποίηση');
    return;   
  }    
       
  $('#' + com).append(returned);
  
  $('#a_c_' + id).toggle("fast");
  $('#add_comment_' + id).fadeToggle("fast");
  
  $('#comments_counter_' + id).html(eval($('#comments_counter_' + id).html()) + 1);
         
  document.getElementById('comments_textarea_' + id).value = '';
  
  com = null;
  user = null;
  text = null;
}

function del_comment(id, n)
{                       
  jConfirm('Θέλετε σίγουρα να διαγραφεί το σχόλιο?', 'Επιβεβαίωση', function(r) {
    if (r)
    {                         
      svs(hostname + '/updates/iu.php?code=delete_comment&id=' + id);                         
      $('#c_b_' + id).hide();
      var num = eval($('#comments_counter_' + n).html());
      if (num > 1)
        $('#comments_counter_' + n).html(num - 1); 
      else
        $('#comments_counter_' + n).html("0");
      $('#sub_comment_' + id).fadeToggle("fast");  
    }
  });             
}

function add_band_comment(b_id, ref)
{                         
  var user = document.getElementById('g_username_text').value;
  var text = document.getElementById('g_comments_textarea').value;

  if (user.length == 0)
  {
    jAlert('Εισάγετε Ψευδώνυμο!', 'Ειδοποίηση');
    return;
  }
  
  if (text.length == 0)
  {
    jAlert('Εισάγετε Σχόλιο!', 'Ειδοποίηση');
    return;
  }
  
  text = text.replace(/\n/g, '<br />');    
  var returned = svs(hostname + '/updates/iu.php?code=add_comment&type=' + ref + '&id=' + b_id + '&user=' + user + '&text=' + text);
  if (returned.substring(0, 4) != '<div')
  {
    jAlert(returned, 'Ειδοποίηση');
    return;   
  }                                                     
    
  $('#comments_div').append(returned);
  
  $('#add_comment_btn').toggle("fast");
  $('#add_comment_box').fadeToggle("fast");
                                                                                        
  document.getElementById('g_comments_textarea').value = '';
              
  user = null;
  text = null;
}



function del_band_comment(id)
{                       
  jConfirm('Θέλετε σίγουρα να διαγραφεί το σχόλιο?', 'Επιβεβαίωση', function(r) {
    if (r)
    {                         
      svs(hostname + '/updates/iu.php?code=delete_comment&id=' + id);                         
      $('#c_b_' + id).hide();                                                           
      $('#sub_comment_' + id).fadeToggle("fast");  
    }
  });             
}

function add_guestbook_comment()
{                             
  var user = document.getElementById('g_username_text').value;
  var text = document.getElementById('g_comments_textarea').value;
                                                     
  if (user.length == 0)
  {
    jAlert('Εισάγετε Ψευδώνυμο!', 'Ειδοποίηση');
    return;
  }
  
  if (text.length == 0)
  {
    jAlert('Εισάγετε Σχόλιο!', 'Ειδοποίηση');
    return;
  }
  
  text = text.replace(/\n/g, '<br />');  
       
  var returned = svs(hostname + '/updates/iu.php?code=add_comment&type=' + 10 + '&id=' + 1 + '&user=' + user + '&text=' + text);
     
  if (returned != '')
  {
    jAlert(returned, 'Ειδοποίηση');
    return;   
  }        
     
  user = null;
  text = null;
  
  update('guestbook');
}


function del_guestbook_comment(id)
{                       
  jConfirm('Θέλετε σίγουρα να διαγραφεί το σχόλιο?', 'Επιβεβαίωση', function(r) {
    if (r)
    {             
      var returned = svs(hostname + '/updates/iu.php?code=delete_comment&id=' + id);            
      if (returned == 'ok')
        update('guestbook');  
      else
        alert(returned);
    }
  });             
}

function view_photo(id, b_id, link)
{             
  $('#load_div').show();         
  $('#photo_div').fadeIn(1000);
  $('#photo_div').load(hostname + "/updates/iu.php?code=view_photo&id="+id+"&b_id="+b_id + "&rsid=" + parseInt(Math.random()*99999999));
}                                    

function resize_photo_div(image)
{                   
  $('#main_img_1').css('max-width', get_page_width() - 40 + 'px');
  $('#main_img_1').css('max-height', get_page_height() - 100 + 'px');
  $('#main_img_1').css('height', get_page_height() - 100 + 'px');     
           
  $('#main_img_1').show();     
  
  $('#load_div').hide();
                                                         
  $('#main_img_2').css('width', image.width + 'px');  
  $('#main_img_2').css('height', image.height + 'px');             
                                       
  $('#main_img_1').css('left', (get_page_width()/2)-(image.width/2) + "px");       
  $('#main_img_1').css('top', (get_page_height()/2)-(image.height/2) + 25 + "px");   
                                                                            
  $('#main_img_2').css('left', (get_page_width()/2)-(image.width/2) + "px");            
  $('#main_img_2').css('top', (get_page_height()/2)-(image.height/2) + 25 + "px");  
    
  $('#photo_title').css('width', image.width + 8 + 'px'); 
  $('#photo_title').css('left', (get_page_width()/2)-(image.width/2) + "px");       
  $('#photo_title').css('top', (get_page_height()/2)-(image.height/2) - 30 + "px");
     
  $('#photo_ref').css('left', (get_page_width()/2)+(image.width/2) - 145 + "px");            
  $('#photo_ref').css('top', (get_page_height()/2)+(image.height/2) + 6 + "px");    
                   
  $('#photo_ref').html('Εικόνα '+(counter+1)+' από '+r.length); 
  $('#band_lbl').html(r[counter][1]);                 
               
  $('#main_img_1').fadeIn(1000);                                                                                            
  $('#main_img_2').fadeOut(1000, function(){ 
    view_info();  
    photo_work = 0;
    document.getElementById('main_img_2').src = document.getElementById('main_img_1').src;   
  });  
}

function page_resize()                        
{         
  if (document.getElementById('main_img_1'))          
    resize_photo_div(document.getElementById('main_img_1'));
}     

function register_btn()
{
  if (document.getElementById('reg_username_text').value == '')   
    jAlert('Εισάγετε Ψευδώνυμο!', 'Ειδοποίηση');
  else                        
  if (document.getElementById('reg_password_text').value == '')   
    jAlert('Εισάγετε Κωδικό!', 'Ειδοποίηση');
  else
  if (document.getElementById('reg_confirm_text').value == '')   
    jAlert('Εισάγετε Επιβεβαίωση!', 'Ειδοποίηση');
  else
  if (document.getElementById('reg_email_text').value == '')   
    jAlert('Εισάγετε Διεύθυνση e-mail!', 'Ειδοποίηση');
  else
  if (document.getElementById('reg_password_text').value != document.getElementById('reg_confirm_text').value)   
    jAlert('Λάθος Επιβεβαίωση Κωδικού!', 'Ειδοποίηση');
  else               
  {
    cursor_wait(); 
    $('#wait_message').show();     
    url = hostname + '/updates/iu.php?code=register_btn&username=' + document.getElementById('reg_username_text').value + "&password=" + document.getElementById('reg_password_text').value + "&email=" + document.getElementById('reg_email_text').value + "&label=" + document.getElementById('reg_label_text').value;     
    var asdf = $.ajax({url: url, async: false}).responseText;         
    $('#wait_message').fadeOut(250);
    eval(asdf);
    asdf = null;  
    cursor_clear(); 
  }
}

function change_password_btn()
{                                 
  if (document.getElementById('acc_password_text').value == '')   
    jAlert('Εισάγετε τον τρέχοντα Κωδικό!', 'Ειδοποίηση');
  else                                     
  if (document.getElementById('acc_new_text').value == '')   
    jAlert('Εισάγετε Νέο Κωδικό!', 'Ειδοποίηση');
  else
  if (document.getElementById('acc_confirm_text').value == '')   
    jAlert('Εισάγετε Επιβεβαίωση!', 'Ειδοποίηση');       
  else      
  if (document.getElementById('acc_new_text').value != document.getElementById('acc_confirm_text').value)   
    jAlert('Λάθος Επιβεβαίωση Κωδικού!', 'Ειδοποίηση');
  else             
  {
    cursor_wait(); 
    $('#wait_message').show();     
    url = hostname + '/updates/iu.php?code=change_password_btn&password=' + document.getElementById('acc_password_text').value + "&new=" + document.getElementById('acc_new_text').value;     
    var asdf = $.ajax({url: url, async: false}).responseText;         
    $('#wait_message').fadeOut(250);
    eval(asdf);
    asdf = null;  
    cursor_clear(); 
  }
} 

function forgot_btn()              
{     
  if (document.getElementById('for_username_text').value == '')   
    jAlert('Εισάγετε Ψευδώνυμο!', 'Ειδοποίηση');            
  else                              
  if (document.getElementById('for_email_text').value == '')   
    jAlert('Εισάγετε Διεύθυνση e-mail!', 'Ειδοποίηση');            
  else               
  {
    cursor_wait(); 
    $('#wait_message').show();     
    url = hostname + '/updates/iu.php?code=forgot_btn&username=' + document.getElementById('for_username_text').value + '&email=' + document.getElementById('for_email_text').value;     
    var asdf = $.ajax({url: url, async: false}).responseText;         
    $('#wait_message').fadeOut(250);
    eval(asdf);
    asdf = null;  
    cursor_clear(); 
  }
}

function artist_info(id)
{                  
  $('#artist_info_div').load(hostname + '/updates/iu.php?code=view_artist&id=' + id);  
  $('#artist_info_div').css('left', ((get_page_width()/2) - 250) + "px");                                          
  $('#artist_info_div').css('top', ((get_page_height()/2) - 150) + "px");     
  $('#artist_info_div').fadeIn(750);   
}

function search_box_press(e, str)
{       
  var unicode = e.keyCode? e.keyCode : e.charCode;   
  
  if (unicode == 13)
  {
    if (str == '')   
    {
      jAlert('Εισάγετε κείμενο προς Αναζήτηση!'); 
      return false;
    }
    update('search', 'string=' + str);
    return false; 
  }
  else
    return true;
}
