dojo.require("dojo.string")
dojo.require("dojo.dom")
dojo.require("dojo.html.*")
dojo.require("dojo.html.layout")
dojo.require("dojo.html.style")
dojo.require("perse.*")
dojo.require("perse.animation")
dojo.require("perse.mvc")
dojo.require("perse.animation")
dojo.require("lgz.web.animation")

dojo.provide("lgz.web.work")

lgz.web.work= new function(){

	this.reload = function(view, name, evt){
		var index = perse.helper.slice(name, -2, -1, '.')
		if (index) {
			var url = window.location
			window.location = url.toString().split('/').slice(0, -1).join('/') + '/' + index + '/'
		}
	}

	this.toggle = function(view, name, evt){
		var index = perse.helper.slice(name, 0, 1, '.')
		var moment = view.getElement(index)
		if (index) {
			// used because of a safari bug
			var title = dojo.html.getElementsByClass('moment_title', moment)[0]
			if (evt.type == 'mouseout') {
				title.style.display = 'none'
				moment.style.top = '0px'
			} else if (evt.type == 'mouseover') {
				// play sound
				perse.mvc.models.update('lgz.web.animation.play', index)
				title.style.display = 'block'
				moment.style.top = '-27px'
				var hide = dojo.html.getElementsByClass('moment_hide', moment)[0]
				dojo.html.setOpacity(hide, 0.0)
			}
		}
	}

	this.hideMomentRandomly = function(){
		if (lgz.web.animation.LeftNavigation.lookup('move', 'move') == false &&
		    lgz.web.animation.RightNavigation.lookup('move', 'move' == false)){
			var node =  document.getElementById('page.center.content')
			var works = dojo.html.getElementsByClass('moment_hide', node)
			for (var w in works){
				var work = works[w]
				var moment = work.parentNode
				// invoke only if not toggled
				if (moment.style.top != '-27px'){ 
					var r1 = Math.random()
					var r2 = Math.random()
					if (r2 > 0.8){
						if (r1 > 0.8){
							dojo.html.setOpacity(work, 1.0)
						} else {
							dojo.html.setOpacity(work, 0.0)
						}
					}
		
				}
			}				
		}
		window.setTimeout(function () {lgz.web.work.hideMomentRandomly()}, 1000)
	}

	// content view
	this.Content = new perse.mvc.View('page.center.content')
	this.Content.addListener('onmouseout', this.toggle, false)
	this.Content.addListener('onmouseover', this.toggle, false)
	this.Content.addListener('onclick', this.reload)
	this.Content.addModel('amount_of_moments', 'lgz.timebar')
	this.Content.addSubscriber('amount_of_moments', lgz.web.animation.extendWorks)

	// Initialize the global registration
	this.init = function() {
		// view registration
		perse.mvc.view.register(lgz.web.work.Content)
		
		// initialize the view by this update respecting the screen size
		perse.mvc.models.update('lgz.timebar', {amount: lgz.web.animation.amountOfShownMoments()})
		lgz.web.work.hideMomentRandomly()
	}
}

dojo.addOnLoad(lgz.web.work.init)
