﻿/* COMMON CLICK ACTIONS */
	function getURL(url){
		if(typeof messenger!=='undefined') messenger.killRequest();
		document.location.href = url;
	}
	function submitForm(formname){
		if(typeof messenger!=='undefined') messenger.killRequest();
		document.forms[formname].submit();
	}
	
/* HEADER COLLAPSE */
	function onMenuClick(){
		var evt = window.event || arguments.callee.caller.arguments[0];
		var target = evt.target || evt.srcElement;
		var myEl = new Element(target);
		var check_classes = ['menu_left', 'menu_mid', 'menu_right', 'menu_center', 'menu_in_left', 'menu_in_center', 'menu_in_right', 'menu_arrows', 'menu_logo'];
		var do_toggle = false;
		check_classes.each(function(item, index){
			do_toggle = (do_toggle || myEl.hasClass(item));
		});
		if(do_toggle){
			$('site_header').toggle();
			$('mmbar_outer').toggleClass('menu_outer_pad');
			//$('menu_logo').toggle();
			var value = 0;
			if($('site_header').getStyle('display')=='none'){
				value = 1;
			}
			var myHTMLRequest = new Request.HTML({url:'index.php'}).send('sec=ajax&sub1=savesetting&var=header&value='+value);
		}
	}

/* POPUP DIVS WITH AUTOHIDE ON CLICK OUTSIDE */
	var autohide_watch = false;
	var autohide_bind = false;
	function watchAutoHide(status, div_id){
		autohide_watch = status;
		addAutohideDiv(div_id);
		if(status && !autohide_bind){
			document.addEvent("mousedown", checkThenHide.bind(this));
			autohide_bind = true;
		}
	}
	var autohide_divs = [];
	var autohide_onhide = new Hash();
	function addAutohideDiv(nudiv){
		if(!autohide_divs.contains(nudiv)) autohide_divs.include(nudiv);
	}
	function checkThenHide(ev) {
		if (autohide_watch && autohide_divs.length > 0) {
			ev = new Event(ev);
			var hideprompt = true;
			var propCount = 0;
			for(var prop in ev.target){
				propCount++;
			}
			if(propCount==0) return false;
			var myEl = new Element(ev.target);
			var all_parents = myEl.getParents();
			var parent_counter = 0;
			var autohide_counter = 0;
			while(parent_counter<all_parents.length && hideprompt){
				autohide_divs.each(function(item, index){
					if (all_parents[parent_counter].get('id') == item) hideprompt = false;	//dont autohide if click within popup div
				});
				parent_counter++;
			}
			if (hideprompt) hideDeletePrompt();
		}
	}
	function hideDeletePrompt(){
		watchAutoHide(false, "");
		autohide_divs.each(function(item, index){
			if($(item)){
				$(item).setStyle('display', 'none');
				if(autohide_onhide.has(item)){
					eval(autohide_onhide.get(item)+"");
				}
			}else{
				autohide_divs.erase(item);
			}
		});
	}
	var delete_url = '';
	var delete_list_id = '';
	function deleteConfirmed(){
		if(delete_url!=''){
			if(delete_list_id!=''){
				//ajax page load
				loadList(delete_list_id, delete_url);
			}else{
				//full page load
				getURL(delete_url);
			}
		}
		blackoutClose();
	}
	function showDeletePrompt(delete_item_url, list_id){
		delete_list_id = list_id;
		delete_url = delete_item_url;
		blackout('uni_delete_prompt');
	}
	
	var noti_icon_anim = null;
	function animateNotificationIcon(btn_id){
		if($(btn_id+"_icon")){
			if(noti_icon_anim==null){
				noti_icon_anim = setInterval("animateNotificationIcon('"+btn_id+"')", 200);
			}
			var current_pos = $(btn_id+"_icon").getStyle('background-position');
			var new_pos = '0px 0px';
			if(current_pos=='0px 0px'){
				new_pos = '0px -22px';
			}else if(current_pos=='0px -22px'){
				new_pos = '0px -44px';
			}else if(current_pos=='0px -44px'){
				new_pos = '0px -66px';
			}else if(current_pos=='0px -66px'){
				new_pos = '0px -88px';
			}
			$(btn_id+"_icon").setStyle('background-position', new_pos);
		}
	}
	function showNotifications(btn_id, prompt_id){
		if(!autohide_onhide.has(prompt_id)){
			//set function to execute on hide
			autohide_onhide.set(prompt_id, "notificationOnHide('"+btn_id+"');");
		}
		watchAutoHide(true, prompt_id);
		var pos = $(btn_id).getPosition();
		var posX = pos['x'] + 1;
		var posY = pos['y'] + 35;
		$(prompt_id).setStyle('display','block');
		$(prompt_id).setStyle('top',posY);	//posY
		$(prompt_id).setStyle('left',posX);	//posX
		$(btn_id).setStyle('display','none');
		$(btn_id+"_on").setStyle('display','block');
		if(!$(btn_id+"_icon").hasClass('icon_22_notification_dimm')){
			if(noti_icon_anim!=null) clearInterval(noti_icon_anim);
			$(btn_id+"_icon").setStyle('background-position', '0px 0px');
			$(btn_id+"_icon").addClass('icon_22_notification_dimm');
		}
		var myHTMLRequest = new Request.HTML({url:'index.php'}).send('sec=ajax&sub1=notificationmark');
	} 
	function notificationOnHide(btn_id){
		$(btn_id).setStyle('display','block');
		$(btn_id+"_on").setStyle('display','none');
	}
	function selectText(objId){
		var div = document.getElementById(objId);
		var sel, range;
		if (window.getSelection && document.createRange) {
			range = document.createRange();
			range.selectNodeContents(div);
			sel = window.getSelection();
			sel.removeAllRanges();
			sel.addRange(range);
		} else if (document.body.createTextRange) {
			range = document.body.createTextRange();
			range.moveToElementText(div);
			range.select();
		}
	}
	function announcementOff(id){
		$('site_announcement_'+id).hide();
		var myHTMLRequest = new Request.HTML({url:'index.php'}).send('sec=ajax&sub1=savesetting&var=announcement_'+id+'&value=0');
	}
	
