// JavaScript Document

window.addEvent('domready', domIsReady);

// for Mozilla browsers
if (document.addEventListener) {
  document.addEventListener("DOMContentLoaded", checkDomReady, false);
}


var vTime = 1600;
var vOuter = 't_videobanner';
var vInner = 't_videobannercontent';
var vOpenBtn = 't_v_open';
var vOpenCSS = '.t_videobanneropen';
var vCloseCSS = '.t_videobannerclose';
var vCloseBtn = '<a href="#" onclick="hideVideoPlayer();return false;" id="t_v_close"><span class="hidden">close</span></a>';

var vSlideFx = null;
var videoIsOpen=false;
var videoInAnim=false;
var vInnerDiv = null;
var vFirstrun = 0;//$_session
var vDomIsSet=false;

var navBtns = ['t_logo_a', 't_hem_a', 't_chok_a', 't_kakao_a', 't_ra_a', 't_nyhet_a'];
var contentHeight;
var bodyID;
var isIE = /*@cc_on!@*/false;
var isIE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;



if(isIE6){
	window.addEvent('resize', ie6bgfix);
}
function ie6bgfix(){
	if(isIE6){
		if($('t_centerstrip').style.height!=$('t_resetcontainer').offsetHeight){
			$('t_centerstrip').style.height=$('t_resetcontainer').offsetHeight;
		}
	}
}



function domIsReady(){
	vDomIsSet=true;
	
	//remember the body id
	bodyID = document.body.getAttribute('id');
	
	//ie 6 bg height fix
	ie6bgfix();
	
	//remember the video banner's content
	vInnerDiv = $(vOuter).innerHTML;
	
	//opens the video on the homepage if it is the first page visited
	if(vFirstrun==1){
		var tmp = vTime;
		vTime = 0;
		openVideoLayer();
		vTime = tmp;
		vFirstrun=0;
	};
	
	//update nav links so we can close the video feature before re-directing
	for (var i=0; i<navBtns.length; i++){
		$(navBtns[i]).setAttribute('onclick', 'handleNavClick("'+$(navBtns[i]).href+'");return false;');
	};
	
}


function checkDomReady(){
	if(vDomIsSet=false){
		setTimeout('domIsReady()',500);
	}
	else if(vFirstrun==1){//check video has opened
		var tmp = vTime;
		vTime = 0.3;
		setTimeout('openVideoLayer()',500);
		vTime = tmp;
		vFirstrun=0;
	};
}


/*
VIDEO BANNER FEATURE FUNCTIONS
*/
//all internal site page links should call this to ensure that we animate the video out before we redirect.
function handleNavClick(requestURL){
	if(requestURL == document.URL){
		
		if(!videoInAnim && videoIsOpen){
			hideVideoPlayer();
		}
	}else{
		window.location = requestURL;
	}
}


function openVideoLayer(){
	document.body.setAttribute('id', '');
	if(!videoInAnim && !videoIsOpen){
		try{
			vSlideFx.cancel();
		}catch(e){
		};
		
		$(vOuter).innerHTML = vCloseBtn+'<div id="'+vInner+'"></div>';
		$('t_v_close').style.visibility='hidden';
		
		//remember the height of the content area
		contentHeight = $('t_content').offsetHeight;
		
		//hide the content
		var contentFx = new Fx.Morph('t_content', {duration: vTime,  transition: 'quart:inOut'});
		contentFx.start({'height':0});
		
		//alpha the page header
		var headerFx = new Fx.Morph('t_header', {duration: vTime,  transition: 'quart:inOut'});
		headerFx.start('.t_headerfadeout');
		
		//alpha the page footer
		var footerFx = new Fx.Morph('t_footer', {duration: vTime,  transition: 'quart:inOut'});
		footerFx.start('.t_alpha25');
		
		//show the video
		vSlideFx = new Fx.Morph(vOuter, {duration: vTime,  transition: 'quart:inOut'});
		vSlideFx.addEvent('complete', vSlideFxComplete);
		vSlideFx.start(vOpenCSS);
		videoInAnim=true;
	};
};


function closeVideoLayer(){
	if(!videoInAnim && videoIsOpen){
		try{
			vSlideFx.cancel();
		}catch(e){
		};
		//show the page content
		var contentFx = new Fx.Morph('t_content', {duration: vTime,  transition: 'quart:inOut'});
		contentFx.start({'height':contentHeight});
		//show the page header
		var headerFx = new Fx.Morph('t_header', {duration: vTime,  transition: 'quart:inOut'});
		headerFx.start('.t_headerfadein');
		//show the page footer
		var footerFx = new Fx.Morph('t_footer', {duration: vTime,  transition: 'quart:inOut'});
		footerFx.start('.t_alpha100');
		//hide the video
		vSlideFx = new Fx.Morph(vOuter, {duration: vTime,  transition: 'quart:inOut'});
		vSlideFx.addEvent('complete', vSlideFxComplete);
		vSlideFx.start(vCloseCSS);
		videoInAnim=true;
	};
	document.body.setAttribute('id', bodyID);
};


