var UserClicked			= false;
var popupLoaded			= false;
var pwysoCyswllt		= false;
var wediGorffenLlwytho	= false;

//Rhan o'r cod sydd yn pigo i fynu cau'r ffenest
document.onkeydown		= spy;
document.onmousedown	= spy;

//Cael ei alw gan yr onload yn y tudalen
function wrthAgor() {

  //Ychwanegu onclick handler i bob cyswllt
  var a = document.getElementsByTagName('a');
  for (var i=0;i<a.length;i++) {
    a[i].onclick = gwirioSafle;
  } 
  wediGorffenLlwytho = true; //newidyn i ddweud fod y tudalen wedi gorffen llwytho
}

//Ffwythiant sydd yn cael ei alw pan mae'r tudalen yn cael ei gau
function wrthGau() {
  popup();
}

//Pigo i fynu os mae allwedd neu botwm llygoden yn cael ei bwyso, a hefyd os mae rhywun yn clicio ar 
function spy(strMath) {

  //Gweld os mai cyswllt mae'r defnyddiwr wedi pwyso. angen hwn i stopio'r ffenest agor 
  //ar yr onunload os mai cyswllt ydy o.
  if (strMath == "cyswllt")
    pwysoCyswllt = true; 
  else
    pwysoCyswllt = false;

  
  window.clearTimeout(); //clirio'r teimar
  UserClicked=true;  //adnabod fod y defnyddiwr wedi pwyso botwm
  window.setTimeout("UserClicked=false;",2000); //gosod teimar i newid gwerth UserClicked yn ol
}

//Ffwythiant sydd yn cael ei alw gan wrthGau
function popup() {
  if(!UserClicked && !pwysoCyswllt && wediGorffenLlwytho)  //gwirio fod y defnyddiwr heb wedi pwyso botwm, heb wedi clicio cyswllt a fod y tudalen wedi gorffen llwytho
    loadpopup("cau_ffenest");  //agor y popup
}

//Rhan o'r cod sydd yn pigo i fynu external links
function gwirioSafle(strCyfeiriad) {

  spy("cyswllt"); //galw ffwythiant i ddweud bod cyswllt wedi ei bwyso

  //peidio promptio gyda safleoedd allanol sydd yn "rhan" o'r safle
  var strSafleGwynedd		= 'www.gwynedd.gov.uk';
  var strSafleRightnow		= 'custhelp.com';
  var strSafleMewnrwyd		= 'mewnrwyd.gwynedd.gov.uk';

  if (strCyfeiriad) 
	strCyfeiriad = strCyfeiriad.target;
  else 
	strCyfeiriad = window.event.srcElement;

  strCyfeiriad = new String(strCyfeiriad);

  var intIndecsSafleGwynedd = strCyfeiriad.indexOf(strSafleGwynedd, 0);
  var intIndecsSafleRightnow = strCyfeiriad.indexOf(strSafleRightnow, 0);
  var intIndecsSafleMewnrwyd = strCyfeiriad.indexOf(strSafleMewnrwyd, 0);

  //os nad ydy'r cyswllt yn pwyntio i'r safle yma, yna galw'r popup
  if (intIndecsSafleGwynedd == -1 && intIndecsSafleRightnow == -1 && intIndecsSafleMewnrwyd == -1) 
	loadpopup("cyswllt_allanol"); 
}

//Ffwythiant i alw'r popup
function openpopup(strMath) {
    topLeftX = 0;
	topLeftY = 0;
	ratioAsked = 10;	//Ratio of people visiting against those being shown questionnaire
	optionList = '';
	//optionList += ',fullscreen=yes';	//Full-screen mode (IE 4 onwards only)
	//optionList += ',scrollbars=yes';	//Scrollbars
	//optionList += ',menubar=yes';	    //Menu bar (File, Edit, View etc.)
	//optionList += ',location=yes';	//Location field to enter  URL
	//optionList += ',resizable=yes';	//Allow user to resize window.
    var strCwestiwn;
	var iaith = new String(get_cookie('iaith'));
	iaith = iaith.substr(iaith.length-1, iaith.length);

	if (iaith == "1") {
      strCwestiwn = 'Would you like to fill in our questionnaire?';
	  surveyName = "/ffurflenni/resources_directorate/resources_directorate.htm";	//Can be a full URL or a reference name
	} else {
      strCwestiwn = 'A fuasech yn hoffi llenwi ein holiadur i mewn?';
	  surveyName = "/ffurflenni/cyfadran_adnoddau/cyfadran_adnoddau.htm";	//Can be a full URL or a reference name
    }

    var width=800;
    if (parseFloat(navigator.appVersion)>=4)
        width=screen.width;
    
    if (width<700)
        pageSize = 'width=550,height=420';	//Screen resolution < 800x600
    else
        pageSize = 'width=590,height=590';	//Screen resolution 800x600 or above	

    if (strMath == "cyswllt_allanol" || navigator.appName == "Microsoft Internet Explorer")
      blnEisiauPopup = confirm(strCwestiwn);
	else
	  blnEisiauPopup = true;

	if (blnEisiauPopup )
	{
      if (navigator.appName =="Netscape") 
        ffenest = window.open(surveyName,'',pageSize+',screenX='+topLeftX+',screenY='+topLeftY+optionList);
      else 
        ffenest = window.open(surveyName,'',pageSize+',left='+topLeftX+',top='+topLeftY+optionList);
	}
    return true;		    
}


//Ffwythiant sydd yn gwirio'r cwci er mwyn gweld os oes angen popup
function get_cookie(Name) {
    var search = Name + "="
    var returnvalue = "";
    if (document.cookie.length > 0) {
      offset = document.cookie.indexOf(search)
      if (offset != -1) { // if cookie exists
        offset += search.length
        // set index of beginning of value
        end = document.cookie.indexOf(";", offset);
        // set index of end of cookie value
        if (end == -1)
          end = document.cookie.length;
        returnvalue=unescape(document.cookie.substring(offset, end));
      }
    }
    return returnvalue;
}


//Ffwythiant sydd yn gwirio fod angen galw'r popup
function loadpopup(strMath) {
  //***	USER SETTINGS ***//
  dropCookie = true;
  //***	END OF USER SETTINGS ***//

  if (get_cookie('popped')=='') {
    if (dropCookie) {
    	//***		USER SETTINGS		***//

	  var pickRandom = true;
      var ratioAsked = 10;
      var strCwestiwn;
	  var iaith = new String(get_cookie('iaith'));
	  iaith = iaith.substr(iaith.length-1, iaith.length);

	if (iaith == "1") {
      strCwestiwn = 'The Council is collecting information in order to improve our website, can you spare two minutes to fill in our questionnaire?';
	  surveyName = "/ffurflenni/resources_directorate/resources_directorate.htm";	//Can be a full URL or a reference name
	} else {
      strCwestiwn = 'Mae\'r Cyngor yn casglu gwybodaeth ar gyfer gwella ein safle we, a fyddech cystal a chymrud dau funud i lenwi ein holiadur?';
	  surveyName = "/ffurflenni/cyfadran_adnoddau/cyfadran_adnoddau.htm";	//Can be a full URL or a reference name
    }

	  if (((parseInt(Math.random() * ratioAsked)) == 1 || pickRandom == false) && !popupLoaded)	{
        if(openpopup(strMath) && dropCookie) {
	      popupLoaded = true;
          var expireDate = new Date();
          expireDate.setFullYear(expireDate.getFullYear()+1);
          document.cookie="popped=yes;expires=" + expireDate.toGMTString();
        }
      }
    }
  }
}
