// Place your application-specific JavaScript functions and classes here

/*Jquery 1.4.2 - IE7 Bug Workaround*/
$(function(){
	$.ajaxSetup({xhr:function(){
            return window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
	}})
});
		
/* PASSWORD STRENGTH */
function bindPasswordStrength(){
	var strength_text = $('#password-strength-lang').text();	
	
	$('.password-field').jpassword({
		length: 6,
		lang: {
			basic: strength_text			
		}
	});
};

function bindChangePlayer(){
	$('#right-change-player-button').click(function(){		
		var complete_height = $(document).height();
		$('.overlay').css({			
			height: complete_height
		});
		
		$('.overlay').css("display","inline");
		$('#right-change-player-popup').css("display","block");
		
		ChangePlayerButton("right");
	});	
	$('#left-change-player-button').click(function(){		
		var complete_height = $(document).height();
		$('.overlay').css({			
			height: complete_height
		});
		
		$('.overlay').css("display","inline");
		$('#left-change-player-popup').css("display","block");
		
		ChangePlayerButton("left");
	});
	
};

function bindLoginPopup(){
	
	$('#login-button').click(function(){		
		
		var complete_height = $(document).height();
		$('.overlay').css({			
			height: complete_height
		});	
		
		$('.overlay').css("display","inline");
		$('#login-popup').fadeIn('normal');
		
		$('#session_email').focus();
	});	
};

function close_change_player(){
	
	$('.overlay').css("display","none");
	$('.general-popup').css("display","none");	
}

function close_login(){
	$('.overlay').css("display","none");
	$('#login-popup').css("display","none");
	
}

//This is called from the login popup, onComplete
show_login_popup = function(){
		
	path = $('#reload_page').attr('rel');
	if (path == 'true') {
		window.location.reload();		
	}else if (path != ''){
		window.location = path;		
	}
		
}

bindVideoThumbnail = function(){
	
	$('#video_thumbnail').click(function(){
		
		$('#video_thumbnail').css('display', 'none');		
		
		var intro_video = document.getElementById("intro_video");
		if (intro_video){						
			intro_video.playVideo();
		}		
		
		return false;
	})
}

bindTermsPage = function(){
	
	$('#terms_tab').click(function(){
		$(this).addClass('selected');		
		$('#terms_content').css('display', 'block');
		$('#privacy_tab').removeClass('selected');
		$('#privacy_content').css('display', 'none');
		return false;
	})
}

bindPrivacyPage = function(){
	
	$('#privacy_tab').click(function(){
		$(this).addClass('selected');
		$('#privacy_content').css('display', 'block');
		$('#terms_tab').removeClass('selected');		
		$('#terms_content').css('display', 'none');
		return false;
	})
	
}

function Ranking(obj){
	var o = this;	
	//this.currentSelected = null //1.commented by Rz
	this.url = null; //url where to go with ajax
	this.fieldName = null;//parameter send with ajax
	this.obj = obj;	
	document.getElementById(obj).o = this;//attach object of ranking
	
	selected_value = $("#"+obj+"> a.selected") 
	this.currentSelected = selected_value.length != 0 ? selected_value : null //1.added by Rz. Click the selected element	
	
	var as = $("#"+obj+">*");	
	if (this.currentSelected == null) {
		as.addClass("unselected"); //2.commented by Rz
	}
	
	//events that can be used and overrided
	this.onsuccess = function(data){};
	this.select = function(obj){
		if(obj==null)
			{
				$("#"+this.obj+">*").addClass("unselected");
				return;
			}
			
		$(obj).removeClass("unselected");
		$(obj).prevAll().removeClass("unselected");
		$(obj).nextAll().addClass("unselected");
	}
	
	this.select(this.currentSelected) //2.added by Rz . Mark as selected
	
	this.onclick = function(obj){
		this.currentSelected = obj;
		this.select(obj);
		//activate ajax
		if(this.url!=null)
		{
			var dataToSend = o.fieldName+"="+o.currentSelected.innerHTML+"&from=" + $(obj).parent().attr('rel');			
			$.ajax({
				url:this.url,
				data:dataToSend,
				success:function(data){o.onsuccess(data);}
			});
		}
	}
	this.onmouseover = function(obj){
		this.select(obj);
	}
	this.onmouseout = function(obj){
		this.select(this.currentSelected);	
	}
	//init events
	this.post = function(url,fieldName){o.url = url;o.fieldName = fieldName;return o;};
	as.click(function(){
				var p = this.parentElement || this.parentNode;
				p.o.onclick(this);
				return false;});
	as.mouseover(function(){
				var p = this.parentElement || this.parentNode;
				p.o.onmouseover(this);});		
	as.mouseout(function(){
				var p = this.parentElement || this.parentNode;
				p.o.onmouseout(this);});
}

bindYouDecide = function(from){
	
	if (from == 'left'){
		
		pid = $('#left_player_id')
		id = 0
		if (pid) id = pid.val()
		
		for (i=1;i<=4;i++){
		(new Ranking("Rank" + i)).post("/player_analysis/set_score/"+id,"value", "from", "gica").onsuccess = function(data){
			$("#left_player_score").html(data)
		};	
		}	
	}else{
		pid = $('#right_player_id')
		id = 0
		if (pid) id = pid.val()
		for (i=5;i<=8;i++){
		(new Ranking("Rank" + i)).post("/player_analysis/set_score/"+id,"value", "from").onsuccess = function(data){
			$("#right_player_score").html(data)
		};	
		}	
	}		
}

