String.prototype.rot13 = function(){
    return this.replace(/[a-zA-Z]/g, function(c){
        return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
    });
};


function lpopup(addr, w, h){
var w=w || 500, h=h || 500, l=(screen.availWidth - w) / 2, t=(screen.availHeight - h) / 2,
op='toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,copyhistory=no,width='+w+',height='+h+',left='+l+',top='+t;
window.open(addr, "_blank", op);
}


jQuery(function(){

    _home_url = jQuery('#menu ul li a').first().attr('href');

    jQuery('#livechat').click(show_support);
    jQuery('#logo').click(function(){document.location=jQuery('#menu ul li a').first().attr('href');});
       
    jQuery('.infolink').click(function(){
	var url = _home_url + '/moreinfo/' + jQuery(this).attr('rel');
	lpopup(url, 700, 600);
    });
    
    // jQuery('#l-policy, #l-faq, #l-bonuses').click(invoke_dialog).attr('href', 'javascript:void(0)');
});


function invoke_dialog(){
    var id = jQuery(this).attr('id').replace('l-','');
    
    if(!dialogs[id]){
	jQuery.ajax({
	    url: _home_url + '/ajax/content?do=text&id=' + id,
	    dataType: 'json',
	    success: function(json){
		if(json.id){
		    json.text = json.text.replace(/href="\#(\S+)"/g, "href=\"javascript:mscroll('#$1')\"");
		    jQuery('#dlg-' + json.id + ' .cnt').html(json.text);
		}
		dialogs[json.id] = 1;
		jQuery('#dlg-' + json.id).dialog('open');
	    }
	});
    }else{
	jQuery('#dlg-' + id).dialog('open');
    }
}

function mscroll(element){
    var divOffset = jQuery(element).parent().parent().offset().top;
    var pOffset = jQuery(element).offset().top;
    var pScroll = parseInt(pOffset - divOffset);
    jQuery(element).parent().parent().animate({scrollTop: '+=' + pScroll + 'px'}, 1000);
}

utilVars = {};
dialogs = {};

function show_support(){
	var support_url = "http://livechatrx.com/online/";
	var id = "rxsupport";

	if(!utilVars[id] || utilVars[id].closed){ 
                utilVars[id]=lpopup(support_url, 640, 560);
        } 
        utilVars[id].focus();
}


