// V 0.0017
APIKey = '364062cce767fff4c10983d724c24f10';
businessID = '899326';
listingID = '893292';

fullstarimagelocation = 'http://www.orsanairservices.com/images/smallStarFullDark.gif';
halfstarimagelocation = 'http://www.orsanairservices.com/images/smallStarHalfDark.gif';
url = "http://api.n49.ca/API/rest/JSON?api_key=" + APIKey + "&action=Reviews.getAllReviews";
reviewuslink = "http://www.n49.ca/d/" + listingID + "?review_me=1";

var jQueryScriptOutputted = false;

function initJQuery() {
    if (typeof(jQuery) == 'undefined') {
        if (!jQueryScriptOutputted) {
            jQueryScriptOutputted = true;
            document.write("<scr" + "ipt type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js\"></scr" + "ipt>");
        }
        setTimeout("initJQuery()", 50);
    } else {
        $(function() {
			texttobereplaced = '';
			$("a[href='http://www.n4.ca/d/listingid']").attr('href', 'http://www.n49.ca/d/' + listingID);
            $.getJSON(url + "&callback=?", function(data) {
                jQuery.each(data.reviews.review, function(i, val) {
                    id = val.id;
                    rating = parseFloat(val.rating);
                    link = val.link;
                    title = val.title;
                    userlink = val.author.link;
                    username = val.author.username;
                    added = val.added;
                    long_description = val.long_description;
                    link = val.link;
                    fullstarimage = '<img src="' + fullstarimagelocation + '" alt="*"/>';
                    halfstarimage = '<img src="' + halfstarimagelocation + '" alt="*"/>';
                    fullstars = '';
                    halfstars = '';
                    for (i = 1; i <= parseInt(rating / 2, 10); i++) {
                        fullstars = fullstars + fullstarimage;
                    }
                    if (parseInt(rating / 2, 10) != parseFloat(rating / 2)) {
                        halfstars += halfstarimage;
                    }
                    rankingstr = fullstars + halfstars;
                    if (username == 'non-member') {
                        texttobereplaced = texttobereplaced + '<h3><a href="' + link + '">' + title + '</a></h3>' + rankingstr + '<br/>' + added + '<p>' + long_description + '</p><br/>';
                    } else {
                        texttobereplaced = texttobereplaced + '<h3><a href="' + link + '">' + title + '</a></h3>' + rankingstr + '<br/>By: <a href="' + userlink + '">' + username + '</a><br/>' + added + '<p>' + long_description + '</p><br/>';
                    }
                });
				if (texttobereplaced != ''){
					$("#reviews-wrap").html(texttobereplaced);
				}
            });
            
        });
    }
}
initJQuery();
