/*
 * Social Creative Core Library 1.5
 * http://www.social-creative.com/
 * Copyright(c) 2009, Social Creative, SARL.
 *
 * See the LICENCE.txt in root directory
 *
 */

function add_to_bookmarks () {
	if (navigator.appName != 'Microsoft Internet Explorer') window.sidebar.addPanel(document.title, window.location.href, '') ;
	else window.external.AddFavorite(window.location.href, document.title) ;
}

function go_search_sidebar () {
	var q = document.getElementById('qsidebar').value ;
	var c = document.getElementById('csidebar').value ;
	if(q == '0' || q == 'Tapez un mot clef...') q = '' ;
	if(c == '0' || c == 'Rechercher dans la catégorie') c = '' ;
	
	if(c != '') {
		window.location.href = c+'?q='+encodeURIComponent(q) ;
	}
	else {
		window.location.href = 'recherche?q='+encodeURIComponent(q) ;
	}
}

jQuery.fn.extend({
   findPos : function() {
       var obj = jQuery(this).get(0) ;
       var curleft = obj.offsetLeft || 0 ;
       var curtop = obj.offsetTop || 0 ;
       while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft ;
			curtop += obj.offsetTop ;
       }
       return {left: curleft, top: curtop} ;
   }
});
 
$(document).ready(function(){
	$("#loginForm").validate({
		submitHandler: function(form) {
			if(typeof formBlocked == 'undefined' || !formBlocked) {
				formBlocked = true ;
				
				if(typeof LANG == 'undefined') LANG = {} ;
				if(typeof LANG.all_login_loading == 'undefined') LANG.all_login_loading = 'Connexion en cours...' ;

				create_loading('loginForm', LANG.all_login_loading, {fontWeight: 'bold', fontSize: '110%'}) ;
				
				if(typeof(socialFusion) != 'undefined') {
					socialFusion.setLogin({
						login: document.getElementById('login').value,
						password: document.getElementById('passwd').value,
						auto: document.getElementById('autocon').checked
					}) ;
				}
				
				jQuery(form).ajaxSubmit({
					success: function (response, type, form) {
						var obj = eval("(" + response + ')') ;

						if(obj.success) {
							if(typeof(socialFusion) != 'undefined') {
								socialFusion.connection(function () { window.location.reload() ; }) ;
							}
							else {
								window.location.reload() ;
							}
						}
						else {
							destroy_loading('loginForm') ;
							formBlocked = false ;
							alert_msg(obj.errors.reason) ;
						}
					}
				});
			}
		}
	});
	
	$("#newsletterForm").validate({
		submitHandler: function(form) {
			if(typeof formBlocked == 'undefined' || !formBlocked) {
				formBlocked = true ;
				
				jQuery(form).ajaxSubmit({
					success: function (response, type, form) {
						var obj = eval("(" + response + ')') ;
						
						if(obj.success) {
							formBlocked = false ;
							document.getElementById('newsletter_mail').value = '' ;
							valid_msg(LANG.newsletter_send_confirmation) ;
						}
						else {
							formBlocked = false ;
							alert_msg(obj.errors.reason) ;
						}
					}
				}) ;
			}
		}
	}) ;	
	
});

function logout (url) {
	$.post(url, {}, function(response) {
		if(typeof(socialFusion) != 'undefined') {
			socialFusion.logout(function () { window.location.reload() ; }) ;
		}
		else {
			window.location.reload() ;
		}
	}, 'html') ;
}

function date_picker(element) {
	$(element).datepicker({
		changeMonth: true,
		changeYear: true,
		yearRange: '1920:2010',
		dateFormat: LANG.all_date_picker_format,
		firstDay: LANG.all_date_picker_first_day,
		dayNames: [LANG.day_01, LANG.day_02, LANG.day_03, LANG.day_04, LANG.day_05, LANG.day_06, LANG.day_07],
		dayNamesMin: [LANG.day_01.substr(0,2), LANG.day_02.substr(0,2), LANG.day_03.substr(0,2), LANG.day_04.substr(0,2), LANG.day_05.substr(0,2), LANG.day_06.substr(0,2), LANG.day_07.substr(0,2)],
		dayNamesShort: [LANG.day_01.substr(0,3), LANG.day_02.substr(0,3), LANG.day_03.substr(0,3), LANG.day_04.substr(0,3), LANG.day_05.substr(0,3), LANG.day_06.substr(0,3), LANG.day_07.substr(0,3)],
		monthNames: [LANG.month_01, LANG.month_02, LANG.month_03, LANG.month_04, LANG.month_05, LANG.month_06, LANG.month_07, LANG.month_08, LANG.month_09, LANG.month_10, LANG.month_11, LANG.month_12],
		monthNamesShort: [LANG.month_01.substr(0,3), LANG.month_02.substr(0,3), LANG.month_03.substr(0,3), LANG.month_04.substr(0,3), LANG.month_05.substr(0,3), LANG.month_06.substr(0,3), LANG.month_07.substr(0,3), LANG.month_08.substr(0,3), LANG.month_09.substr(0,3), LANG.month_10.substr(0,3), LANG.month_11.substr(0,3), LANG.month_12.substr(0,3)]
	}) ;
}