/* COUCH RATING STARS */
	function ratingStarClick(varname, num){
		if(document.getElementById(varname).value == num) num = 0;
		for(var i=1; i<=5; i++){
			document.getElementById('rating_star_'+varname+'_'+i).className='rating_star_'+((i<=num)? '1' : '0');
			$('rating_label_'+varname+'_'+i).setStyle('display', ((i==num)? 'inline' : 'none'));
		}
		document.getElementById(varname).value=num;
	}
	function ratingStarMouseover(varname, num){
		for(var i=1; i<=5; i++){
			$('rating_label_'+varname+'_'+i).setStyle('display', ((i==num)? 'inline' : 'none'));
		}
	}
	function ratingStarMouseout(varname){
		num = document.getElementById(varname).value;
		for(var i=1; i<=5; i++){
			$('rating_label_'+varname+'_'+i).setStyle('display', ((i==num)? 'inline' : 'none'));
		}
	}

/* SCREENNAME RESTRICTIONS */
	var nameCheckRequest = '';
	var nameCheckTimeout = '';
	function nameKeyup(varname, user, urlpreview, livecheck){
		if(document.getElementById(varname).value!=""){
			document.getElementById(varname).value = normalizeName(document.getElementById(varname).value);
			document.getElementById(varname).value = document.getElementById(varname).value.replace(/[^A-Za-z0-9_-]+/g, '');
		}
		$(urlpreview).set('html', document.getElementById(varname).value);
		if(nameCheckTimeout!='') clearTimeout(nameCheckTimeout);
		nameCheckTimeout = setTimeout("checkName('"+varname+"', '"+user+"', '"+urlpreview+"', '"+livecheck+"')", 500);
	}
	function normalizeName(str){
		var list = {
		'Š':'S', 'š':'s', 'Đ':'Dj', 'đ':'dj', 'Ž':'Z', 'ž':'z', 'Č':'C', 'č':'c', 'Ć':'C', 'ć':'c',
		'À':'A', 'Á':'A', 'Â':'A', 'Ã':'A', 'Ä':'A', 'Å':'A', 'Æ':'A', 'Ç':'C', 'È':'E', 'É':'E',
		'Ê':'E', 'Ë':'E', 'Ì':'I', 'Í':'I', 'Î':'I', 'Ï':'I', 'Ñ':'N', 'Ò':'O', 'Ó':'O', 'Ô':'O',
		'Õ':'O', 'Ö':'O', 'Ø':'O', 'Ù':'U', 'Ú':'U', 'Û':'U', 'Ü':'U', 'Ý':'Y', 'Þ':'B', 'ß':'Ss',
		'à':'a', 'á':'a', 'â':'a', 'ã':'a', 'ä':'a', 'å':'a', 'æ':'a', 'ç':'c', 'è':'e', 'é':'e',
		'ê':'e', 'ë':'e', 'ì':'i', 'í':'i', 'î':'i', 'ï':'i', 'ð':'o', 'ñ':'n', 'ò':'o', 'ó':'o',
		'ô':'o', 'õ':'o', 'ö':'o', 'ø':'o', 'ù':'u', 'ú':'u', 'û':'u', 'ý':'y', 'ý':'y', 'þ':'b',
		'ÿ':'y', 'Ŕ':'R', 'ŕ':'r', 'ü':'u', 'ç':'c', ' ':'-' };
		for(var c in list) {
			str = String(str).replace(new RegExp(c, "g"), list[c]);
		}
		return str;
	}
	function checkName(varname, user, urlpreview, livecheck){
		var name_value = document.getElementById(varname).value;
		if(name_value.length<3){
			$(livecheck).set('html', '&nbsp;');
		}else{
			if(nameCheckRequest!='') nameCheckRequest.cancel();
			nameCheckRequest = new Request.HTML({url:'index.php', update:livecheck}).send('sec=ajax&sub1=namecheck&user='+user+'&value='+name_value);
		}
	}

