// generator: o-Design

// custom events
document.domLoadedEvent = (function(){
	if(!this.domLoadedEventDone){
		Element.fire(document, '1site1:domloaded')
		this.domLoadedEventDone = true
	}
}).bind(document)
Event.observe(document, 'dom:loaded', document.domLoadedEvent)
if(Prototype.Browser.IE) Event.observe(window, 'load', document.domLoadedEvent)

Event.observe(window, 'resize', function(){
	if(window.rezizeTimerId) clearTimeout(window.rezizeTimerId)
	else setTimeout(function(){Element.fire(document, '1site1:resize')}, 50)
})


// Cookie object
var Cookie = {
	set: function(name, value, daysToExpire, path, domain, secure){
		var expire = '';
		if(daysToExpire != undefined){
			var d = new Date()
			d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)))
			expire = '; expires=' + d.toGMTString()
		}
		return (document.cookie = escape(name) + '=' + escape(value || '') + expire + (path ? ';path='+path : ';path=/') + (domain ? ';domain='+domain : '') + (secure ? ';secure' : ''))
	},
	get: function(name){
		var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'))
		return (cookie ? unescape(cookie[2]) : undefined)
	},
	getKeys: function(){
		var list = []
		$A(document.cookie.split(/\s*;\s*/)).each(function(cookie){
			cookie = cookie.split(/\s*=\s*/)
			var key = unescape(cookie[0])
			list.push(key)
		})
		return list
	},
	erase: function(name){
		var cookie = Cookie.get(name) || true;
		Cookie.set(name, '', -1);
		return cookie;
	},
	
	accepted: false,
	rejected: false,
	accept: function(){
		if(Cookie.accepted) return true
		else if(Cookie.rejected) return false
		
		var accept = null
		
		if(typeof navigator.cookieEnabled == 'boolean') accept = navigator.cookieEnabled
		else{
			Cookie.set('_test', '1');
			accept = (Cookie.erase('_test') = '1')
		}
		
		if(accept) Cookie.accepted = true
		else Cookie.rejected = true
		
		return accept
	}
};

// Base Href
Event.observe(document, '1site1:domloaded', function(){
	var b = document.getElementsByTagName('BASE')
	if(b && b[0] && b[0].href){
		b = b[0].href
		if(b.substr(b.length-1) != '/') b = b+'/'
		document.location.baseHref = b
	}
	else document.location.baseHref = document.location.href.replace(/(^[htpsfilef]+:\/\/[\w\._-]+(\:\d+)?\/).*/, '$1')
})

// root_design
function doPngFix(el){
	var blankImg = 'graphics/trans.gif'
	var bgImg = el.currentStyle.backgroundImage || el.style.backgroundImage
	
	if(el.tagName == 'IMG'){
		if(el.currentStyle.width == 'auto' && el.currentStyle.height == 'auto') el.style.width = el.offsetWidth + 'px'
		doPngFilter(el, el.src, 'scale')
		el.src = blankImg
	}
	else if(bgImg && bgImg != 'none'){
		bgImg.match(/^url[("']+(.*\.png)[)"']+$/i)
		var s = RegExp.$1
		if (el.currentStyle.width == 'auto' && el.currentStyle.height == 'auto') el.style.width = el.offsetWidth + 'px'
		el.style.backgroundImage = 'none'
		doPngFilter(el, s, 'crop')
		// IE link fix.
		for(var n = 0; n < el.childNodes.length; n++){
			if(el.childNodes[n].style) el.childNodes[n].style.position = 'relative'
		}
	}
}

function doPngFilter(el, s, m){
	var f = 'DXImageTransform.Microsoft.AlphaImageLoader'
	if(el.filters[f]){
		el.filters[f].enabled = s ? true : false
		if(s) with (el.filters[f]){
			el.src = s
			el.sizingMethod = m
		}
	}
	else if(s) el.style.filter = 'progid:'+f+'(src="'+s+'",sizingMethod="'+m+'")'
}

// design_autoscroll_2
obj_design_scroller_2 = Class.create({
	initialize: function(id, speed, dir, axis, useCookie){
		if(speed <= 0 || speed > 100) return false
		
		this.id = id
		var scroll = $(id)
		this.useCookie = (useCookie && Cookie.accept())
		this.speed = 1 / (speed / 1.33)
		this.x = axis == 'x'
		this.y = !this.x
		this.dir = dir == -1 ? -1 : 1
		
		this.offset = this.useCookie ? (parseInt(Cookie.get(this.id)) || 0) : 0
		
		scroll.clonePosition(scroll.up())
		scroll.setStyle({overflow: 'hidden'})
		
		var contentDiv = scroll.down()
		this.contentDim = contentDiv.getDimensions()
		
		this.slider = Element('div').setStyle({position: 'relative'})
		
		if(this.y) this.slider.setStyle({height: (this.contentDim.height * 2) + 'px', top: (this.dir > 0 ? this.offset - this.contentDim.height : this.offset) + 'px'})
		else this.slider.setStyle({width: (this.contentDim.width * 2) + 'px', left: (this.dir > 0 ? this.offset - this.contentDim.width : this.offset) + 'px'})
		
		var table = new Element('table', {border: 0, cellspacing: 0, cellpadding: 0})
		table.insert('<tbody><tr><td></td>'+(this.y ? '</tr><tr>' : '')+'<td></td></tr></tbody>')
		var cell1 = table.down('td'), cell2 = table.down('td', 1)
		cell1.update(contentDiv.remove())
		cell2.update(cell1.down().cloneNode(true))
		
		this.slider.update(table)
		scroll.update(this.slider)
		
		this.start()
		
		var t = this
		scroll.observe('mouseover', function(){t.stop()})
		scroll.observe('mouseout', function(){t.start()})
		
	},
	
	stop: function(){
		if(this.periodical) this.periodical.stop()
		this.periodical = null
		if(this.useCookie){
			if(this.cookiePeriodical) this.cookiePeriodical.stop()
			Cookie.set(this.id, this.offset, 1)
		}
	},
	
	start: function(){
		if(this.periodical || !this.speed) return false
		
		this.periodical = new PeriodicalExecuter((function(pe){
			this.offset += this.dir
			if(this.y){
				if(this.offset > this.contentDim.height) this.offset = 0
				if(this.offset < 0 - this.contentDim.height) this.offset = 0
				this.slider.setStyle({top: (this.dir > 0 ? this.offset - this.contentDim.height : this.offset) + "px"})
			}
			else{
				if(this.offset > this.contentDim.width) this.offset = 0
				if(this.offset < 0 - this.contentDim.width) this.offset = 0
				this.slider.setStyle({left: (this.dir > 0 ? this.offset - this.contentDim.width : this.offset) + "px"})
			}
		}).bind(this), this.speed)
		
		if(this.useCookie){
			this.cookiePeriodical = new PeriodicalExecuter((function(pe){
				Cookie.set(this.id, this.offset, 1)
			}).bind(this), 3)
		}
	}
	
});
