var scriptAjax = null;

function init(){
	
	setInit()

	setEvent()
	
	
}

function setInit(){
	
	
	scriptAjax = domain+'/ajax.php'				
	
	
 
	
	//$('.boxContent').height(612);
	$.ajaxSetup({
					async:false,
					cache:true,
					type:'POST'
				})
	if($.browser.msie){
		
		$('.showMachine').css('margin-top','auto')
	}
				
	$('img.setLangue').click(hEvent)
				
	$('.articleContent',$('.boxThumbnail'))
					.jScrollPane({
															showArrows:true, 
															arrowSize: 15,
															scrollbarWidth: 14, 
															scrollbarMargin :5,
															wheelSpeed :18,
															animateTo:false, 															
															dragMinHeight:0,
															dragMaxHeight:99999,
															animateInterval:100,
															animateStep:3,
															maintainPosition:true,
															scrollbarOnLeft:false,
															reinitialiseOnImageLoad:true
														});	

	$('.zoomImg').zoomimage({
								border: 2,
								centered: true,
								hideSource: true,
								Opacity:0.5,
								duration:300,
								easing:'linear',
								controls:true,
								controlsTrigger:'mouseover',
								prevent:14,
								caption:true
								
							});


				
	$('.iconeA').addClass('iconeAOn')
	$('.titleA').addClass('titleAOn')
	showMachine('img/machine_1.jpg')
	setCarousel();
    setGoldBook();
    setContactForm(); 
    setRestoHeading()
    
}


function setRestoHeading(){
	$('.restoHeading').click(hEvent)
	$('.onglet-presse').each(
								function(){
									$(this).data('dataHref',$(this).attr('href'))
								}
							)
}


function setEvent(){
	$(window).bind('resize',hEvent)
	$('.carouselItem').click(hEvent)
	$('.carouselItem').hover(
		function(){
			var sParam = $(this).attr('jsParam')
			var tParam = sParam.split('|')
			showMachine(tParam[0])
		},
		function(){
					
		}		
	)
	$('.selector').click(hEvent)
	
	$('.restoThumbnail').hover(
		function(){
			src = $(this).attr('src')
			$(this).attr('src',src.replace(/-off/,'-on'))
		},
		function(){
			src = $(this).attr('src')
			$(this).attr('src',src.replace(/-on/,'-off'))			
		}
	)
	
	$('.selector').hover(
		function(){
			$('.iconeA').removeClass('iconeAOn')
			$('.iconeB').removeClass('iconeBOn')
			$('.iconeC').removeClass('iconeCOn')
			$('.iconeD').removeClass('iconeDOn')
			$('.title',$('.selector')).removeClass('titleAOn')
			$('.title',$('.selector')).removeClass('titleBOn')
			$('.title',$('.selector')).removeClass('titleCOn')
			$('.title',$('.selector')).removeClass('titleDOn')
			
			if($('.icone',$(this)).hasClass('iconeA')){
				$('.iconeA',$(this)).addClass('iconeAOn')
				$('.titleA',$(this)).addClass('titleAOn')
				showMachine('img/machine_1.jpg')
			}
			if($('.icone',$(this)).hasClass('iconeB')){
				$('.iconeB',$(this)).addClass('iconeBOn')
				$('.titleB',$(this)).addClass('titleBOn')
				showMachine('img/machine_2.jpg')
			}
			if($('.icone',$(this)).hasClass('iconeC')){
				$('.iconeC',$(this)).addClass('iconeCOn')
				$('.titleC',$(this)).addClass('titleCOn')
				showMachine('img/machine_3.jpg')
			}
			if($('.icone',$(this)).hasClass('iconeD')){
				$('.iconeD',$(this)).addClass('iconeDOn')
				$('.titleD',$(this)).addClass('titleDOn')
				showMachine('img/machine_4.jpg')
			}
		},
		function(){
			/*
			if($('.icone',$(this)).hasClass('iconeLeft')){
				$('.iconeLeft',$(this)).removeClass('iconeLeftOn')
			}
			if($('.icone',$(this)).hasClass('iconeRight')){
				$('.iconeRight',$(this)).removeClass('iconeRightOn')
			}
			*/			
		}	
	)
	
	$('.itemContainer').hover(
		function(){
			//itemCenterOn
			//alert($(this).html())
			$('.itemContent',$(this)).addClass('itemCenterOn')
		},
		function(){
			$('.itemContent',$(this)).removeClass('itemCenterOn')
		}	
	)
	$('.buttonToEnlight').hover(
		function(){
			var n = $('img',$(this)).attr('src')
			tn = n.split('_')
			$('img',$(this)).attr('src',tn[0]+'_on_'+tn[1])
		},
		function(){
			var n = $('img',$(this)).attr('src')
			$('img',$(this)).attr('src',n.replace(/_on/,''))
		}
	)
}

function showMachine(image){
	if(!$('.showMachine').size())return
	param = {
				action:'showMachine',
				subAction:'display',
				file:domain+'/'+image
			}
	
	$('.showMachine').fadeOut(250,function(){$('.showMachine').load(scriptAjax,param)})
	$('.showMachine').fadeIn(250)
	/*
	if($.browser.msie){
		
		$('.showMachine').css('margin-top','auto')
	}
	*/
}