/* DISPLAY SWITCHES */
	function toggleDisplay(id){
		$(id).toggle();
	}
	function toggleInlineDisplay(id){
		if($(id).getStyle('display')=='none'){
			$(id).setStyle('display', 'inline');
		}else{
			$(id).setStyle('display', 'none');
		}
	}
	function hideAndShow(show_elements, all_elements){
		for(var i=0; i<all_elements.length; i++){
			if(in_array(all_elements[i], show_elements)){
				$(all_elements[i]).setStyle('display', 'block');
			}else{
				$(all_elements[i]).setStyle('display', 'none');
			}
		}
	}
	function hideAndShowInv(hide_elements, all_elements){
		for(var i=0; i<all_elements.length; i++){
			if(in_array(all_elements[i], hide_elements)){
				$(all_elements[i]).setStyle('display', 'none');
			}else{
				$(all_elements[i]).setStyle('display', 'block');
			}
		}
	}
	function hideElements(element_array){
		for(var i=0; i<element_array.length; i++){
			$(element_array[i]).setStyle('display', 'none');
		}
	}
	function showElements(element_array){
		for(var i=0; i<element_array.length; i++){
			if(element_array[i]!="") $(element_array[i]).setStyle('display', 'block');
		}
	}
	function switchTab(element_array, active_tab){
		for(var i=0; i<element_array.length; i++){
			if(element_array[i]==active_tab){
				if($(element_array[i]).getStyle('display')!='block') $(element_array[i]).setStyle('display', 'block');
			}else{
				if($(element_array[i]).getStyle('display')!='none') $(element_array[i]).setStyle('display', 'none');
			}
		}
	}
	function syncTab(element_array, to_tab, from_tab){
		for(var i=0; i<element_array.length; i++){
			if(element_array[i]=='PH_Privacy'){
				//special case for privacy lock
				var set_value = getCheckedValue(document.forms[from_tab+''].elements[element_array[i]+'']);
				setCheckedValue(document.forms[to_tab+''].elements[element_array[i]+''], set_value);
				switchTab(new Array(to_tab+'_PH_Privacy_privacy_lock_0', to_tab+'_PH_Privacy_privacy_lock_1', to_tab+'_PH_Privacy_privacy_lock_2'), to_tab+'_PH_Privacy_privacy_lock_'+set_value);
			}else{
				setInput(to_tab+'_'+element_array[i], document.getElementById(from_tab+'_'+element_array[i]).value);
			}
		}
	}
	function togglePhotoForms(box){
		if($('upload_box') && (box=='uploader' || (box=='gallery' && $('upload_box').getStyle('display')=='block'))){
			$('upload_box').toggle();
			toggleInlineDisplay('uploader_origin');
		}
		if($('gallery_edit_box') && (box=='gallery' || (box=='uploader' && $('gallery_edit_box').getStyle('display')=='block'))){
			$('gallery_edit_box').toggle();
			toggleInlineDisplay('gallery_edit_origin');
		}
	}
	function flagmenuOver(){
		$('flagmenu_inner').setStyle('display','block');
		$('flagmenu_outer').removeClass('flagmenu_closed');
		$('flagmenu_outer').addClass('flagmenu_open');
	}
	function flagmenuOut(){
		$('flagmenu_inner').setStyle('display','none');
		$('flagmenu_outer').removeClass('flagmenu_open');
		$('flagmenu_outer').addClass('flagmenu_closed');
	}
	function editMenuOver(id){
		$(id).setStyle('display','block');
		$(id+'_gear').setStyle('display','none');
	}
	function editMenuOut(id){
		$(id).setStyle('display','none');
		$(id+'_gear').setStyle('display','inline');
	}
	function zoomRepos(id){
		var pos = $(id+'_img').getPosition();
		var posX = pos['x']+2;
		var posY = pos['y']+2;
		$(id+'_zoom').setStyle('left',posX);
		$(id+'_zoom').setStyle('top',posY);
	}
	function pgBtnPos(id){
		var size = $('Shout_'+id).getSize();
		if($('pg_prev_btn')) $('pg_prev_btn').setStyle('height', size.y);
		if($('pg_next_btn')) $('pg_next_btn').setStyle('height', size.y);
	}
	function photoChkOver(id){
		$(id+'_img').setStyle('display','block');
	}
	function photoChkOut(id){
		if(!$(id).checked) $(id+'_img').setStyle('display','none');
	}
	function commentFormFocus(input_id, btn_id){
		$(input_id).addClass('in_area_h_shouthigh');
		$(btn_id).setStyle('display','block');
	}
	function overtextFocus(input_id){
		if($(input_id+'_over')) $(input_id+'_over').setStyle('display','none');
	}
	function overtextBlur(input_id){
		//if(document.getElementById(input_id).value==''){
		if($(input_id).value=='' && $(input_id+'_over')){
			$(input_id+'_over').setStyle('display','block');
		}
	}
	function switchHomeImage(num){
		var bg_pos = 0;
		if(num==2){
			bg_pos = -465;
		}else if(num==3){
			bg_pos = -930;
		}
		$('home_center_sprite').set('tween', {transition: Fx.Transitions.Bounce.easeOut});
		$('home_center_sprite').tween('background-position', '0px '+bg_pos+'px');
		switchTab(new Array('hoceim1','hoceim2','hoceim3'), 'hoceim'+num);
	}
	function animHome(repeat){
		$('home_center_hub_color_outer').set('tween', {duration: 1500});
		$('home_center_hub_color_outer').setStyle('padding-top', 0);
		$('home_center_hub_color_outer').tween('padding-top', '280px');
		$('home_center_hub_color').set('tween', {duration: 1500});
		$('home_center_hub_color').setStyle('background-position', '0px 40px');
		$('home_center_hub_color').tween('background-position', '0px -240px');
		setTimeout('animHomeBlip1()', 1000);
	}
	function animHomeBlip1(){
		$('home_center_hub_color_blip').setStyle('height', 5);
		$('home_center_hub_color_blip').setStyle('margin-left', 5);
		$('home_center_hub_color_blip').setStyle('margin-top', 130);
		$('home_center_hub_color_blip').setStyle('background-position', '0px -130px');
		$('home_center_hub_color_blip').setStyle('display', 'block');
		setTimeout('animHomeBlip2()', 150);
	}
	function animHomeBlip2(){
		$('home_center_hub_color_blip').setStyle('height', 20);
		$('home_center_hub_color_blip').setStyle('margin-top', 100);
		$('home_center_hub_color_blip').setStyle('background-position', '0px -100px');
		setTimeout('animHomeBlip3()', 200);
	}
	function animHomeBlip3(){
		$('home_center_hub_color_blip').setStyle('margin-left', 0);
		setTimeout('animHomeBlip4()', 150);
	}
	function animHomeBlip4(){
		$('home_center_hub_color_blip').setStyle('display', 'none');
	}
	function init_trace_box(){
		if(sessionStorage.getItem('trace_box_state')=='block' && $('trace_box')){
			$('trace_box').setStyle('display','block');
		}
	}
	function toggle_trace_box(){
		$('trace_box').toggle();
		sessionStorage.setItem('trace_box_state', $('trace_box').getStyle('display'));
	}
