// JavaScript Document
var isIE6 = false;
$('html').attr('id', 'js');

//js google analytics popin
function gAnalytic(name) {
	if(name!=null || name==''){
		pageTracker._trackPageview('/temoignage/{'+name+'}');
	}
}

// Mise à jour des pays
function updatepays(page, npays) {
  var page = 'http://www.nespresso-pro.com/ooh/home_fr_fr.html';
  if(npays == '0') {
	 return;
	}
	 document.location.href = page + '?npays=' + npays;
}

// cookie Manager
var cookieManage = {
	get:function(name){
		var cookieValue = "";
		var search = name + "=";
		if(document.cookie.length > 0) { 
			offset = document.cookie.indexOf(search);
			if (offset != -1) { 
				offset += search.length;
				end = document.cookie.indexOf(";", offset);
				if (end == -1) {
				    end = document.cookie.length;
				 }
				cookieValue = unescape(document.cookie.substring(offset, end))
			}
		}
		return cookieValue;
	},
	set:function(name, value, days){
		var expire = "";
		if(days != null) {
			expire = new Date((new Date()).getTime() + days * 86400000);
			expire = "; expires=" + expire.toGMTString();
		}
		document.cookie = name + "=" + escape(value) + expire;
	},
	boolean:function(name){
		var test = false;
		var search = name + "=";
		if(document.cookie.length > 0) { 
			offset = document.cookie.indexOf(search);
			if (offset != -1) {
				test = true;
			}
		}
		return test;
	}
};

//to manage and show testimonial bloc
var testiManage = {
	init:function(){
		if(cookieManage.boolean('nestesti-fr')){
			var newAnswer ='';
			var answer = cookieManage.get('nestesti-fr');
			if(answer=='testi2'){
				newAnswer = 'testi0';				
			} else {
				newAnswer = $('#'+answer).next().attr('id');
			}
			$('#'+answer).show();
			cookieManage.set("nestesti-fr", newAnswer, 1);
		} else {
			$('#testi0').show();
			cookieManage.set("nestesti-fr", "testi2", 1);
		}
		
	}
};

/*
//Alterner les deux pushs Flash : Ristretto et Singobata
var flashPushes = {
    init:function() {
        if(cookieManage.boolean('flashPushes-fr')){
            var newAnswer ='';
			var answer = cookieManage.get('flashPushes-fr');
			if( answer == 'singatoba' ) {
				newAnswer = 'ristrettoIndia';				
			} else {
				newAnswer = 'singatoba';
			}
			$('#'+answer).show();
			cookieManage.set("flashPushes-fr", newAnswer, 1);
		} else {
			$('#ristrettoIndia').show();
			cookieManage.set("flashPushes-fr", "singatoba", 1);
        }
    }
};
*/
var flashPushes = {
    init:function() {
        $('#singatoba').show();
    }
};

// js select home product machines/cafes
var bgdOverProduct = {
		init:function(){
			$('.products dd').hover(function(){
				$(this).addClass('over');
			},function(){
				$(this).removeClass('over');
			});
			
			$('.products dd').click(function(){
				window.location = $(this).children('a').attr('href');
			});
		}
    };
//end select home product machines/cafes

//js testimonial texte
var menuThema = {
	init:function(){
		$('#menuThema li a').click(function(){
			menuThema.changeDiv($(this).attr('href'));
			menuThema.changeMenu($(this));
			return false;
		});
		$('#testimonial ul.nav li.prev').click(function(){
			menuThema.prevQuestion();
			return false;
		});
		$('#testimonial ul.nav li.next').click(function(){
			menuThema.nextQuestion();
			return false;
		});
	},
	changeDiv:function(a){
		var id = a.substring(a.indexOf('#'),a.length);
		$('.contentTesti').hide();
		$('#testimonial '+id).show();
	},
	changeMenu:function(e){
		$('#menuThema li').removeClass('selected');
		e.parent().addClass('selected');
	},
	nextQuestion:function(){
		$('#testimonial .contentTesti').hide();
		if($('#menuThema li:last').hasClass('selected')){
			$('#menuThema li').removeClass('selected');
			$('#menuThema li:first').addClass('selected');
			$('#testimonial .contentTesti:first').show();
		}	else {
			$('#menuThema li.selected').removeClass('selected').next().addClass('selected');
			var target = $('#testimonial li.selected a').attr('href');
			var id = target.substring(target.indexOf('#'),target.length);
			$('#testimonial '+id).show();
		}
	},
	prevQuestion:function(){
		$('#testimonial .contentTesti').hide();
		if($('#menuThema li:first').hasClass('selected')){
			$('#menuThema li').removeClass('selected');
			$('#menuThema li:last').addClass('selected');
			$('#testimonial .contentTesti:last').show();
		}	else {
			$('#menuThema li.selected').removeClass('selected').prev().addClass('selected');
			var target = $('#testimonial li.selected a').attr('href');
			var id = target.substring(target.indexOf('#'),target.length);
			$('#testimonial '+id).show();
		}
	}
};

