﻿
/* 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 = [];
	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');
			}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);
			}
		}
		hideDeletePrompt();
	}
	function showDeletePrompt(btn_id, prompt_id, delete_item_url, list_id){
		watchAutoHide(true, prompt_id);
		delete_list_id = list_id;
		delete_url = delete_item_url;
		var pos = $(btn_id).getPosition();
		var posX = pos['x'];
		var posY = pos['y'];
		$(prompt_id).setStyle('display','block');
		$(prompt_id).setStyle('top',posY);	//posY
		$(prompt_id).setStyle('left',posX);	//posX
	}

/* 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 */
	function checkInputScreenname(varname){
		if(document.getElementById(varname).value!=""){
			document.getElementById(varname).value = normalizeScreenname(document.getElementById(varname).value);
			document.getElementById(varname).value = document.getElementById(varname).value.replace(/[^A-Za-z0-9_-]+/g, '');
		}
	}
	function normalizeScreenname(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;
	}

/* 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 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 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');
	}
	
/* SOME AJAX CALLS */
	function loadList(container, url){
		$(container).load(url);
		var axListScrollToFx = new Fx.Scroll(window).toElement(container);
	}
	function loadBox(container, url, show_loading){
		if(show_loading){
			var size = $(container).getSize();
			$(container).set('html', getLoaderImg(size.x, size.y));	//'<div class="ajax_loading" style="width:'+size.x+'px;height:'+size.y+'px;"></div>');
		}
		$(container).load(url);
	}
	function messageRead(message_id, value){
		var myHTMLRequest = new Request.HTML({url:'index.php', update:'mm_newmessages'}).send('sec=ajax&sub1=messageread&id='+message_id+'&value='+value);
	}
	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 thumbsFrom(photo_id, caller){
		var tfc_id = 'tfc_'+photo_id;
		var stth_id = 'stth_'+photo_id;
		var el = new Element(caller);
		var pos = el.getPosition();
		var posX = pos['x']+10;
		var posY = pos['y']+10;
		if($(tfc_id)){
			$(tfc_id).set("html", "&nbsp;");
			$(tfc_id).setStyle("display", "block");
		}else{
			var thumbsFromContainer = new Element('div', {
				id: tfc_id,
				'class': 'popupbox',
			    'html': '&nbsp;',
				'styles': {
			        'display': 'block',
			        'top': posY,
			        'left': posX
				}
		    });
			$(stth_id).grab(thumbsFromContainer);
		}
		$(tfc_id).setStyle("top", posY);
		$(tfc_id).setStyle("left", posX);
		watchAutoHide(true, tfc_id);
		var myHTMLRequest = new Request.HTML({url:'index.php', update:tfc_id}).send('sec=ajax&sub1=thumbsfrom&id='+photo_id);
	}

/* 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 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);
			}
		}
		if($('active_tab')){
			document.getElementById("active_tab").value = active_button;
		}
	}
	function getLoaderImg(width, height){
		return '<div class="ajax_loading" style="width:'+width+'px;height:'+height+'px;"></div>';
	}
	function imageReload(imageurl){
		var now = new Date();
		new Asset.image(imageurl+'?reload='+now.getTime(), {id: 'myImage'+imageurl, title: 'myImage'+imageurl});
	}

/* FLASH */
function embedFlashMovie(src, movieid, width, height){
	return new Swiff(src, {
	    id: movieid,
	    width: width,
	    height: height,
	    container: $('swf_'+movieid),
	    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 loadStylesheet(stylesheet, id){
		new Asset.css('/'+stylesheet);
	}
	var current_theme = '';
	var current_bg = '';
	function updateLogoTypo(themeName, darkBg, lan){
		$('site_logo_typo').removeClass('site_logo_typo_'+current_theme+'_'+current_bg+'_'+lan);
		current_theme = themeName;
		current_bg = darkBg;
		$('site_logo_typo').addClass('site_logo_typo_'+current_theme+'_'+current_bg+'_'+lan);
	}

/* 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;
			}
		}
	}

/* 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 top=0;
	var overlay;
	var center;
	var prompt_divs = new Array();
	function blackout(prompt_id){
		if(prompt_id!="" && prompt_divs.length>0) switchTab(prompt_divs, prompt_id);
		blackoutPosition();
		blackoutSetup(true);
		$("lbCenter").setStyle("display", "block");
		$("lbOverlay").setStyle("opacity", 0.8);
		state = 1;
	}
	function blackoutClose(){
		if (state) {
			state = 0;
			$("lbCenter").setStyle("display", "none");
			$("lbOverlay").setStyle("display", "none");
		}
		return false;
	}
	function blackoutPosition() {
		$("lbOverlay").setStyle("top", window.getScrollTop());
		$("lbOverlay").setStyle("height", window.getHeight());
		$("lbCenter").setStyle("top", (window.getScrollTop()+(window.getHeight() /3)));
	}
	function blackoutSetup(open) {
		$("lbOverlay").style.display = open ? "block" : "none";
		var fn = open ? "addEvent" : "removeEvent";
		window[fn]("scroll", blackoutPosition)[fn]("resize", blackoutPosition);
		document[fn]("keydown", keyDown);
	}
	function keyDown(event) {
		switch(event.code) {
			case 27:	// Esc
			case 88:	// 'x'
			case 67:	// 'c'
				blackoutClose();
		}
		return false;
	}
