/* layout/general */

if(document.getElementById) {
	window.getObj = function(id) {
		return document.getElementById(id);
	}
} else {
	window.getObj = function(id) {
		return false;
	}
}
function layout() {
//alert('test');
	var cHeight=0;
	var sHeight=0;
	
	if (window.document.documentElement && window.document.documentElement.scrollHeight){
		sHeight = window.document.documentElement.scrollHeight;
	} else if (window.document.body){
		sHeight = window.document.body.scrollHeight;
	}
	
	if (!!window.innerHeight){
		cHeight = document.body.scrollHeight;
	} else if (window.document.documentElement && window.document.documentElement.clientHeight){
		cHeight = window.document.documentElement.clientHeight;
	} else if (window.document.body){
		cHeight = window.document.body.clientHeight;
	}
	
	//alert(document.documentElement.scrollHeight+':'+self.innerHeight+':'+document.documentElement.clientHeight+':'+document.body.scrollHeight);
	
	var left = document.getElementById('left');
	var frontcontent = document.getElementById('front-contents');
	var content = document.getElementById('contents');
	var content_wide_white = document.getElementById('content_wide-white');
	var iframecontent = document.getElementById('iframe-contents');
	var externdoc = document.getElementById('externdoc');
	var related = document.getElementById('related');
	var right = document.getElementById('right');
	
	var contactdb_txt = document.getElementById('contactdb_txt');
	var advsearch_txt = document.getElementById('advsearch_txt');
	
	if( left ) left.style.height=((cHeight>=sHeight?cHeight:sHeight)-57)+'px';
	if( content ) content.style.height=((cHeight>=sHeight?cHeight:sHeight)-102)+'px';
	if( content_wide_white ) content_wide_white.style.height=((cHeight>=sHeight?cHeight:sHeight)-80)+'px';
	if( frontcontent ) frontcontent.style.height=((cHeight>=sHeight?cHeight:sHeight)-80)+'px';
	if( iframecontent ) iframecontent.style.height=((cHeight>=sHeight?cHeight:sHeight)-80)+'px';
	if( externdoc ) externdoc.style.height=((cHeight>=sHeight?cHeight:sHeight)-40)+'px';
	if( related ) related.style.height=((cHeight>=sHeight?cHeight:sHeight)-40)+'px';
	if( right ) right.style.height=((cHeight>=sHeight?cHeight:sHeight)-60)+'px';
	if( contactdb_txt ) contactdb_txt.style.height=((cHeight>=sHeight?cHeight:sHeight)-137)+'px';	
	if( advsearch_txt ) advsearch_txt.style.height=((cHeight>=sHeight?cHeight:sHeight)-145)+'px';
}

window.onresize=layout;

/* form validation */
function chkFormContactDatabase(f){
	
	if (f&&((f.searchtype.selectedIndex==0&&(f.company.value.replace(/ /g,'')!=''||f.cat_comp.selectedIndex!=0))||(f.searchtype.selectedIndex==1&&(f.person.value.replace(/ /g,'')!=''||f.cat_name.selectedIndex!=0))||f.country_id.selectedIndex!=0||f.genre_id.selectedIndex!=0)){
		return true;
	}
	alert('Please state search criteria(s).');
	return false;
}
function changeFormContactDatabase(idx){
	switch(idx){
		case 0:
			document.getElementById('div_name').style.display='none';
			document.getElementById('div_company').style.display='block';
			document.getElementById('div_cat_name').style.display='none';
			document.getElementById('div_cat_company').style.display='block';
			break;
		case 1:
			document.getElementById('div_name').style.display='block';
			document.getElementById('div_company').style.display='none';
			document.getElementById('div_cat_name').style.display='block';
			document.getElementById('div_cat_company').style.display='none';
			break;
	}
}

function changeFormSearch(idx){
	var f=document.getElementById('frmSearch');
	var fid=document.getElementById('id');
	var fkeyword=document.getElementById('frmSearchKeywords');	
	var fi01=document.getElementById('i01');
	var fperson=document.getElementById('person');
	var fcompany=document.getElementById('company');

	switch(idx){
		case 0:
			f.method='get';
			fid.value='107';
			break;
		case 1:
			f.method='get';
			fid.value='142';
			fi01.value=fkeyword.value;
			break;
		case 2:
			f.method='get';
			fid.value='150';
			fcompany.value=fkeyword.value;
			break;
		case 3:
			f.method='get';
			fid.value='150';
			fperson.value=fkeyword.value;
			break;
	}
}

/* cookie */
function clearCookie(cookieName) {
	var now = new Date();
	var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);
	setCookie(cookieName, 'cookieValue', yesterday);
}
function setCookie(cookieName, cookieValue, expires, path, domain, secure) {
	
	document.cookie =
		escape(cookieName) + '=' + escape(cookieValue)
		+ (expires ? '; expires=' + expires.toGMTString() : '')
		+ (path ? '; path=' + path : '')
		+ (domain ? '; domain=' + domain : '')
		+ (secure ? '; secure' : '');
}
function getCookie(cookieName) {
	var cookieValue = false;
	var posName = document.cookie.indexOf(escape(cookieName) + '=');
	if (posName != -1) {
		var posValue = posName + (escape(cookieName) + '=').length;
		var endPos = document.cookie.indexOf(';', posValue);
		if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));
		else cookieValue = unescape(document.cookie.substring(posValue));
	}
	return (cookieValue);
}

