	
	// Global variables
	var glossary_terms = Array();
	var glossary_definition  = Array();
	var glossary_id = Array();
	
	var product_name = Array();
	var product_ingredients = Array();
	var product_id = Array();
	
	var overlay = $("<div id='popup_overlay'></div>");
	
	// This variable is used to know if the popup showed is a resultss chart
	var divIsChart = false;

	var trackPath = "www.olayprofessional.com/";
	var thePath = window.location.pathname;
	var pageName = thePath.substring(thePath.lastIndexOf('/') + 1);
	
	showPopup = function () {
		$.get(arguments[0],'',function(){
			$("#popup_content").html(arguments[0]).fadeIn(600);
			
			$(".close").click(hidePopup);
		});
	}
	
	hidePopup = function () {
		$("#popup_content").fadeOut(600);
	}

	showPop = function () {
		var isChart = false;
		if(arguments[1]){
			isChart = true;
		}	
		$.get(arguments[0],function(){
			$("#general_popup").html(arguments[0]);
			$(".close").click(hidePop);
			showOverlay();
			if (isChart){
				divIsChart = true;
				locatePopup(isChart);
			}
			else{
				locatePopup();
			}
			$("#general_popup").fadeIn(600);
			togglecheck();
		});
	}
	
	hidePop = function () {
		$("#general_popup").fadeOut(600);
		$('#general_popup').removeClass('chart');
		divIsChart = false;
		hideOverlay();
	}

	showOverlay = function () {
		$("#main").append(overlay.click(hidePop));
		$("#popup_overlay").width($(window).width());
		$("#popup_overlay").height($(window).height());
	}
	
	hideOverlay = function () {		
		$("#popup_overlay").remove();
	}
	
	locatePopup = function () {
		var winheight = $(window).height();
		var winwidth = $(window).width();
		if($.browser.opera){
			winwidth = window.innerWidth;
			winheight = window.innerHeight;
		}
		var y =700;
		var x =1210;
		var dif_y = winheight - y;
		var dif_x = winwidth - x;
		if(dif_y < 0){
			dif_y = 0;
		}
		if(dif_x < 0){
			dif_x = 0;
		}
		var mainWidth = 590;
		var mainHeight = 472;
		var navsWidth = 258;
		var mainPadding = 90;
		var leftLocation = 10;

		mainWidth = mainWidth + mainPadding;
		var popWidth = $("#general_popup").width();
		var popHeight = $("#general_popup").height();
		if(popWidth < mainWidth){
			leftLocation = (mainWidth - popWidth) / 2;}
		var topLocation = (mainHeight - popHeight) / 2;
		if(arguments[0]){
			topLocation = topLocation + 45;
		}
		$("#general_popup").css('top',topLocation+dif_y);
		$("#general_popup").css('left',leftLocation+navsWidth+dif_x);
	}

	handleResponse = function(){
		glossaryInfo = arguments[0].documentElement;
		glossary_id_inf = $('term_id',glossaryInfo);
		glossary_terms_inf = $('term',glossaryInfo);
		glossary_definition_inf = $('definition',glossaryInfo);
		
		for( i = 0; i < glossary_id_inf.length; i++){
			index = $(glossary_id_inf[i]).text();
			glossary_terms[index] = $(glossary_terms_inf[i]).text();
			glossary_definition[index] = $(glossary_definition_inf[i]).text();
		}
		
		// Adding clicks to Terms
		$('.terms').click(searchTermClick);
	}
	
	handleIngredientsResponse = function(){
		ingredientsInfo = arguments[0].documentElement;
		product_id_inf = $('product_id',ingredientsInfo);
		product_name_inf = $('product',ingredientsInfo);
		product_ingredients_inf = $('ingredients',ingredientsInfo);
		
		for( i = 0; i < product_id_inf.length; i++){
			index = $(product_id_inf[i]).text();
			product_name[index] = $(product_name_inf[i]).text();
			product_ingredients[index] = $(product_ingredients_inf[i]).text();
		}
		
		// Adding clicks to Ingredients
		$('.prod').click(searchIngredientsClick);
	}
	
	searchTermClick = function(){
		classes = $(arguments[0].target).attr('class').split(' ');
		var id_term = -1;
		for( ind in classes ){
			if (classes[ind].indexOf('term_') != -1){
				id_term = classes[ind].split('_')[1]
			}
		}
		if (id_term == -1){
			return false;
		}
		var trackUrl = trackPath + pageName + "/" + id_term;
		//alert(trackUrl);
		pageTracker._trackPageview(trackUrl);
		$.get("files/glossary.html", function(){
			showPopupGlossary(glossary_terms[id_term],
			glossary_definition[id_term],arguments[0]);
		});
		
	}
	
	searchIngredientsClick = function(){
		classes = $(arguments[0].target).attr('class').split(' ');
		var id_prod = -1;
		for( ind in classes ){
			if (classes[ind].indexOf('prod_') != -1){
				id_prod = classes[ind].split('_')[1]
			}
		}
		
		if (id_prod == -1){
			return false;
		}
		
		$.get("files/ingredients.html", function(){
			showPopupIngredients(product_name[id_prod],
			product_ingredients[id_prod],arguments[0]);
		});
		
	}

	
	showPopupGlossary = function(){
		$('#general_popup').html(arguments[2]);
		$('#pop_header span').html(arguments[0]);
		$('.definition p').html(arguments[1]);
		$('.close').click(function(){$('#general_popup').fadeOut(600); hideOverlay();});		
		showOverlay();
		locatePopup();
		$('#general_popup').fadeIn(600);
	}
	
	showPopupIngredients = function(){
		$('#general_popup').html(arguments[2]);
		$('#pop_header span').html(arguments[0]);
		$('#the_ingredients p').html(arguments[1]);
		$('.close').click(function(){$('#general_popup').fadeOut(600); hideOverlay();});
		showOverlay();
		var evtDesc = "FullIngredients";
		var trackUrl = trackPath + pageName + "/" + evtDesc;
		//alert(trackUrl);
		pageTracker._trackPageview(trackUrl);
		
		locatePopup();
		$('#general_popup').fadeIn(600);
	}

	
	/* A Custom checkbox*/
	togglecheck = function () {
		$("input.chk").attr("checked","");
		$("div.chk").toggle(				 
		function () {
			var chkbox_id = "#"+arguments[0].target.id+"_chk";
			$(this).addClass("checked").removeClass("unchecked");
			$(chkbox_id).attr("checked","checked");},
		function () {
			var chkbox_id = "#"+arguments[0].target.id+"_chk";
			$(this).addClass("unchecked").removeClass("checked");
			$(chkbox_id).attr("checked","");});
	}
	
	
	init = function () {
	   // Utils
	   if ($('body').attr('id') == 'bdpreorder'){
			refr = document.referrer.split('/');
			refr = refr[refr.length - 1];
			if (refr == "proXStarter.html"){
				$('span.back').css('display','block');
			}
		}
		$('#showPopup').click(function(){showPopup('/en-US/geton_form.jsp');});
		$('.sendaFriendPopup').click(function(){showPop('files/sendafriend.html');});
		$('#showGuaranteed').click(function(){showPop('files/guaranteed.html');});
		$('#showGuaranteed2').click(function(){showPop('files/guaranteed.html');});
		$('#showGuaranteedIndex').click(function(){showPop('files/guaranteedindex.html');});
		$('#showproX').click(function(){showPop('files/which_products.html');});
		$('#olayLive').click(function(){showPop('files/olay_en_consultation.html');});
		$('#expect').click(function(){showPop('files/what_expect.html');});
		$('#aa_expect').click(function(){showPop('files/what_expect.html');});
		$('#iwp_expect').click(function(){showPop('files/iwp_what_expect.html');});
		$('#iwp_compare').click(function(){showPop('files/iwp_compare.html');});
		$('#results').click(function(){showPop('files/results.html');});
		$('#results_hfc').click(function(){showPop('files/results_hfc.html','chart');});
		$('#results_wsc').click(function(){showPop('files/results_wsc.html','chart');});
		$('#results_dfc').click(function(){showPop('files/results_dfc.html','chart');});
		$('#results_dwt').click(function(){showPop('files/results_dwt.html','chart');});
		$('#results_arl').click(function(){showPop('files/results_arl.html','chart');});
		$('#results_erc').click(function(){showPop('files/results_erc.html','chart');});
		$('#olayAlliance').click(function(){showPop('files/olayAlliance.html','chart');});
		$('.showusage').click(function(){showPop('files/usage.html');});
		$('.showprotocol').click(function(){showPop('files/usage_protocol.html');});
		$.ajax({
		   url: "files/glossary.xml",
		   success: handleResponse,
		   dataType: 'xml',
		   error: function(){alert('Comunication error:' + arguments[1]);}
		 });
		$.ajax({
		   url: "files/ingredients.xml",
		   success: handleIngredientsResponse,
		   dataType: 'xml',
		   error: function(){alert('Comunication error:' + arguments[1]);}
		 });
	}
	
	$(init);