var nav_status = false;
function navigation(id){
	var target = document.getElementById('navigation');
	var nav_title = document.getElementById('nav-title');
	if (nav_status == false || nav_status == undefined){
		nav_status = true;
		Effect.BlindDown(id, { duration: 0.5 });
		nav_title.style.backgroundImage = "url('"+path+"/images/arrw_open.jpg')";
		nav_title.style.backgroundColor = "#EEE";
	} else {
		nav_status = false;
		Effect.BlindUp(id, { duration: 0.5 });
		nav_title.style.backgroundImage = "url('"+path+"/images/arrw_close.jpg')";
		nav_title.style.backgroundColor = "#FFF";
	}
	target.childNodes[1].style.backgroundRepeat = "no-repeat";
}
function navigationExpand(id){
	var target = document.getElementById(id);
	if (target.status == false || target.status == undefined){
		target.status = true;
		Effect.BlindDown(id, { duration: 0.5 });
		navigationClose(id);
	} else {
		target.status = false;
		Effect.BlindUp(id, { duration: 0.5 });
	}
}

function navigationClose(id){
	for (i = 0; i < 11; i++){
		var nm = "m" + i;
		if (nm != id){
			var target = document.getElementById(nm);
			target.status = false;
			Effect.BlindUp(nm, { duration: 0.5 });
		}
	}
}

var gc = 0;
var gs = 1;
var sid;
var vc = false;
var t;
var loop = true;

function no4_gallery_init(){
	var n;
	var si;
	vc = videoChk(location.hash);
	if (location.hash == ""){
		n = 1;
	} else {
		if (vc){
			n = location.hash;
			n = n.split("#video-").join("");
			gs = n;
		} else {
			n = location.hash;
			n = n.split("#").join("");
			gs = n;
		}
	}
	if (vc){
		galleryVideo(n);
	} else {
		no4_gallery_view(n);
	}
	clearInterval(si);
	si = setInterval(no4_gallery_check, 500);
}

function videoChk(str){
	var isVideo = str.split("#video-");
	if (isVideo.length == 2){ return true }
	return false;
}

function no4_gallery_prev(){
	vc = videoChk(location.hash);
	if (gs > 1){
		gs--;
		if (vc){
			location.hash = "video-"+gs;
			sendMoviePath(gs);
		} else { 
			location.hash = gs;
			no4_gallery_view(gs);
		}
	}
}
function no4_gallery_next(p){
	vc = videoChk(location.hash);
	if (gs < images.length){
		gs++;
		if (vc){
			location.hash = "video-"+gs;
			sendMoviePath(gs);
		} else { 
			location.hash = gs;
			no4_gallery_view(gs);
		}
	}
}

function no4_gallery_stop(){
	loop = false;
	clearTimeout(t);
	var target = document.getElementById('slideplay');
	target.src = path + "/images/play.jpg";
	var target = document.getElementById('slidebutton');
	target.href = "javascript:no4_gallery_play()";
}

function no4_gallery_play(){
	loop = true;
	if (gs == images.length){
		gs = 0;
	}
	no4_gallery_next();
}

function no4_gallery_view(n){
	
	if (n != 0){
		var target = document.getElementById('no4-gallery');
		var tmp_chk = document.getElementById("loader");
		if (tmp_chk) {
			Effect.Appear(tmp_chk, { duration: 0.3 });
		} else {
			var tmp = document.createElement('div');
			tmp.id = "loader";
			tmp.innerHTML = "loading..."
			target.appendChild(tmp);
		}
		var tmp = document.createElement('img');
		tmp.style.display = "none";
		tmp.src = images[n-1];
		tmp.id = "img" + gc;
		sid = tmp.id;
		tmp.width = "550";
		tmp.height = "365";
		tmp.onload = chk;
		gs = n;
		gc++;
		target.appendChild(tmp);
		
		var c_display = "none";
		
		tmp_chk = document.getElementById("slidebutton");
		if (tmp_chk) {
			if ( getStyle("slideplay","display") == "block" ) c_display = "block";
			
			var d = document.getElementById('no4-gallery');
			var olddiv = document.getElementById("slidebutton");
			var sb = document.getElementById('slidebutton');
			d.removeChild(olddiv);	
		}
		
		var a_tmp = document.createElement('a');
		a_tmp.id = "slidebutton";
		a_tmp.setAttribute("onmouseover","hideSlideButton = false");
		a_tmp.setAttribute("onmouseout","hideSlideButton = true");
		(loop == true ) ? a_tmp.href = "javascript:no4_gallery_stop()" : a_tmp.href = "javascript:no4_gallery_play()";
		
		var target = document.getElementById('no4-gallery');
		var tmp = document.createElement('img');
		(loop == true ) ? tmp.src = path + "/images/pause.jpg" : tmp.src = path + "/images/play.jpg";
		tmp.id = "slideplay";
		tmp.style.display = c_display;
		a_tmp.appendChild(tmp);
		target.appendChild(a_tmp);
		
		no4_gallery_select(n);
		var video_player = document.getElementById("no4-gallery-video");
		if (video_player){
			var img_tab = document.getElementById("tab-image");
			img_tab.style.backgroundColor = "#333333";
			var vdo_tab = document.getElementById("tab-video");
			vdo_tab.style.backgroundColor = "#EEEEEE";
			var no4_gallery = document.getElementById("no4-gallery");
			video_player.innerHTML = '';
		}
	}
}