/* SOME AJAX CALLS */
	function loadList(container, url){
		$(container).load(url);
		autoScroll(container);
	}
	function loadBox(container, url, show_loading){
		if(show_loading){
			var size = $(container).getSize();
			$(container).set('html', getLoaderImg(size.x, size.y));
			autoScroll(container);
		}
		$(container).load(url);
	}
	function autoScroll(container){
		var position = $(container).getPosition();
		var top_offset = window.getScrollTop();
		var bot_offset = top_offset + window.getHeight();
		if((position.y < top_offset) || (position.y > bot_offset)){
			var axListScrollToFx = new Fx.Scroll(window).toElement(container);
		}
	}
	function changePrivacy(subject_id, value, user_id){
		var myHTMLRequest = new Request.HTML({url:'index.php'}).send('sec=ajax&sub1=privacychange&id='+subject_id+'&value='+value+'&user='+user_id);
	}
	function thumbsUp(photo_id, value){
		var myHTMLRequest = new Request.HTML({url:'index.php', update:'stth_'+photo_id}).send('sec=ajax&sub1=thumbsup&id='+photo_id+'&value='+value);
	}
	function thumbsFromOver(caller, off_x, off_y){
		var el = new Element(caller);
		if(el){
			var pop_el = el.getFirst('.popupbox');
			if(pop_el){
				var pos = el.getPosition();
				pop_el.setStyle("top", (pos.y+off_y));
				pop_el.setStyle("left", (pos.x+off_x));
			}
		}
	}
	function loadUserMenu(container_id, user_id){
		if($(container_id).get('html')!=''){
			//$(container_id).toggle();
		}else{
			var myHTMLRequest = new Request.HTML({url:'index.php', update:container_id}).send('sec=ajax&sub1=usermenu&user='+user_id);
		}
	}
	function checkProject(project_id, value){
		var myHTMLRequest = new Request.HTML({url:'index.php'}).send('sec=ajax&sub1=projectchecked&id='+project_id+'&value='+value);
	}
	function saveSetting(name, value){
		var myHTMLRequest = new Request.HTML({url:'index.php'}).send('sec=ajax&sub1=savesetting&var='+name+'&value='+value);
	}
