/**
 * @author alainpelloux
 */

var $BASE_URL = "";


$(document).ready(function() {
	
	$content = $("#content");
	if ($.browser.msie && parseFloat($.browser.version) < 7.0) {
		$content.css('height', $content.height() < 550 ? "550px" : "auto");
	}
	
	presentation("#content");
	
	$.ajaxSetup({
		type: "POST"
	});
	
	var $menu1 = $("#menu-1 + ul");
	var menu_current = $menu1.hasClass("WAI") ? $("#menu-2 + ul") : $menu1;
	$("#menu-1, #menu-2")
		.bind('click', function (event) {/*event.preventDefault();*/})
		.bind('mouseover', function (event) {
			menu_current.addClass("WAI");
			menu_current = $("#"+this.id+" + ul");
			menu_current.removeClass("WAI");
		})
	;
	
	$("#login,#password").each(function () {
		var value = this.value;
		this.value = "";
		$(this).Watermark(value);
	});
	
	/*
	
	$(window).bind('blur', function () {
		
		$(window).unbind().bind('focus', function () {
			
			$(window).unbind();
			
			if ($("#ttl").is('*')) {
				
				var n = 10;
				var ttl = setInterval(function () {
					
					if (n==0) {
						clearInterval(ttl);
						$("#inform").fadeOut("normal", function () {$("#inform").remove();});
					}
					else
						$("#ttl").text("Ce bloc disparaîtra dans "+(n--)+" secondes");
				}, 1000);
				
			}
		});
	});
	
	*/
	
	
	/*
	 * if (ie) { // check for Internet Explorer
	document.onfocusin = onFocus;
	document.onfocusout = onBlur;
} else {
	window.onfocus = onFocus;
	window.onblur = onBlur;
}
	 * 
	 * 
	 */
	
	
	/**
	 *  TABS
	 **/
	
	var hash_tabs = {}
	
	$(".tabs").each(function (index) {
		
		var $this = $(this);
		
		$("li > a", $this).each(function (i) {
			
			$current = $(this).parent();
			if ($current.hasClass("tab-current"))
				hash_tabs[index] = $current;
			
			$(this).bind('click', function (event) {
				var $parent = $(this).parent();
				event.preventDefault();
				$.ajax({
					
					url: $(this).attr('href')+"&ajax=get",
					beforeSend: function (XMLHttpRequest) {
						waiting($this.next().attr('id'));
					},
					success: function (data, textStatus) {
						hash_tabs[index].addClass("tab").removeClass("tab-current");
						$this.next().replaceWith(data);
						func_pager();
						presentation($this.next());
						hash_tabs[index] = $parent.addClass("tab-current").removeClass("tab");
						$(".read", $this.next()).bind('click', read_func);
					}
				});
			});
		});
	});
	
	
	/**
	 * PAGINATION
	 **/
	
	var func_pager = function () {
	
		$("#pager-ul>li>a").bind('click', function (event) {
			
			event.preventDefault();
			
			var target = $("#pager").parent().attr('id');
			var $this = $(this);
			
			$.ajax({
				url: $this.attr('href')+"&ajax=get",
				beforeSend: function (XMLHttpRequest) {
					waiting(target);
				},
				success: function (data, textStatus) {
					$("#"+target).replaceWith(data);
					func_pager();
				}
			});
					
			$.scrollTo("#header", 1000);
			
		});
	}
	func_pager();
	
	/**
	 * LINKS
	 **/
	
	
	/**
	 * TRI RUBRIQUES
	 **/
	
	$("#section").bind('change', function (event) {
		
		event.preventDefault();
		
		var target = $("#pager").parent().attr('id');
		$form = $(this).parent().parent();
		
		$.ajax({
			url: $form.attr('action')+"&ajax=get",
			data: "section="+this.value,
			beforeSend: function (XMLHttpRequest) {
				waiting(target);
			},
			success: function (data, textStatus) {
				$("#"+target).replaceWith(data);
				$("#refresh").html($(data).attr("vars"));
				func_pager();
			}
		});
		
	});
	
	
	/**
	 * LIRE UN ARTICLE
	 **/
	
	$(".read").bind('click', read_func);
	
		
	/**
	 * AJOUTER AUX FAVORIS
	 **/
	/*
	$("#classify").bind('click', function (event) {
		event.preventDefault();
		$.ajax({
			url: $BASE_URL+"/index.php?module=news&do=share&q=classify&ajax=get",
			success: function (data) {
				$("#inform-msg").text(data);
				setTimeout(function () {$("#inform-msg").text("")}, 2000);
			}
		});
	});
	*/
	
	/**
	 * ENVOYER UN COMMENTAIRE
	 **/
	
	var $form = $("#comment-form");
	$form.ajaxForm({
		url: $form.attr('action')+"&ajax=get",
		beforeSubmit: function (data) {
			if (data[0].value == "")
				return false;
		},
		success: function (data) {
			$("#comment-form").after(data);
			var $nb_comment = $("#nb_comment")
			$nb_comment.text(parseInt($nb_comment.text()) + 1)
		}
    });
	
	
	/**
	 * S'INSCRIRE
	 **/
	
	$("#post-first_name,#post-last_name,#post-email,#post-password,#remind-email,#auth-login,#auth-password,#post-sujet").each(function () {
		
		$this = $(this);
		
		if ($this.is('input')) {
			var value = this.value;
			this.value = "";
			$(this).Watermark(value);	
		}
		else if ($this.is('textarea')) {
			var value = $this.text();
			$this.text("");
			$(this).Watermark(value);
		}
	});
	
	
	/**
	 * RADIO BUTTON
	 **/
	
	//$('input[type=radio]', $("#vote-form")).each(init_vote);

	
	
	
	
	
	_IE_compliance ($("#article-action"))
	
});




