/*
	Surcouche javascript
*/

$(document).ready(function(){
//Application de la surcouche javascript
DomainFinder_verification = true;

$('#body #header').append('<img class="loading load" src="res/load.gif">');

//Modification des pages du menu par des evênements ajax

$('#header ul').delegate('a','click',function(e){
  
  
  //Beurk, c'est vieux, c'est CRADE (ahhh mes débuts en jQuery!)
  //mais pas le temps de le mettre à jour; Désolé !
  
  var $link = $(this);
  
  if(!$link.hasClass('ajx')){
      return;
  } else {
    e.preventDefault();
  }
  
	$('#body #header ul li a').removeClass('on');
	$('#body #header').append('<img class="loading load" src="res/load.gif">');
	
	if(pageTracker)//google
		pageTracker._trackPageview($link.attr('href'));
		
	$("#content").load($link.attr('href')+"_no",{},function()
		{
			$('#body #header img[class*=load]').remove();
			layer($link.attr('href'));
		});
	$(this).addClass('on');
	return false;
  
  
});

	
	//Fsub.addClass('disabled');
	//Fsub.attr('disabled','true');

if($('.adas2'))
{
	$('.adas2').unbind('mouseover');
	$('.adas2').mouseover(function(){window.setTimeout('$(".adas2").height(65);',5000);});
}	
	

$('#body #header img[class*=load]').remove();



});

function research()
{
	$('.frmgenerate').unbind('submit');
	$('.frmgenerate').bind('submit', function() { return false; })
				.submit(function(){
					if($('#word').val() == '' || $('#word').val().length < 4)
					{
						msg($('fieldset:eq(1) p:eq(1)'),'You must type more than 3 characters.',5000);
						return false;
					}
					else
					{
						notif=false;
						if(document.getElementById('update_yes') && document.getElementById('update_yes').checked == true)
							notif=true;
							pageTracker._trackPageview('mail/mailinglist');
						
						if($('#email').val() && $('#email').val() > '')
							pageTracker._trackPageview('mail/report');
						
						$('#body #header').append('<img class="loading load" src="res/load.gif">');
						$.post('p.generate.php', {
						word:$('#word').val(),
						email:$('#email').val(),
						nfo_update:notif}, function(data){
							
							if(!data)
							{
								msg($('.frmgenerate'),'Maintain',5000);
							}
							else
							{
								if($('.fieldsetgenerate').length == 0)
								{
									$('.frmgenerate').append(data);
								}
								else
								{
									$('.fieldsetgenerate').remove();
									$('.frmgenerate').append(data);
								}
								
								if(pageTracker)//google
									pageTracker._trackPageview('generate/'+$('#word').val());
									
								layer('generate');
								
								
							}
							
							$('#body #header img[class*=load]').remove();
						});
						return false;
					}
				});
}

