var mySlide;
var toolTips;
var registerSlide;
var loginSlide;
var registerSlideStatus = 'close';
var loginSlideStatus = 'close';
var hostingSlideStatus = 'close';
var customSlideStatus = 'close';
var transfert = false;
var isIphone = navigator.userAgent.toLowerCase().indexOf('iphone') > -1;
var slideDuration = isIphone ? 0 : 1000;

window.addEvent('domready', function() {

	var customTips = $$('.tooltip');
	if( customTips != null) {
		toolTips = new Tips(customTips, {
			showDelay: 100,  
			hideDelay: 100, 
			className: 'anything',
			offsets: {
				'x': 10,
				'y': 10
			},
			fixed: false,
			onShow: function(toolTipElement){
				toolTipElement.fade(.95);
			},	
			onHide: function(toolTipElement){
   				toolTipElement.fade(0);
			}
		});
		toolTipsIE6 = new Tips(customTips, {
			showDelay: 100,
			hideDelay: 100,
			className: 'anything',
			offsets: {
				'x': -220,
				'y': 10
			},
			fixed: false,
			onShow: function(toolTipElement){
				toolTipElement.fade(.95);
			},
			onHide: function(toolTipElement){
				toolTipElement.fade(0);
			}
		});
	}

	if( $( 'verticalSlide') != null) {
		mySlide = new Fx.Slide('verticalSlide', {
			onStart: function(how, mode){
				$( 'slideHidden').setStyle( 'display', 'block');
			}
		});
		mySlide.hide()
		if( $( 'resultSearch') != null) {
			$( 'resultSearch').addEvent('click', function(e){
				if( e != undefined) {
					new Event(e).stop();
				}
				setDomains();
				setTimeout( function() {
					window.location.hash='#search'
				}, 170);
			});
		}
		if( $( 'domainSubmit') != null) {
			$( 'domainSubmit').addEvent( 'click', function(e) {
				if( e != undefined) {
					new Event(e).stop();
				}
				setDomains();
				setTimeout( function() {
					window.location.hash='#search'
				}, 170);
			});
		}
	}

	if( $( 'registerForm') != null) {
		registerSlide = new Fx.Slide('registerForm', {
			duration: slideDuration,
			onComplete: function() {
				if( this.open == true) {
					$( 'register').checked = 'checked';
					registerSlideStatus = 'open';
				} else {
					$( 'register').checked = false;
					registerSlideStatus = 'close';
				}
				if( loginSlideStatus == 'toOpen') {
					loginSlide.toggle();
				}
			}
		});
		registerSlide.hide()
		$$('.register').addEvent('click', function(e){
			if( e != undefined) {
				new Event(e).stop();
			}
			setTimeout( function() {
				window.location.hash='#identity'
			}, 170);
			if( registerSlide.open == true) {
				registerSlide.toggle();
			} else {
				if( loginSlide.open == true) {
					registerSlideStatus = 'toOpen';
					loginSlide.toggle();
				} else {
					registerSlide.toggle();
				}
			}
		});
	}

	if( $( 'loginForm')) { 
		loginSlide = new Fx.Slide( 'loginForm', {
			duration: slideDuration,
			onComplete: function() {
				if( this.open == true) {
					$( 'login').checked = 'checked';
					loginSlideStatus = 'open';
				} else {
					$( 'login').checked = false;
					loginSlideStatus = 'close';
				}
				if( registerSlideStatus == 'toOpen') {
					registerSlide.toggle();
				}
			}
		});
		loginSlide.hide()
		$$( '.login').addEvent('click', function(e){
			if( e != undefined) {
				new Event(e).stop();
			}
			setTimeout( function() {
				window.location.hash='#identity'
			}, 170);
			if( loginSlide.open == true) {
				loginSlide.toggle();
			} else {
				if( registerSlide.open == true) {
					loginSlideStatus = 'toOpen';
					registerSlide.toggle();
				} else {
					loginSlide.toggle();
				}
			}
		});
	}

	var hostingSlide = undefined;
	var customSlide = undefined;
	if( $( 'hostingForm') != null) {
		hostingSlide = new Fx.Slide( 'packHosting', {
			duration : 700,
			onComplete: function() {
				if( this.open == true) {
					hostingSlideStatus = 'open';
					if( $( 'customForm') != null && $( 'customForm').hasClass( 'activ')) {
						$( 'customForm').removeClass( 'activ');
					}
				} else {
					hostingSlideStatus = 'close';
					if( customSlideStatus == 'toOpen') {
						if( $( 'hostingForm').hasClass( 'activ')) {
							$( 'hostingForm').removeClass( 'activ');
						}
					} else {
						if( $( 'customForm') != null &&!$( 'customForm').hasClass( 'activ')) {
							$( 'customForm').addClass( 'activ');
						}
						if( $( 'bothEngage')) {
							$( 'bothEngage').setStyle( 'display', 'none'); 
						}
					}
				}
				if( customSlideStatus == 'toOpen') {
					customSlide.toggle();
				}
			}
		});
		hostingSlide.hide();
		$( 'hostingSubmit').addEvent( 'click', function(e) {
			if( e != undefined) {
				new Event(e).stop();
			}
			if( hostingSlide.open == true) {
				hostingSlide.toggle();
			} else {
				if( !$( 'hostingForm').hasClass( 'activ')) {
					$( 'hostingForm').addClass( 'activ');
				}
				if( $defined( customSlide)) {
					if( customSlide.open == true) {
						hostingSlideStatus = 'toOpen';
						customSlide.toggle();
					} else {
						hostingSlide.toggle();
					}
				} else {
					hostingSlide.toggle();
				}
			}
			if( $( 'bothEngage')) {
				$( 'bothEngage').setStyle( 'display', ''); 
			}
		});
		if( $( 'bothEngage') != null) {
			$( 'bothEngage').setStyle( 'display', 'none');
		}
	}

	if( $( 'customForm') != null) {
		customSlide = new Fx.Slide( 'packCustom', {
			duration: 700,
			onComplete: function() {
				if( this.open == true) {
					customSlideStatus = 'open';
					if( $( 'hostingForm') && $( 'hostingForm').hasClass( 'activ')) {
						$( 'hostingForm').removeClass( 'activ');
					}
				} else {
					customSlideStatus = 'close';
					if( hostingSlideStatus == 'toOpen') {
						if( $( 'customForm').hasClass( 'activ')) {
							$( 'customForm').removeClass( 'activ');
						}
					} else {
						if( $( 'hostingForm') && !$( 'hostingForm').hasClass( 'activ')) {
							$( 'hostingForm').addClass( 'activ');
						}
						if( $( 'bothEngage')) {
							$( 'bothEngage').setStyle( 'display', 'none'); 
						}
					}
				}
				if( hostingSlideStatus == 'toOpen' && $defined( hostingSlide)) {
					hostingSlide.toggle();
				}
			}
		});
		customSlide.hide();
		$( 'customSubmit').addEvent( 'click', function(e) {
			if( e != undefined) {
				new Event(e).stop();
			}
			if( customSlide.open == true) {
				customSlide.toggle();
			} else {
				if( !$( 'customForm').hasClass( 'activ')) {
					$( 'customForm').addClass( 'activ');
				}
				if( $defined( hostingSlide)) {
					if( hostingSlide.open == true) {
						customSlideStatus = 'toOpen';
						hostingSlide.toggle();
					} else {
						customSlide.toggle();
					}
				} else {
					customSlide.toggle();
				}
			}
			if( $( 'bothEngage')) {
				$( 'bothEngage').setStyle( 'display', ''); 
			}
		});
	}

	if( $( 'transferDomain') != null) {
		if( transfert == false) {
			setTransfers();
			transfert = true;
		}
	}

	if( $( 'ndd') != null) {
		$( 'ndd').focus();
	}

	if( $( 'hints')) {
		$('hints').addEvent( 'mouseover', function(e){
			if( e != undefined) {
				new Event(e).stop();
			}
			$( 'answerOpen').setStyle( 'display', 'block');
		});
		$('hints').addEvent( 'mouseout', function(e){
			if( e != undefined) {
				new Event(e).stop();
			}
			$( 'answerOpen').setStyle( 'display', 'none');
		});
	}

	if( $( 'ong-content-1')) {
		$( 'ong-1').addEvent( 'click', function(e) {
			if( e != undefined) {
				new Event(e).stop();
			}	
			$( 'ong-content-0').removeClass( 'activ');
			$( 'ong-content-1').addClass( 'activ');
			$( 'functions').addClass( 'activ');
			$( 'details').removeClass( 'activ');
		});
		$( 'function').addEvent( 'click', function(e) {
			if( e != undefined) {
				new Event(e).stop();
			}	
			$( 'ong-content-0').removeClass( 'activ');
			$( 'ong-content-1').addClass( 'activ');
			$( 'functions').addClass( 'activ');
			$( 'details').removeClass( 'activ');
		});
	}

	if( $( 'ong-content-0')) {
		$( 'ong-0').addEvent( 'click', function(e) {
			if( e != undefined) {
				new Event(e).stop();
			}
			$( 'ong-content-1').removeClass( 'activ');
			$( 'ong-content-0').addClass( 'activ');
			$( 'functions').removeClass( 'activ');
			$( 'details').addClass( 'activ');
		});
		$( 'presentation').addEvent( 'click', function(e) {
			if( e != undefined) {
				new Event(e).stop();
			}
			$( 'ong-content-1').removeClass( 'activ');
			$( 'ong-content-0').addClass( 'activ');
			$( 'functions').removeClass( 'activ');
			$( 'details').addClass( 'activ');
		});
	}

	if( $( 'cbButton')) {
		$( 'cbButton').addEvent( 'click', function(e) {
			if( e != undefined) {
				new Event(e).stop();
			}
			if( checkCGV()) {
				$( 'paymentCB').submit();
			}
		});
	}

	if($('divideButton')) {
		$('divideButton').addEvent('click', function(e) {
			if(e != undefined) {
				new Event(e).stop();
			}
			if(checkCGV()) {
				$('formDividePayment').submit();
			}
		});
	}

	if( $( 'virementButton')) {
		$( 'virementButton').addEvent( 'click', function(e) {
			if( e != undefined) {
				new Event(e).stop();
			}
			if( checkCGV()) {
				if(isIphone){
					$('paymentVirement').submit();
				}
				else{
					var box = new MooDialog.Confirm(
						'Votre commande sera validée à réception de votre virement, la facture vous sera ensuite envoyée par email.',
						function() {
							$( 'paymentVirement').submit();
						},
						null,
						{
							title: 'Confirmation',
							size: {
								height: 150,
								width: 400
							}
						}
					);
				}
			} else {
				return false;
			}
		});
	}

	if( $( 'chequeButton')) {
		$( 'chequeButton').addEvent( 'click', function(e) {
			if( e != undefined) {
				new Event(e).stop();
			}
			if( checkCGV()) {
				if(isIphone){
					$('paymentVirement').submit();
				}
				else{
					var box = new MooDialog.Confirm(
						"Votre commande sera validée à réception de votre chèque, la facture vous sera ensuite envoyée par email.",
						function() {
							$( 'paymentCheque').submit();
						},
						null,
						{ 
							title: 'Confirmation', 
							size: { 
								height: 150,
								width:400
							}
						 }
					);
				}
			} else {
				return false;
			}
		});
	}
	if ($('admin-article-modifier-form')) {
		var textContentRow = $$('textarea[name="article[content]"]')[0].getParent('tr');
		if ($$('input.article-type-radio:checked').get('value') == 3) {
			$(textContentRow).setStyle('display', 'none');
			$$('textarea[name="article[content]"]')[0].appendText('');
		}
		$$('input.article-type-radio').addEvent('change', function() {
			if ($$('input.article-type-radio:checked').get('value') == 3) {
				 $(textContentRow).setStyle('display', 'none');
				$$('textarea[name="article[content]"]')[0].appendText(' ');
			} else {
				$(textContentRow).setStyle('display', '');
			}
		});
	}

});