function presentation ($target) {

	if ($().bt != undefined){
		$().bt.defaults.fill = '#F3F3F5';
		$().bt.defaults.strokeStyle = '#858282';
		
	
		$("p.tooltip", $target).bt({
			positions: ['bottom', 'top'],
			cssStyles: {color: $.browser.msie ? '555555' : 'CDCDCE', fontStyle:'italic'},
			width: 455,
			cornerRadius: 0,
			animate: false,
			spikeLength: 10,
			spikeGirth: 20
		});		
	}

}


function waiting (id) {
	
	var height = $("#"+id).height();
	
	if (parseInt(height) < 30) height = 100;
	
	$("#"+id).replaceWith(
		$('<div id="'+id+'"><div class="waiting"><img src="'+$BASE_URL+'/storage/img/icon/icone-chargement.gif"/></div></div>').height(height)
	);
}

function read (data, textStatus) {
	
	$("#inform").remove();
	$("#content").prepend(data);
	
	$("#inform-comment").bind('click', function (event) {
		event.preventDefault();
		window.location.href = $("#link-read").attr('href')+"#commentaire"; 
	});
	
	$("#inform-notation>label>input").bind('change', function () {
		
		var value = this.value;
		
		$.ajax({
			url: $BASE_URL+"/index.php?module=news&do=note&ajax=",
			data: "value="+value,
			success: function () {
				for (i=0; i<value; i++) {
					var target = hash_checkbox[i].checkbox;
					target.unbind().attr('class', "radio-1").css('cursor', "default");
				}
				for (i=value; i<5; i++) {
					var target = hash_checkbox[i].checkbox;
					target.unbind().css('cursor', "default");
				}
			}
		});
	});
	
	$("#inform-share").bind('click', function (event) {
		event.preventDefault();
		$.ajax({
			url: $BASE_URL+"/index.php?module=news&do=share&q=share&ajax=get",
			success: function (data) {
				$("#inform").fadeOut("normal", function () {
					$("#inform").replaceWith(data);
					var $email = $("#share-email").val("")
					$email.Watermark($email.prev().text());
					
					var $form = $("#form-share");
					$form.ajaxForm({
						url: $form.attr('action')+"&ajax=get",
						success: read
				    });
					/*$("#share-submit").bind('click', function (event) {
						event.preventDefault();
						
						
					});*/
				});
			}
		});
	});
	
	$("#inform-classify").bind('click', function (event) {
		event.preventDefault();
		$.ajax({
			url: $BASE_URL+"/index.php?module=news&do=share&q=classify&ajax=get",
			success: function (data) {
				$("#inform-msg").text(data);
				setTimeout(function () {$("#inform-msg").text("")}, 2000);
			}
		});
	});
	
	$("#ttl").parent().bind('click', function (event) {
		event.preventDefault();
		$("#inform").fadeOut("normal", function () {$("#inform").remove();});
	});
	
	$('input[type=radio]', $("#inform")).each(init_vote);
	
	_IE_compliance($("#inform"));
	
}

var hash_checkbox = {};
	
function init_vote (index) {
		
	var $this = $(this);
	
	if ($this.attr('disabled'))
		return 0;
	
	var $next = $this.next();
	
	hash_checkbox[index] = new Object();
	hash_checkbox[index].checkbox = $next;
	hash_checkbox[index].classe = $next.attr('class');
	hash_checkbox[index].checkbox.css('cursor', "pointer");
	
	$next.bind('mouseover', function () {
		
		for (i=0; i<=index; i++) {
			var target = hash_checkbox[i].checkbox;
			target.attr('class', "radio-2");
		}
		
		for (i=index+1; i<5; i++) {
			var target = hash_checkbox[i].checkbox;
			target.attr('class', "radio-0");
		}
		
	});
	
	$next.bind('mouseout', function () {
		
		for (i=0; i<5; i++) {
			var target = hash_checkbox[i].checkbox;
			target.attr('class', hash_checkbox[i].classe);
		}
		
	});
	
}

function read_func (event) {
	
	//event.preventDefault();
		
	$.scrollTo("#header", 1000);
	
	$this = $(this).attr('target', "_blank");
	var url = $this.attr('href');
	
	$.ajax({
		url: $BASE_URL+"/index.php?module=news&do=read&ajax=get",
		data: "rewrite="+(url.substring(url.lastIndexOf('/') + 1)),
		success: read
	});
	
}


function _IE_compliance (element) {
	
	if ($.browser.msie && $.browser.version < 7) {
		if (element == null) {element = $('body')}
		$("label.pointer", element).each(function () {
			if (!$(this).attr('for')) {
				var fc = this.getElementsByTagName("input");
				//fc.length == 0 ? fc = this.getElementsByTagName("select") : null;
				this.attachEvent("onclick", function () {
					alert("click");
					!$(fc[0]).is(":text") ? fc[0].click() : fc[0].focus();
				});
			}
		});
	}
}