// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

IPublish = new Object();

IPublish.DateReplacer = Class.create()
IPublish.DateReplacer.prototype = {
  initialize: function(target){
    this.target = target
    this.monthNames = ['januar', 'februar', 'mars', 'april', 'mai', 'juni', 'juli', 'august', 'september', 'oktober', 'november', 'desember']
    this.dateRegExp = /([12][0-9]{3})-([0-1][0-9])-([0-3][0-9]) ([0-2][0-9]):([0-5][0-9])/g
  },
  replace: function(){
    return this.target.replace(
      this.dateRegExp,
      function(ma, y, mon, d, h, min, i){
        var date = new Date(Date.UTC(y, mon-1, d, h, min))
        var s = ''
        s += date.getDate()
        s += ' '+this.monthNames[date.getMonth()]
        
        if (date.getFullYear() != (new Date).getFullYear()) {
          s += ' '+date.getFullYear()
        }
        
        var hours = date.getHours()
        if (hours<10) {hours = '0'+hours}
        
        var minutes = date.getMinutes()
        if (minutes<10) {minutes = '0'+minutes}
        
        s += ' klokken '+hours+':'+minutes
        
        return s
      }.bind(this)
    )
  }
}
//IPublish.DateReplacer.replaceIn = function(element){
//  element = $(element)
//  element.innerHTML = (new IPublish.DateReplacer(element.innerHTML)).replace()
//}

IPublish.attachDiscoverableTrigger = function(element) {
	this.event = 'click'
	this.revealed = false
	
	if(
		element.tagName.toLowerCase() == 'input' && 
		element.getAttribute('type').toLowerCase() == 'checkbox' &&
		(navigator.userAgent.indexOf('MSIE') == -1)
		) {
			this.event = 'change'
		}
		
		Event.observe(element, this.event, function(e){
			document.getElementsByClassName(element.getAttribute('trigger'), 'content').each(function(el){
				Element.toggle(el)
			})
		})
}

IPublish.fiftyCupsOfCoffeeAndYouKnowItsOn = function(){
  //IPublish.DateReplacer.replaceIn('container')

	document.getElementsByClassName('js-discoverable', 'content').each(function(element){
		if (!Element.hasClassName(element, 'discovered')) {
			Element.hide(element)
		}
	})
	
	document.getElementsByClassName('js-discoverable-trigger', 'content').each(function(element){
		IPublish.attachDiscoverableTrigger(element)
	})

}
//Get some error on this, not sure what it i good for either.
//Event.onReady(IPublish.fiftyCupsOfCoffeeAndYouKnowItsOn)

//This is used by the blog register page so that we can have a column 
//showing the most viewed blog... 
//No cookie check so people can in theory cheat...
function register_click(client_id) {
	var click_add_url = "/clients/register_click"
	new Ajax.Request(click_add_url, {
		parameters: $H({id: client_id}).toQueryString()
	})
}

function register_hits(client_id,client_url,ip) {
  var hits_url = client_url+"/hits/create";
  if (Cookie.accept() && !Cookie.exists("ipublish_"+client_id)) {
    Cookie.set("ipublish_"+client_id,client_id,null);
    new Ajax.Request(hits_url, {
      parameters: $H({id: client_id, ip: ip}).toQueryString(),
      method: 'post'
    })
  }
}

