
// initialise dom ready events
window.addEvent('domready', initialiseSite);


var m_menuTween=null;
var m_imagesElement=null;
var m_workButtonElement=null;
var m_imagesHeight=0;
var m_menuOpen=false;


function initialiseSite()
{
	var myThis=this;
	
	m_imagesElement = $("images");	
	m_workButtonElement = $("workButton");
	
	m_imagesHeight = m_imagesElement.getSize().y;		
	m_imagesElement.setStyle("margin-top", String(-m_imagesHeight)+"px");	
	
	
	m_workButtonElement.addEvent("mouseenter", openMenu);
	m_imagesElement.addEvent("mouseleave", closeMenu);	
	
	$('myElement').setStyle('opacity', 0);
	$('tweet').addEvent('click', function(){
		if($('myElement').getStyle('opacity') == 0)
			$('myElement').fade(1);
		else
			$('myElement').fade(0);
	});
	$$('.closer').addEvent('click', function() {
		if($('myElement').getStyle('opacity') == 0)
			$('myElement').fade(1);
		else
			$('myElement').fade(0);
	});

	
	// get thumb buttons and add their mouse events
	var l_thumbButtons = $$("#imagesContainer .thumbButton");
	var l_thumbButton=null;
	
	for(var b=0;b<l_thumbButtons.length;b++)
	{
		l_thumbButton = l_thumbButtons[b];
		l_thumbButton.addEvent("mouseenter", function(){myThis.thumbButtonOver(this);});
		l_thumbButton.addEvent("mouseleave", function(){myThis.thumbButtonOut(this);});
		l_thumbButton.addEvent("click", function(){myThis.thumbButtonClick(this);});
	}
	
	
	// get share buttons and add their mouse events
	var l_shareButton = $$("#contentShare .shareButton");
	var l_twitterButton = $$("#contentShare .twitter")
	var l_flickrButton = $$("#contentShare .flickr")
	
	l_twitterButton.addEvent("mouseenter", function(){myThis.shareButtonOver(this);});
	l_twitterButton.addEvent("mouseleave", function(){myThis.shareButtonOut(this);});
	
	l_flickrButton.addEvent("mouseenter", function(){myThis.shareButtonOver(this);});
	l_flickrButton.addEvent("mouseleave", function(){myThis.shareButtonOut(this);});
	l_flickrButton.addEvent("click", function(){myThis.shareButtonClick(this);});
	
	l_shareButton.addEvent("mouseenter", function(){myThis.shareButtonOver(this);});
	l_shareButton.addEvent("mouseleave", function(){myThis.shareButtonOut(this);});
	l_shareButton.addEvent("click", function(){myThis.shareButtonClick(this);});

}

function thumbButtonOver(a_thumbButton)
{
	var l_imgElement = a_thumbButton.getElements("img")[0];
	l_imgElement.setStyle("margin-top", "0px");
}

function thumbButtonOut(a_thumbButton)
{
	var l_imgElement = a_thumbButton.getElements("img")[0];
	l_imgElement.setStyle("margin-top", "-72px");
}

function thumbButtonClick(a_thumbButton)
{
	var l_linkId = a_thumbButton.get("rel");
	window.location.href = l_linkId;
}

function shareButtonOver(a_shareButton)
{
	var l_imgElement = a_shareButton.getElements("img");
	l_imgElement[0].setStyle("display", "none");
	l_imgElement[1].setStyle("display", "block");
}

function shareButtonOut(a_shareButton)
{
	var l_imgElement = a_shareButton.getElements("img");
	l_imgElement[0].setStyle("display", "block");
	l_imgElement[1].setStyle("display", "none");
}

function shareButtonClick(a_shareButton)
{
	var l_linkId = a_shareButton.get("rel");
	showWebsite(l_linkId);
}



function showWebsite(a_url)
{
	//alert("showWebsite  "+a_url);
	
	// hide content
	var l_content = $("contentSection");
	l_content.setStyle("display", "none");
	
	$("contentBG_Repeat").setStyle("display", "none");
	$("contentBG_Left").setStyle("display", "none");
	$("contentBG_Right").setStyle("display", "none");
	
	
	
	var l_iframe = $("contentIframe");
	l_iframe.setStyle("display", "block");
	l_iframe.src = a_url;
	
}



function openMenu()
{
	showHideMenu(true);
}

function closeMenu()
{
	showHideMenu(false);
}

function showHideMenu(a_show)
{
	if(!a_show)a_show=false;
	
	if(a_show != m_menuOpen)
	{
		var l_currentY = m_imagesElement.getPosition().y;
		
		// cancel current tween
		if(m_menuTween!=null)m_menuTween.cancel();
		m_menuTween=null;
		
		// bug in menu jumping up and down
		if(l_currentY >=0)l_currentY=0;
		
		// get the to position for the tween
		var l_toY = a_show ? 0 : -m_imagesHeight;
		
		// start new tween
		m_menuTween = new Fx.Tween("images", {duration:300, wait:false});
		m_menuTween.start("margin-top", String(l_currentY)+"px", String(l_toY)+"px");
		
		m_menuOpen = a_show;
	}
}