/**
 * Scripting for omegatiming.pl
 * 
 * @author: Kuba Porębski <kubar3k@gmail.com>
 * @copyright: Omega Kraków
 * 
 */

Om = {};

Om.Conf = {};
Om.Conf.ajaxEnabled = !true;

Om.init = function() {
  this.changeImage();
  if (this.Conf.ajaxEnabled) {
    this.Events.attachToMenu();
    this.Events.attachToContent();
  }
  
  
  //Ext.History.init();
}

Om.Ajax = {};
Om.Ajax.getData = function(url) {
  if (!this.check(url)) {
    window.location.href = url;
    return;
  }
  
  this.history.push(url);
  url += "&ajax=true";
  
  var kesz = this.cache(url);
  if (kesz != null) {
  	this.setHTML(kesz);
	  return;
  }	
  
  this.loading(true);
  
  $.ajax({
    url: url,
    method: "GET",
    success: Om.Ajax.setPage,
	  error: function(xhr, type) {
		  alert('Wystąpił nieoczekiwany błąd. Szczegóły: \n\nBłąd: ' + type + '\nStatus: ' + xhr.status);
		  Om.Ajax.loading(false);
	  }
  })
}

Om.Ajax.setPage = function(response) {
  Om.Ajax.setHTML(response);
  Om.Ajax.cache(this.url, response);
  Om.Ajax.loading(false);
  
  //Ext.History.add(this.url.substr( this.url.indexOf("?") + 1 ).replace(/\&ajax=true/i, ''));
}

Om.Ajax.setHTML = function(html) {
  $("#content").html(html);
  Om.Events.attachToContent();
  
  $("#goback a").unbind('click').click(function()
  {
    //Ext.History.back();
    var prev = Om.Ajax.history[ Om.Ajax.history.length - 2 ];
    
    Om.Ajax.getData(prev);
    
    return false;
  });
}

Om.Ajax.cacheTime = 120; // ilosc sekund

(function(){
  var C = {};
  Om.Ajax.cache = function(url, content) {
    var time = (new Date).getTime();

    // zapis do keszu
    if (!!content) {

      C[url] = {
        time: time,
        content: content
      }

    // wczytanie z kesza
    }else{
      if (typeof C[url] != "undefined") {

        var storedTime = C[url].time;
        if ((time - storedTime) >= (Om.Ajax.cacheTime * 1000)){
          delete C[url];
          return null;
        }else
          return C[url].content;
      }else{
        return null;
      }


    }  
  }
})();

Om.Ajax.loading = function(state) {
  state = !!state;
  
  document.documentElement.style.cursor = state ? "progress" : "";
}

Om.Ajax.check = function(url){ 
  if (!Om.Conf.ajaxEnabled)
    return true;
    
  if (typeof url == "string")
    return !(url.indexOf("page=") < 0);
  return false;
};


Om.random = function(a, b) {
  // kod bazuje z mootools 1.2 - $random
  return Math.floor(Math.random() * (b - a + 1) + a);
}

Om.Ajax.history = [];

Om.Events = {};
Om.Events.attachToMenu = function() {
  $("#menu a.ajax").click(this.onclick);
}

Om.Events.onclick = function(){
  Om.Ajax.getData(this.href);
  return false;
}

Om.Events.attachToContent = function() {
  $("#content").find("a").bind('click', Om.Events.onclick);
}

Om.changeImage = function() {
  //harasim 6,12,2009 - zmiana delay z 30 na 5
  var headers = 31, curr = 0, idx = 0, queue = [], delay = 5;
  
  queue[0] = Number($("#headimg")[0].src.replace(/[^\d]/g, ''));
  
  var generateQueue = function(clear){
    
    // usun wszystkie elementy z kolejki
    if (clear) while(queue.length) queue.pop();
	
    // ustaw tablice queue
    for(var i = 0, r; i < headers; i++) {
      
      // wylosuj numerek
      r = Om.random(1, headers);
	  
      // ale unikaj powtarzania sie numerkow
      while (queue.indexOf(r) >= 0) r = Om.random(1, headers);
      
      // dodaj nr do kolejki
      queue[i] = r;
    }
  }
  
  generateQueue();
    
  var after = function(){
    
    curr = queue[idx];
    if (idx < headers - 1) {
      idx++; 
    }else{
      idx = 0;
      generateQueue(true);
    }
    
    this.src = "images/header" + curr + ".jpg";
    this.onload = function()
    {
      $("#headimg").fadeIn(800);
    }
    setTimeout(change, delay * 1000);
  }
  
  var change = function() {
    $("#headimg").fadeOut(1000, after);
  }
  
  setTimeout(change, delay * 1000);
}

if ($.browser.msie) {
	Array.prototype.indexOf = function(el) {
		for(var i = 0; i < this.length; i++) if (this[i] == el) return i;
		return -1;
	}
}

baseURL = location.protocol + "//" + location.host + location.pathname;
/*
Ext.History.on('change', function(token)
{
  var url = baseURL + "?" + token;
  
  Om.Ajax.getData(url);
});
*/

$(function(){
  Om.init();
});