function hEvent(e){
	var t = e.type,o = e.target;
	switch(t){
		case 'resize':
			window.location.href = window.location.href
			break;
		case 'click':
			
			if($(o).hasClass('restoHeading')){
				/*
				var url = $(o).attr('href')
				var tUrl = url.split('/')
				lUrl = tUrl[tUrl.length-1]
				$('.onglet-presse').each(
											function(){
												nUrl = $(this).data('dataHref')+'/'+lUrl
												//alert(nUrl+' '+this.href)
												this.href = nUrl
												
												alert(this.href)
											}
										)				
				*/
			}
		
			switch(o.id){
				case 'idSendBooking':
					sendBookingForm()
					return
					break
			}		
		
			if($(o).hasClass('carouselItem')){
				var sParam = $(this).attr('jsParam')
				var tParam = sParam.split('|')				
				window.location.href = tParam[1]
			}
			if($(o).hasClass('setLangue')){
				param = {
							action:'setLangue',
							subAction:'setLangue',
							langueID:$(o).attr('jqParam')
						}
				
				$('#debug').load(scriptAjax,param)	
				//alert(window.location.href)
				window.location.href = window.location.href
				return false;
			}
			
			break;
		case 'mouseenter':
		
		
			break;
		case 'mouseleave':
		
			break;
	}
}

function setCarousel(){
	var centerX = Math.floor($(window).width()/2)-40
	$(".carousel").html($(".imgCarousel").html())
	$(".carousel").carousel3d({ 
								control:'mouse',
								speed:1,
								radiusX:250,
								radiusY:70,
								centerX:centerX,
								centerY:570,
								perspective:60,
								padding:24,
								fadeEffect:0,
								textBox:0 
	
							}); 
	
}

function setGoldBook(){
    if(!$('.gbForm',$('#boxGoldBook')).length)return
    $.ajaxSetup({
                    async:false,
                    cache:false,
                    type:'POST'
                })    
    data = {
                'action':'setGDForm',
                'subAction':'dspGDForm'
           }            
    gbf =  $('.gbForm',$('#boxGoldBook'))
    
    $(gbf).load(scriptAjax,data)
    
    
    //$($('button'),$(gbf)).click(handleGBEvent)
	$(gbf).click(handleGBEvent)
}

function handleGBEvent(e){
    if(!$('.gbForm',$('#boxGoldBook')).length)return 
    
    t = e.type
    o = e.target
    
    gbf =  $('.gbForm',$('#boxGoldBook')) 
    
    switch(t){                                                                              
        case 'click':
        
            switch(o.id){
                case 'goldBookOn':
                     $('.zoneForm',$(gbf)).toggle()
                    break;
                case 'goldBookOff':
                     $('.zoneForm',$(gbf)).toggle()
                    break;
                case 'goldBookOk':
                    //alert('etape 1')
                    if(!checkForm($('form',$(gbf))))return
                    data = $('form',$(gbf)).serializeArray()
                    $('.gbDebug').load( scriptAjax, data )
                    //alert('etape 2')
                    data = {'action':'setGDForm','subAction':'setGDRecordData'}
                    $('.gbDebug').load( scriptAjax, data )
                    window.location.href = window.location.href
                    break;
            }
            break;    
    }
}

function checkForm(oF){
    te = 0
    reMail = /^[a-zA-Z0-9\.\-_]+@[a-zA-Z0-9]+\.[a-zA-Z]{2,5}$/i;    
    mail = ''
    $('input.required,textarea',$(oF)).each(
        function(){
             //if(this.name=='fldEmail')mail=this.value
             if(!this.value.length){
                te = 1
                check=false;   
             }
        }    
    )
    
    /*if(!mail.match(reMail)){
        te = 2   
    }*/
    
    switch(te){
        case 1:
            alert('Renseignez les champs obligatoires, SVP !')
            break;
        case 2:
            alert('L\'e-mail est incorrect !')   
            break;
    }
    
    return !te;
}

function setContactForm(){
	if(!$('#boxContactForm').length)return
	form = $('#boxContactForm')
	data = {
				'action':'setForm',
				'subAction':'loadContact'
	}
	
	$(form).load(scriptAjax,data)
	
	$('#idSendBooking').click(hEvent)
	
}
function sendBookingForm(){
	checked = false
	restaurant = "Vous devez s\351lectionner un restaurant !\n"
	champs_obligatoire = ""
	$('.radio').each(
		function(){
					$(this).css({'backgroundColor':'white','color':'black'})
					if (this.checked) {
						checked = true
						restaurant = ""
					}
				}
		)
	$('.required').each(
		function(){
					$(this).css({'backgroundColor':'white','color':'black'})
					state = (!this.value.length)?false:checked
					if(!state){
						$(this).css({'backgroundColor':'#FFA500','color':'white'})	
						checked = false
						champs_obligatoire = "La saisie des champs orange est obligatoire !"
					}
				}
		)
		
	if(!checked){
		alert('Attention :\n'+restaurant+''+champs_obligatoire)
		return
	}
	
	data = $('.data, .radio').serializeArray()
	
	$('.debugBooking').load(scriptAjax,data)
	
	data = {
				'action':'setSending',
				'subAction':'sendMail'
	}
	$('.debugBooking').load(scriptAjax,data)	

	//if(flagSending){
		//$('#boxContactForm').html('<div style="color:white;font-size:12px;width:350px;padding:10px 0px 10px 0px">Votre demande de r&eacute;servation nous a bien &eacute;t&eacute; transmise.</div>')
	//}	
}

