var boxLabels = { VIEW_ALL_POSTS:'View all posts',
                  GO_TO_BLOG:'Go to blog',
                  ADD_YOUR_BLOG_HERE:'Add your blog here',
                  HIDE:'Hide' }

document.write('<div id="bloglinks" class="linkbox" style="display:none"></div>')

function hideDiv(divId){ if (!$(divId)) return; $(divId).style.display='none'}
function showDiv(divId){ if (!$(divId)) return; $(divId).style.display='block'}
function toggleDiv(divId) {
 if (!$(divId)) return
 if ($(divId).style.display == 'none') {
  $(divId).style.display = 'block'
 } else {
  $(divId).style.display = 'none' 
 }
}


//Variable and functions to keep the Top Post divs on and off
var lastTopDivShown = null
function switchTopList(divName) {
    if (lastTopDivShown==null) {
        showDiv(divName)
    } else {
        showDiv(divName)
        hideDiv(lastTopDivShown)
    }
    lastTopDivShown = divName
}

var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)

// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;

// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0

var curX = 0
var curY = 0

var lastX = 0
var lastY = 0

// Main function to retrieve mouse x-y pos.s

function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.documentElement.scrollLeft
    tempY = event.clientY + document.documentElement.scrollTop
    //FUCKING IE, not only it considers mouse position
    //relative to the current view, but it didn't retrieve
    //the scroll value with document.body.scrollTop... MOTHERFUCKERS
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  // show the position values in the form named Show
  // in the text fields named MouseX and MouseY

  return true
}

function fillBox(blogId,blogName,blogUrl,blogRSS,languageCodes,isPodcast) {
    var codes = languageCodes.split(',')

    var flagsHtml = ''
    
    var linkEnd = '.gif" border="0" ></a> ';

    if (codes.length > 0) {
        for (i=0; i < codes.length; i++) {
            linkStart = '<a href="./?lang='+codes[i]+'"><img src="/images/flags/';
            if (codes[i]=='en')
                flagsHtml += linkStart + 'US' + linkEnd
            if (codes[i]=='es')
                flagsHtml += linkStart + 'ES' + linkEnd
            if (codes[i]=='pt')
                flagsHtml += linkStart + 'BR' + linkEnd            
        }
    } 

    html = ''

    var rssImgSrc = (isPodcast=='true') ? 'http://static.flickr.com/61/183818874_4ee0c79a59_o.gif':'http://static.flickr.com/45/127876601_e61fb85e22_o.png';

    html += '<h3><a href="'+ blogRSS +'"><img border="0" src="'+rssImgSrc+'"></a> ' + blogName + '</h3>';

    blogNameNoQuotes = blogName
   blogNameNoQuotes.replace("'","")

    html += '<div class="flags">' + flagsHtml + '</div>';
    html += '<li><a onClick=\"hideDiv(\'bloglinks\')\" href=\"/blog/' + blogId + '/'+blogNameNoQuotes+ '\">' + boxLabels.VIEW_ALL_POSTS + '</a></li>';

    html += '<li><a href=\"' + blogUrl + '\" onClick=\"hideDiv(bloglinks)\" target=\"_blank\">' + boxLabels.GO_TO_BLOG + '</a></li>';
    html += '<li>&nbsp;</li>'

    html += '<li>'
    html += '<a href=\"http://wedoit4you.com/news4you/add_blog.php\">' + boxLabels.ADD_YOUR_BLOG_HERE + '</a></li>'

    html += '<li>&nbsp;</li>'
    html += '<li>&nbsp;</li>'
    html += '<li><u><a onClick=\"javascript:var bloglinks = $(\'bloglinks\'); new Effect.Fade(bloglinks);\">' + boxLabels.HIDE + '</a></u></li>';

    box = $('bloglinks');
    //box.innerHTML = '';
    box.style.position = 'absolute';
    box.innerHTML = html;

    curX = tempX
    curY = tempY

    //move towards the left if the icon is too much to the right
    if (curX > 500) {
       curX -= 210
       tempX -= 210
    }


    //If it wasn't there before
    if (box.style.display == 'none') {
        box.style.left = tempX + 'px';
        box.style.top = tempY + 'px';
        new Effect.Appear(box,{duration:0.5, opacity:0.6});
        
        lastX = tempX
        lastY = tempY

        //alert('('+curX+','+curY+') - ('+lastX+','+lastY+')')
    } else {
    //just move it with ajax efects.
        moveByX = -(lastX - curX)
        moveByY = -(lastY - curY)
        new Effect.MoveBy(box, moveByY, moveByX, {duration:0.4});
        lastX = curX
        lastY = curY
    }

    //showDiv('bloglinks');
}

//returns true if the fgiven email is a valid address, false otherwise.
function isEmailValid(email) {
  var emailRegexp=/^[^@\s]+@[^@\.\s]+(\.[^@\.\s]+)+$/
  return emailRegexp.test(email)
}

//Validates that a list of comma separated emails is valid.
function isEmailListValid(list) {
 emails = list.split(',')

 for (i=0; i < emails.length-1; i++) {
   if (!isEmailValid(emails[i].substr(' ','')))
    return false
 }
}