/* PRIVATE MESSAGES */
	function setActiveThread(thread){
		var active_id = 'thrd_message_'+thread;
		var elements = $('message_menu').getElements('.grbox_out');
		elements.each(function(item, index){
			if(item.hasClass('pressed')){
				item.removeClass('pressed');
				if(!item.hasClass('gray')){
					item.addClass('gray');
				}
			}
		});
		if($(active_id).hasClass('white')){
			$(active_id).addClass('gray');
			$(active_id).removeClass('white');
		}
		$(active_id).addClass('pressed');
		var thread_request = new Request.HTML({
			url:'index.php', 
			update:'message_pane', 
			onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
				onThreadLoaded(thread);
			}
		}).send('sec=ajax&sub1=messages&thread='+thread+'&url='+message_list_url);
		return false;
	}
	function onThreadLoaded(thread){
		var pane_position = $('message_pane').getPosition();
		var top_offset = pane_position.y - window.getScrollTop();
		var new_top_padding = 0;
		if(top_offset<0){
			new_top_padding = top_offset*(-1);
		}
		$('message_pane').setStyle('padding-top', new_top_padding+'px');
	}
	function markThreadRead(thread){
		//make sure the thread is still active, before marking the thread as read
		if($('thrd_message_'+thread) && $('thrd_message_'+thread).hasClass('pressed')){
			var myHTMLRequest = new Request.HTML({url:'index.php', update:'mm_newmessages'}).send('sec=ajax&sub1=messages&sub2=read&thread='+thread);
		}
	}
/* DYNAMIC UI STUFF */
	function updateFcMessageCount(nuvalue){
		$('um_newmessages').set("html", nuvalue);
	}
	function togglePhoto(photo_id, src_1, src_2){
		if($(photo_id+"").getProperty('src')==src_1){
			$(photo_id+"").setProperty('src', src_2);
		}else{
			$(photo_id+"").setProperty('src', src_1);
		}
	}
	function reloadCaptcha(captcha_url, img_id) {
		var now = new Date();
		var nu_src = captcha_url+now.getTime();
		document.getElementById(img_id).src = nu_src;
	}
	function clickPrivacyBtn(prefix, num){
		setActiveButton(new Array(prefix+'_btn_'+'0', prefix+'_btn_'+'1', prefix+'_btn_'+'2'), prefix+'_btn_'+num, 'btn_list_s_active', 'btn_list_s');
		switchTab(new Array(prefix+'_lock_'+'0', prefix+'_lock_'+'1', prefix+'_lock_'+'2'), prefix+'_lock_'+num);
		setInput(prefix, num);
	}
	function setActiveButton(button_array, active_button, active_class, inactive_class){
		for(var i=0; i<button_array.length; i++){
			if(button_array[i]==active_button){
				$(button_array[i]).removeClass(inactive_class);
				$(button_array[i]).addClass(active_class);
			}else{
				$(button_array[i]).removeClass(active_class);
				$(button_array[i]).addClass(inactive_class);
			}
		}
	}
	function getLoaderImg(width, height){
		var style = '';
		if(width>0 && height>0){
			style = ' style="width:'+width+'px;height:'+height+'px;"';
		}
		return '<div class="ajax_loading"'+style+'></div>';
	}
	function imageReload(imageurl){
		var now = new Date();
		new Asset.image(imageurl+'?reload='+now.getTime(), {id: 'myImage'+imageurl, title: 'myImage'+imageurl});
	}
	function cbxOnChangeInputSetup(cbx_id, txt_id, url_params){
		$(cbx_id).addEvent('change', function(){
			cbxOnChangeInputRefresh(cbx_id, txt_id, url_params);
		});
	}
	function cbxOnChangeInputRefresh(cbx_id, txt_id, url_params){
		var myHTMLRequest = new Request.HTML({
			url:'index.php', 
			onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
				$(txt_id).value = responseHTML;
			}
		}).send(url_params+'&value='+$(cbx_id).value);
	}
	function prefillCompanyInfo(){
		if(!$('PB_FK_Company') || $('PB_FK_Company').value==0) return;
		var request = new Request.JSON({
			url: 'index.php',
			onComplete: function(jsonObj) {
				if(jsonObj!=null){
					for (var key in jsonObj) {
						if (jsonObj.hasOwnProperty(key)) {
							if($(key+"").value==''){
								$(key+"").value = jsonObj[key];
							}
						}
					}
				}
			}
		}).send('sec=ajax&sub1=json&sub2=companyinfo&mapping=PB&id='+$('PB_FK_Company').value);
	}
