// B.H.

function mk_do_nothing() { return; }

function top_menu_hin(elm,bkg_img) {
	elm.style.backgroundImage = "url("+escape(bkg_img)+")";
	elm.style.color = "#ffffff";
}

function top_menu_hout(elm) {
	elm.style.backgroundImage = "none";
	elm.style.color = "#000000";
}

function mk_do_logout() {
	new Ajax.Request('/mk_ajax_server.php',{
		parameters: { op: 'logout' }, method: 'get',
		onSuccess: function(tr) {
			res = tr.responseText;
			document.location = document.location;
		},
		onFailure: function(tr) {
			// if we fail to logout by AJAX, let's try an old good way.. ;)
			document.location = "/logout.php";
		}
	});
	return false;
}

window.sel_tb = {
	cont: null,
	last_json: null,
	html_save: null,
	init: function() {
		this.cont = jQuery('#daily_tb_container');
		this.go('init');
	},
	go: function(dir) {
		last_msg_id = this.last_json?this.last_json.msg_id:0;
		this.html_save = this.cont.html();
		this.cont.html('<div style="text-align:center;padding: 20px 0px 30px 0px">'+
    	       '<img src="/images/gallery_new/ajax-loader1.gif" alt="טוען..." />'+
    	       '</div>');
		new Ajax.Request('mk_ajax_server.php',{
			parameters: { op: 'daily_talkback_go', msg_id: last_msg_id, dir: dir }, 
			method: 'get',
			onSuccess: (function(tr) {
				if (!tr.headerJSON) { return; }
				data = tr.headerJSON;
				
				if (data.prev) jQuery('#daily_tb_prev_span').addClass('dtalkback_link_active');
				else jQuery('#daily_tb_prev_span').removeClass('dtalkback_link_active');
				if (data.next) jQuery('#daily_tb_next_span').addClass('dtalkback_link_active');
				else jQuery('#daily_tb_next_span').removeClass('dtalkback_link_active');
				
				if (data.success) {
					this.last_json = data;
					this.cont.hide().html(tr.responseText).slideDown(300);
				} else {
					this.cont.html(this.html_save);
				}
			}).bind(this),
			onFailure: (function(tr){
				this.cont.html(this.html_save);
				alert("Failure!\n"+tr.responseText);
			}).bind(this)
		});
	}
}

function article_vote(art_id,score) {
	new Ajax.Updater('article_votes','/mk_ajax_server.php',{
		parameters: { op: 'article_votes', id: art_id, score: score }, method: 'get',
		onComplete: function(oReq,json) {
			if (!json) { alert('no JSON!'); return; }
			if (json.status=='ok') {
				alert('הצבעתך נקלטה בהצלחה');
			} else if (json.status=='voted') {
				alert('כבר הצבעת על כתבה זו!');
			} else if (json.status=='refresh') {
				// show up nothing, we're just updating HTML
			} else {
				alert('אירעה שגיעה בקליטת ההצבעה. אנו מצטערים, אנא נסו שנית!\n\n'+json.error_msg);
			}
		}
	});
}

function send_password_reminder() {
	email = $F('password_reminder_email');
	if (!email.match(/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i)) {
		alert('כתובת דוא"ל אינה תקינה!');
		return;
	}
	new Ajax.Request('/mk_ajax_server.php',{
		parameters: { op: 'password_reminder', email: email }, method: 'get',
		onSuccess: function(tr) {
			res = tr.responseText;
			if (res=='no-such-user') {
				alert('כתובת דוא"ל שהזנת אינה רשומה במערכת! נא לנסות שנית');
				$('password_reminder_email').focus();
			} else if (res=='activation') {
				alert('עדיין לא אימתת את כתובת דוא"ל שלך\n'+
					'קישור לאימות נשלח שוב לכתובת שציינת. תודה');
				document.location = document.location;
			} else if (res=='ok') {
				alert('הסיסמה נשלחה לכתובת דוא"ל שלך. תודה');
				document.location = document.location;
			} else {
				alert('שגיאת התקשרות לשרת\n\n'+res);
			}
		},
		onFailure: function(tr) {
			res = tr.responseText;
			alert('*** שגיאת התקשרות לשרת ***\n'+res);
		}
	});
}

function mk_ensure_login() {
	if (window.site_user_json.id>0) {
		return true;
	}
	forms_class.OpenLoginForm();
	return false;
}

