﻿/*
Created By One_v.co.il
Author: Arnold
*/

jQuery(function ($) {
    if ($.browser.msie && parseFloat($.browser.version) < 9) {
        $(".evenTable").find("tr:odd").addClass("even");
    }
});

function get_rss_feed(url, useProxy, getAll, numOfRecords, containerID, shownum, animate) {
    if (useProxy) {
        url = "proxy/proxy.aspx?url=" + url;
    }
    //clear the content in the div for the next feed.
    $("#" + containerID).empty();
    $("#" + containerID).append('<div class="preloader"></div>');
    var blogList = [];

    $.ajax({
        type: 'GET',
        url: url,
        dataType: 'xml',
        success: function (d) {
            var records;

            var items = $(d).find('item');
            if (getAll) {
                records = items.length;
            } else {
                records = numOfRecords;
            }


            var shownumList = "";
            var _tempItem = "";
            for (i = 0; i < records; i++) {
                if (items[i] != undefined) {
                    var $item = $(items[i]);
                    var name = $item.find('name').text();
                    var title = $item.find('title').text();
                    var link = $item.find('link').text();
                    var image = $item.find('image').text();
                    var description = $item.find('description').text();

                    var hasDesc = (description.length > 0);
                    if (hasDesc) {
                        description = description.replace(/<.*?>/g, '').substring(0, ((description.length > 150) ? 150 : description.length)) + "...";
                    }

                    _tempItem += '<div id="post-item-' + i + '" class="post-item">' +
                                    '<div class="post-img rfloat">' +
                                        '<img src="' + image + '" alt="' + name + '" />' +
                                    '</div>' +
                                    '<div class="post-data">' +
                                        '<a href="' + link + '" target="_blank">' +
                                            '<span class="post-name">' + name + '</span>' +
                                            '<span class="post-title">' + title + '</span>';
                    if (hasDesc) {
                        _tempItem += '<span class="post-description">' + description + '</span>';
                    }
                    _tempItem += '</a>' +
                                    '</div>' +
                                '</div>';

                    if ((shownum && shownum != 0 && i < shownum) || !animate) {
                        shownumList += _tempItem;
                    }
                    if (animate) {
                        blogList.push(_tempItem);
                    }
                    _tempItem = "";

                }
            }
            if ((shownum && shownum != 0) || !animate) {
                $("#" + containerID).html(shownumList);
            }
            if (animate) {
                showOneBlog(containerID, blogList, 0, shownum);
            }
        }
    });

};


function get_rss_feed_live(url, useProxy, getAll, numOfRecords, containerID) {
    if (useProxy) {
        url = "proxy/proxy.aspx?url=" + url;
    }
    //clear the content in the div for the next feed.
    $("#" + containerID).empty();
    $("#" + containerID).append('<div class="preloader"></div>');

    $.get(url, function (d) {
        var records;
        var _html = "";
        var items = $(d).find('item');
        if (getAll) {
            records = items.length;
        } else {
            records = numOfRecords;
        }


        for (i = 0; i < records; i++) {
            if (items[i] != undefined) {
                var $item = $(items[i]);
                var title = $item.find('title').text();
                var link = $item.find('link').text();
                var description = $item.find('description').text().replace(/<.*?>/g, '').substring(0, 150);

                _html += '<li><a href="' + link + '" target="_blank">' + title + '</a><br/>' + description + '...</li>';
            }
        }
        $("#" + containerID).html(_html);
    });


};




var enterOnce = true;
function showOneBlog(containerID, blogList, counter, startIndex) {
    if (startIndex && enterOnce) {
        counter = (startIndex > blogList.length) ? blogList.length : startIndex;
        enterOnce = false;
    }
    setTimeout(function () {
        
        //clearTimeout(interval);
        if (counter == 0) {
            $("#" + containerID).find(".preloader").remove();
        } else if (counter == blogList.length) {
            counter = 0;
        }
      
        var obj = $("#" + containerID).prepend(blogList[counter]).find("div.post-item:first").superShow(function () {
            counter++;
            showOneBlog(containerID, blogList, counter);
        });
    }, 5000);
};


jQuery.fn.superShow = function (callback) {
    var currH = $(this).outerHeight();
    return this.css({ height: 0, opacity: 0 }).animate({
        height: currH
    }, 500, function () {
        $(this).animate({
            opacity: 1
        }, 1000, function () {
            if (jQuery.browser.msie) {
                removeFiltes($(this)[0]);
            }
            if (callback && typeof (callback) == 'function') {
                callback();
            }
        });
    });
};

String.prototype.format = function () {
    var s = this,
        i = arguments.length;

    while (i--) {
        s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
    }
    return s;
};

// **** Remove Opacity-Filter in IE ****
function removeFiltes(element) {
    if (element.style.removeAttribute) {
        element.style.removeAttribute('filter');
    }
};