function vSlideFxComplete(){
	ie6bgfix();
	videoInAnim=false;
	videoIsOpen = (parseFloat($(vOuter).style.height) >50) ? true : false;
	if(videoIsOpen){
		writeVideoPlayer();
		$('t_v_close').style.visibility='visible';
	}else{
		$(vOuter).innerHTML = vInnerDiv;
	};
};


function writeVideoPlayer(){
	var mov = 'http://vimeo.com/moogaloop.swf';
	var flashvars = {
		clip_id: 11134067,
		show_portrait: 0,
		show_byline: 0,
		show_title: 0,
		autoplay: 1,
		js_api: 1, // required in order to use the Javascript API
		js_onLoad: 'vimeo_player_loaded', // moogaloop will call this JS function when it's done loading (optional)
		js_swf_id: 'moogaloop1' // this will be passed into all event methods so you can keep track of multiple moogaloops (optional)
	};
	var params = {
		allowScriptAccess: 'always',
		allowfullscreen: 'true',
		wmode: 'transparent'
	};
	var attributes = {
		id: vInner
	};
	swfobject.embedSWF(mov, vInner, '1188', '588', '10', 'js/expressInstall.swf', flashvars, params, attributes);
};


function unwriteVideoPlayer(){
	try{
		swfobject.removeSWF(vInner);
	}catch(e){
	};
	$(vOuter).innerHTML = '<div id="'+vInner+'" style="width:1188px; height:588px; background-color:#000;"></div>';
}


function showVideoPlayer(){
	try{
		vSlideFx.cancel();
	}catch(e){
	};
	vSlideFx = new Fx.Morph(vInner, {duration: 1200});
	vSlideFx.start('.t_alpha100');
}


function hideVideoPlayer(){
	try{
		vSlideFx.cancel();
	}catch(e){
	};
	try{
		$(vInner).api_pause();
	}catch(e){
	}
	
	if(isIE==true){
		$(vOuter).innerHTML = '<div id="'+vInner+'"></div>';
		closeVideoLayer();
	}else{
		vSlideFx = new Fx.Morph(vInner, {duration: 900});
		vSlideFx.addEvent('complete', closeVideoLayer);
		vSlideFx.start('.t_alpha0');
	}
	try{
		$('t_v_close').style.visibility="hidden";
	}catch(e){
	}
}


function vimeo_player_loaded(){
	var vfx = new Fx.Morph(vInner, {duration: 0});
	vfx.set('.t_alpha0');
	setTimeout('showVideoPlayer()', 50);
	$(vInner).api_addEventListener('onFinish',   'vimeo_on_finish');
};


function vimeo_on_finish(){
	unwriteVideoPlayer();
	setTimeout('hideVideoPlayer()', 50);
};
/*
END VIDEO BANNER FUNCTIONS
*/








/*
OVERLAY FUNCTIONS
*/
function ajaxOverlay(file, displaySize){
	var xhr = null;
	if(window.XMLHttpRequest){
		xhr = new XMLHttpRequest();
	}else if(window.ActiveXObject){
		xhr = new ActiveXObject("Microsoft.XMLHTTP");
	}else {
		return;
	}
		
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4){
			createFloatingLayer(xhr.responseText, displaySize);
		}
	}
	
	xhr.open ('GET', file, true);
	xhr.send ('');
}


function createFloatingLayer(data, displaySize){
	var f_target = $('t_resetcontainer');
	var f_overlay = document.createElement('div');
	var f_overlaybg = document.createElement('div');
	var f_container = document.createElement('div');
	var f_content = document.createElement('div');
	var f_close = document.createElement('a');
	
	f_overlay.setAttribute('id','t_overlay');
	f_overlaybg.setAttribute('id','t_overlaybg');
	
	if(displaySize=='sml'){
		f_container.setAttribute('id','t_overlay_container_sml');
	}else if(displaySize=='mid'){
		f_container.setAttribute('id','t_overlay_container_mid');
	}else if(displaySize=='lrg'){
		f_container.setAttribute('id','t_overlay_container_lrg');
	}else{
		f_container.setAttribute('id','t_overlay_container_xlrg');
	}
	
	f_content.setAttribute('id', 't_overlay_content');
	f_close.setAttribute('id', 't_overlay_close');
	f_close.onclick=function(){removeFloatingLayer();};
	
	f_target.appendChild(f_overlay);
	f_overlay.appendChild(f_overlaybg);
	f_overlay.appendChild(f_container);
	
	f_container.appendChild(f_content);
	f_content.innerHTML = data;
	
	f_container.appendChild(f_close);
	
	window.scroll(0,0);
}


function removeFloatingLayer(){
	var f_target = $('t_resetcontainer');
	var f_overlay = $('t_overlay');
	f_target.removeChild(f_overlay);
}

/*
END OVERLAY FUNCTIONS
*/