bindAddPlayer = function(from){
	
	(new Autocomplete("Auto" + from, "/player_analysis/search",{minCharsStartSearch:3,timeUntilSearchStart:0.5})).onselect = function(textSelected){
				var btnAdvice = $("#add-button-" + from).removeClass("get-advice-notactive");
				btnAdvice.click(function(){
					$.ajax(
						{
							url:textSelected+"?add=" + from,
							success:function(result){
								$("#" + from + "_player").html(result);
								bindChangePlayer();
								bindYouDecide(from);								
							}
						});
					});
				btnAdvice.keyup(function(e){
					if(e.keyCode==13)
						$.ajax(
						{
							url:textSelected+"?add=" + from,
							success:function(result){
								$("#" + from + "_player").html(result);
								bindChangePlayer();								
								bindYouDecide(from);
						    }
						});
				});
				setTimeout(function(){btnAdvice.focus();},100);
		};
}

ChangePlayerButton = function(from){
		
	var auto = from == "left" ? document.autos[0] : document.autos[1];
	//auto.inputNone was before I switched to "Type Player Name Here";
	auto.input.val(auto.inputNone);		
	auto.hrefSelected = null;
}	

updatePlayer = function(result,from){
	$('.overlay').css("display","none");
	$('#' + from + '-change-player-popup').fadeOut('normal');
	$("#" + from + "_player").html(result);								
	bindChangePlayer();
	bindYouDecide(from);
}

initAutocomplete = function(from){
	
	var btnAdvice = $("#" + from + "-change-button").removeClass("get-advice-notactive");
	var autocomplete = new Autocomplete("Auto20" + from, "/player_analysis/search",{timeUntilSearchStart:0.5,ignoreWidth:true});
	autocomplete.onselect = function(textSelected){
				this.hrefSelected = textSelected;
				setTimeout(function(){btnAdvice.focus();},100);
		};
				btnAdvice.click(function(){
					
					if(btnAdvice.alreadyClicked)return;else btnAdvice.alreadyClicked = true;
					
					var auto =  from=="left"?document.autos[0]:document.autos[1];
					if(auto.hrefSelected==null)return;
					$.ajax(
						{
							url:getAutocompleteValue(from)+"?add=" + from,
							success:function(result){
								updatePlayer(result,from);
								btnAdvice.alreadyClicked = null;																						
							}
						});
					});
				/*btnAdvice.keyup(function(e){
					var auto =  from=="left"?document.autos[0]:document.autos[1];
					if(auto.hrefSelected==null)return;
					if(e.keyCode==13)
						$.ajax(
						{
							url:getAutocompleteValue(from)+"?add=" + from,
							success:function(result){
								updatePlayer(result,from);																				
							}
						});
				});*/
		document.autos.push(autocomplete);
}

bindEditNote = function(note_id){
	
	ed = new tinymce.Editor('#note_body_'+ note_id, {
		width: 700,
			theme: 'advanced',
			force_br_newlines : true,			
			theme_advanced_toolbar_location : "top",
			theme_advanced_buttons1 : "bold, italic, underline, strikethrough, separator, undo, redo, link, unlink, separator, cleanup, removeformat, charmap",
			theme_advanced_buttons2 : "",
			theme_advanced_buttons3 : ""			
	});
	

 	$('#note_body_' + note_id).editable(
	 	{
 			type: 'wysiwyg',
 			editor: ed,
 			onSubmit:function submitData(content){
				alert(content.current);				
				$.ajax({type: "GET",
					url: "/notes/" + note_id + "/edit",
					data : 'update_value=' + encodeURIComponent(content.current)														
				});
 			},
 			submit:'Save',
 			cancel:'Cancel',			
			onCancel: function(){
				$(".btnEditNote").show();
				unBindEditor(note_id);//Important !!
			}				
		});	
	 
	 $("#note_body_" + note_id).click();
	 $(".btnEditNote").hide();	 	
}

/* Unbind all events from a dom*/
function unBindEditor(id){	
	$("#note_body_" + id).unbind();
	return false;
}

bindSourceAccuracyTabs = function(){
	
	$('#season_date').click(function(){
		$("#historical_content").hide();
		$("#season_content").show();
		$("#season_date").addClass('selected_tab');
		$("#historical").removeClass('selected_tab');
		$('.medalDescr').show();
	});
	
	$('#historical').click(function(){
		$("#season_content").hide();
		$("#historical_content").show();
		$("#historical").addClass('selected_tab');
		$("#season_date").removeClass('selected_tab');
		$('.medalDescr').hide();		
	});
	
}

$(document).ready(function() {	
	
	bindLoginPopup();
	bindTermsPage();
	bindPrivacyPage();
	
	// PASSWORD STRENGTH
	if($('.password-field').length) {
		bindPasswordStrength();
	};	

});