function getStyle(el,styleProp){
	var x = document.getElementById(el);
	if (x.currentStyle)
		var y = x.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	return y;
}


function no4_gallery_select(n){
	var selected;
	var vdo_menu = document.getElementById("no4-gallery-video-menu");
	vdo_menu.style.display = "none";
	var img_menu = document.getElementById("no4-gallery-menu");
	img_menu.style.display = "block";
	
	for (i = 1; i <= images.length; i++){
		selected = document.getElementById('gm-'+i);
		selected.style.textDecoration = "none";	
	}
	selected = document.getElementById('gm-'+n);
	selected.style.textDecoration = "underline";
}

function no4_gallery_video_select(n){
	var selected;
	var img_menu = document.getElementById("no4-gallery-menu");
	img_menu.style.display = "none";
	var vdo_menu = document.getElementById("no4-gallery-video-menu");
	vdo_menu.style.display = "block";
	
	for (i = 1; i <= videos.length; i++){
		selected = document.getElementById('gmv-'+i);
		selected.style.textDecoration = "none";	
	}
	selected = document.getElementById('gmv-'+n);
	selected.style.textDecoration = "underline";
}

function no4_gallery_check(){
	vc = videoChk(location.hash);
	var n = location.hash;
	if (vc){
		n = n.split("#video-").join("");
	} else {
		n = n.split("#").join("");
	}
	
	if (gs != n){
		if (vc){
			sendMoviePath(n);
		} else {
			no4_gallery_view(n);
		};
	}
}

function clear(){
	var chk = document.getElementById("img"+ (gc-2));
	var d = document.getElementById('no4-gallery');
	
	if (chk) d.removeChild(chk);
}

function chk(){
	var loader = document.getElementById("loader");
	Effect.Appear(sid, { duration: 0.5, afterFinish: clear });
	Effect.Fade(loader, { duration: 0.3 });
	n = location.hash;
	n = n.split("#").join("");
	if ( images.length == n ) {
		if (loop) t = setTimeout("location.hash = 1", 5000);
	} else {
		if (loop) t = setTimeout("no4_gallery_next(true)", 5000);
	}
}

function hidebutton(){
	if (hideSlideButton){
		Effect.Fade('slideplay', { duration: 0.5 });
	} else {
		Effect.Appear('slideplay', { duration: 0.5 });
	}
}

var hideSlideButton = false;

function hideplay(){
	//if (hideSlideButton) Effect.Fade('slideplay', { duration: 0.5, onFinish: slide_hide });
	hideSlideButton = true;
	sh = setTimeout("hidebutton()", 250);
}

function showplay(){
	//if (!hideSlideButton) Effect.Appear('slideplay', { duration: 0.5 });
	hideSlideButton = false;
	sh = setTimeout("hidebutton()", 250);
	
	setTimeout()
}

function galleryShow(){
	Effect.Appear('no4-gallery-image', { duration: 0.5 });
}

function galleryVideo(n){
	gs = 0;
	var img_tab = document.getElementById("tab-image");
	img_tab.style.backgroundColor = "#EEEEEE";
	var vdo_tab = document.getElementById("tab-video");
	vdo_tab.style.backgroundColor = "#333333";
	var no4_gallery = document.getElementById("no4-gallery");
	no4_gallery.innerHTML = '<div id="no4-gallery-video"></div>';
	
	var flashvars = { path: path+"/assets/player.swf" };
	var params = {};
	var attributes = { id: "videoplayer" };
	swfobject.embedSWF(path+"/assets/loader.swf", "no4-gallery-video", "550px", "365px", "9.0.0", path+"/assets/expressInstall.swf", flashvars, params, attributes);
}

function galleryImage(){
	gs = 0;
	var img_tab = document.getElementById("tab-image");
	img_tab.style.backgroundColor = "#333333";
	var vdo_tab = document.getElementById("tab-video");
	vdo_tab.style.backgroundColor = "#EEEEEE";
	var no4_gallery = document.getElementById("no4-gallery");
	no4_gallery.innerHTML = '';
	
	no4_gallery_init();
}

function thisMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}
function sendToActionScript(value) {
	thisMovie("videoplayer").sendToActionScript(value, path);
}

function sendMoviePath(n){
	soundStop();
	if (n == undefined){
		sendToActionScript(videos[gs-1]);
		no4_gallery_video_select(gs);
	} else {
		sendToActionScript(videos[n-1]);
		no4_gallery_video_select(n);
		gs = n;
	}
}

var fileCount = 3;

function addfile(){
	fileCount++;
	var target = document.getElementById("works");
	var nm = "w" + fileCount;
	var node = '<li><input type="file" name="'+nm+'" /></li>';
	target.innerHTML = target.innerHTML + node;
}

var fields = new Array("job", "surname", "telephone", "mobile", "email", "company", "position");

function validatejob(){
	var form = document.jobapp;
	var error = new Array();
	
	for (i = 0; i < fields.length; i++){
		if (form[fields[i]].value == ""){
			form[fields[i]].style.border = "1px solid #CC0000";
			error.push(i);
		} else {
			form[fields[i]].style.border = "1px solid #DDD";
		}
	}
	
	if (error.length == 0){
		form.submit();
	}
}

function resetjob(){
	var form = document.jobapp;
	for (i = 0; i < fields.length; i++){
		form[fields[i]].style.border = "1px solid #DDD";
	}
}


// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)


// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0

// Main function to retrieve mouse x-y pos.s

function getMouseXY(e) {
	
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  // show the position values in the form named Show
  // in the text fields named MouseX and MouseY
  var mxy = new Array();
  mxy.push(tempX);
  mxy.push(tempY);
  return mxy;
}


document.onclick = test;

function test(e){
	if (nav_status == true){
		var nav = document.getElementById('navigation');
		var height = nav.offsetHeight;
		var xy = getMouseXY(e);
		if (xy[0] > 875 || xy[0] < 380 || xy[1] < 35 || xy[1] > (height+35)){
			navigation('menu');
		}
	}
}


function sendToSound(value) {
	var target = document.getElementById('soundon');
	target.style.textDecoration = "underline";
	target = document.getElementById('soundoff');
	target.style.textDecoration = "none";
	thisMovie("soundplayer").sendToSound(value);
}

function soundStop() {
	var target = document.getElementById('soundon');
	target.style.textDecoration = "none";
	target = document.getElementById('soundoff');
	target.style.textDecoration = "underline";
	if (thisMovie("soundplayer")){ thisMovie("soundplayer").soundStop(); };
}

function soundPlay() {
	var target = document.getElementById('soundon');
	target.style.textDecoration = "underline";
	target = document.getElementById('soundoff');
	target.style.textDecoration = "none";
	thisMovie("soundplayer").soundPlay();
}

function sendSoundPath(){
	sendToSound(soundPath);
}

function sound_play() {
	soundPlay();
}

function sound_stop() {
	soundStop();
}

function sound_init(){
	var target = document.getElementById('sound');
	target.style.display = "block";
	
	var flashvars = { path: path+"/assets/soundplayer.swf" };
	var params = {};
	var attributes = {};

	attributes.id = "soundplayer";

	swfobject.embedSWF(path+"/assets/loader.swf", "soundplayerholder", "10px", "10px", "9.0.0", path+"/assets/expressInstall.swf", flashvars, params, attributes);

}

var hmc = 0;

function home_chk(){
	var loader = document.getElementById("loader");
	Effect.Appear(sid, { duration: 0.5, afterFinish: clear });
	Effect.Fade(loader, { duration: 0.3, onFinish: clear });
	t = setTimeout("no4_gallery_home()", 5000);
}

function no4_gallery_home_view(n){
	var target = document.getElementById('no4-gallery');
	var tmp_chk = document.getElementById("loader");
	if (tmp_chk) {
		Effect.Appear(tmp_chk, { duration: 0.3 });
	} else {
		var tmp = document.createElement('div');
		tmp.id = "loader";
		tmp.innerHTML = "loading..."
		target.appendChild(tmp);
	}
	
	var tmp = document.createElement('img');
	tmp.style.display = "none";
	tmp.src = images[n-1];
	tmp.id = "img" + gc;
	sid = tmp.id;
	tmp.width = "550";
	tmp.height = "365";
	tmp.onload = home_chk;
	gs = n;
	gc++;
	target.appendChild(tmp);
	
	target = document.getElementById('next');
	target.innerHTML = '<a href="' + links[n-1] + '">see project detail</a>';
}


function no4_gallery_home(){
	//loop = false;
	hmc++;
	if (hmc == (images.length+1) ){
		hmc = 1;
	}
	no4_gallery_home_view(hmc);
}