/* PHOTO LIGHTBOX */
	var phlb_seq = null;
	var phlb_current = 0;
	function startPhotoLightbox(sequence_url, first_item_id){
		blackout('blackout_photos');
		var request = new Request.JSON({
			url: sequence_url,
			onComplete: function(jsonObj) {
				if(jsonObj!=null && jsonObj.photos!=null){
					phlb_seq = jsonObj.photos;
					var first_num = photoLightboxFind(first_item_id);
					photoLightboxLoadImg(first_num);
				}
			}
		}).send();
	}
	function photoLightboxFind(id){
		for(var i=0; i<phlb_seq.length; i++){
			if(phlb_seq[i].id==id) return i;
		}
		return 0;
	}
	function photoLightboxNext(){
		var num = phlb_current+1;
		if(num>=phlb_seq.length) num = 0;
		photoLightboxLoadImg(num);
	}
	function photoLightboxPrev(){
		var num = phlb_current+1;
		if(num<0) num = phlb_seq.length-1;
		photoLightboxLoadImg(num);
	}
	function photoLightboxLoadImg(num){
		phlb_current = num;
		var dims = '';
		if(phlb_seq[phlb_current].width>0 && phlb_seq[phlb_current].height>0){
			var img_size = photoLightboxLimitSize(phlb_seq[phlb_current].width, phlb_seq[phlb_current].height);
			dims = ' width="'+img_size.width+'" height="'+img_size.height+'"';
			if($('lb_prev')) $('lb_prev').setStyle('height', img_size.height);
			if($('lb_next')) $('lb_next').setStyle('height', img_size.height);
		}
		$('lb_image_cont').set('html','<img id="lb_image" src="'+phlb_seq[phlb_current].src+'" onload="photoLightboxLoaded();"'+dims+' />');
		blackoutPosition();
	}
	function photoLightboxLoaded(){
		var img_size = photoLightboxLimitSize($('lb_image').width, $('lb_image').height);
		$('lb_image').setStyle('height', img_size.height);
		$('lb_image').setStyle('width', img_size.width);
		if($('lb_prev')) $('lb_prev').setStyle('height', img_size.height);
		if($('lb_next')) $('lb_next').setStyle('height', img_size.height);
		blackoutPosition();
	}
	function photoLightboxLimitSize(img_w, img_h){
		var win_h = window.getHeight() - 60;
		var win_w = window.getWidth() - 60;
		if(img_h>win_h){
			img_w = img_w * (win_h/img_h);
			img_h = win_h;
		}
		if(img_w>win_w){
			img_h = img_h * (win_w/img_w);
			img_w = win_w;
		}
		return {width: img_w, height: img_h};
	}
/* STUDY */
	function fieldstatusPrompt(study_hash, status, status_label){
		document.getElementById('fieldstatus_'+study_hash).value = status;
		$('fieldstatus_label_'+study_hash).set('html', status_label);
		blackout('blackout_fieldstatus_'+study_hash);
	}
/* FLASH */
function blackoutFlashVideo(src, movieid, containerid, width, height){
	$(containerid).setStyle('width',width);
	$(containerid).setStyle('height',height);
	embedFlashVideo(src, movieid, containerid, width, height);
	blackout('vid_lb');
}
function embedIFrameVideo(src, movieid, containerid, width, height){
	$(containerid).set('html', '<iframe width="'+width+'" height="'+height+'" src="'+src+'" id="'+movieid+'" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowfullscreen></iframe>');
}
function embedFlashVideo(src, movieid, containerid, width, height){
	return new Swiff(src, {
	    id: movieid,
	    width: width,
	    height: height,
	    container: $(containerid),
	    params: {
	        wmode: 'opaque',
	        allowFullScreen: true
	    }
	});
}

/* SCOREBOARD SCROLLING */
	var scoreScrollFx = null;
	var scoreScrollNum = 0;
	var scoreScrollMax = 10;
	function scoreScrollLeft(){
		if(scoreScrollNum>0) scoreScrollTo((scoreScrollNum-4), true);
	}
	function scoreScrollRight(){
		if(scoreScrollNum<scoreScrollMax) scoreScrollTo((scoreScrollNum+4), true);
	}
	function scoreScrollTo(target, animate){
		if(scoreScrollFx==null) var listScrollToFx = new Fx.Scroll('match_mini_box');
		if(target<0) target = 0;
		if(target>scoreScrollMax) target = scoreScrollMax;
		scoreScrollNum = target;
		var newpos = target*85;
		if(animate){
			listScrollToFx.start(newpos, 0);
		}else{
			listScrollToFx.set(newpos, 0);
		}
		var reduced_opacity = 0.3;
		if(scoreScrollNum<=0) $('match_mini_btn_left').setStyle('opacity', reduced_opacity);
		if(scoreScrollNum>0 && $('match_mini_btn_left').getStyle('opacity')==reduced_opacity) $('match_mini_btn_left').setStyle('opacity', 1);
		if(scoreScrollNum>=scoreScrollMax) $('match_mini_btn_right').setStyle('opacity', reduced_opacity);
		if(scoreScrollNum<scoreScrollMax && $('match_mini_btn_right').getStyle('opacity')==reduced_opacity) $('match_mini_btn_right').setStyle('opacity', 1);
	}
	
/* THEME CHANGE ON THE FLY */
	function selectTheme(input_design, input_bg, set_design, set_bg){
		/*var old_active_lbl = 'lbl_'+document.getElementById(input_design+"").value+'_'+document.getElementById(input_bg+"").value;
		var new_active_lbl = 'lbl_'+set_design+'_'+set_bg;
		if($(old_active_lbl)){
			$(old_active_lbl).removeClass('theme_btn_label_active');
		}
		if($(new_active_lbl)){
			$(new_active_lbl).addClass('theme_btn_label_active');
		}*/
		var old_active = 'theme_btn_'+document.getElementById(input_design+"").value+'_'+document.getElementById(input_bg+"").value;
		var new_active = 'theme_btn_'+set_design+'_'+set_bg;
		if($(old_active)){
			$(old_active).removeClass('theme_active');
		}
		if($(new_active)){
			$(new_active).addClass('theme_active');
		}
		setInput(input_design, set_design);
		setInput(input_bg, set_bg);
	}
	function previewTheme(logo_typo_color, lan, css_main, css_bg){
		$('site_logo_typo').erase('class');
		$('site_logo_typo').addClass('site_logo_typo_'+logo_typo_color+'_'+lan);
		new Asset.css('/'+css_main);
		new Asset.css('/'+css_bg);
	}

