var ie = false;
if (navigator.appName == "Microsoft Internet Explorer") {
	ie = true;
}
var video_pre_spot;
var video_pre_spot_link;
function ouvrir2(what){
	window.open(what, "ouverture", "toolbar=no, directories=no, status=no, scrollbars=yes, titlebar=no, resizable=no, menubar=no, location=no, personalbar=no, width=900, height=820");
}

/*function ouvrir(what){
	window.open(what, "ouverture", "toolbar=no, status=yes, scrollbars=yes, resizable=no, width=520, height=550");
}



window.addEvent('bodyresize',function(e){
	try{
	    if (window.parent.window.document.getElement('#compte_iframe')){
	        y = $('site').getSize().y+100;
	        window.parent.window.document.getElement('#compte_iframe').setStyle('height', y);
	    }
	}catch(e){}
});

window.addEvent('load', function(){
	window.fireEvent('bodyresize');
});

*/
//Flash pro
function change_date_flash(){
    if ($('flash_pro')){
        //$('flash_pro').addEvent('click', function(){
            new Request.HTML({
                url: '/reponse_flash_pro.ajax',
                data: 'loc_id='+$('flash_pro').get('value'),
                /*onComplete: function(ret){
            		alert('bordèle à cul');
                    $('flash_pro_dates').set('html', ret);
                }*/
            	update: $('flash_pro_dates')
            }).send();
        //});
    }
}

// Options particulier
function options_menu(button_class, div_ids){
    $$('.'+button_class).each(function(item){
        item.addEvent('click', function(e){
            //var e = new Event(e);
            e.stop();
            
            if (item.retrieve('affiche')){
                $(div_ids+'_'+item.get('id')).setStyle('display', 'none');
                item.store('affiche', false);
            }else{
                $(div_ids+'_'+item.get('id')).setStyle('display', 'block');
                item.store('affiche', true);
            }
            window.fireEvent('bodyresize');
        });
    });
}
var pre_spot;

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

    var Tips1 = new Tips($$('img.tooltip'), {fixed:true,offsets:{'x':15, 'y':-5}});
  	if($('actualisation')) new Tips($('actualisation'), {text:""});
  	// if($('logout_button')) new Tips($('logout_button'), {text:""});
	$(document.body).setStyle('background','#e4e4e4 url(http://www.7enlocation.com/style/fond.jpg) no-repeat scroll 50% 0');
       
	// $$('label').each(function(item){
	// 	item.addEvent('click',function(){
	// 		var forItem = item.get('for');
	// 		$(forItem).click();
	// 	});
	// });
	
    //y = (document.documentElement && document.documentElement.scrollHeight) ? document.documentElement.scrollHeight :(document.body.scrollHeight > document.body.offsetHeight) ? document.body.scrollHeight : document.body.offsetHeight;
	
    // window.fireEvent('bodyresize');
    // try{
    // 	window.parent.window.scrollTo(0,0);
    // }catch(ex){}
    //Cookie.write('compte_height', y, {domain: '7enlocation.com.lan', duration:1, 'path':'/'});
