// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var min=8;
var med=13
var max=18;

function checkCommentPagination(params){
    if(params.evalJSON().page_text){
	$$('ul.nav_menu a').first().click();
    }
    if(params.evalJSON().page_question){
	$$('ul.nav_menu a')[1].simulate('click');

    }
    if(params.evalJSON().page_event){
	$$('ul.nav_menu a')[2].simulate('click');
	
    }
    if(params.evalJSON().page_notice){
	$$('ul.nav_menu a')[3].simulate('click');
	
    }
}

function increaseFontSize() {
   var p = document.getElementsByTagName('p');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=max) {
         s += 1;
      }
      p[i].style.fontSize = s+"px"
   }
}
function decreaseFontSize() {
   var p = document.getElementsByTagName('p');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=min) {
         s -= 1;
      }
      p[i].style.fontSize = s+"px"
   }   
}
function selectAllCheckBoxes(){
  $$('input.email_contacts').each(function(t){
  t.checked = 'checked';
  }
  );
}

/*function removeLink(event){
  event.findElement().remove();
}*/

function sendRequest(event){
  var hot_topic = this;
  new Ajax.Updater('mod_hot_topics',hot_topic.url,{
    method: 'get',
    onComplete: function(response){
      if($('mod_hot_topics')){new Effect.Highlight('mod_hot_topics')};
      hot_topic.removeHotTopicLink(); 
    }
  }); 
  event.stop();
}

function fetchHotTopicLinks(){
  $$('span.link_to_hot_topic a').each(function(l){
    var link = new HotTopic(l);
    link.reference.observe('click',sendRequest.bind(link)); 
  })      
}

function toggleUsersRated(event){
  var element = event.findElement();
  var usersRated = element.up(1).next()
  if(usersRated.style.display == ""){
    element.innerHTML = "kto oceniał?"
    element.removeClassName('bg_close');
  }
  if(usersRated.style.display == "none"){ 
    element.innerHTML = "zamknij"
    element.addClassName('bg_close');
  }
  //element.up(1).next().toggle();
  usersRated.toggle();
  event.stop();
}

function fetchWhoRated(){
  var links = $$('a.show_users_rated') 
  if(links.size() > 0){
    links.each(function(link){
      link.observe('click',toggleUsersRated);
    })
  }
}

function initFileUploads() {
    var fakeFileUpload = document.createElement('div');
    fakeFileUpload.className = 'fakefile';
    fakeFileUpload.appendChild(document.createElement('input'));
    var image = document.createElement('img');
    image.src='/images/bg-input.gif';
    fakeFileUpload.appendChild(image);
    var inputs = document.getElementsByTagName("input");
    for (var i=0;i<inputs.length;i++) {
        if (inputs[i].type != "file") continue;
        if (inputs[i].parentNode.className != "fileinputs") continue;
        inputs[i].className = "file hidden";
        var clone = null;
        for (var j=0; inputs[i].parentNode.childNodes[j]; j++) {
            if (inputs[i].parentNode.childNodes[j].className == "fakefile") {
                clone = inputs[i].parentNode.childNodes[j];
            }
        }
        if (clone == null) {
            clone = fakeFileUpload.cloneNode(true);
            clone.removeAttribute("id");
            inputs[i].parentNode.appendChild(clone);
        }
        inputs[i].relatedElement = clone.getElementsByTagName("input")[0];
        inputs[i].onchange = inputs[i].onmouseout = function () {
            this.relatedElement.value = this.value;
        }
    }
}

function stopSearchFileItems(){
  var formSearch = $('search_box_file_items')
  if(formSearch){
    formSearch.observe('submit',function(event){event.stop();});
  } 
}

function hideRatedUsers(){
  var users_rated = $$('div.users_rated')
  if(users_rated.size() > 0){
    users_rated.invoke('hide')
  }
}

function spinner() {
    return $("spinner_wrap").down(".loader_bg").clone(true);
}

function bindSpinner(element) {
    var target = $($(element).readAttribute("rel")) || $(element);
    if (target) {
        var _spinner = $(element).down(".loader_bg");
        if (_spinner) {
            _spinner.show();
        } else {
            target.insert(spinner());
        }
    }
}

