function $(obj_id)
{
    return document.getElementById(obj_id);
}

function init(page)
{	
    switch(page)
    {
        case 'home':
            var text_cols = getElementsByClassName('col_w1', 'div', $('content'));
            var max_height = 0;
            if(text_cols) for(var i = 0; i < text_cols.length; i++)
            {
                if(text_cols[i].offsetHeight > max_height) max_height = text_cols[i].offsetHeight;
            }
            $('home_sig_wrap').style.height = max_height + 'px';
            break;
		case 'about':
			var text_cols = getElementsByClassName('col_w1', 'div', $('about_col_wrap'));
            var max_height = 0;
            if(text_cols) for(var i = 0; i < text_cols.length; i++)
            {
                if(text_cols[i].offsetHeight > max_height) max_height = text_cols[i].offsetHeight;
            }
            $('about_pdf_wrap').style.height = max_height + 'px';
			break;
		case 'work':
			if($('case_pdf_wrap'))
			{
				var max_height = 0;
				if($('vid_case'))
				{
					max_height = $('vid_case').offsetHeight;
				}
				else
				{
					var text_cols = getElementsByClassName('col_w1', 'div', $('work_text_wrap'));
					if(text_cols) for(var i = 0; i < text_cols.length; i++)
					{
						if(text_cols[i].offsetHeight > max_height) max_height = text_cols[i].offsetHeight;
					}
				}
				$('case_pdf_wrap').style.height = max_height + 'px';
			}
			break;
    }
	if (typeof(is_fading) != 'undefined') if (is_fading) return false;
	var _content = $('content');
	var _work_nav_wrap = $('work_nav_wrap');
	var _work_project_type = $('work_project_type');
	var _showreel_title = $('showreel_title');
	var _opacity = 0;
	if(do_fade)
	{
		is_fading = setInterval(function()
		{
			if(_opacity < 100)
			{
				percent_faded = (_opacity / 100);
				ease = (Math.sin(percent_faded * 3.14159265) * 5) + 2;
				_opacity = _opacity + ease < 100 ? Math.round(_opacity + ease) : 100;
				//console.log(_opacity);
				_content.style.opacity = _opacity / 100;
				_content.style.filter = 'alpha(opacity=' + (_opacity) + ')';
				if(_work_nav_wrap)
				{
					_work_nav_wrap.style.opacity = _opacity / 100;
					_work_nav_wrap.style.filter = 'alpha(opacity=' + (_opacity) + ')';
				}
				if(_work_project_type)
				{
					_work_project_type.style.opacity = _opacity / 100;
					_work_project_type.style.filter = 'alpha(opacity=' + (_opacity) + ')';
				}
				if(_showreel_title)
				{
					_showreel_title.style.opacity = _opacity / 100;
					_showreel_title.style.filter = 'alpha(opacity=' + (_opacity) + ')';
				}
			}
			else
			{
				_content.style.opacity = 1;
				_content.style.filter = 'alpha(opacity=100)';
				if(_work_nav_wrap)
				{
					_work_nav_wrap.style.opacity = 1;
					_work_nav_wrap.style.filter = 'alpha(opacity=100)';
					open_work_nav();
				}
				if(_work_project_type)
				{
					_work_project_type.style.opacity = 1;
					_work_project_type.style.filter = 'alpha(opacity=100)';
				}
				if(_showreel_title)
				{
					_showreel_title.style.opacity = 1;
					_showreel_title.style.filter = 'alpha(opacity=100)';
				}
				clearInterval(is_fading);
				is_fading = null;
			}
			
		}, 30);
	}
	else
	{
		if(_work_nav_wrap) open_work_nav();
	}
}

function open_work_nav()
{
	var max_width = $('work_nav_title_act_inner').offsetWidth;
	var _width = 0;
	is_opening = setInterval(function()
	{
		if(_width < max_width)
		{
			percent_opened = (_width / max_width);
			ease = (Math.sin(percent_opened * 3.14159265) * 5) + 2;
			_width = _width + ease < max_width ? _width + ease : max_width;
			$('work_nav_title_act').style.width = _width + 'px';
			$('work_nav_item_act').style.width = 'auto';
		}
		else
		{
			$('work_nav_title_act').style.width = max_width + 'px';
			clearInterval(is_opening);
			is_opening = null;
		}
		if(_width > 200)
		{
			$('work_nav_title_act').style.width = '200px';
			clearInterval(is_opening);
			is_opening = null;
		}
		
	}, 30);
}

function close_work_nav(uri)
{
	var min_width = 0;
	var start_width = $('work_nav_title_act').offsetWidth;
	var _width = $('work_nav_title_act').offsetWidth;
	is_closing = setInterval(function()
	{
		if(_width > min_width)
		{
			percent_closed = ((start_width - _width) / start_width);
			ease = (Math.sin(percent_closed * 3.14159265) * 5) + 2;
			_width = _width - ease > min_width ? _width - ease : min_width;
			$('work_nav_title_act').style.width = _width + 'px';
			$('work_nav_item_act').style.width = (_width + 18) + 'px';
		}
		else
		{
			$('work_nav_item_act').style.width = '18px';
			$('work_nav_title_act').style.width = min_width + 'px';
			clearInterval(is_closing);
			is_closing = null;
			if(uri) go_to(uri);
		}
	}, 30);
}