var expertnessList = {
	init:function(){
		$('ul.linksSite li a').click(
			function(){
				expertnessList.change($(this).attr("href"),$(this));
				return false;
			}
		);
	},
	change:function(ancre,a){
		$('div#content div.on').removeClass('on').addClass('off');
		$('div#content div'+ancre).removeClass('off').addClass('on');
		$('ul.linksSite li a').removeClass('selected');
		a.addClass('selected');
	}
};
//end js testimonial texte

var selectDl = {
	init:function(){
		$('div.accounts dl dt').click(function(){
			if($(this).parent().hasClass('open')){
				return false;
			} else {
				if($(this).hasClass('on')){
					$(this).removeClass('on');
					$(this).next('dd').slideUp('slow');
				} else {
					$('div.accounts dl dt').removeClass('on');
					$('div.accounts dl dd').slideUp('slow');
					$(this).addClass('on');
					$(this).next('dd').slideDown('slow');
				}
			}
		});
	}
};


//placement du flash
var setFlash = {
		urlFlv :'',
		init:function(href){
			$('.openPopin').click(function(){
				setFlash.getUrlVideo($(this).attr('href'));
			});
		},
		getUrlVideo:function(href){
			setFlash.urlFlv = href.substring(href.indexOf('urlflv=')+7,href.length);
		},
		writeFlash:function(urlFlv){
			var fo = new FlashObject("/swf/player.swf", "Player", "640", "380", "9", "#000000", true);
				fo.addParam("noscale", "true");
				fo.addParam("quality", "best");
				fo.addParam("allowFullScreen", "true");
				fo.addParam("play", "true");
				fo.addVariable("url_vid", urlFlv);
				fo.addVariable("swfPath", "/swf/");
				fo.addVariable("xmlPath", "/Xml/captions.xml");
				fo.write("id_flash");	
		}
    };
	
function timestamp() {
	return Math.round(new Date().getTime() / 1000);
}

var setPushSingatoba = {
	init:function(){
		var fo = new FlashObject("/fr-fr/swf/Nespresso_OP-Machines.swf?t="+timestamp(), "Player", "300", "110", "9", "#000000", true);
			fo.addParam("allowFullScreen", "false");
			fo.addParam("quality", "high");
			fo.addParam("wmode", "transparent");
			fo.write("singatoba");
	}
}

var setPushRistrettoIndia = {
    init:function(){
		var fo = new FlashObject("/fr-fr/swf/ristretto-india_fr.swf?t="+timestamp(), "Player", "300", "110", "9", "#000000", true);
			fo.addParam("allowFullScreen", "false");
			fo.addParam("quality", "high");
			fo.addParam("wmode", "transparent");
			fo.write("ristrettoIndia");
	}
}

var setPushTesti = {
	init:function(){
		var fo = new FlashObject("/fr-fr/swf/300x116_v2.swf?t="+timestamp(), "Player", "300", "120", "9", "#000000", true);
			fo.addParam("allowFullScreen", "false");
			fo.addParam("quality", "high");
			fo.addParam("wmode", "transparent");
			fo.write("testi3");
	}
}

var setSwfSingatoba = {
		init:function(){
			var fo = new FlashObject("/fr-fr/swf/facelift_fr.swf?t="+timestamp(), "Player", "920", "372", "9", "#000000", true);
			fo.addParam("allowFullScreen", "false");
			fo.addParam("quality", "high");
			fo.addParam("wmode", "window");
			fo.write("id_flash");			
		}
}

var setSwfRistrettoIndia = {
		init:function(){
			var fo = new FlashObject("/fr-fr/swf/ristretto-india-webpage_fr.swf?t="+timestamp(), "Player", "920", "372", "9", "#000000", true);
			fo.addParam("allowFullScreen", "false");
			fo.addParam("quality", "high");
			fo.addParam("wmode", "window");
			fo.write("id_flash");			
		}
}

var setSwfSynergy4 = {
		init:function(){
			var fo = new FlashObject("/fr-fr/swf/920x371_landingPage_fr.swf?t="+timestamp(), "Player", "920", "371", "9", "#000000", true);
			fo.addParam("allowFullScreen", "false");
			fo.addParam("quality", "high");
			fo.addParam("wmode", "window");
			fo.write("id_flash");			
		}
}