function checkCGV() {

	var message = 'Merci de valider les conditions générales de vente.';
	if( !$( 'cgv').checked) {
		if(isIphone)
			alert(message);
		else{
			var box = new MooDialog.Alert( 
				message,
				{
					title: 'Avertissement',
					size: {
						width: 400
					},
					scroll: true
				}
			);
		}
		return false;
	}
	return true;
}

function changeRenewPrice( id, prices, month) {

	var value = $( 'select[' + id + ']').value;
	var priceTd = $( 'price[' + id + ']');

	var price = 0;
	if( prices[value] != null) {
		var price = prices[value];
	}

	var txt = price.toFixed(2).toString().replace( ".", ",") + " €";
	if( month) {
		txt += " / mois";
	} else {
		txt += " / an";
	}
	priceTd.set( 'html', txt);
}

function changeUpgradeSitePrice( prices, oldPrice) {
	
	var value = $( 'customId').value;
	var priceTd = $( 'priceTd');

	var price = 0;
	if( prices[value] != null) {
		var price = prices[value] - oldPrice;
	}

	var txt = price.toFixed(2).toString().replace( ".", ",") + " €";
	priceTd.set( 'html', txt);	
}

function checkOption( option) {
	
	if( option == 'referencement') {
		var select = $( 'options').getElement( 'select');
		if( select.value == '') {
			alert( 'Merci de sélectionner le site que vous souhaitez référencer.');
			return false;
		}
		var checked = false;
		var radios = $( 'options').getElements( 'input[type=radio]');
		radios.each( function( radio) {
			if( radio.checked) {
				checked = true;
			}
		});
		if( !checked) {
			alert( 'Merci de sélectionner le nombre d\'annuaire d\'inscription de votre site.');
			return false;
		}
	}	
	return true;
}

