$(window).addEvent('domready', function() {
	initBehaviors();
});

var navtimer;
var bodyclass;

function initBehaviors() {
	bodyclass = ($E('body').className.indexOf(" ") == -1) ? $E('body').className : $E('body').className.substr(0, $E('body').className.indexOf(" "));

	$ES('div.casestudy').addEvents({
		'mouseover' : function() {
			this.style.cursor = 'pointer';
			$ES('.casestudy_img', this).forEach(function(el) {
				el.style.display = 'none';
			});
		},
		'mouseout' : function() {
			$ES('.casestudy_img', this).forEach(function(el) {
				el.style.display = 'block';
			});
		},
		'click' : function() {
			if ($ES('a', this).length > 0) {
				switch($ES('a', this)[0].getAttribute('rel')) {
					case 'poplayer_left':
					case 'poplayer_right':
					case 'poplayer_bottomright':
						poplayer(this);
				}
			}
		}
	});
	
	$ES('div.design').addEvents({
		'click' : function() {
			if ($ES('a', this).length > 0) {
				switch($ES('a', this)[0].getAttribute('rel')) {
					case 'poplayer_left':
					case 'poplayer_right':
					case 'poplayer_bottomright':
						poplayer(this);
				}
			}
		}
	});
	
	$ES('.home a').each(function(el) {
		el.onclick = function() {
			switch (this.getAttribute('rel')) {
				case 'poplayer_left':
				case 'poplayer_right':
				case 'poplayer_bottomright':
					return false;
				default:
					return true;
			}
		}
	});
	
	$ES('#mainnavcontainer li').each(function(el) {
		if (el.id.replace(/nav_/,'') == bodyclass) {
			el.className = 'active';
		}
		var subel = $(el.id.replace(/nav_/,'subnav_'));
		if (subel) {
			subel.style.left = el.getPosition().x + 'px';
		}
		el.onmouseover = function() {
			clearTimeout(navtimer);
			this.className = 'active';
			activatenav(this);
		}
		el.onmouseout = function() {
			clearTimeout(navtimer);
			navtimer = setTimeout('deactivatenav()', 1000);
		}
	});
	
	$ES('.clientlist li').each(function(el) {
		if (el.className == 'has-projectlist') {
			var sublist = $ES('div.projectlist', el)[0];
			var Slider = new Fx.Slide(sublist.id, {duration: 250});
			Slider.hide();
			$ES('a', el)[0].onclick = function() {
				Slider.toggle();
				return false;
			}
/*			$ES('.subprojects_close a', $(sublist.id))[0].onclick = function() {
				Slider.toggle();
				return false;
			}*/
		}
	});
}

function activatenav(caller) {
	$ES('#mainnavcontainer li').each(function(el) {
		if (el == caller) {
			el.className = 'active';
		} else {
			el.className = '';
		}
	});
	$ES('.subnavcontainer').each(function(el) {
		if (el.id.replace(/subnav_/,'nav_') == caller.id) {
			el.style.display = 'block';
			el.onmouseover = function() {
				clearTimeout(navtimer);
			}
			el.onmouseout = function() {
				clearTimeout(navtimer);
				navtimer = setTimeout('deactivatenav()', 1000);
			}
		} else {
			el.style.display = 'none';
		}
	});
}

function deactivatenav() {
	clearTimeout(navtimer);
	$ES('#mainnavcontainer li').each(function(el) {
		if (el.id.replace(/nav_/,'') == bodyclass) {
			el.className = 'active';
		} else {
			el.className = '';
		}
	});
	$ES('.subnavcontainer').each(function(el) {
		if (el.id.replace(/subnav_/,'') == bodyclass) {
			el.style.display = 'block';
		} else {
			el.style.display = 'none';
		}
	});
}

var scrollorigin;

function poplayer(obj) {
	$('poplayer').style.display = 'none';
	document.location.hash = obj.id.replace(/sq_/, '');
//	alert(obj.id);
	var ajx = new Ajax($ES('a', obj)[0].getAttribute('href'), {method: 'get', onComplete: function() { complete_poplayer(obj) }, update: $('popcontent'), evalScripts: true}).request();
}

function complete_poplayer(obj) {
//	window.scrollTo(0, scrollorigin);
	$('poplayer').style.display = 'none';

	if ($ES('a', obj)[0].getAttribute('rel') == 'poplayer_left') {
		$('poplayer').style.left = (obj.getPosition().x - 28) + 'px';
		$('poplayer').style.top = (obj.getPosition().y - 28) + 'px';
	} else if ($ES('a', obj)[0].getAttribute('rel') == 'poplayer_right') {
		$('poplayer').style.left = (obj.getPosition().x - 274) + 'px';
		$('poplayer').style.top = (obj.getPosition().y - 28) + 'px';
	} else if ($ES('a', obj)[0].getAttribute('rel') == 'poplayer_bottomright') {
		$('poplayer').style.left = (obj.getPosition().x - 274) + 'px';
		$('poplayer').style.top = (obj.getPosition().y - 274) + 'px';
	}
	$('poplayer').style.display = 'block';
	
	sIFR.replace(pillgothic, {
		selector: '.casestudycontent h1',
		wmode: 'transparent',
		css: [
			'.sIFR-root { font-weight: bold; color: #000000; leading: -6 }',
			'a { color: #405c79; text-decoration: none; }',
			'a:hover { color: #941571; }'
		],
		tuneHeight: -11,
		offsetTop: -5,
		offsetLeft: -1,
		ratios: [7,1.58,8,1.47,10,1.5,12,1.44,17,1.42,20,1.41,23,1.4,29,1.39,30,1.38,32,1.37,35,1.38,39,1.37,41,1.36,42,1.37,44,1.36,47,1.37,54,1.36,56,1.35,57,1.36,59,1.35,60,1.36,62,1.35,63,1.36,65,1.35,66,1.36,97,1.35,98,1.34,100,1.35,101,1.34,102,1.35,104,1.34,105,1.35,107,1.34,108,1.35,110,1.34,111,1.35,113,1.34,114,1.35,116,1.34,117,1.35,119,1.34,120,1.35,122,1.34,123,1.35,1.34]
	});

	$ES('.close', $('poplayer')).addEvents({
		'mouseover': function() {
			this.style.cursor = 'pointer';
		},
		'click': function() {
			document.location.hash = '0';
			$('poplayer').style.display = 'none';
		}
	});
	
}

function loadhomecontent() {
	if (document.location.hash.length > 2) {
		var elid = document.location.hash.substr(document.location.hash.indexOf("#") + 1);
		poplayer($('sq_' + elid));
	}
}