var setSwfOpMachines = {
		init:function(){
			var fo = new FlashObject("/fr-fr/swf/accordeon.swf?t="+timestamp(), "Player", "920", "371", "9", "#000000", true);
			fo.addParam("allowFullScreen", "false");
			fo.addParam("quality", "high");
			fo.addParam("wmode", "window");
			fo.write("id_flash");			
		}
}

var selectCountry = {
	init:function(){
	    $('#footer li span.select').unbind('click');
		$('#footer li span.select').click(function(){
			var offset = $(this).parent().offset();
			selectCountry.addSelectMask(offset);
			var selectC = $('#footer li.select .scrollable').html();
			$(document.body).append('<div class="box[id=country]" id="scrollable" style="position:absolute;height:295px;top:'+(offset.top-298)+'px; left:'+offset.left+'px;">'+selectC+'</div>');
			box.ui('scroll').create({
    			element: '#scrollable',
			    buttons: false
		    });
			$('#scrollable').show();
			box.ui('scroll.country').compute();
		});	
	},
	addSelectMask:function(offset){
		var size = $(document.body).getSize();
		$(document.body).append('<div class="selectMask" style="z-index:9;width:'+size.width+'px; height:'+size.height+'px; position:absolute; top:0px; left:0px;"></div>');
		$('#footer li span.select').unbind('click');
		$('.selectMask, #footer li span.select').click(function(){
		    $('#scrollable').remove();
			$('.selectMask').remove();
			box.ui('scroll').destroy('country');
			selectCountry.init();
		});
	}
};

var loginForm = {
	init:function(){
		loginForm.setForm();
		$('#loginForm #submitLog').click(function(){
			loginForm.post();
			return false;
		});
	},
	post:function(){
		login = $('#loginForm #login').val();
		pass = $('#loginForm #password').val();
		if(login == '' || pass == '' ) {
			window.location.href = 'http://nesclub.nespresso.com/?market=fr&lang=fr&channel=pro&action=login';
		} else {
			$.ajax({
				type: "GET",
				url: "/saveCookies.php?login=" + login + "&pwd=" + pass,
				success: function(){
					$('#loginForm').submit();
				}, 
				dataType: "html"
		 	});
		}
	},
	setForm:function(){
		if(cookieManage.boolean('login_club')){
			$('#loginForm #login').val(cookieManage.get('login_club'));	
		}
		if(cookieManage.boolean('pwd_club')){
			$('#loginForm #password').val(cookieManage.get('pwd_club'));	
		}
	}
};

box.dom(document).ready(function() {
  	// init footer country list
  	selectCountry.init();

	$('#menuTop li a, #menuThema li a').hover(function(){
		$(this).parent().addClass("on");
	},function(){
   		$(this).parent().removeClass("on");
	 });
	
	$('a.blank').click(function(){
		window.open($(this).attr('href'));
		return false;
	});
	
	//js header form login&pass
	$('#loginForm #login, #loginForm #password').focus(function(){
		$(this).val('');
	});
	$('#loginForm a').click(function(){
		window.open($(this).attr('href'),"pass");
		return false;
	});
	
	//popup CallBack
	$('.openPopup').click(function(){
		var url = $(this).attr('href');
		window.open(url,"popuplinkeo","width=550, height=350, top=5, left=5, toolbar=no, menubar=no, location=no, directories=no, status=no, scrollbars=no, resizable=no");
		return false;
	});
	
	//initialisation js page products cafe/machine
	if(document.getElementById('hpProducts')){
		bgdOverProduct.init();
	}
	
	//initialisation js entreprise -> expertise
	if(document.getElementById('b2b') && $('#main').hasClass('expertness')){
		expertnessList.init();
		selectDl.init();
	}
	
	if(document.getElementById('popinCallBack')&&document.getElementById('popup')){
		$('.closePopin').click(function(){
			window.close();
			return false;
		});
	}
	//init push testimonial video & texte
	testiManage.init();
    
    flashPushes.init();
	
	loginForm.init();
	
	if(document.getElementById('services')){
		$('.puhsContextual .pushB2b').click(function(){
			window.location = $('.puhsContextual .pushB2b h3 a').attr('href');
		});
	}
	
	if(document.getElementById('singatoba')){
		setPushSingatoba.init();	
	}
/*
    if(document.getElementById('ristrettoIndia')){
        setPushRistrettoIndia.init();
    }
*/
	if($('#main').hasClass('singatoba')){
		setSwfSingatoba.init();
	}

    if($('#main').hasClass('ristrettoIndia')){
        setSwfRistrettoIndia.init();
    }

	if($('#main').hasClass('synergy4')){
        setSwfSynergy4.init();
    }

	if($('#main').hasClass('OpMachines')){
        setSwfOpMachines.init();
    }
	
	if(document.getElementById('testi3')){
		setPushTesti.init();	
	}
});	