function iecheck() {
  if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
    var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
    var iever = (rslt != null && Number(rslt[1]) >= 5.5 && Number(rslt[1]) <= 7 );
  }
  return iever;
}

BloggleyAd = new function() {
  var BASE_URL = 'http://bloggley.com/';
  var STYLESHEET = BASE_URL + "xss_magic.css";
  var CONTENT_URL = BASE_URL;
  var ROOT = 'my_xss_magic';

  function requestStylesheet(stylesheet_url) {
    stylesheet = document.createElement("link");
    stylesheet.rel = "stylesheet";
    stylesheet.type = "text/css";
    stylesheet.href = stylesheet_url;
    stylesheet.media = "all";
    document.lastChild.firstChild.appendChild(stylesheet);
  }

  function requestContent( bloggley_id,blog_id,style) {
    var script = document.createElement('script');
    // How you'd pass the current URL into the request
    // script.src = CONTENT_URL + '&url=' + escape(local || location.href);
    CONTENT_URL +=  'bloggley/dynamic_images?bloggley_id='+bloggley_id+'&blog_id='+blog_id+'&style='+style;
    script.src = CONTENT_URL;
    document.getElementsByTagName('head')[0].appendChild(script);
  }

	this.init = function(bloggley_id,blog_id,style) {
	  this.serverResponse = function(data,links) {
	    if (!data) return;
	    var div = document.getElementById(ROOT);
	    var txt = new Array();
	    for (var i = 0; i < data.length; i++) {
	      //if (txt.length > 0) { txt += " "; }
		  if(links[i]=="")
              txt.push("<td><img src='" + data[i] +"'  style='margin:2px;height:125px;width:125px;padding:2px;border:2px solid #CCCCCC;' /></td>");
		  else
		     txt.push("<td><a href='"+ links[i] +"' target='_blank'><img src='" + data[i] +"' style='margin:2px;height:125px;width:125px;padding:2px;border:2px solid #CCCCCC;' /></a></td>");
	    }
      
		var select_blog_url = "http://bloggley.com/bloggley/selected_bloggley/"+bloggley_id;
		for(var j= 0;j< 6-data.length;j++){
		 txt.push("<td><a href='"+ select_blog_url + "' target='_blank' ><img src='http://bloggley.com/images/default.png' style='margin:2px;height:125px;width:125px;padding:2px;border:2px solid #CCCCCC;' /></a></td>");
		}
    if (style==0)
      {
        txt.push("<td width='125px' ><a href='http://bloggley.com' target='_blank'><font color='red'>Powered by Bloggley</font></a></td>");
      }
     if (style==1)
      {
        txt.push("<td width='125px' style='padding-left:2px' ><a href='http://bloggley.com' target='_blank'><font color='red'>Powered by Bloggley</font></a></td>");
      }   
      if (style==2)
      {
        txt.push("<td colspan='3' ><a href='http://bloggley.com' target='_blank'><font color='red'>Powered by Bloggley</font></a></td>");
      }        
       if (style==3)
      {
        txt.push("<td colspan='2' style='text-align:center'><a href='http://bloggley.com' target='_blank'><font color='red'>Powered by Bloggley</font></a></td>");
      }     
   
	    var html_text = applyStyle(txt,style);
      //var temp = new Array();
      //temp.push("<tr><td><a href='http://www.bloggley.com'>Powered by Bloggley</a></td></tr>");
      html_text = html_text.replace(/,/g,'');
			div.innerHTML = "";
            div.innerHTML = "<table width='200' >" + html_text + "</table>" +
                    "<img src='http://bloggley.com/advertise/show_ad?y=" +
                     bloggley_id+"&g="+blog_id+"' alt='' width='0px' height='0px' style='display:none;' />";
	    div.style.display = 'block'; 
	  }
	  requestStylesheet(STYLESHEET);
    document.write("<div id='" + ROOT + "'  style='display: none;width:200;'></div>");
	  requestContent(bloggley_id,blog_id,style);
	  var no_script = document.getElementById('no_script');
	  if (no_script) { no_script.style.display = 'none'; }
	}
}

function applyStyle(txt,style){
  var html_text = "";
  if(style==3){
  for(var i=0;i<txt.length;i++){
				if(i%2==0)
				 html_text  = html_text + "<tr>"+txt[i];
				else
				 html_text  = html_text + txt[i] + "</tr>";
			}
  }
  
  else if(style==0){
    html_text = "<tr>" + txt +"</tr>"
  }
  
  else if(style==1){
    for(var i=0;i<txt.length;i++){
				 html_text  = "<tr>" + html_text + txt[i] + "</tr>";
				
			} 
  }
  
  else if(style==2){
	     for(var j=1;j<3;j++){
		  var n= j*3;
		  var k = (j-1)*3;
		  html_text =  "<tr>" + html_text;
		  for(var i=k;i < n;i++){
				 html_text  = html_text + txt[i] ;
				
			} 
			html_text =  html_text+ "</tr>"
			
		 }
      txt = txt.toString().split(",");
			html_text += "<tr align='center'>"+txt[txt.length-1]+"</tr>" 
  }
  
  return html_text;
}