function simple_msg (message, func) {
	document.getElementById('simpleContent').innerHTML = message ;

	if($("#dialog-simple") && typeof($("#dialog-simple").dialog('isOpen')) != 'undefined') {
		$("#dialog-simple").dialog('open') ;
	}
	else {
		var buttons = {} ;
		buttons[''+LANG.all_ok] = function() {
			$(this).dialog('close') ;
		} ;

		$("#dialog-simple").dialog({
			bgiframe: true,
			resizable: true,
			height:200,
			modal: true,
			overlay: { opacity: 0.5 },
			buttons: buttons,
			close: function () {
				if(typeof(func) != 'undefined') func() ;
			}
		});
	}
}

function alert_msg (message, func) {
	document.getElementById('errorContent').innerHTML = message ;

	if($("#dialog-error") && typeof($("#dialog-error").dialog('isOpen')) != 'undefined') {
		$("#dialog-error").dialog('open') ;
	}
	else {
		var buttons = {} ;
		buttons[''+LANG.all_ok] = function() {
			$(this).dialog('close') ;
		} ;

		$("#dialog-error").dialog({
			bgiframe: true,
			resizable: true,
			height:200,
			modal: true,
			overlay: { opacity: 0.5 },
			buttons: buttons,
			close: function () {
				if(typeof(func) != 'undefined') func() ;
			}
		});
	}
}

function valid_msg (message, func) {
	document.getElementById('validContent').innerHTML = message ;

	if($("#dialog-valid") && typeof($("#dialog-valid").dialog('isOpen')) != 'undefined') {
		$("#dialog-valid").dialog('open') ;
	}
	else {
		var buttons = {} ;
		buttons[''+LANG.all_ok] = function() {
			$(this).dialog('close') ;
		} ;

		$("#dialog-valid").dialog({
			bgiframe: true,
			resizable: true,
			height:200,
			modal: true,
			overlay: { opacity: 0.5 },
			buttons: buttons,
			close: function () {
				if(typeof(func) != 'undefined') func() ;
			}
		});
	}
}

function ask_msg (message, okFunc, cancelFunc, func, okText, cancelText) {
	document.getElementById('askContent').innerHTML = message ;

	okText = okText || LANG.all_ok ;
	cancelText = cancelText || LANG.all_cancel ;

	var buttons = {} ;
	buttons[''+cancelText] = function() {
		$(this).dialog('destroy') ;
		if(typeof(cancelFunc) != 'undefined' && cancelFunc) cancelFunc() ;
	} ;
	buttons[''+okText] = function() {
		$(this).dialog('destroy') ;
		if(typeof(okFunc) != 'undefined' && okFunc) okFunc() ;
	} ;

	$("#dialog-ask").dialog({
		bgiframe: true,
		resizable: true,
		height:200,
		modal: true,
		overlay: { opacity: 0.5 },
		buttons: buttons,
		close: function () {
			if(typeof(func) != 'undefined' && func) func() ;
		}
	});
}

function create_loading (elId, text, style) {
	style = style || [] ;
	var elToFade = document.getElementById(elId) ;
	var elToFadeJ = $('#'+elId) ;
	var height = elToFadeJ.height() ;
	var paddingTop = 0 ;
	if(height > 25) {
		paddingTop = (height - 25) / 2 ;
		height -= paddingTop ;
	}
	elToFade.style.opacity = '.2' ;
	elToFade.style.filter = 'alpha(opacity=20)' ;
	var el = document.createElement('div') ;
	el.id = elId+'Loading' ;
	el.style.textAlign = 'center' ;
	el.style.position = 'absolute' ;
	el.style.width = elToFadeJ.width() + 'px' ;
	el.style.height = height + 'px' ;
	var position = elToFadeJ.findPos() ;
	el.style.top = position.top + 'px' ;
	el.style.left = position.left + 'px' ;
	el.style.paddingTop = paddingTop + 'px' ;
	el.style.paddingTop = paddingTop + 'px' ;
	var item ;
	for(item in style) {
		el.style [item] = style[item] ;
	}
	var images = 'themes/default/images' ;
	if(typeof style_cookie != 'undefined' && style_cookie == 'phpBBstyle') images = '../themes/default/images' ;
	if(typeof USER == 'object' && typeof USER.IMAGES_URL != 'undefined') images = USER.IMAGES_URL ;
	el.innerHTML = '<img src="'+images+'/jquery/loading.gif" /> ' + text ;
	document.body.appendChild(el) ;
}

function destroy_loading (elId) {
	if(document.getElementById(elId+'Loading')) {
		document.body.removeChild(document.getElementById(elId+'Loading')) ;
		document.getElementById(elId).style.opacity = '' ;
		document.getElementById(elId).style.filter = '' ;
	}
}