/* film navigation */
var filmNavCurrentId=-1;

function filmGetIndex(id){
	var idx=0;
	if(filmSearchResult)
		for(var i=0;i<filmSearchResult.length;i++)
			if(filmSearchResult[i]==id){
				idx=i;break;
			}
	return idx;
}
function filmNavNext(){
	if($('#externdoc') && filmSearchResult && filmNavCurrentId!=-1){
			var idx = filmGetIndex(filmNavCurrentId);
			idx = (idx+1 >= filmSearchResult.length ? 0 : idx+1);
			filmNavCurrentId = filmSearchResult[idx];
			$('#externdoc').attr('src','/showroom/da/?id=4&movieid='+filmNavCurrentId);
	}
}
function filmNavPrevious(){
	if($('#externdoc') && filmSearchResult && filmNavCurrentId!=-1){
			var idx = filmGetIndex(filmNavCurrentId);
			idx = (idx-1 < 0 ? filmSearchResult.length-1 : idx-1);
			filmNavCurrentId = filmSearchResult[idx];
			$('#externdoc').attr('src','/showroom/da/?id=4&movieid='+filmNavCurrentId);
	}
}
function filmNavToList(){
	if($('#searchFrm1')){
		$('#searchFrm1').submit();
	}
}

//jQuery init stuff

$(function() {
	//layout();
					
	$('tr.movie td').tooltip({
		delay: 10,
		track: true,
		extraClass: 'fkn-synopsis',
		fade: false//400
	});

	$('.icon-info').tooltip({
		delay: 10,
		track: true,
		extraClass: 'fkn-icon',
		showURL: false, 
		bodyHandler: function() {
			//alert(this.tipsrc);
			return $('<img alt=""/>').attr("src", $(this).attr("tipsrc")); 
			},
		top: -59,
		left: -89
	});
	
	$('.icon-info2').tooltip({
		delay: 10,
		track: true,
		extraClass: 'fkn-icon',
		showURL: false, 
		bodyHandler: function() { 
			return $('<img alt=""/>').attr("src", $(this).attr("tipsrc")); 
			},
		top: -59,
		left: -59 
	});
	$('.icon-info3').tooltip({
		delay: 10,
		track: true,
		extraClass: 'fkn-icon',
		showURL: false, 
		bodyHandler: function() { 
			return $('<img alt=""/>').attr("src", $(this).attr("tipsrc")); 
			},
		top: -59,
		left: -89 
	});
	
	$('.icon-info4').tooltip({
		delay: 10,
		track: true,
		extraClass: 'fkn-icon',
		showURL: false, 
		bodyHandler: function() {
			//alert(this.tipsrc);
			return $('<img alt=""/>').attr("src", $(this).attr("tipsrc")); 
			},
		top: -59,
		left: -127 
	});

	$('#jqm1').jqm({ajax: '@url' , trigger: 'a.icon-add'});		

});
/* adv search frm*/
function advsearchToggleGroup(obj){
	if ($(obj).hasClass('search-head-0')) {
		$(obj).removeClass('search-head-0')
		$(obj).addClass('search-head-1')
		$('#'+obj.id+'-content').css('display','block');
	} else {
		$(obj).removeClass('search-head-1')
		$(obj).addClass('search-head-0')
		$('#'+obj.id+'-content').css('display','none');
	}
}

function initNFDetailView(doc_id,id,list){
	var dataIds = (list || '').split(',');
	var dataIndex = 0;
	for(var i = 0; i < dataIds.length; i++)
		if(dataIds[i] == id){
			dataIndex = i;
			break;
		}

	$('#listback').css('display',getCookie('listURL')?'':'none');
	$('#listback').attr('href',(getCookie('listURL') ? getCookie('listURL').replace(/&amp;/g,'&') : '#'));
	$('#listprevious').attr('href','?id='+doc_id + '&dataId='+(i>0?dataIds[i-1]:dataIds[dataIds.length-1]));
	$('#listnext').attr('href','?id='+doc_id + '&dataId='+(dataIds.length-1 > i?dataIds[i+1]:dataIds[0]));
	
	$('#listprevious').css('display',(dataIds.length > 1?'':'none'));
	$('#listnext').css('display',(dataIds.length > 1?'':'none'));
}
function projectMeeting(id,cancel){
	var req = {
		url: '/?template_id=1224',
		type: 'GET',
		data: { project_id: id },
		dataType: "html",
		success: function(data) {
			window.location.reload(true);
		}
	};

	if(cancel)  req.data.cancel = 1;
	$.ajax(req);	
}
function personMeeting(id,cancel){
	var req = {
		url: '/?template_id=1224',
		type: 'GET',
		data: { person_id: id },
		dataType: "html",
		success: function(data) {
			window.location.reload(true);
		}
	};

	if(cancel)  req.data.cancel = 1;
	$.ajax(req);	
}

function nfplayer(id){
	var width=720;
	var height=630;
	if(typeof oWindow != 'undefined' && oWindow){oWindow.close();}
	
	var sOptions = "toolbar=no,status=no,resizable=yes,dependent=yes" ;
	sOptions += ",width=" + width ;
	sOptions += ",height=" + height ;
	
	oWindow=window.open('/?template_id=1225&dataId='+id,'_player',sOptions) ;
	if (oWindow) oWindow.focus();
	else alert('Please allow this page to open a new window, or turn off your popup-killer.');
}