/* INPUTS */
	function setInput(varName, nuValue){
		document.getElementById(varName+"").value = nuValue;
	}
	function getCheckedValue(radioObj) {
		if(!radioObj)
			return "";
		var radioLength = radioObj.length;
		if(radioLength == undefined)
			if(radioObj.checked)
				return radioObj.value;
			else
				return "";
		for(var i = 0; i < radioLength; i++) {
			if(radioObj[i].checked) {
				return radioObj[i].value;
			}
		}
		return "";
	}
	function setCheckedValue(radioObj, newValue) {
		if(!radioObj)
			return;
		var radioLength = radioObj.length;
		if(radioLength == undefined) {
			radioObj.checked = (radioObj.value == newValue.toString());
			return;
		}
		for(var i = 0; i < radioLength; i++) {
			radioObj[i].checked = false;
			if(radioObj[i].value == newValue.toString()) {
				radioObj[i].checked = true;
			}
		}
	}
	function toggleCheckBox(checkId){
		var checkBox = document.getElementById(checkId+"");
		if(checkBox) checkBox.checked = !checkBox.checked;
	}
	function toggleCheckBoxList(checkList){
		checkList.each(function(item, index){
			toggleCheckBox(item);
		});
	}
	function checkCheckBox(checkId){
		var checkBox = document.getElementById(checkId+"");
		if(checkBox) checkBox.checked = true;
	}
	function checkBoxSEupdate(checkId){
		var newClass = 'checkbox_se_unchecked';
		var oldClass = 'checkbox_se_checked';
		if($(checkId).checked){
			newClass = 'checkbox_se_checked';
			oldClass = 'checkbox_se_unchecked';
		}
		$(checkId+'_img').removeClass(oldClass);
		$(checkId+'_img').addClass(newClass);
	}
	function radioSEupdate(containerId){
		var elements = $(containerId).getElements('.checkbox_se_chk');
		elements.each(function(item, index){
			var newClass = 'radio_se_unchecked';
			var oldClass = 'radio_se_checked';
			if(item.checked){
				newClass = 'radio_se_checked';
				oldClass = 'radio_se_unchecked';
			}
			$(item.id+'_img').removeClass(oldClass);
			$(item.id+'_img').addClass(newClass);
		});
	}
/* TOGGLE SOMETHING BASED ON CHECKBOX */
	function checkBoxTogglesDivSetup(checkID, divID){
		$(checkID).addEvent('click', function(){
			checkBoxTogglesDivRefresh(checkID, divID);
		});
		checkBoxTogglesDivRefresh(checkID, divID);
	}
	function checkBoxTogglesDivRefresh(checkID, divID){
		var checkBox = document.getElementById(checkID);
		if(checkBox.checked){
			$(divID).show();
		}else{
			$(divID).hide();
		}
	}
	