/*
	Developed by Robert Nyman, http://www.robertnyman.com
	Code/licensing: http://code.google.com/p/getelementsbyclassname/
*/
var getElementsByClassName = function (className, tag, elm){
	if (document.getElementsByClassName) {
		getElementsByClassName = function (className, tag, elm) {
			elm = elm || document;
			var elements = elm.getElementsByClassName(className),
				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
			for(var i=0, il=elements.length; i<il; i+=1){
				current = elements[i];
				if(!nodeName || nodeName.test(current.nodeName)) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	else if (document.evaluate) {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
			for(var j=0, jl=classes.length; j<jl; j+=1){
				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
			}
			try	{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
			}
			catch (e) {
				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
			}
			while ((node = elements.iterateNext())) {
				returnElements.push(node);
			}
			return returnElements;
		};
	}
	else {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
			for(var k=0, kl=classes.length; k<kl; k+=1){
				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
			}
			for(var l=0, ll=elements.length; l<ll; l+=1){
				current = elements[l];
				match = false;
				for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
					match = classesToCheck[m].test(current.className);
					if (!match) {
						break;
					}
				}
				if (match) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	return getElementsByClassName(className, tag, elm);
};

function valid_email(address)
{
	var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

    return address.match(emailRegEx);
}

function trim(str)
{
	while (str.substring(0,1) == ' ') str = str.substring(1, str.length);
	while (str.substring(str.length-1, str.length) == ' ') str = str.substring(0,str.length-1);
	return str;
}

function validate(form)
{
    switch(form)
    {
        case 'contact':
            form = $('contact_form');
			var error_area = $('contact_error');
            error_area.innerHTML = '';
            
			//check name
			if(!trim(form['c_name'].value)) error_area.innerHTML+= '<p>Please enter your name</p>';
			//check email
			if(!valid_email(form['c_email'].value)) error_area.innerHTML+= '<p>Please enter a valid Email address</p>';
			//check message
			if(!trim(form['c_message'].value))  error_area.innerHTML+= '<p>Please enter a message</p>';
			if(error_area.innerHTML == '') form.submit();
        break;
    }
}

function toggle_plus(which,on)
{
	if(on)
	{
		$('main_plus_'+which).style.display = 'block';
		$('thumb_plus_1_'+which).style.display = 'block';
		$('thumb_plus_2_'+which).style.display = 'block';
	}
	else
	{
		$('main_plus_'+which).style.display = 'none';
		$('thumb_plus_1_'+which).style.display = 'none';
		$('thumb_plus_2_'+which).style.display = 'none';
	}
}

function move_element(obj,wrap,inner)
{
	var scroll_to_pos = obj.offsetLeft * -1;
	var current_pos = inner.offsetLeft;
	original_pos = current_pos;
	var movement = scroll_to_pos - current_pos;

	if (typeof(is_scrolling) != 'undefined') clearInterval(is_scrolling);
	if (movement)
	{
		is_scrolling = setInterval(function()
		{
			current_pos = inner.offsetLeft;
			percent_moved = ((current_pos - original_pos) / movement);
			ease = (Math.sin(percent_moved * 3.14159265) * 30) + 2;
			if(
				(
					(movement > 0 && current_pos+ease > scroll_to_pos)
					||
					(movement < 0 && current_pos-ease < scroll_to_pos)
				)
				||
				(
					(movement > 0 && current_pos + wrap.offsetWidth == inner.offsetWidth)
				)
			)
			{
				inner.style.left = scroll_to_pos + 'px';
				clearInterval(is_scrolling);
			}
			else inner.style.left = (movement > 0 ? current_pos+ease : current_pos-ease) + 'px';
		}, 30);
	}
}

var is_scrolling = new Array();
function move_slideset(which,max)
{
	var slidesets = new Array();
	slidesets[0] = $('image_main_'+which);
	slidesets[1] = $('image_thumb_1_'+which);
	slidesets[2] = $('image_thumb_2_'+which);
	
	var scroll_to_pos = new Array();
	scroll_to_pos[0] = slidesets[0].offsetWidth * -1;
	scroll_to_pos[1] = slidesets[1].offsetWidth * -1;
	scroll_to_pos[2] = slidesets[2].offsetWidth * -1;
	
	var current_pos = new Array();
	current_pos[0] = 0;
	current_pos[1] = 0;
	current_pos[2] = 0;
	
	var movement = new Array();
	movement[0] = scroll_to_pos[0] - current_pos[0];
	movement[1] = scroll_to_pos[1] - current_pos[1];
	movement[2] = scroll_to_pos[2] - current_pos[2];
	
	var finished = new Array();
	finished[0] = false;
	finished[1] = false;
	finished[2] = false;
	
	if (typeof(is_scrolling[which]) != 'undefined') if (is_scrolling[which]) return false;
	is_scrolling[which] = setInterval(function()
	{
		toggle_plus(which,false);
		if(which < max) toggle_plus(which+1,true);
		else toggle_plus(1,true);
		for(var slide = 0; slide < slidesets.length; slide++)
		{
			if (movement[slide])
			{
				current_pos[slide] = slidesets[slide].offsetLeft;
				percent_moved = ((current_pos[slide]) / movement[slide]);
				ease = (Math.sin(percent_moved * 3.14159265) * 30) + 2;
				if(
					(
						(movement[slide] > 0 && current_pos[slide]+ease > scroll_to_pos[slide])
						||
						(movement[slide] < 0 && current_pos[slide]-ease < scroll_to_pos[slide])
					)
				)
				{
					slidesets[slide].style.left = scroll_to_pos[slide] + 'px';
					finished[slide] = true;
				}
				else
				{
					var prev_slide = slide - 1;
					if(prev_slide > -1)
					{
						if(slidesets[prev_slide].offsetLeft < -100) slidesets[slide].style.left = (movement[slide] > 0 ? current_pos[slide]+ease : current_pos[slide]-ease) + 'px';
					}
					else slidesets[slide].style.left = (movement[slide] > 0 ? current_pos[slide]+ease : current_pos[slide]-ease) + 'px';
				}
				if(finished[0] && finished[1] && finished[2])
				{
					clearInterval(is_scrolling[which]);
					is_scrolling[which] = null;
					rejig_slideset(which);
				}
			}
		}
	}, 30);
}

function rejig_slideset(which)
{
	var _work_slideshow_wrap = $('work_slideshow_wrap');
	main_images = getElementsByClassName('image_main', 'div', _work_slideshow_wrap);
	//move em all up
	for(var i = 0; i < main_images.length; i++) main_images[i].style.zIndex = (+main_images[i].style.zIndex) + 1;
	//slot the other one in
	$('image_main_'+which).style.zIndex = 0;
	$('image_main_'+which).style.left = 0;
	thumb_images = getElementsByClassName('image_thumb', 'div', _work_slideshow_wrap);
	//move em all up
	for(var i = 0; i < thumb_images.length; i++) thumb_images[i].style.zIndex = (+thumb_images[i].style.zIndex) + 1;
	//slot the other one in
	$('image_thumb_1_'+which).style.zIndex = 0;
	$('image_thumb_1_'+which).style.left = 0;
	$('image_thumb_2_'+which).style.zIndex = 0;
	$('image_thumb_2_'+which).style.left = 0;
}

function go_to(uri)
{
	if (typeof(is_fading) != 'undefined') if (is_fading) return false;
	var _content = $('content');
	var _work_nav_wrap = $('work_nav_wrap');
	var _work_project_type = $('work_project_type');
	var _showreel_title = $('showreel_title');
	var _opacity = 100;
	if(do_fade)
	{
		is_fading = setInterval(function()
		{
			if(_opacity > 0)
			{
				percent_faded = (_opacity / 100);
				ease = (Math.sin(percent_faded * 3.14159265) * 5) + 2;
				_opacity = _opacity - ease > 0 ? Math.round(_opacity - ease) : 0;
				_content.style.opacity = _opacity / 100;
				_content.style.filter = 'alpha(opacity=' + (_opacity) + ')';
				if(_work_nav_wrap)
				{
					_work_nav_wrap.style.opacity = _opacity / 100;
					_work_nav_wrap.style.filter = 'alpha(opacity=' + (_opacity) + ')';
				}
				if(_work_project_type)
				{
					_work_project_type.style.opacity = _opacity / 100;
					_work_project_type.style.filter = 'alpha(opacity=' + (_opacity) + ')';
				}
				if(_showreel_title)
				{
					_showreel_title.style.opacity = _opacity / 100;
					_showreel_title.style.filter = 'alpha(opacity=' + (_opacity) + ')';
				}
			}
			else
			{
				_content.style.opacity = 0;
				_content.style.filter = 'alpha(opacity=0)';
				if(_work_nav_wrap)
				{
					_work_nav_wrap.style.opacity = 0;
					_work_nav_wrap.style.filter = 'alpha(opacity=0)';
				}
				if(_work_project_type)
				{
					_work_project_type.style.opacity = 0;
					_work_project_type.style.filter = 'alpha(opacity=0)';
				}
				if(_showreel_title)
				{
					_showreel_title.style.opacity = 0;
					_showreel_title.style.filter = 'alpha(opacity=0)';
				}
				clearInterval(is_fading);
				is_fading = null;
				location.href = uri;
			}
			
		}, 30);
	}
	else location.href = uri;
}