var langGlobal = 'en';
var contentVisible = 0;
var cloudOpen = null;
var curCloud = null;
var menuInt = 0;
var menuItemInt = 0;
var curStage = '';
var remCluster = new Array();
var clusterMem = new Array();
var titlePos = new Array();
var childMem = new Array();
var fontWrapper = '';

window.addEvent('domready', function() {
   //buildMenu();
   //buildCloud();
   //alert (lang.getCurrentLanguage());
    
    if (httpLang=='ko'||httpLang=='ko-kr'||httpLang=='kr')langGlobal = 'ko';
    if (langGlobal=='ko')random = Math.ceil(Math.random()*3);
    else random = Math.ceil(Math.random()*6);
   //alert (random);
	$('logoPh').set('html','<img src="html/img/header_'+random+'.gif" class="logo">&nbsp'); 
	
 	$('mailingList').addEvents({
	'focus': function() {
	
		if ($('mailingList').value=='enter your e-mail'||$('mailingList').value=='e-mail 주소 입력')$('mailingList').value='';
	}
	});
	$('logoPh').addEvents({
	'click': function() {
		if (contentVisible == 1)closeContent()
	}
	});
   if (goTo=='NULL')'';
   else{
    menu= goTo.substr(3,1)
    item= goTo.substr(8,1)
    lang= goTo.substr(13,2)
    if (lang=='kr')lang ='ko';
    langGlobal = lang;
    showContent(menu,item)
   }
   switchLang(langGlobal);
});
var menu1en = new Array('the 6th biennale','overview','curatorial team','staff','sponsors','press');
var menu2en = new Array('practical information','schedule','venues','visiting information','audio-guide','matrix -s');
var menu3en = new Array('exhibition','theme','artists');
var menu4en = new Array('programs','performance','outdoor project','education','symposium');
var menu5en = new Array('&','photo','video','live','twitter','facebook');


var menu1ko = new Array('the 6th biennale','개요','큐레이토리얼 팀','스태프','후원','보도자료');
var menu2ko = new Array('practical information','전시일정','전시장소','방문안내','오디오가이드','매트릭스-s');
var menu3ko = new Array('exhibition','전시주제','참여작가');
var menu4ko = new Array('programs','퍼포먼스','야외프로젝트','교육프로그램','심포지움');
var menu5ko = new Array('&','사진','비디오','라이브','트위터','페이스북');

var overviewen=new Array('','overview','organization','past biennials');
var overviewko=new Array('','개요','조직','지난 비엔날레');

var pressen = new Array('','press release','logo','contact');
var pressko = new Array('','보도자료','로고','연락처');

var museumAdressEn = 'Seoul Museum of Art. 30 Misulgwan-gil, Jung-gu, Seoul. 100-813, South Korea';
var museumAdressKo = '서울시 중구 미술관길 30 서울시립미술관. 100-813';



