The first few lines just sets some variables.
if(plugin) I think is testing if the variable does not equal null, because that would be the case if the objects it is string to access don't exist.
It then gets a number or else write some bad html. document.write() gets the document object and writes something to it.
This it does the if thing again, why not just do it once? and write some more html.
I suppose it is trying to write some ads.