function getPageSize(){
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
}

var MSNConversationUrl = '';
function Microsoft_Live_Messenger_PresenceButton_startConversation(conversationUrl){
	MSNConversationUrl = conversationUrl;
	var pageSize = getPageSize();
	$('#overlay').css({height: pageSize[1] + 'px'});
	$(window).resize(function(){
		pageSize = getPageSize();
		$('#overlay').css({height: pageSize[1] + 'px'});
	})
	$('#overlay').show();
	$('#form-pre-msn').fadeIn();
	$.scrollTo(0);
	
	$('#overlay').click(function(){
		$('#overlay').hide();
		$('#form-pre-msn').hide();
		$('#msg-msn-popup').hide();
	});
	$('#bt-fechar-form-msn').click(function(){
		$('#overlay').hide();
		$('#form-pre-msn').hide();
	});
}

function cadastraMSN(sid){
	var nome = document.getElementById('msn-nome');
	var email = document.getElementById('msn-email');
	var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if(nome.value.length < 3){
		alert('Digite o seu nome');
		return false;
	}
	else if(!email.value.match(emailRegEx)){
		alert('Digite corretamente seu e-mail');
		return false;
	}
	else{
		// Ajax
		$.post('cadastra-msn.php?sid='+sid, {nome: nome.value, email: email.value}, function(){
			$('#overlay').hide();
			$('#form-pre-msn').hide();
			if(!abreJanelaMSN(MSNConversationUrl)){
				$('#overlay').show();
				$('#msg-msn-popup').fadeIn();
				$.scrollTo(0);
			}
		})
	}
}

function abreJanelaMSN(conversationUrl){
	$('#overlay').hide();
	$('#msg-msn-popup').hide();
	var url = conversationUrl + "&buttonhost=" + document.location.hostname;
	return window.open(url, '_blank', 'height=350px,width=500px');
}

function Microsoft_Live_Messenger_PresenceButton_onStyleChange(element)
{
    if (element && element.presence)
    {
        Microsoft_Live_Messenger_PresenceButton_onPresence(element.presence);
    }
}

function Microsoft_Live_Messenger_PresenceButton_onPresence(presence)
{
    var idx = presence.id.indexOf('@');
    if (idx >= 0)
    {
        var id = presence.id.substr(0, idx);

        var element = document.getElementById('Microsoft_Live_Messenger_PresenceButton_' + id);
        if (element)
        {
            element.innerHTML = "";
        
            var conversationUrl = element.attributes['msgr:conversationUrl'].value;
            var width = parseInt(element.attributes['msgr:width'].value);
            var height = 30;

            var bgColor = element.attributes['msgr:backColor'].value;
            var altBgColor = element.attributes['msgr:altBackColor'].value;
            var color = element.attributes['msgr:foreColor'].value;

            element.className = 'Microsoft_Live_Messenger_PresenceButton';

            var outerFrame = document.createElement('div');

            var link = document.createElement('a');
            link.style.textDecoration = 'none';
            link.style.color = color;
            link.href = 'javascript:Microsoft_Live_Messenger_PresenceButton_startConversation("' + conversationUrl + '");';

            var innerFrame = document.createElement('div');
            innerFrame.style.padding = '4px';
            //innerFrame.style.textAlign = 'center';

            var statusIcon = document.createElement('img');
            statusIcon.style.border = 'none';
            statusIcon.style.verticalAlign = 'middle';
            
            if(presence.status.toLowerCase() == 'offline'){
            	statusIcon.src = '/msn_off.png';
            }
            else{
            	statusIcon.src = '/msn_on.png';
            }
            
            statusIcon.alt = presence.statusText;
            statusIcon.title = presence.statusText;

            var displayName = document.createElement('span');
            displayName.style.fontFamily = '"Segoe UI", Tahoma, Verdana, sans-serif';
            displayName.style.fontSize = '9pt';
            displayName.title = presence.displayName;

            if (displayName.innerText !== undefined)
            {
                displayName.innerText = presence.displayName;
            }
            else if (displayName.textContent !== undefined)
            {
                displayName.textContent = presence.displayName;
            }

            innerFrame.appendChild(statusIcon);
            //innerFrame.appendChild(displayName);
            link.appendChild(innerFrame);
            outerFrame.appendChild(link);
            element.appendChild(outerFrame);
            
            element.presence = presence;
        }
    }
}


