﻿
$(document).ready(function () {
    
    //NEWS FEED
    function randOrd() {
        return (Math.round(Math.random()) - 0.5);
    }

    feedsArray = new Array('http://www.recruiter.co.uk/XmlServers/navsectionRSS.aspx?navsectioncode=6',
                        'http://www.personneltoday.com/feed/d3dd72cd-0f98-455d-a28c-c43c135800e6/Personnel%20Today/PersonnelToday.com%20-%20Recruitment%20and%20staffing%20news.xml')

    feedsArray.sort(randOrd);
    strFeed = feedsArray[0];

    $.jGFeed(strFeed,
    function (feeds) {
        // Check for errors
        if (!feeds) {
            // there was an error
            return false;
        }
        // do whatever you want with feeds here
        for (var i = 0; i < feeds.entries.length; i++) {
            var entry = feeds.entries[i];
            // Entry title
            var _title = entry.title;
            var _link = entry.link;
            var _snippet = entry.snippet;
            var _content = entry.content;

            if (_content.length > 100) {
                _content = _content.substr(0, _content.indexOf("."))
            }
            _content += " ...";

            $('#newsFeed').append('<div class="newsItem" />');
            $('.newsItem:last').append('<div class="newsHeading"><a href="' + _link + '">' + _title + '</a></div>')
                .append('<div class="newsContent">' + _content + '</div> ');

            $('.newsItem:even').css('background-color', '#F7F7F7');
        }
    }, 4);


    //SEARCH BOX


    //RECENT REVIEWS
    //var here  ==> global scope
    var comments = [];
    var commentParts = [];
    var shortComment = [];

    $('.comments').each(function (i) {
        //no var  ==> global scope
        comments[i] = $(this).text();
        commentParts = comments[i].split('.');
        shortComment[i] = commentParts[0] + '. ';
        $(this).text(shortComment[i]);
    });

    $('.comments').after('<div class="toggleComments shortened"> more ... </div >').show();

    //Add toggle() event
    $('.toggleComments').click(function () {
        var index = $(".toggleComments").index(this);

        if ($(this).hasClass('shortened')) {
            $(this).removeClass('shortened').text(' less ...');
            $(this).prev().text(comments[index]);

        }
        else {
            $(this).addClass('shortened').text(' more ...');
            $(this).prev().text(shortComment[index]).show('slow');
        }
    });


});

//STAR RATINGS
$(function () {
    $('span.stars').stars();
});

$.fn.stars = function () {
    $(this).each(function () {
        //Get the value
        var val = parseFloat($(this).html());
        if (isNaN(val)) {
            val = 0;
            $(this).after('Not yet reviewed').wrap('<div>');
        }
        //Make sure valuse is between 0 and 5
        val = val > 10 ? 10 : (val < 0 ? 0 : val);
        val = Math.round(val * 2) / 2; //Round to nearest half
        //Calculate physical size
        var size = 8 * val;
        //Create stars holder
        var stars = $('<span class="stars"><span></span></span>');
        //Adjust yellow star width
        stars.find('span').width(size);
        //replace numerical value with stars
        $(this).replaceWith(stars);
    });
}