function getPrintDirectories() {

	var value = $( 'customSiteId').value;
	if( value != '') {
		var getDirectories = new Request({
			url: '/rpc.php',
			data: 'csid=' + value + '&type=getDirectories',
			async: true,
			timeout: 5000,
			onSuccess: function( response) {
				informations = JSON.decode( response);
				if( informations == null) {
					return false;
				}
				
				for( i in informations) {
					if( $( 'directory_' + i)) {
						if( informations[i] == false) {
							$( 'directory_' + i).setStyle( 'display', 'none');
						} else {
							$( 'directory_' + i).setStyle( 'display', '');
						}
					}
				}
			}
		});
		getDirectories.send();
	}
}

function getPrintWebBuild() {

	var value = $( 'customSiteId').value;
	if( value != '') {
		var getWebBuild = new Request({
			url: '/rpc.php',
			data: 'csid=' + value + '&type=getWebBuild',
			async: true,
			timeout: 5000,
			onSuccess: function( response) {
				informations = JSON.decode( response);
				if( informations == null) {
					return false;
				}
				
				$( 'buildSite').value = informations['pages'];
				$( 'buildTitle').set( 'html', 'Création ' + informations['pages'] + ' pages');
				var price = informations['price'].toFixed(2).toString().replace( ".",",");
				$( 'buildPrice').set( 'html', price + ' € <span>HT</span>');
				$( 'webBuild').setStyle( 'display', '');
			}
		});
		getWebBuild.send();
	}
}

function changePackInput( product) {

	if( $( 'packInput')) {
		$( 'packInput').value = product;
	}
}