/*
    
    switch(Browser.Engine.name){
        case 'gecko' :
            if (Browser.Engine.version < 19){
                var browser_to_install = '<a href="http://www.google.com/chrome" alt="Téléchargez Chrome">Chrome</a>';
            }
        break;
        default :
            var browser_to_install = '<a href="http://www.google.com/chrome" alt="Téléchargez Chrome">Chrome</a>';
        break;
    }
    
    if (browser_to_install  && 1==2){
	try {
        //Vous utilisez actuellement le navigateur IE 6. Pour un affichage optimal de 7enlocation, nous vous conseillons de télécharger IE 7
        var message = 'Pour un affichage optimal de 7enlocation, nous vous conseillons de télécharger et d\'utiliser '+browser_to_install+'.';
        var alert_navigateur = new Element('div', {'class':'alert_navigateur'});
        alert_navigateur.set('html', message);
        alert_navigateur.inject($("header"), 'bottom');
	}
	catch (err) {
		
	}
    }
    */

    
    
    
    options_menu('option_button', 'option')
    
    
    
    
    
   // home_not_logged
   //au depart c'est le bouton radio des particuliers qui est checké =>
   //si le radio est affiche => on affiche pas le formulaire professionnel
   if  ($('insc_professionnel') && $('insc_particulier')) {
		   $('insc_professionnel').setStyle('display','none');
   }
   
   
   // si il n'y a que le formulaire professionnel
   if ($('insc_professionnel') && !$('insc_particulier')){
	   	$('pack_image').setStyle('background-image','url(/style/notlogged/packpro.png)');
   }
    
    
   
   $$('input[name=part_pro]').each(function(item){
    	
    	item.addEvent('click', function(){
        	if (item.checked){
                switch (item.value){
                    case 'part':
						// if ($('pack_image')) $('pack_image').setStyle('background-image','url(/style/notlogged/pack.png)');
                    	// $('infos_pack_image').setStyle('background-image','url(/style/particulier/description_part-l.png)');
                        $('insc_particulier').setStyle('display','block');
                        $('insc_professionnel').setStyle('display','none');
                        break;
                    case 'pro':
                    	// if ($('pack_image')) $('pack_image').setStyle('background-image','url(/style/notlogged/packpro.png)');
                    	// $('infos_pack_image').setStyle('background-image','url(/style/pro/description_pro-l.png)');
                        $('insc_particulier').setStyle('display','none');
                        $('insc_professionnel').setStyle('display','block');
                        break;
                }
            }
        });
    });

   
   if ($('insc_particulier')){
	   $('insc_particulier').addEvent('submit', function(){
		   $$('input.depot_annonce').each(function (item){
			   item.setStyle('display','none');
			   item.getParent().set('html','Veuillez patienter...');
		   });
	   });
   }

	if ($$('input.input_submit_option')) {
		$$('form.c_form_option').each(function(item) {
			addAlertOption(item);
		});
	}

	if ($$('a.facture_annuleContent')) {
		$$('a.facture_annuleContent').each(function(item) {
			item.addEvent('click', function(e) {
				(new Event(e)).stop();
				var msgConfirm = "";
				if (this.get('type') == 1) {
					msgConfirm = "Confirmez-vous l'annulation ?<br><span class='attention'>Important :</span> Les options dépendant de cet <b>abonnement</b> seront également annulées";
				} else if (this.get('type') == 3) {
					msgConfirm = "Confirmez-vous l'annulation ?<br><span class='attention'>Important :</span> Il est impossible de profiter de cette réduction ultérieurement.";
				} else if (this.get('type') == 5) {
					msgConfirm = "Confirmez-vous l'annulation de ce coupon ?";
				} else {
					msgConfirm = "Confirmez-vous l'annulation ?";
				}
				apprise(msgConfirm, {'confirm':true}, function(r)
				{
					if(r) {
						document.location = item.get('href');
					}
				});
			})
		});
	}
	if ($('coupon_btn')) {
		var coupon_btn = $('coupon_btn');
		coupon_btn.addEvent('click', function(e){
            e=new Event(e);
    		e.stop();
            if ($('coupon_text').value.length > 0) {
                var datas = 'fac_id=' + coupon_btn.get('fac_id') + '&coupon=' + $('coupon_text').value;
                var ajax = new Request({
    				url: '/reponse_fac_coupon.ajax',
                    method: 'post',
    				data: datas,
    				onComplete: function(responseText){
                        var rep = JSON.decode(responseText);
    					if (rep['status'] == 'error') {
                            apprise(rep['msg']);
                        }
                        else {
                            if (rep['status'] == 'ok') {
                                apprise(rep['msg'],null,function (r) {window.location.reload()});
                            }
                            else {
                                if (rep['status'] == 'gratuit') {
                                    apprise(rep['msg']);
                                    window.location.href='/';
                                }
                                else {
                                    apprise('Les coupons sont momentanément indisponibles');
                                }
                            }
                        }
    				}
    			});
            ajax.send();
            }
            else {
                apprise('Veuillez saisir votre coupon !');
            }
        });
	}
	
	if ($('offert_btn')) {
		var offert_btn = $('offert_btn');
		offert_btn.addEvent('click', function(e){
            e=new Event(e);
    		e.stop();
            if ($('offert_text').value.length > 0) {
                var datas = 'fac_id=' + offert_btn.get('fac_id') + '&libelle=' + $('offert_text').value;
                var ajax = new Request({
    				url: '/reponse_fac_offert.ajax',
                    method: 'post',
    				data: datas,
    				onComplete: function(responseText){
                        var rep = JSON.decode(responseText);
    					if (rep['status'] == 'error') {
                            apprise(rep['msg']);
                        }
						else window.location.reload();
    				}
    			});
            ajax.send();
            }
            else {
                apprise('Veuillez saisir un libellé !');
            }
        });
	}
	
	if ($('complement_btn')) {
		var complement_btn = $('complement_btn');
		complement_btn.addEvent('click', function(e) {
			(new Event(e)).stop();
			apprise('Valeur du complément TTC :',{'input':'0'},function (ret_prix) {
				if (ret_prix) {
					var datas = 'fac_id=' + complement_btn.get('fac_id') + '&libelle=' + $('complement_text').value + '&prix='+ret_prix;
					new Request({
	    				url: '/reponse_fac_comp.ajax',
	                    method: 'post',
	    				data: datas,
	    				onComplete: function(responseText){
	                        var rep = JSON.decode(responseText);
	    					if (rep['status'] == 'error') {
	                            apprise(rep['msg']);
	                        }
							else window.location.reload();
	    				}
	    			}).send();
				}
			});
		});
	}
	
	if ($('nb_loc')) {
		var nb_loc = $('nb_loc');
		var nb_loc_ok = $('nb_loc_ok');
		$$('input[type=checkbox]').addEvent('change', function() {
			if (Number.from(nb_loc.get('html')) < Number.from(nb_loc_ok.get('html')) || !this.checked) {
				if(this.checked) {
					this.getParent('div').getChildren('img').set('src','/style/communs/mail_ok.png');
					nb_loc.set('html',Number.from(nb_loc.get('html'))+1);
				} else {
					this.getParent('div').getChildren('img').set('src','/style/communs/mail_restreint.png');
					nb_loc.set('html',Number.from(nb_loc.get('html'))-1);
				}
			} else {
				apprise("Maximum d'annonce atteind");
				this.checked = false;
			}
		});
	}
	
	if ($('h_toggle_ch')) {
		$('h_toggle_ch').addEvent('click', function(r) {
			$('horizontal_slide_ch').removeClass('desactive');
		});
		if ($('h_toggle')) {
			$('h_toggle').addEvent('click', function(r) {
				$('horizontal_slide').removeClass('desactive');
			});
		}
	}
	
	// if ($('masquer_header')) {
	// 	$('masquer_header').addEvent('click',function(e) {
	// 		(new Event(e)).stop();
	// 		$('fond_header').hide();
	// 	})
	// }
	video_pre_spot = $('video_pre_spot');
	video_pre_spot_link = $('video_pre_spot_link');
	if (video_pre_spot) {
		pre_spot = new Swiff('/style/player.swf', {
		    id: 'pre_spot',
		    width: video_pre_spot.get('width'),
		    height: video_pre_spot.get('height'),
			container :video_pre_spot,
		    params: {
		        wMode: "transparent"
		    },
		    vars: {
		        src: "/style/spot/prespot.flv",autostart: "true",ei_prefix: "pre_spot_"
		    }
		});
		if (ie) {
			video_pre_spot.setStyle('opacity','1');
		} else {
			video_pre_spot.set('tween', {duration: '2000'});
			video_pre_spot.tween('opacity','1');
		}
		video_pre_spot_link.addEvent('click',function (r) {
			if (ie) {
				video_pre_spot.setStyle('opacity','0');
			} else {
				video_pre_spot.set('tween', {duration: '800'});
				video_pre_spot.tween('opacity','0');
			}
			activ_video_rubrique();
		});
	}

	function activ_video_rubrique() {
		var video_rubrique = $('video_rubrique');
		var video = $('video');
		var start_auto = video.get('debug');
		new Swiff('/style/player.swf', {
		    id: 'video_rub',
		    width: video.get('width'),
		    height: video.get('height'),
			container :video,
		    params: {
		        wMode: "transparent"
		    },
		    vars: {
		        src: "/style/spot/"+video.get('name'),autostart: "true", controls:start_auto,ei_prefix: "video_rub_"
		    }
		});
		video_rubrique.setStyle('display','block').setStyle('opacity','0');
		if (ie) {
			video_rubrique.setStyle('opacity','1');
		} else {
			video_rubrique.set('tween', {duration: '3000'});
			video_rubrique.tween('opacity','1');
		}
		$('video_close').addEvent('click',function(r){
			video_rub_onComplete();
		});
	}
});

function video_rub_onComplete() {
	$('video_rubrique').setStyle('display','none').setStyle('opacity','0');
	$('video').set('html','');
	
	video_pre_spot.setStyle('display','block');
	if (ie) {
		video_pre_spot.setStyle('opacity','1');
	} else {
		video_pre_spot.setStyle('opacity','0').tween('opacity','1');
	}
	
}
// 
// function intro_compte_onComplete() {
// 	$('video_intro_wrapper').setStyle('display','none').setStyle('opacity','0');
// 	$('video_pre_spot').setStyle('display','block').setStyle('opacity','0').tween('opacity','1');
// }


//Function add apprise sur les boutons options
function addAlertOption(item) {
	item.addEvent('submit', function(e) {
		var e = new Event(e).stop();
		// alert($$('location_form_flash select')[0].getSelected());
		var departDate = item.getElements('#selectDate')[0].getSelected()[0].get('text');
		var displayName = item.get('displayname');
		apprise("Confirmez-vous l'achat de cette option ?<br>"+
		"Type : "+displayName+"<br>"+
		"Date de départ : "+departDate, {'confirm':true}, function(r)
		{
			if(r) {
				item.submit();
			}
		});
	})
}