function unbindSpinner(element) {
    var spinner = $(element).down(".loader_bg");
    if (spinner) spinner.hide();
}

var FlashNotice = FlashNotice || {};

FlashNotice.update = function(content) {
    FlashNotice.resetAnimation();
    $$(".flash_wrap").invoke("update", content);
    $$(".flash_wrap").invoke("show");
    FlashNotice.timeout();
}

FlashNotice.resetEffect = function(effect) {
    effect.render(1.0);
    effect.event("beforeFinish");
    if (effect.finish) effect.finish();
    effect.event("afterFinish");
    effect.cancel();
}

FlashNotice.resetAnimation= function() {
    Effect.Queues.get("flash_notice").each(function(e) {FlashNotice.resetEffect(e);})
}

FlashNotice.hide = function() {
    var flashElement = $("wrapper").down(".flash_wrap #flash_notice");
    if (flashElement != null && !flashElement.empty()) {
        Effect.Fade(flashElement, {queue: {scope: "flash_notice"}});
        Effect.BlindUp(flashElement, {queue: {scope: "flash_notice"}});
    }
}

FlashNotice.timeout = function() {
    setTimeout("FlashNotice.hide()", 10000);
}

function initAjaxPagination() {
    var container = $(document.body);
    if (container) {
        container.observe("click", function(e) {
            var el = e.element();
            if (el.match(".pagination.ajax a")) {
                bindSpinner(el.up(".pagination.ajax"));
                new Ajax.Request(el.href, {method: "get"});
                e.stop();
            }
        });
    }
}
/*function hover_circle(){
$("start_om_btn_1").observe('mouseover', function() {
     $('start_om_circle_1').setStyle({ background: 'url(/images/start_om_circle.gif) no-repeat 0 -67px' });
     $("start_om_btn_1n").setStyle({ color: '#DE0000'});
});
$("start_om_btn_1").observe('mouseout', function() {
     $('start_om_circle_1').setStyle({ background: 'url(/images/start_om_circle.gif) no-repeat 0 0' });
     $("start_om_btn_1n").setStyle({ color: '#808080'});
});
$("start_om_btn_2").observe('mouseover', function() {
     $('start_om_circle_2').setStyle({ background: 'url(/images/start_om_circle.gif) no-repeat 0 -67px' });
     $("start_om_btn_2n").setStyle({ color: '#DE0000'});
});
$("start_om_btn_2").observe('mouseout', function() {
     $('start_om_circle_2').setStyle({ background: 'url(/images/start_om_circle.gif) no-repeat 0 0' });
     $("start_om_btn_2n").setStyle({ color: '#808080'});
});
$("start_om_btn_3").observe('mouseover', function() {
     $('start_om_circle_3').setStyle({ background: 'url(/images/start_om_circle.gif) no-repeat 0 -67px' });
     $("start_om_btn_3n").setStyle({ color: '#DE0000'});
});
$("start_om_btn_3").observe('mouseout', function() {
     $('start_om_circle_3').setStyle({ background: 'url(/images/start_om_circle.gif) no-repeat 0 0' });
     $("start_om_btn_3n").setStyle({ color: '#808080'});
});
$("start_om_btn_4").observe('mouseover', function() {
     $('start_om_circle_4').setStyle({ background: 'url(/images/start_om_circle.gif) no-repeat 0 -67px' });
     $("start_om_btn_4n").setStyle({ color: '#DE0000'});
});
$("start_om_btn_4").observe('mouseout', function() {
     $('start_om_circle_4').setStyle({ background: 'url(/images/start_om_circle.gif) no-repeat 0 0' });
     $("start_om_btn_4n").setStyle({ color: '#808080'});
});
$("start_om_btn_5").observe('mouseover', function() {
     $('start_om_circle_5').setStyle({ background: 'url(/images/start_om_circle.gif) no-repeat 0 -67px' });
     $("start_om_btn_5n").setStyle({ color: '#DE0000'});
});
$("start_om_btn_5").observe('mouseout', function() {
     $('start_om_circle_5').setStyle({ background: 'url(/images/start_om_circle.gif) no-repeat 0 0' });
     $("start_om_btn_5n").setStyle({ color: '#808080'});
});
$("start_om_btn_6").observe('mouseover', function() {
     $('start_om_circle_6').setStyle({ background: 'url(/images/start_om_circle.gif) no-repeat 0 -67px' });
     $("start_om_btn_6n").setStyle({ color: '#DE0000'});
});
$("start_om_btn_6").observe('mouseout', function() {
     $('start_om_circle_6').setStyle({ background: 'url(/images/start_om_circle.gif) no-repeat 0 0' });
     $("start_om_btn_6n").setStyle({ color: '#808080'});
});

$("start_om_btn_1n").observe('mouseover', function() {
     $('start_om_circle_1').setStyle({ background: 'url(/images/start_om_circle.gif) no-repeat 0 -67px' });
     $("start_om_btn_1n").setStyle({ color: '#DE0000'});
});
$("start_om_btn_1n").observe('mouseout', function() {
     $('start_om_circle_1').setStyle({ background: 'url(/images/start_om_circle.gif) no-repeat 0 0' });
     $("start_om_btn_1n").setStyle({ color: '#808080'});
});
$("start_om_btn_2n").observe('mouseover', function() {
     $('start_om_circle_2').setStyle({ background: 'url(/images/start_om_circle.gif) no-repeat 0 -67px' });
     $("start_om_btn_2n").setStyle({ color: '#DE0000'});
});
$("start_om_btn_2n").observe('mouseout', function() {
     $('start_om_circle_2').setStyle({ background: 'url(/images/start_om_circle.gif) no-repeat 0 0' });
     $("start_om_btn_2n").setStyle({ color: '#808080'});
});
$("start_om_btn_3n").observe('mouseover', function() {
     $('start_om_circle_3').setStyle({ background: 'url(/images/start_om_circle.gif) no-repeat 0 -67px' });
     $("start_om_btn_3n").setStyle({ color: '#DE0000'});
});
$("start_om_btn_3n").observe('mouseout', function() {
     $('start_om_circle_3').setStyle({ background: 'url(/images/start_om_circle.gif) no-repeat 0 0' });
     $("start_om_btn_3n").setStyle({ color: '#808080'});
});
$("start_om_btn_3m").observe('mouseover', function() {
     $('start_om_circle_3').setStyle({ background: 'url(/images/start_om_circle.gif) no-repeat 0 -67px' });
     $("start_om_btn_3n").setStyle({ color: '#DE0000'});
});
$("start_om_btn_3m").observe('mouseout', function() {
     $('start_om_circle_3').setStyle({ background: 'url(/images/start_om_circle.gif) no-repeat 0 0' });
     $("start_om_btn_3n").setStyle({ color: '#808080'});
});
$("start_om_btn_4n").observe('mouseover', function() {
     $('start_om_circle_4').setStyle({ background: 'url(/images/start_om_circle.gif) no-repeat 0 -67px' });
     $("start_om_btn_4n").setStyle({ color: '#DE0000'});
});
$("start_om_btn_4n").observe('mouseout', function() {
     $('start_om_circle_4').setStyle({ background: 'url(/images/start_om_circle.gif) no-repeat 0 0' });
     $("start_om_btn_4n").setStyle({ color: '#808080'});
});
$("start_om_btn_5n").observe('mouseover', function() {
     $('start_om_circle_5').setStyle({ background: 'url(/images/start_om_circle.gif) no-repeat 0 -67px' });
     $("start_om_btn_5n").setStyle({ color: '#DE0000'});
});
$("start_om_btn_5n").observe('mouseout', function() {
     $('start_om_circle_5').setStyle({ background: 'url(/images/start_om_circle.gif) no-repeat 0 0' });
     $("start_om_btn_5n").setStyle({ color: '#808080'});
});
$("start_om_btn_6n").observe('mouseover', function() {
     $('start_om_circle_6').setStyle({ background: 'url(/images/start_om_circle.gif) no-repeat 0 -67px' });
     $("start_om_btn_6n").setStyle({ color: '#DE0000'});
});
$("start_om_btn_6n").observe('mouseout', function() {
     $('start_om_circle_6').setStyle({ background: 'url(/images/start_om_circle.gif) no-repeat 0 0' });
     $("start_om_btn_6n").setStyle({ color: '#808080'});
});

$("start_om_circle_1").observe('mouseover', function() {
     $("start_om_btn_1n").setStyle({ color: '#DE0000'});
});
$("start_om_circle_1").observe('mouseout', function() {
     $("start_om_btn_1n").setStyle({ color: '#808080'});
});
$("start_om_circle_2").observe('mouseover', function() {
     $("start_om_btn_2n").setStyle({ color: '#DE0000'});
});
$("start_om_circle_2").observe('mouseout', function() {
     $("start_om_btn_2n").setStyle({ color: '#808080'});
});
$("start_om_circle_3").observe('mouseover', function() {
     $("start_om_btn_3n").setStyle({ color: '#DE0000'});
});
$("start_om_circle_3").observe('mouseout', function() {
     $("start_om_btn_3n").setStyle({ color: '#808080'});
});
$("start_om_circle_4").observe('mouseover', function() {
     $("start_om_btn_4n").setStyle({ color: '#DE0000'});
});
$("start_om_circle_4").observe('mouseout', function() {
     $("start_om_btn_4n").setStyle({ color: '#808080'});
});
$("start_om_circle_5").observe('mouseover', function() {
     $("start_om_btn_5n").setStyle({ color: '#DE0000'});
});
$("start_om_circle_5").observe('mouseout', function() {
     $("start_om_btn_5n").setStyle({ color: '#808080'});
});
$("start_om_circle_6").observe('mouseover', function() {
     $("start_om_btn_6n").setStyle({ color: '#DE0000'});
});
$("start_om_circle_6").observe('mouseout', function() {
     $("start_om_btn_6n").setStyle({ color: '#808080'});
});
}
*/
function hover_circle(){
  var startTabsN = ['start_om_btn_1n','start_om_btn_2n','start_om_btn_3n',
                    'start_om_btn_4n','start_om_btn_5n','start_om_btn_6n']
  var startTabs = ['start_om_btn_1','start_om_btn_2','start_om_btn_3','start_om_btn_4','start_om_btn_5','start_om_btn_6']
  var startTabsCircle = ['start_om_circle_1','start_om_circle_2','start_om_circle_3','start_om_circle_4',
                        'start_om_circle_5','start_om_circle_6']

  startTabs.each(function(l,index){
    $(l).observe('mouseover',function(){
      $(startTabsCircle[index]).setStyle({ background: 'url(/images/start_om_circle.gif) no-repeat 0 -67px' });
      $(startTabsN[index]).setStyle({ color: '#DE0000'});
    })
    $(l).observe('mouseout',function(){
      $(startTabsCircle[index]).setStyle({ background: 'url(/images/start_om_circle.gif) no-repeat 0 0' });
      $(startTabsN[index]).setStyle({ color: '#808080'});
    })
  });

  startTabsN.each(function(l,index){
    $(l).observe('mouseover',function(){
      $(startTabsCircle[index]).setStyle({ background: 'url(/images/start_om_circle.gif) no-repeat 0 -67px' });
      this.setStyle({ color: '#DE0000'});    
    })
    $(l).observe('mouseout',function(){
      $(startTabsCircle[index]).setStyle({ background: 'url(/images/start_om_circle.gif) no-repeat 0 0' });
      this.setStyle({ color: '#808080'});
    })
  });

  startTabsCircle.each(function(l,index){
    $(l).observe('mouseover',function(){
      $(startTabsN[index]).setStyle({ color: '#DE0000'});    
    })
    $(l).observe('mouseout',function(){
     $(startTabsN[index]).setStyle({ color: '#808080'});
    })
  });   
}

function initAll2(){
  document.on('ajax:before', showLoadingContent);
  document.on('ajax:complete', hideLoadingContent);
  fetchHotTopicLinks();
  hideRatedUsers();
  fetchWhoRated();  
  stopSearchFileItems();
  initAjaxPagination();
  FlashNotice.timeout();
  hover_circle();
  //$$('span.link_to_hot_topic a').invoke('observe','click',removeLink);
}

document.observe('dom:loaded', initAll2);

var HotTopic = Class.create({
  initialize: function(reference){
    this.reference = reference;
    this.url = reference.readAttribute('href')
  },

  removeHotTopicLink: function(){
    return this.reference.remove();
  }
});
