$(document).ready(function() {
	

	if(navigator.platform == 'iPad' )
		$("#bottomLog").css("position", "static");


	// home )page form submit
	$( '#homepageSubmitButton' ).click(
	function()
	{
		$('#surveyForm').submit();
	}
	);
	
	// Comments toggle
    $( '#showLatestComments' ).click(
	function()
	{
		$('#commentsToggle').slideToggle("fast");
	}
	);
	
	// Downloads toggle
    $( '#see_all_dl' ).click(
	function()
	{
		$('.remaining').slideToggle("fast");
	}
	);
	
	// profile selector
	

	$('#profil').jqSelectBox("Sélectionnez votre profil");


	// Jqmodal stuff

    // $('#trafficDialog').jqm();
    $('#airportDialog').jqm({toTop: false});
    $('#bourseDialog').jqm();
	$('#newsletter_subscription').jqm();
    
    
    /*$('#trafficLink').click(function(){
        $('#trafficDialog').jqmShow();
    });*/
    
    $('#bourseLink').click(function(){
        $('#bourseDialog').jqmShow();
    });
    
    $('#airportLink').click(function(){
        $('#airportDialog').jqmShow();
    });
	
	
	// hover stuff for icons
	$( 'a.hover_icon' ).hover(
	function()
	{
		//$('#eservices_helper').css('color',$(this).css('color'));
		$('#eservices_helper em').html($(this).attr('title'));
	}
	,
	function()
	{
		//$('#eservices_helper').css('color','#000000');
		$('#eservices_helper em').html('');
	}
	);


	// Blur stuff for search box
	$('#SearchText').focus(function() {
		if($(this).val() == 'Votre recherche...')
	  		$(this).val('');
	});
	
	$('#SearchText').blur(function() {
		if($(this).val() == '')
	  		$(this).val('Votre recherche...');
	});
	
	// faq stuff
	$('.faq_question').click(function() {
		$(this).siblings('.faq_reponse').filter(":first").toggle();
	});
	
	if($('#profileFaqsButton')!=undefined)
	{
		$('#profileFaqs').jqm();	
		
		$('#profileFaqsButton').click(function() {
			$('#profileFaqs').jqmShow();
		});
	}
	
	if($('#profileContactsButton')!=undefined)
	{
		$('#profileContacts').jqm();	
		
		$('#profileContactsButton').click(function() {
			$('#profileContacts').jqmShow();
		});
	}
	
	$('#profil').change(function(){
		$(location).attr('href','/profile/set/'+$(this).val());
	});
	
	$('a.email').each(function()
	{
		var link = $(this).attr('href');
		var text = $(this).html();
		
		link = link.replace(/\(at\)/g,'@');
		link = link.replace(/\(dot\)/g,'.');
		
		text = text.replace(/\(at\)/g,'@');
		text = text.replace(/\(dot\)/g,'.');
		
		$(this).attr('href', link);
		$(this).html( text);
	});
	
	$('#newsletterSubscriptionButton').click(function() {
			$('#newsletter_subscription').jqmShow();
		});
		
	
	// Font size stuff
	var COOKIE_NAME = 'ccib_style';
	var options = { path: '/', expires: 10 };
	var currentContrast = 'no';
	var currentFontSize = '11';
	
	if($.cookie(COOKIE_NAME)!=null)
	{
		var cookieValue=$.cookie(COOKIE_NAME);
		
		values=cookieValue.split('|');
		
		
		currentFontSize=values[0];
		currentContrast=values[1];
		
		
		$('body').css('font-size',currentFontSize+'px');
		
		if(currentContrast=='yes')
			increaseContrast();
		
		updateDisplay(currentFontSize,currentContrast);
	}
		

	
	function increaseFontSize()
	{
	
		var fontsize = $('body').css('font-size');
		fontsize=fontsize.substring(0,2);
		fontsize++;
		currentFontSize=fontsize;
		$('body').css('font-size',fontsize+'px');
		updateDisplay(currentFontSize,currentContrast);
		$.cookie(COOKIE_NAME,fontsize+'|'+currentContrast,options);
	
	}
	
	function decreaseFontSize()
	{
		var fontsize = $('body').css('font-size');
		fontsize=fontsize.substring(0,2);
		fontsize--;
		currentFontSize=fontsize;
		$('body').css('font-size',fontsize+'px');
		updateDisplay(currentFontSize,currentContrast);
		$.cookie(COOKIE_NAME,fontsize+'|'+currentContrast,options);
		
	}
	
	
	
	function updateDisplay(size,contrast)
	{
		if(size<=11)
		{
			$('#decrease').hide();
		}
		else if (size>=15)
		{
			$('#increase').hide();
		}
		else
		{
			$('#decrease').show();
			$('#increase').show();
		}
		
		if(contrast=='yes')
		{
			$('#contrast_inc').hide();
			$('#contrast_dec').show();
		}
		else
		{
			$('#contrast_inc').show();
			$('#contrast_dec').hide();
		}
			
	}
	
	$('#increase').click(function(){
		increaseFontSize();
	});
	
	$('#decrease').click(function(){
		decreaseFontSize();
	});
	
	// Contrast stuff
	
	function increaseContrast()
	{
		
		$('body,html').css('background-image','none');
		$('body,html,.jqmWindow').css('background-color','#222222');
		$('body,.boxTitle,#leftMenu>ul>li>a,.jqmWindow').css('color','#fff');
		$('#header').css('background-image',"url('/extension/cci/design/cci/images/headerBG-contrast.jpg')");
		$('#topMenu').css('background-image',"url('/extension/cci/design/cci/images/menuBG-contrast.png')");
		currentContrast='yes';
		updateDisplay(currentFontSize,currentContrast);
		$.cookie(COOKIE_NAME,currentFontSize+'|'+currentContrast,options);
	}
	
	function decreaseContrast()
	{
		$('body,html').css('background-color','#F7F7F7');
		$('body,html').css('background-image',"url('/extension/cci/design/cci/images/mainBG.jpg')");
		$('.jqmWindow').css('background-color','#EEEEEE');
		$('body,.boxTitle,#leftMenu>ul>li>a,.jqmWindow').css('color','#000');
		$('#header').css('background-image',"url('/extension/cci/design/cci/images/headerBG.jpg')");
		$('#topMenu').css('background-image',"url('/extension/cci/design/cci/images/menuBG.png')");
		currentContrast='no';
		updateDisplay(currentFontSize,currentContrast);
		$.cookie(COOKIE_NAME,currentFontSize+'|'+currentContrast,options);
	}
	
	$('#contrast_inc').click(function(){
		increaseContrast();
	});
	
	$('#contrast_dec').click(function(){
		decreaseContrast();
	});
	
	updateDisplay(currentFontSize,currentContrast);
	
	/*
	 * FS - MAIL SHARING
	 */
	// check address
	$('#forward_mail').live('keyup', function(){
		var reg = new RegExp('^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*@[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*[\.]{1}[a-z]{2,6}$', 'i');
		var mailtest = $('#forward_mail').val();
		if(reg.test(mailtest)){
			$('#add_mail').css('cursor', 'pointer');
			$('#add_mail img').attr('src', '/extension/cci/design/ccicom/images/icons/plus.png');
		} else{
			$('#add_mail').css('cursor', 'default');
			$('#add_mail img').attr('src', '/extension/cci/design/ccicom/images/icons/plus_desactive.png');
		}
	})
	// add address in the list
	$('#add_mail').live('click', function(){
		var count_div = $('#recup_mail').find('.add_mail').length;
		if(count_div < 5){
			if($('#add_mail').css('cursor')=='pointer'){
				var mail = $('#forward_mail').val();
				var deleteImg = '<img style="vertical-align: middle" src="/extension/socnet/design/standard/images/delete.png" alt="X" class="delete_mail" />';
				var inputHidden = '<input type="hidden" value="'+mail+'" name="emails[]" />';
				$('#recup_mail').append('<div class="add_mail">'+deleteImg+'&nbsp;'+mail+inputHidden+'</div>');
				$('.delete_mail').css('cursor', 'pointer');
				$('#forward_mail').val('');
				$('#add_mail').css('cursor', 'default');
				$('#add_mail img').attr('src', '/extension/cci/design/ccicom/images/icons/plus_desactive.png');
			}
		} else{
			alert('Vous ne pouvez pas ajouter plus de cinq adresses.');
		}
	})
	// delete an address in the list
	$('.delete_mail').live('click', function(){
		$(this).parent().remove();
	})
	// send the mail
	$('#send_mail').live('click', function(){
		var listMails = $('#recup_mail :input').serialize();
		var sendData = {text: $('#forward_text').text(), community_object_id: $('#community_object_id').val()};
		$.post(	
			'/socnet/cci_partage_link', 
			{	
				emails:listMails, 
				text:$('#forward_text').val(),
				name:$('#forward_name').val(),
				community_object_id:$('#community_object_id').val()
			}, 
			function(data){
				if(data.result == 'ok'){
					$('#updateprofileimage').text('');
					$('#updateprofileimage').append('<strong>Le mail a bien été envoyé.</strong>');
				} else{
					$('#updateprofileimage').text('');
					$('#updateprofileimage').append('<strong>Echec de l\'envoie.</strong><br />'+data.result);
				}
			}, 
			'json'
		)
	})
	
	// Comment popup
  	$('.ccicom_dialog').live('click',function(){
  		var linkAJAX = $(this).attr('href');
  		$('#ccidialog').jqm({ajax: linkAJAX});
		$('#ccidialog').jqmShow();
	});
	
});