/* EVENT MATCH SELECTION */	
	function evtMatchSelectSetup(props){
		/* expects Hash({isMatchVarName, dateVarName, seasonVarName, matchVarName, seasonValue, matchValue, seasonContainer, matchContainer, seasonLine, matchLine})	*/
		var dateVarName = props.get('dateVarName');
		//setup onchange event for date cbx
		$(dateVarName+'_Year').addEvent('change', function(){
			evtMatchSelectRefresh(props);
		});
		$(dateVarName+'_Month').addEvent('change', function(){
			evtMatchSelectRefresh(props);
		});
		$(dateVarName+'_Day').addEvent('change', function(){
			evtMatchSelectRefresh(props);
		});
		//setup onchange event for is match checkbox
		$(props.get('isMatchVarName')).addEvent('click', function(){
			evtMatchSelectIsMatch(props);
		});
		evtMatchSelectIsMatch(props);
	}
	function evtMatchSelectRefresh(props){
		var checkBox = document.getElementById(props.get('isMatchVarName'));
		if(checkBox.checked){
			var dateVarName = props.get('dateVarName');
			var date = document.getElementById(dateVarName+'_Year').value + '-' + document.getElementById(dateVarName+'_Month').value + '-' + document.getElementById(dateVarName+'_Day').value;
			var seasonValue = props.get('seasonValue');
			if($(props.get('seasonVarName'))) seasonValue = document.getElementById(props.get('seasonVarName')).value;
			var myHTMLRequest = new Request.HTML({
				url:'index.php', 
				update:props.get('seasonContainer'), 
				onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
					evtMatchSelectSeasonLoaded(props);
				}
			}).send('sec=ajax&sub1=input&sub2=season&name='+props.get('seasonVarName')+'&value='+seasonValue+'&date='+date);
		}
	}
	function evtMatchSelectIsMatch(props){
		var checkBox = document.getElementById(props.get('isMatchVarName'));
		if(checkBox.checked){
			evtMatchSelectRefresh(props);
			$(props.get('seasonLine')).show();
			$(props.get('matchLine')).show();
		}else{
			$(props.get('seasonLine')).hide();
			$(props.get('matchLine')).hide();
		}
	}
	function evtMatchSelectSeasonLoaded(props){
		if($(props.get('seasonVarName'))){
			$(props.get('seasonVarName')).addEvent('change', function(){
				evtMatchSelectSeasonChanged(props);
			});
		}
		evtMatchSelectSeasonChanged(props);
	}
	function evtMatchSelectSeasonChanged(props){
		var checkBox = document.getElementById(props.get('isMatchVarName'));
		if(checkBox.checked){
			var dateVarName = props.get('dateVarName');
			var date = document.getElementById(dateVarName+'_Year').value + '-' + document.getElementById(dateVarName+'_Month').value + '-' + document.getElementById(dateVarName+'_Day').value;
			var seasonValue = props.get('seasonValue');
			if($(props.get('seasonVarName'))) seasonValue = document.getElementById(props.get('seasonVarName')).value;
			var matchValue = props.get('matchValue');
			if($(props.get('matchVarName'))) matchValue = document.getElementById(props.get('matchVarName')).value;
			var myHTMLRequest = new Request.HTML({
				url:'index.php', 
				update:props.get('matchContainer'),
				onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
					if($(props.get('matchVarName'))){
						$(props.get('matchVarName')).addEvent('change', function(){
							var selected_matches = $(props.get('matchVarName')).getSelected();
							document.getElementById(props.get('titleVarName')).value = selected_matches[0].text;
						});
					}
				}
			}).send('sec=ajax&sub1=input&sub2=match&name='+props.get('matchVarName')+'&value='+matchValue+'&date='+date+'&season='+seasonValue);
		}
	}
	
/* UTILS */
	function in_array(needle, haystack, argStrict) {
		var found = false, key, strict = !!argStrict;
		for (key in haystack) {
			if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
				found = true;
				break;
			}
		}
		return found;
	}

/* LIGHTBOX BLACKOUT */
	var state = 0;
	var cur_prompt;
	var focused_btn = null;
	function blackout(prompt_id){
		if(prompt_id!=""){
			var prompts = $('lbPrompts').getChildren('div');
			for(var i=0; i<prompts.length; i++){
				if(prompts[i].get('id')==prompt_id){
					if(prompts[i].getStyle('display')!='block') prompts[i].setStyle('display', 'block');
					//find the okay btn
					var btns = $$('#'+prompt_id+' a.btn_color');
					if(btns.length>0){
						focused_btn = btns[0];
						focused_btn.focus();
					}else{
						focused_btn = null;
					}
				}else{
					if(prompts[i].getStyle('display')!='none') prompts[i].setStyle('display', 'none');
				}
			}
		}
		cur_prompt = prompt_id;
		$("lbCenter").setStyle("display", "block");
		$("lbOverlay").setStyle("opacity", 0.8);
		blackoutPosition();
		blackoutSetup(true);
		state = 1;
	}
	function blackoutClose(){
		if(state){
			state = 0;
			if(cur_prompt=='vid_lb'){
				$('vid_lb_in').set('html', '<span></span>');
			}
			$("lbCenter").setStyle("display", "none");
			blackoutSetup(false);
			if(focused_btn){
				focused_btn.blur();
			}
		}
		return false;
	}
	function blackoutPosition() {
		var site_size = $('site_content').getSize();
		var mmbar_size = $('mmbar_outer').getSize();
		var mmbar_pos = $('mmbar_outer').getPosition();
		var lbo_height = window.getHeight();
		var lbo_width = window.getWidth();
		if(site_size.y>lbo_height) lbo_height = site_size.y;
		if(mmbar_size.x>lbo_width) lbo_width = mmbar_size.x;
		$("lbOverlay").setStyle("height", lbo_height);
		$("lbOverlay").setStyle("width", lbo_width);
		var center_size = $("lbCenter").getSize();
		var top = window.getScrollTop() + (window.getHeight() / 2) - (center_size.y/2);
		var left = mmbar_pos.x + (mmbar_size.x / 2) - (center_size.x/2);
		if(top<10) top = 10;
		if(left<10) left = 10;
		$("lbCenter").setStyle("top", Math.round(top));
		$("lbCenter").setStyle("left", Math.round(left));
	}
	function blackoutSetup(open) {
		$("lbOverlay").style.display = open ? "block" : "none";
		var fn = open ? "addEvent" : "removeEvent";
		//window[fn]("scroll", blackoutPosition)[fn]("resize", blackoutPosition);
		window[fn]("resize", blackoutPosition);
		document[fn]("keydown", blackoutKeyDown);
	}
	function blackoutKeyDown(event) {
		if(event.code==27){	//Esc
			blackoutClose();
			return false;
		}
		return true;
	}