window.affiances_switcher = {
	show_speed: 200,
    active: null,
    active_slider: null,
    show: function(id,slider_elm) {
        new_elm = jQuery('#affiances_details_'+id);
        if (new_elm.size()<1) return;
        if (this.active && (new_elm[0]==this.active[0])) {
            old_elm = this.active;
            this.active_slider.removeClass('affiances_slider_up').addClass('affiances_slider_down');
            this.active = this.active_slider = null;
            old_elm.slideUp(this.show_speed);
            return;
        };
        slider_elm = jQuery(slider_elm);
        if (this.active) {
            old_elm = this.active;
            this.active_slider.removeClass('affiances_slider_up').addClass('affiances_slider_down');
            this.active = new_elm;
            this.active_slider = slider_elm;
            this.active_slider.addClass('affiances_slider_up').removeClass('affiances_slider_down');
            old_elm.slideUp(this.show_speed);
            this.active.slideDown(this.show_speed);
        } else {
            this.active = new_elm;
            this.active.slideDown(this.show_speed);
            this.active_slider = slider_elm;
            this.active_slider.addClass('affiances_slider_up').removeClass('affiances_slider_down');
        }
    }
}

window.htimes_box = {
	current: null,
	current_what: null,
	select: function(what) {
		if (this.current) {
			this.current.removeClassName('gallery_hotbox_header_item_selected');
			jQuery('#htimes_inner_'+this.current_what).hide();
		}
		this.current = $('htimes_box_'+what).addClassName('gallery_hotbox_header_item_selected');
		this.current_what = what;
		jQuery('#htimes_inner_'+what).show();
	},
	init: function() {
		elm = $('htimes_contents');
		if (!elm) return; // no htimes_box in the DOM, give up
		this.select('yom');
		jQuery('#htimes_locs').change((function() {
			$('htimes_contents').innerHTML = '<div style="text-align:center;padding: 20px"><img src="/images/gallery_new/ajax-loader1.gif" alt="טוען..." /></div>';
			loc = $F('htimes_locs');
			new Ajax.Request('/mk_ajax_server.php', {
				parameters: { op: 'htimes_box', loc: loc }, method: 'get',
				onSuccess: (function(tr) {
					res = tr.responseText;
					$('htimes_contents').innerHTML = res; 
					this.select(this.current_what);
				}).bind(this),
				onFailure: function(tr) {
					$('htimes_contents').innerHTML = '<div style="text-align:center;padding: 20px; color: red; font-weight:bold; font-size: 12px">שגיאת התחברות לשרת</div>';
				}
			});
		}).bind(this));
	}
};

// comments

function update_comments_dom() {
	if (window.site_user_json.id) {
	    // user logged in
		jQuery('.comment-username').each(function() {
			this.value = window.site_user_json.username;
		});
		if (window.site_user_json.msg_autoapprove) {
			jQuery(".msg_autoapprove_warning_username").text(window.site_user_json.username+", שימ/י לב!");
			jQuery(".msg_autoapprove_warning").show();
		} 
	}
}

// comreply

function comreply_open(id) {
	var comreply_id = id;
	var comreply_wrapper = jQuery('#comreply_'+id+':hidden');
	if (comreply_wrapper.size()<1) return;
	comreply_wrapper.html('<div class="com_comreply_inner" style="text-align: center">'+
		'<img src="/images/jumbo_banner_close.gif" width="16" height="16" alt="סגור" '+
		'style="float: right; margin: 2px; cursor: pointer" '+
		'onclick="comreply_close('+id+')" />'+
		'<img src="/images/gallery_new/ajax-loader1.gif" alt="טוען..." /><br />'+
		'טוען...</div>').slideDown(500).data('shown',true);
	jQuery.ajax({type: 'GET', url: '/mk_ajax_server.php', 
		data: {op: 'com_comreply', id: id}, dataType: 'html',
		success: function(data, status) {
			comreply_wrapper.html(data);
			update_comments_dom();
		},
		error: function(resp) {
			alert("שגיאה\n"+resp.responseText);
			comreply_close(comreply_id);
		}
	});
}

function comreply_close(id) {
	var comreply_wrapper = jQuery('#comreply_'+id+'');
	if (!comreply_wrapper.data('shown')) return;
	comreply_wrapper.slideUp(500).data('shown',false);
}

function comreply_submit(frm, id) {
	
	return false;
}

// Effectively, disable animations on IE. jQuery effects on IE work too much slowly
if (!jQuery.support.opacity) jQuery.fx.off = true;

// Some init actions, called on DomReady by jQuery
jQuery(function() {
	window.sel_tb.init();
	window.htimes_box.init();
	update_comments_dom();
});
