var keywords = ["care", "compassion", "sensitivity", "respect", "dedication"];

function hideHeartsAndSlogan(id, cb) {
  id = ( id == 0 ) ? 4 : (id-1);
  //console.log("hide: #slogan_" + keywords[id]);
  jQuery("#heart_1").hide();
  jQuery("#heart_2").hide();
  jQuery("#heart_3").hide();
  jQuery("#heart_4").hide();
  jQuery("#heart_5").hide();
  jQuery("#heart_6").hide();
  jQuery("#slogan_" + keywords[id]).hide();
  cb();
}

function showHeart(hId, cb){
  //console.log("show heart: #heart_" + hId);
  if (hId < 6) {
    hId++;
    jQuery("#heart_" + hId).fadeIn(1000, function() { showHeart(hId, cb); });
  }
  else {
    return cb();
  }
}

function showHearts(cb) {
  showHeart(0, cb);
}

function hideSlogan(id, cb) {
  //jQuery("#slogan_" + keywords(id)).fadeOut("slow", cb);
}
function showSlogan(id, cb) {
	var prefix = "#load_";
	if (jQuery("#load_" + keywords[1]).length == 0) {
		prefix = "#slogan_";
	}

	jQuery("#slogan_" + keywords[id]).fadeIn("slow", function(){
    var prev = (id == 0) ? 4 : (id-1);
    jQuery(prefix + keywords[prev]).fadeOut(1000, function(){
      jQuery(prefix + keywords[id]).fadeIn(1000, cb);
    });
  });
}
// 1- 888- 757 - 6500

function rotateSlogan(i) {
  //console.log("rotate:" + keywords[i]);
  hideHeartsAndSlogan(i, function(){
    showHearts(function(){
      showSlogan(i, function() {
        i++;
        if (i > 4){
          i = 0;
        }
        setTimeout(function(){
          rotateSlogan(i);
        }, 3000);
      });
    });
  });
}
function mencode(anchor, addr, country) {
	var address = addr.replace(/.*levelke\/([a-z0-9._%-]+)\/([a-z0-9._%-]+)/i, '$1' + '@' + '$2' + '.' + country);
	anchor.setAttribute('href','mailto:' + address);
}

function restore(anchor) {
	anchor.setAttribute('href', 'javascript:void(0)');
}

function enablePopup() {
    $("#news_popup").modal({
        minHeight:400,
        minWidth: 600,
        overlayClose:true,
        onOpen: function (dialog) {
            dialog.overlay.fadeIn('slow', function () {
                dialog.data.hide();
                dialog.container.fadeIn('slow', function () {
                    dialog.data.fadeIn('slow');
                });
            });
        },
        onClose: function (dialog) {
        	dialog.data.fadeOut('slow', function () {
                dialog.container.hide('slow', function () {
                    dialog.overlay.fadeOut('slow', function () {
                        $.modal.close();
                    });
                });
            });
			createCookie("adpopup","1",1);
        }
    });
}

function createCookie(name,value,days) {
	if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
	}
	else {
		var expires = "";
	}
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

jQuery(function() {
   rotateSlogan(1);
   var elem = document.getElementById("news_popup");
   if (elem) {
	if (readCookie("adpopup") == null) {
		enablePopup();
	}
   }
});

