// JavaScript Document
//var bgImage = new Image();
var bgImage = null;
var tmpBgImage = null;
var resized = false;
var left = 0;
var originalContentHeight;
var contentTween;
var contentListTween;
var bgImageOne = document.createElement('IMG');
bgImageOne.id = "bg1";
var bgImageTwo = document.createElement('IMG');
bgImageTwo.id = "bg2";
var isProject;
var currentBg = null;

var firstRun = true;

function layoutSite()
{
	var windowWidth = window.getSize().x;
	var siteWidth = $('site_container').getCoordinates().width;
	var siteHeight = $('site_container').getCoordinates().height;
	var contentTop = $('banner').getCoordinates().top + $('banner').getCoordinates().height;
	var contentHeight = siteHeight - ($('banner').getCoordinates().height + $('footer').getCoordinates().height);
	
	left = Math.floor(((windowWidth / 2) - (siteWidth / 2)));
	if(left < 0)left = 0;
	
	//Hide site
	$('main_container').setStyle('visibility', 'hidden');
	
	// Position elements
	$('banner').setStyle('left', left);
	$('content').setStyle('left', left);
	$('content').setStyle('top', contentTop);
	$('content').setStyle('height', contentHeight);
	$('content_list_bg').setStyle('left', left);
	$('content_list_bg').setStyle('top', $('banner').getCoordinates().height + $('banner').getCoordinates().top);
	$('content_list_bg').setStyle('height', contentHeight);
	$('content_list_bg').setStyle('opacity', 0.8);
	
	// SET UP CONTENT AREA
	$('content_container').setStyle('height', contentHeight);	
	$('content_container_bg').setStyle('height', contentHeight);
	$('content_container_bg').setStyle('opacity', 0.7);
//	$('content_container').setStyle('left', $('content_list_bg').getCoordinates().width);
//	$('content_container_bg').setStyle('left', $('content_list_bg').getCoordinates().width);
	
	if($('content_body_container'))
	{
		$('content_body_container').setStyle('height', Math.abs(contentHeight - $('content_title').getCoordinates().height));
	}
	if($('content_list_body_container'))
	{
		$('content_list_body_container').setStyle('height', Math.abs(contentHeight - $('content_list_title').getCoordinates().height));
	}
//	$('content_body').setStyle('height', Math.abs(contentHeight - $('content_title').getCoordinates().height) - 20);
	$('content_list').setStyle('height', contentHeight);
	
	// SET UP FOOTER AREA
	$('footer').setStyle('left', left);
	$('footer').setStyle('top', (siteHeight - $('footer').getSize().y) + "px");
	$('footer_bg').setStyle('opacity', 0.40);
	
	if(!resized)
	{
		$('site_bg_container').appendChild(bgImageOne);
		$('site_bg_container').appendChild(bgImageTwo);
	}
	
	$('bg1').setStyle('left', left);
	$('bg2').setStyle('left', left);
	
	siteTotalWidth   = $('site_container').getCoordinates().width;
	windowTotalWidth = window.getSize().x;
	leftPos = Math.floor(((windowTotalWidth / 2) - (siteTotalWidth / 2)));
	
	$('main_container').setStyle('visibility', 'visible');
	
	if ($('content_body').innerHTML == ""){
		$('content_body').setStyle('display', 'none');
		$('content_title').setStyle('display', 'none');
		$('content_container_bg').setStyle('display', 'none');
		$('content_container').setStyle('display', 'none');
		
		if($('content_body_container'))
			$('content_body_container').setStyle('height', contentHeight);
			
		$('detailsLink').setStyle('display', 'none');
	}else setupContentScroller();
	
	if($('content_list').innerHTML == ""){
		$('content_list').setStyle('display', 'none');
		$('content_list_bg').setStyle('display', 'none');
	}
	else setupListScroller();
	
	
	// Show site
	$('main_container').setStyle('visibility', 'visible');
	
	if(firstRun){
		firstRun = false;
		onResize();	
		
		if(isProject)adjustContentHeight();
	}
}
function toggleContent()
{
	if($('detailsLink').innerHTML == "Show Text")
		showContent();
	else hideContent();
}
function hideContent()
{
	originalContentHeight = $('content').offsetHeight;
	$('content').setStyle('overflow', 'hidden');
	
	contentTween = $('content').get('tween', {property: 'height', duration: 'short'});
	contentTween.start(0);
	
	contentListTween = $('content_list_bg').get('tween', {property: 'height', duration: 'short'});
	contentListTween.start(0);
	
	$('detailsLink').innerHTML = "Show Text";
}
function showContent()
{
	contentTween = $('content').get('tween', {property: 'height', duration: 'short'});
	contentTween.start(originalContentHeight);
	
	contentListTween = $('content_list_bg').get('tween', {property: 'height', duration: 'short'});
	contentListTween.start(originalContentHeight);
	
	$('detailsLink').innerHTML = "Hide Text";
}
function onBgLoaded()
{
	if(!resized)
	{
		$(this.id).setStyle('opacity', 0);
		$(this.id).tween('opacity', 0, 1);
	}
	else {
		resized = false;
		$(bgImage.id).setStyle('opacity', 1);
	}
}
function setBackground(url)
{
	bgImage.src = url;
}
function onBgImagesLoaded(images)
{
//	alert(images.name);  
}
function onResize()
{
	$('banner').setStyle('visibility', 'hidden');
	$('footer').setStyle('visibility', 'hidden');
	$('footer_bg').setStyle('visibility', 'hidden');
	
	resized = true;
	layoutSite();
	
	$('banner').setStyle('visibility', 'visible');
	$('footer').setStyle('visibility', 'visible');
	$('footer_bg').setStyle('visibility', 'visible');
}
function fixPNG(){
	if (document.all && /MSIE (5\.5|6)/.test(navigator.userAgent) &&
	document.styleSheets && document.styleSheets[0] && document.styleSheets[0].addRule)
	{
	//	document.styleSheets[0].addRule('*', 'behavior: url(iepngfix.htc)');
		// Feel free to add rules for specific elements only, as above.
		// You have to call this once for each selector, like so:
		document.styleSheets[0].addRule('img', 'behavior: url(js/iepngfix.htc)');
		document.styleSheets[0].addRule('div', 'behavior: url(js/iepngfix.htc)');
	}
}
function adjustContentHeight()
{
	$('content_container_bg').setStyle('height', '120px');
	$('content_container').setStyle('height', '120px');	
}
function setupContentScroller()
{
	// First Example
	var el = $('sliderElement');
	
	if(!el)return;
	
	el.setStyle('height', $('content_container_bg').getCoordinates().height - $('content_title').getCoordinates().height);
	//	font = $('fontSize');
	
	if($('content_body').getCoordinates().height <= $('content_body_container').getCoordinates().height)
	{
		el.setStyle('display', 'none');
		return;
	}
	// Create the new slider instance
	var slider = new Slider(el, el.getElement('.knob'), {
		steps: $('content_body').getCoordinates().height - $('content_body_container').getCoordinates().height,	// There are 35 steps
		range: [0],	// Minimum value is 8
		mode: 'vertical', // Slide content vertically.
		wheel: true,
		onChange: function(value){
			$('content_body').setStyle('top', -value);
		}
	}).set(0);
}
function setupListScroller()
{
	// First Example
	var el = $('sliderElement2');
	
	if(!el)return;
	
	el.setStyle('height', $('content_container_bg').getCoordinates().height - $('content_list_title').getCoordinates().height);
	
	if($('content_list_body').getCoordinates().height <= $('content_list_body_container').getCoordinates().height)
	{
		el.setStyle('display', 'none');
		$('content_list_body_container').setStyle('width', '100%');
		return;
	}
	
	// Create the new slider instance
	var slider2 = new Slider(el, el.getElement('.knob'), {
		steps: $('content_list_body').getCoordinates().height - $('content_list_body_container').getCoordinates().height,	// There are 35 steps
		range: [0],	// Minimum value is 8
		mode: 'vertical', // Slide content vertically.
		onChange: function(value){
			$('content_list_body').setStyle('top', -value);
		}
	}).set(0);
}
window.onload = function(){ layoutSite(); fixPNG(); displayBackground(bgIndex, true);};
window.addEvent('resize', onResize);
//window.onload = ;