$(document).ready(function() {
	_pg.init();
    });

_pg = {
    CString:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
    BaseUrl:"http://coolsculpting.patientsguide.com/?",

    init: function() {
	var results = [];
	var result = name = phone = as = null;

	var nodes = $(".results-location");
	var snodes = null;
	if(nodes && nodes.length > 0) {
	    nodes.each(function(idx, item) {
		    result = {
			Name: "",
			Phone: "",
			Website: "",
			Url: "",
			Link: null
		    };
		    
		    var name = $(".loc-link", item);
		    if(name != "") {
			result.Name = name.text();
		    }

		    /*snodes = $("strong", item);
		    if(snodes)
			snodes.each(function(x, i) {
				if(/\d{3}-\d{3}/.test(i.innerHTML))
				    result.Phone = i.innerHTML;
			    });*/
			var phone = $(".telephone", item);
			if(phone != "") {
				result.Phone = phone.text();
			}

		    as = $("a", item);
		    if(as) {
			as.each(function(x, i) {
				if($(this).hasClass("web_link")) {
				    result.Website = $(this).attr('href');
				    result.Link = $(this);
				}
			    });
		    }
		    
		    if(result.Link) {
			result.Url = _pg.BaseUrl + "w="+ _pg.Base64Encode(result.Website) +"&d="+ _pg.Base64Encode(location.href +"|"+ result.Name +"|"+ result.Phone);
			result.Link.attr('href', result.Url);
			//$(result.Link).mouseover(function(r) { return function(e) { r.Link.href = r.Url;  }}(result));
			//$(result.Link).mouseout(function(r) { return function(e) { r.Link.href = r.Website; }}(result));
			    }
		    
		    results.push(result);
		});
	}
    },

    Base64Encode:function(val) {
	var output = "";
	var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
	var i = 0;

	if(!val || val == "")
	    return "";

	while(i < val.length) {
	    chr1 = val.charCodeAt(i++);
	    chr2 = val.charCodeAt(i++);
	    chr3 = val.charCodeAt(i++);

	    enc1 = chr1 >> 2;
	    enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
	    enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
	    enc4 = chr3 & 63;

	    if(isNaN(chr2))
		enc3 = enc4 = 64;
	    else 
		if(isNaN(chr3))
		    enc4 = 64;

	    output = output + this.CString.charAt(enc1) + this.CString.charAt(enc2) + this.CString.charAt(enc3) + this.CString.charAt(enc4);
	}

	return output;
    },

    Base64Decode:function(val) {
	var output = "";
	var chr1, chr2, chr3;
	var enc1, enc2, enc3, enc4;
	var i = 0;

	val = val.replace(/[^A-Za-z0-9\+\/\=]/g, "");
	while(i < val.length) {
	    enc1 = this.CString.indexOf(val.charAt(i++));
	    enc2 = this.CString.indexOf(val.charAt(i++));
	    enc3 = this.CString.indexOf(val.charAt(i++));
	    enc4 = this.CString.indexOf(val.charAt(i++));

	    chr1 = (enc1 << 2) | (enc2 >> 4);
	    chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
	    chr3 = ((enc3 & 3) << 6) | enc4;

	    output = output + String.fromCharCode(chr1);

	    if(enc3 != 64) 
		output = output + String.fromCharCode(chr2);

	    if(enc4 != 64)
		output = output + String.fromCharCode(chr3);
	}

	return output;
    }
};