function printIt(){
	
	  window.print();
}
function mailing(){
	var email = $('mailingList').value
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   	//var address = document.forms[form_id].elements[email].value;
  	if(reg.test(email) == false) {
      alert('Invalid Email Address');
      return false;
   	}else{
   		var jsonRequest = new Request.JSON({url: "html/php/json.php", onSuccess: function(json){
			alert('Thank you, your email adress has been added to the mailinglist')
		}}).get({'submitEmail':email, 'language':langGlobal});
   		
   	}
}
function cloudClose(cluster,mode){
	blendIn(cluster);
	cloudOpen = null;
	
	//alert(titlePos[0].top);
	var thisCluster = $('collumn_'+cluster)
	$('close_'+cluster).setStyle('visibility','hidden');
	thisCluster.setStyle('z-index','0');
	var title 	= thisCluster.getChildren('.clusterTitle');
	var cloud 	= thisCluster.getChildren('.cloudObject');
	var video 	= thisCluster.getChildren('.videoItem');
	var photo 	= thisCluster.getChildren('.photoItem');
	var news 	= thisCluster.getChildren('.newsItem');
	var postit 	= thisCluster.getChildren('.postitItem');
	var combinedContent = cloud.concat(photo, video, news);
	
	if (postit.length>0){
		var titleEffect = new Fx.Morph(postit[0], {duration: '300', transition: Fx.Transitions.Sine.easeOut});
		titleEffect.start({
			    'opacity'	:[0, 100]
		});		
	}
	
	
	var postEffect = new Fx.Morph(title[0], {duration: '300', transition: Fx.Transitions.Sine.easeOut});
	postEffect.start({
		    'left'	: 50+'px', //Morphs the 'height' style from 10px to 100px.
		    'top'	: titlePos[0].top,
		    'width'	: titlePos[0].width,
		    'height': titlePos[0].height,
		    'background-color':titlePos[0].background
	});
	
	for (var a=0; a<combinedContent.length; a++){
		child = combinedContent[a].getChildren();
		
		child.setStyle('visibility',childMem[a].visibility);
		
		child.setStyle('height','auto');
		
		var myEffect = new Fx.Morph(combinedContent[a], {duration: '300', transition: Fx.Transitions.Sine.easeOut});
 		if (mode==0){
		myEffect.start({
		    'left'	: remCluster[a].left, //Morphs the 'height' style from 10px to 100px.
		    'top'	: remCluster[a].top,
		    'width'	: remCluster[a].width,
		    'height': remCluster[a].height,
		    'background-color':remCluster[a].background,
		    'padding':remCluster[a].padding
		});
		}else{
		myEffect.start({
		    'left'	: remCluster[a].left, //Morphs the 'height' style from 10px to 100px.
		    'top'	: remCluster[a].top,
		    'width'	: remCluster[a].width,
		    'height': remCluster[a].height,
		    'padding':remCluster[a].padding
		});
		}

	}
	
}
function testClick(){

}
function cloudLink(link){
	//alert(link);
	window.open(link)
}
function cloudOver(j, i){

	var object = $('objJ'+j+'I'+i);
	var media = object.getChildren('.cloudHidden');
	var overlay = media.getChildren('.cVideoTitle');
	overlay[0].setStyle('visibility','visible');
	//alert (i+' '+j);
}
function cloudOut(j, i){
	var object = $('objJ'+j+'I'+i);
	var media = object.getChildren('.cloudHidden');
	var overlay = media.getChildren('.cVideoTitle');
	overlay[0].setStyle('visibility','hidden');
}
function cloudClick(cluster, xposi, tSize){
	
	if (cloudOpen==1&&cluster==curCloud){
		//cloudClose(curCloud, 1);
	}else{
		if (cloudOpen==1)cloudClose(curCloud, 1);
		if (cluster==curCloud){
			//alert(curCloud)
			curCloud=null;
		}else{
			cloudOpen = 1;
			curCloud = cluster;
			remCluster = new Array();
			var titleOffset = Math.round((((tSize*0.8)*1.3)+3)/2)*2;
			var thisCluster = $('collumn_'+cluster)
			thisCluster.setStyle('z-index','1');
			blendOut(cluster)
			var title 	= thisCluster.getChildren('.clusterTitle');
			var cloud 	= thisCluster.getChildren('.cloudObject');
			var video 	= thisCluster.getChildren('.videoItem');
			var photo 	= thisCluster.getChildren('.photoItem');
			var news 	= thisCluster.getChildren('.newsItem');
			var postit 	= thisCluster.getChildren('.postitItem');
			var xOffset = 0;
			if (cluster ==1)xOffset = -100;
			if (cluster ==2)xOffset = -100;
			if (cluster ==3)xOffset = -200;
			$('close_'+cluster).setStyle('visibility','visible');
			var combinedContent = cloud.concat(photo, video, news);
			//alert (combinedContent)
			var row = 0;
			var col = 0;
			
			if (postit.length>0){
				var postEffect = new Fx.Morph(postit[0], {duration: '300', transition: Fx.Transitions.Sine.easeOut});
				postEffect.start({
					    'opacity'	: 0
				});		
			}
			
			titlePos.push(title[0].getStyles('left', 'top', 'width', 'height', 'background-color'));
			var titleEffect = new Fx.Morph(title[0], {duration: '300', transition: Fx.Transitions.Sine.easeOut});
			//alert(titlePos[0].top)
			titleEffect.start({
				    'left'	: xOffset+'px', //Morphs the 'height' style from 10px to 100px.
				    'top'	: 0-15+'px' //Morphs the 'width' style from 900px to 300px.
			});
		
			
			for (var a=0; a<combinedContent.length; a++){
				remCluster.push(combinedContent[a].getStyles('left', 'top', 'width', 'height', 'background-color', 'padding', 'opacity'));
		
				if (col>3){
					col = 0
					row++;
				}
				
				child = combinedContent[a].getChildren();
				childMem.push(child.getStyles('visibility', 'opacity'));
				xpos = (col*100);
				ypos = (row*100)+(titleOffset-5);
				combinedContent[a].setStyle('overflow','hidden');
				child.setStyle('height','100px');
				child.setStyle('visibility','visible');
				var myEffect = new Fx.Morph(combinedContent[a], {duration: '300', transition: Fx.Transitions.Sine.easeOut});
		 	
				myEffect.start({
				    'left'	: xOffset+xpos+'px', //Morphs the 'height' style from 10px to 100px.
				    'top'	: ypos+'px' , //Morphs the 'width' style from 900px to 300px.
				    'width'	: 90+'px',
				    'height': 90+'px',
				    'background-color': '#000000',
				    'padding': '0'
				    
				});
				
				//els[a].setStyle('top',0+'px');
				col++;
			}
		}
	}
}
function getHeader(){
	var jsonRequest = new Request.JSON({url: "html/php/json.php", onSuccess: function(json){
		//alert(json.content);
		$('sub_header').set('html',json.content);
	}}).get({'getHeader':'ok', 'lang':langGlobal});
}
function buildCloud(){
		var jsonRequest = new Request.JSON({url: "html/php/json.php", onSuccess: function(json){
			$('stage').set('html', json.content);
			curStage = json.content;
			clusterMem = new Array();
			cloudOpen = 0;
			curCloud = null;
			//alert(json.content)
			if (contentVisible ==1)blendOut();
		}}).get({'buildCloud':'ok', 'cloudLang':langGlobal});	
	
}
function showArtist(stamp){
	var jsonRequest = new Request.JSON({url: "html/php/json.php", onSuccess: function(json){
		$('ArtistCollumn2').set('html',json.content);
	}}).get({'getArtist':stamp, 'lang':langGlobal});	
}
function gotoUrl(url){
	//alert(url);
}
function closeContent(){
	$('content').set('html', 'loading');
	$('content').setStyle('visibility', 'hidden');
	$('innerSpace').setStyle('height',720+'px');
	contentVisible = 0;
	blendIn();
}
function buildMenu(){
	for(i=1; i<6; i++){
		var menuOutput='';
		var menu = eval('menu'+i+langGlobal);
		for(j=0; j<menu.length; j++){
			if (j==0) menuOutput+='<b>'+menu[0]+'</b><br/><div class="menuDown">'+fontWrapper;
			else{
				menuOutput+='<div><span class="hand" onclick="showContent(\''+i+'\',\''+j+'\')">'+menu[j]+'</span></div>';
			}
		}
		menuOutput+='</div></div>';
		$('menu'+i).set('html',menuOutput);
	}
}
function showContent(i,j){
if (cloudOpen == 1)cloudClose(curCloud)
blendOut()
	menuInt = i;
	menuItemInt = j;
	var menu = eval('menu'+i+langGlobal)
	var overView = eval('overview'+langGlobal)
	var press = eval('press'+langGlobal);
	if (i==5){
		closeContent()
		if (j==1)window.open('http://www.flickr.com/photos/mediacityseoul/sets/');
		if (j==2)window.open('http://www.vimeo.com/mediacityseoul');
		if (j==3)window.open('http://www.ustream.tv/user/mediacityseoul');
		if (j==4){
			if (langGlobal=='ko')window.open('http://twitter.com/mediacityseoulK')
			else window.open('http://twitter.com/mediacityseoulE')
			}
		if (j==5)window.open('http://www.facebook.com/mediacityseoul');
	}else{
		if (contentVisible==0)$('content').setStyle('visibility','visible');
		var jsonRequest = new Request.JSON({url: "html/php/json.php", onSuccess: function(json){
			$('content').set('html', json.content);
			$('printable').set('html', json.print);
			$('contentCat').set('html', '<span class="cTtitleWrap">'+menu[0]+'</span>');
			$('contentName').set('html', '<span class="cTtitleWrap">'+menu[j]+'</span>');
			/*if(i==4){
				$('contentName').set('html', '<span class="cTtitleWrap">'+menu[1]+'</span>');
				$('contentName2').set('html', '<span class="cTtitleWrap">'+menu[2]+'</span>');
				$('contentName3').set('html', '<span class="cTtitleWrap">'+menu[3]+'</span>');
			}else 
			*/if(i==1&&j==1){
				$('contentName').set('html', '<span class="cTtitleWrap">'+overView[1]+'</span>');
				$('contentName2').set('html', '<span class="cTtitleWrap">'+overView[2]+'</span>');
				$('contentName3').set('html', '<span class="cTtitleWrap">'+overView[3]+'</span>');
			}else if(i==1&&j==5){
				$('contentName').set('html', '<span class="cTtitleWrap">'+press[1]+'</span>');
				$('contentName2').set('html', '<span class="cTtitleWrap">'+press[2]+'</span>');
				$('contentName3').set('html', '<span class="cTtitleWrap">'+press[3]+'</span>');
			}
			contentVisible=1;
			var hoogte = $('content').getStyle('height').toInt();
			hoogte+=350;
			if (hoogte<730)hoogte = 730;
			//alert (hoogte);
			
			$('innerSpace').setStyle('height',hoogte+'px');
			
		}}).get({'getContent':i,'item':j, 'lang':langGlobal});
	}
}
function switchLang(lang){
	if (lang=='en'){
		langGlobal = 'en';
		$('langEn').setStyle('font-weight','bold');
		$('langKo').setStyle('font-weight','normal');
		$('mailingList').value='enter your e-mail';
		$('subscribe').set('html','subscribe to the mailing list');
		fontWrapper='<div class="font_en">';
		
	}else{
		langGlobal = 'ko';
		
		$('langEn').setStyle('font-weight','normal');
		$('langKo').setStyle('font-weight','bold');
		$('mailingList').value='e-mail 주소 입력';
		$('subscribe').set('html','메일링 가입하기');
		fontWrapper='<div class="font_ko">';
	}
	buildCloud();
	buildMenu();
	getHeader();
	logos();
	switchDisclaimer(lang);
	if (contentVisible==1)showContent(menuInt,menuItemInt);
}
function logos(){
	if(langGlobal=='en'){
		$('logosB').set('html','<a href="http://seoulmoa.seoul.go.kr/" target="_blank"><img src="html/img/logo1.gif"></a><a href="http://english.seoul.go.kr/" target="_blank"><img src="html/img/logo2.gif"></a><a href="http://english.hiseoulfest.org/" target="_blank"><img src="html/img/logo3.gif"></a>');
	}else{
		$('logosB').set('html','<a href="http://seoulmoa.seoul.go.kr/" target="_blank"><img src="html/img/logo1.gif"></a><a href="http://www.seoul.go.kr/" target="_blank"><img src="html/img/logo2.gif"></a><a href="http://www.hiseoulfest.org/" target="_blank"><img src="html/img/logo3.gif"></a>');
	}
	$('logosF').set('html','<a href="http://www.frieze.com/" target="_blank"><img src="html/img/frieze.gif"></a>');
}
function switchDisclaimer(lang){
	if (lang=='en'){
	$('museumAdress').set('html','<span class="small" style="line-height:12px;">Seoul Museum of Art.<br/> 30 Misulgwan-gil, Jung-gu, Seoul. 100-813, South Korea')
	$('disclaimer').set('html','<span class="small" style="line-height:12px;">The content of the data clusters on the main page is aggregated by an autonomous computer program and do not reflect the views of the organization.</span>');
	$('copyright').set('html','<span class="small" style="line-height:12px;"><a class="copyright" href="http://creativecommons.org/licenses/by-nc-nd/2.0/" target="_blank">This website follows Creative Commons BY-NC-ND License.</a></span>');
	}else{
	$('museumAdress').set('html','<span class="koreanDC" style="line-height:12px;">서울시 중구 미술관길 30 서울시립미술관. 100-813</span></span>');
	$('disclaimer').set('html','<span class="koreanDC" style="line-height:13px;">메인페이지 하단 데이터 클러스터 상의 내용은 자율적으로 작동하는 컴퓨터 프로그램에 의해 수집되며, 서울시립미술관의 공식입장을 반영하지 않습니다.</span>');
	$('copyright').set('html','<span class="koreanDC" style="line-height:13px;"><a class="copyright" href="http://creativecommons.org/licenses/by-nc-nd/2.0/deed.ko" target="_blank">이 웹사이트는 크리에이티브 커먼스 BY-NC-ND 라이센스를 따릅니다.</a></span>');
	}
}
function blendOut(exempt){
	var els = $('stage').getChildren();
	for (var a=0; a<els.length; a++){
		clusterMem.push(els[a].get('html'));
		if(a!=exempt){
			child = els[a].getChildren();
			grandChild = child.getChildren();
			for (var b=0; b<grandChild.length; b++){
				grandChild[b].setStyle('visibility','hidden');
	
			}
			child.setStyle('background-image','url(\'html/img/trans.gif\')');
			child.setStyle('background-color','transparent');
			child.setStyle('opacity','1');
			}
		}
}

function blendIn(exempt){
	var els = $('stage').getChildren();
	for (var a=0; a<els.length; a++){
		if (a!=exempt){
			els[a].set('html',clusterMem[a]);
		}
	}
	//$('stage').set('html', curStage);
}
function addslashes(str) {
str=str.replace(/\\/g,'\\\\');
str=str.replace(/\'/g,'\\\'');
str=str.replace(/\"/g,'\\"');
str=str.replace(/\0/g,'\\0');
return str;
}
function stripslashes(str) {
str=str.replace(/\\'/g,'\'');
str=str.replace(/\\"/g,'"');
str=str.replace(/\\0/g,'\0');
str=str.replace(/\\\\/g,'\\');
return str;
}
