$(document).ready(function(){
	var deviceAgent = navigator.userAgent.toLowerCase();
    var agentID = deviceAgent.match(/(iphone|ipod|ipad|android)/);	
	 if (agentID) {
        var smartp_names = ['android', 'iphone'];
        var smartp_links_fr = ['http://market.android.com/details?id=fr.telemaque.horoscope', 'http://itunes.apple.com/fr/app/horoscope/id329504506?mt=8'];
		var smartp_links_en = ['http://market.android.com/details?id=fr.telemaque.horoscope', 'http://itunes.apple.com/us/app/my-ihoroscope/id329504506?mt=8'];
		var smartp_name = null;
        var _name = null;
		
		
        if (deviceAgent.match(/android/)) { var _name = 'android'; var smartp_name = 'smartphone'; }
		else {
			var _name = 'iphone';
			if (deviceAgent.match(/iphone/)) { var smartp_name = 'iPhone'; }
        	if (deviceAgent.match(/ipod/)) {  var smartp_name = 'iPod'; }
        	if (deviceAgent.match(/ipad/)) { var smartp_name = 'iPad'; }	
			
		}
		var lgg = $("#nmlang").val();
		
		if(lgg=='fr'){
		var message = 'Consultez gratuitement toutes vos prévisions astro sur votre ' + smartp_name + ' !';
			if ($.cookie('hfr_smartp_link_cookie') == null  && confirm(message)) {
				document.location.href = (smartp_links_fr[$.inArray(_name, smartp_names)]);
				}
		$.cookie('hfr_smartp_link_cookie', true, {path: '/', expires: 7}); 
		}
		else{
		var message = 'Consult all your free astro predictions on your ' + smartp_name + ' !';
				if ( $.cookie('hen_smartp_link_cookie') == null && confirm(message)) {
				document.location.href = (smartp_links_en[$.inArray(_name, smartp_names)]);
				}
			$.cookie('hen_smartp_link_cookie', true, {path: '/', expires: 7});  
		}
	 }
	bodyHeight = 1785;
	bodyWidth = $('body').width();
	myWidth = 423;
	newLeft = (bodyWidth - myWidth)/2;
	$("#i18n_menu").buttonset();
	$("#i18n_menu").show();
	$("#i18n_menu input").click(function(){
		$(location).attr('href',ROOT+$(this).val()) ;	
	});	
	$(".connect_form input:submit").button();
	var lock_coonect_submit = false; 
	$('.connect_form').submit(function(e){
		if(lock_coonect_submit) return false;
		lock_coonect_submit=true;
		var url = $(this).attr('action');
		var data= $(this).serialize();
		$.getJSON(url,data,function callback(json){				
			if(json.is_connected){
				var url=$('.connect_form').attr('call_back');
				$(location).attr('href',url) ;					
			} else {
				$(".top_menu #err_msg").html(json.error);
				$(".top_menu #err_msg").show(200);				
				lock_coonect_submit = false;
				$(document).trigger('passwordMayBeForgotton');
			}									 
		});
		
		return false;
	});	
	$('.top_menu .connect_form input:not(.submit_btn)').focus(function(){
		$(this).css('opacity','1');												  
	});
	$('.top_menu .connect_form input:not(.submit_btn)').blur(function(){
		$(this).css('opacity','0.6');												  
	});
	var pLeft  = $('.top_menu .connect_form_cont a').width();
	var pWidth = $('.top_menu .connect_form').width();
	$(".top_menu #err_msg").css({'left':pLeft,'width':pWidth-20});
	
	$('body').click(function() {
	 	$(".top_menu #err_msg").hide(300);
	 });
	
	 $(".top_menu #err_msg").click(function(event){
		 event.stopPropagation();
	 });
	 
	 
	 
});
var bodyHeight = 0;
var bodyWidth = 0;
var myWidth = 0;
var newLeft = 0;
function showForm(){
	$('#checkYourMail').fadeOut(0);
	$('#fogotchePass, #checkYourMail').animate({'left':newLeft+'px'});
		$('#overlayPAssword').animate({width:bodyWidth+'px'},0,function(){
		$(this).animate({height:bodyHeight+'px'});
	});
	$('.popupPassword').css('visibility','visible');
	$(document).trigger('fogotchePassLoaded');
	$('#validateFogotche').click(function(){
		var mail = $('#theInputEA').attr('value');
		var pass = $('#theInputNewP').attr('value');
		if(verif(mail) && pass.length !=0){
			var url = '/member/password';
			var data = 'email='+$('#theInputEA').attr('value')+'&passwd='+$('#theInputNewP').attr('value');
			$.getJSON(url,data,function callback(json){
				
				if(!json['valid']){
					var goRegister=$('#fogotchePasswordForm').attr('action');
					$(location).attr('href',goRegister) ;					
				} else {
					$('#fogotchePass').css('visibility','hidden');
					$('#checkYourMail').fadeIn(1000,function(){
						setTimeout(function(){
							
							$('#checkYourMail').fadeOut(2000,function(){
								hideForm();
							});
						},4000);
					});
				}						 
			});
		}
	});
}
$(document).bind('fogotchePassLoaded',function(){
	$('#closeFP').click(function(e){
		hideForm();
	});
	$('#overlayPAssword').click(function(e){
		hideForm();
	});
	$('#cancelFogotche').click(function(e){
		hideForm();
	});
});
function hideForm(){
	$('.popupPassword').css('visibility','hidden');
}
function verif(email) { 
   var reg = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,4}$/;
   return (reg.exec(email)!=null)
}
$(document).bind('passwordMayBeForgotton',function(){
	$('a#resendPassword').click(function(e){
		e.preventDefault();
		showForm();
	});
});
