$(document).ready(function(){
    function bannerRegexp(src)
    {
        return src.match(/([0-9]+)x([0-9]+)\.([a-zA-z]+)$/);
    }

    function addNewsItem(lastIdBlock, insertAfter, content, renderDiv)
    {
        if (lastIdBlock <= 0)
        {
           $(renderDiv).html(content); 
        } else
        {
           insertAfter.after(content);
        }
    }

    function novinky(data, textstatus){
        var renderDiv = "#novinky_";
        var errorMsg = "Nemohu načíst novinky.";
        var lastIdBlock = 0;
        var insertAfter;
        var totalHeight = 0;
        
        if(textstatus == "success")
        {
            try
            {
               var d = eval('(' + data + ')');

               if (d.error == "false")
               {
                   $(renderDiv).empty();
                   var content = "";
                   var blockColor;  
                    
                   for (var i = 0; i < d.data.length; i++)
                   {

                       if (i % 2 == 0) blockColor = "#ebd3e0;";
                       else blockColor = "#f5e8ef;";

                       if (d.data[i].type == "banner")
                       {
                           var regexpData = bannerRegexp(d.data[i].src);

                           if (regexpData.length == 4)
                           {
                              var x = regexpData[1];
                              var y = regexpData[2];
                              var ext = regexpData[3];
                              
                              totalHeight += parseInt(y);
                              
                              if (ext == "swf")
                              {
                                  // flash banner
                                  /*
                                  content = "<div id = 'novinka_" + lastIdBlock + "' style='display:block; width: 100%; margin: 0px 0px 10px 0px; background: " + blockColor + "; border-style: solid none none none; border-color: #fff; border-width: 1px; padding: 4px 0px 4px 0px; height: " +  y + "px'>" +
                                            "<div id='novinka_anchor" + lastIdBlock + "' style=' height: " +  y + "px;'></div><div id='novinka_flash" + lastIdBlock + "'></div>" +
                                            "<br style='clear: both' /></div>";
                                  */
/*
                                  // toto je obsah s falesnym tlacitkem
                                  content = "<div id = 'novinka_" + lastIdBlock + "' style='display:block; width: 100%; margin: 0px 0px 0px 0px; background: " + blockColor + "; border-style: solid none none none; border-color: #fff; border-width: 1px; padding: 0px 0px 0px 0px; height: " +  y + "px'>" +
                                            "<div id='novinka_anchor" + lastIdBlock + "' style=' height: " +  y + "px;'></div><div id='novinka_flash" + lastIdBlock + "'></div>" +
                                            "<br style='clear: both' /></div>";
*/                                            

                                  // toto je obsah bez falesneho tlacitka
                                  content = "<div id = 'novinka_" + lastIdBlock + "' style='display:block; width: 100%; margin: 0px 0px 0px 0px; background: " + blockColor + "; border-style: solid none none none; border-color: #fff; border-width: 1px; padding: 0px 0px 0px 0px; height: " +  y + "px'>" +
                                            "<div id='novinka_flash" + lastIdBlock + "'></div>" +
                                            "<br style='clear: both' /></div>";
                                  
                                  addNewsItem(lastIdBlock, insertAfter, content, renderDiv);
                                  
                                  var flashBox = "#novinka_flash" + lastIdBlock;
                                  $(flashBox).flash(
                                      { src: d.data[i].src,
                                        width: x,
                                        height: y,
                                        wmode: 'transparent' },
                                      { version: 8 }
                                  );
/*
                                  var anchorBox = "#novinka_anchor" + lastIdBlock;
                                  $(anchorBox).flash(
                                      { src: '/_tech/global/button.swf',
                                        width: x,
                                        height: y,
                                        wmode: 'transparent',
                                        flashvars: {
                                          where: d.data[i].href,
                                          bwidth: x,
                                          bheight: y
                                        }},
                                      { version: 8 }
                                  );
*/
                                  /*
                                  $(flashBox).css({position: 'absolute', zIndex: 1, height: x, width: y, paddingLeft: '8px'});
                                  $(anchorBox).css({position: 'absolute', zIndex: 10, height: x, width: y, paddingLeft: '8px'});
                                  */
                                  $(flashBox).css({position: 'absolute', zIndex: 1, height: y, width: x, paddingLeft: '0px'});
                                  //$(anchorBox).css({position: 'absolute', zIndex: 10, height: y, width: x, paddingLeft: '0px', cursor: 'pointer'});

                              } else if (ext == "jpg" || ext == "png" || ext == "gif")
                              {
                                  // image banner
                                  /*
                                  content = "<div id = 'novinka_" + lastIdBlock + "' style='text-align: center; display:block; width: 100%; background: " + blockColor + "; border-style: solid none none none; border-color: #fff; border-width: 1px; padding: 4px 0px 4px 0px; min-height: 40px;'>" +
                                            "<a href='" + d.data[i].href + "'><img title='" + d.data[i].title + "' src='" + d.data[i].src + "' style = ' margin: 4px 0px 4px 0px' width='" + x + "' height='" + y + "' /></a>" +
                                            "<br style='clear: both' /></div>";
                                  */
                                  content = "<div id = 'novinka_" + lastIdBlock + "' style='text-align: center; display:block; width: 100%; background: " + blockColor + "; border-style: solid none none none; border-color: #fff; border-width: 1px; padding: 0px 0px 0px 0px; min-height: 38px;'>" +
                                            "<a href='" + d.data[i].href + "'><img title='" + d.data[i].title + "' src='" + d.data[i].src + "' style = ' margin: 1px 0px 1px 0px' width='" + x + "' height='" + y + "' /></a>" +
                                            "<br style='clear: both' /></div>";
                                    
                                  addNewsItem(lastIdBlock, insertAfter, content, renderDiv);
                              }
                           }
                       } else if (d.data[i].type == "news")
                       {
                          /*
                          content = "<div id = 'novinka_" + lastIdBlock + "' style='display:block; width: 100%; background: " + blockColor + "; border-style: solid none none none; border-color: #fff; border-width: 1px; padding: 4px 0px 4px 0px; min-height: 40px;'>" +
                                    "<div style='margin: 4px 0px 4px 8px;'><img src='" + d.data[i].src + "' style='width: 37px; height: 37px; margin-right: 8px; margin-top: 2px; border: solid 1px; border-color: " + d.data[i].titleColor + "; float: left;' />" +
                                    "<strong style='display: block; margin:0px 0px 2px 0px; width:174px; height: 11px; min-width:174px; min-height: 11px; overflow: hidden;  color: " + d.data[i].titleColor + "'>"+ d.data[i].date + " " + d.data[i].title + "</strong>" +
                                    "<a class='fLink' style='color: #58595B; overflow: hidden; width:174px; height: 26px; min-width:174px; min-height: 26px;' href='" + d.data[i].href + "'>" + d.data[i].text + "</a>" +
                                    "<br style='clear: both' /></div></div>";
                          */
                          content =  "<div id = 'novinka_" + lastIdBlock + "' style='display:block; width: 100%; background: " + blockColor + "; border-style: solid none none none; border-color: #fff; border-width: 1px; padding: 7px 0 7px 0;'>";
                          content += "<div style='margin: 0 1px 0 8px;'><img src='" + d.data[i].src + "' style='width: 37px; height: 37px; margin-right: 8px; margin-top: 2px; border: solid 1px; border-color: " + d.data[i].titleColor + "; float: left;' />";
                          content += "<strong style='display: block; margin:0px 0px 2px 0px; width:174px; height: 13px; min-width:174px; min-height: 13px; overflow: hidden;  color: " + d.data[i].titleColor + "'>";
                          if (d.data[i].date.length > 0) {
                            content += d.data[i].date + " "
                          }
                          content += d.data[i].title + "</strong>";
                          content += "<a class='fLink' style='color: #58595B; overflow: hidden; width:174px; height: 27px; min-width:174px; min-height: 27px; line-height: 13px;' href='" + d.data[i].href + "'>" + d.data[i].text + "</a>";
                          content += "<br style='clear: both' /></div></div>";
                                    
                          totalHeight += 60;                    
                                             
                          addNewsItem(lastIdBlock, insertAfter, content, renderDiv);
                       }
                       
                       if (totalHeight >= 250) break;
                        
                       var search = "#novinka_" + lastIdBlock;
                       lastIdBlock++; 
                       insertAfter = $(search);
                   }
               } else window.alert(errorMsg);
            } catch(e)
            {
                window.alert(errorMsg);
            }
        } else
        {
            window.alert(errorMsg);
        }
    }

    var ajaxBase = location.href;
    var ajaxBase = ajaxBase.match(/http:\/\/[^\/]*/i);
    var url2Get = "/_apps/sodexo_novinky/get.asp";
    var url2GetParams = "";
    
    if (typeof(reqId) != "undefined") { if (reqId.length > 0) { url2GetParams += "?id=" + reqId; } }
    if (typeof(reqNoActive) != "undefined") {
      if (reqNoActive.length > 0) { 
        if (url2GetParams.length > 0) {
          url2GetParams += "&noactive=" + reqNoActive;
        } else {
          url2GetParams += "?noactive=" + reqNoActive;
        }
      }
    }

    ajaxBase = ajaxBase[0];
    
    $.post(ajaxBase + url2Get + url2GetParams, novinky);
});