function layer(page)
{
		switch(page)
		{
			case 'home':
				Fsub = $('fieldset:eq(1) p:eq(1) input[type=submit]');
				Fsub.hide();
				$('input[type=text]#word').keyup(function(){
					Fsub.show(500);
				});
				
				$('cite').unbind('click').click(function(){
					$('#word').val($(this).html());
					$('fieldset:eq(1) p:eq(1) input[type=submit]').show(500);
				});
				
				if(!$('.option').html())
				{
					$('#div_opt').addClass('hidden').before('<p class="clear"><a href="javascript:void(0);" class="_opt option">more option</a></p>');
					
					$('.option').unbind('click').click(function(){
						if($(this).html() == 'less')
						{
							$(this).html('more option');
							$('#div_opt').hide(500);
						
						}
						else
						{
							$(this).html('less');
							$('#div_opt').removeClass('hidden').show(500);
						}
						return false;
					});
				}
				
				research();
			break;
			
			case 'generate':
				layer('home');
				
				Fsub = $('fieldset:eq(1) p:eq(1) input[type=submit]');
				Fsub.hide();
				$('input[type=text]#word').keyup(function(){
					Fsub.show(500);
				});
				
				/*
					generate
				*/
				if((jQuery.browser && !jQuery.browser['msie']) || (jQuery.browser['msie'] === true &&  jQuery.browser['version'] > '6.0'))
				{
					$('.resDomainFinder ul').each(function(){
						$(this).hide();
					});
				
				//Ajout le non de domaine à droite + Vérification disponibilite
				i = 0;
				error = 0;
				tld = $('.__tld span._tld');
				domain = $('.__tld span._domain');
				$('.__tld').each(function(){
					$(this).append('<span class="_ssd">'+$('#__ultld_'+tld[i].innerHTML+' li').length+' more...</span>');
					//Nombre de sd dispo
					$(this).prepend('<a id="andd_'+tld[i].innerHTML+'" href="http://www.sedo.de/checkdomainoffer.php?domain='+domain[i].innerHTML+'.'+tld[i].innerHTML+'&partnerid=40598'
					+'&language=us" title="Buy this domain !" alt="Buy this domain !">'
					+'<span class="_tldbg" id="ndd_'+tld[i].innerHTML+'">'+tld[i].innerHTML+'</span></a>');
					
					if(DomainFinder_verification == true)
					{
						$.post('p.domain.php',{'domain':domain[i].innerHTML,'tld':tld[i].innerHTML},function(data)
						{
							if(data)
								data = eval(data);
								
							if(data && data[0] && $("#ndd_"+data[0]))
							{
								if(data[1] == '1')//aucun domaine
								{
									$("#ndd_"+data[0]).colorBlend([{param:"color", fromColor:"grey", toColor:"green",duration:500, isFade:false, cycles:1}]);
								}
								else if(data[1] == '0')
								{
									$("#ndd_"+data[0]).colorBlend([{param:"color", fromColor:"grey", toColor:"red",duration:500, isFade:false, cycles:1}]);
								}
							}
						});
					}
					i++;
				});
				
				//Accordion
				more = $('.resDomainFinder ul');
				b=0;
				$('._ssd').each(function(){
					$(this).click(function(){
						if($(this).text().lastIndexOf('more') >= 0)
						{
							$(this).parent().next().show(100);
							$(this).text('less');
						}
						else
						{
							$(this).parent().next().hide(100);
							$(this).text('more...');
						}
						
					});
					b++;
				});
				
				
				//Si resultat >= 2
				if($('.resDomainFinder ul').length > 1)
				{
					$('fieldset:eq(2)').prepend('<a href="javascript:void(0);" class="_opt">expand all</a>');
					$('fieldset:eq(2) a._opt').click(function(){
						if($(this).text() == 'expand all')
						{
							$(this).text('reduce all');
							$('.resDomainFinder ul').show(500);
						}
						else
						{
							$(this).text('expand all');
							$('.resDomainFinder ul').hide(500);
						}
						
					});
					
					$('fieldset:eq(2)').prepend('<span class="_opt">Legend : domain <span style="color:#008000;">available</span> / <span style="color:#FF0000;">unavailable</span></span>');
				}
				
				}//fin détection IE
				else
				{
					msg($('.resDomainFinder'),'Use firefox or opera for a better user experience.',8000);
				}
				Fsub = $('fieldset:eq(1) p:eq(1) input[type=submit]');
				Fsub.show(500);
			break;
			
			case 'about':
				$('fieldset').hide();
				$('.resDomainFinder').hide();
				$('fieldset:eq(0)').show(500);
				$('fieldset:eq(1)').show(500);
				$('fieldset:eq(2)').show(500);
				window.setTimeout(function(){
				$('.resDomainFinder').show(500)
				},1000);
			break;
			
			case 'contact':
				Fsub = $('.btnsend');
				Fsub.hide();
				$('p.contact textarea').keyup(function(){
					Fsub.show(500);
				});
			break;
		}
	
}

function form_submit(url,data,callback)
{

}

function msg(where,what,hide)
{
	msg_id = new Date();
	msg_id = msg_id.getMilliseconds() + '' + msg_id.getSeconds() +''+ msg_id.getMinutes() +''+ msg_id.getHours();
	where.prepend('<div class="_msg" id="'+msg_id+'">'+what+'<div>');
	$('#'+msg_id).hide();
	$('#'+msg_id).click(function(){
		$(this).hide(250);
	});
	$('#'+msg_id).show(250);
	
	if(hide)
	{
		window.setTimeout(function(){$('#'+msg_id).hide(250);},hide);
	}
}

