// V 0.0018
$APIKey = "a2b866efde7b7093d78419cc97e6c2c5";
$businessID = "907137";
$listingID = "902350";

fullstarimagelocation = 'images/smallStarFullDark.gif';
halfstarimagelocation = 'images/smallStarHalfDark.gif';
url = "http://api.n49.ca/API/rest/JSON?api_key=" + $APIKey + "&action=Reviews.getAllReviews&callback=?";
reviewuslink = "http://www.n49.ca/d/" + $listingID + "?review_me=1";
usesamples = 0;


var m_names = new Array("Jan", "Feb", "Mar","Apr", "May", "Jun", "Jul", "Aug", "Sep","Oct", "Nov", "Dec");


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>");
			if (window.location.hash == "#sample"){
				usesamples = 1;
				document.write("<scr" + "ipt type=\"text/javascript\" src=\"js/samplereviews.js\"></scr" + "ipt>");
			} else if (window.location.hash == "#sample30") {
				usesamples = 1;
				document.write("<scr" + "ipt type=\"text/javascript\" src=\"js/30samplereviews.js\"></scr" + "ipt>");
			}
        }
        setTimeout("initJQuery()", 50);
    } else {
        $(function() {
			texttobereplaced = '';
			$("a[href='http://www.n49.ca/d/listingid']").attr('href', 'http://www.n49.ca/d/' + $listingID);
			
			if (usesamples === 0) {
				$.getJSON(url , function(data) {
					processData(data);
				});
			} else {
				processData(data);
			}
        });
    }
}

function processData(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;
					
					
					var dateArray = added.split(/[- :]/),
					addeddate = new Date(dateArray[0], dateArray[1]-1, dateArray[2], dateArray[3], dateArray[4], dateArray[5]);
					var curr_date = addeddate.getDate();
					var curr_month = addeddate.getMonth();
					var curr_year = addeddate.getFullYear();
					var formateddate = m_names[curr_month] + " " + curr_date + ", " + + curr_year;  
					
                    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 + '<div class="testimonial"><div class="left">' + rankingstr + '<br>' + formateddate + '</div><div class="right"><h3><a href="' + link + '">' + title + '</a></h3><p>' + long_description + '</p></div></div>';
                    } else {
                        texttobereplaced = texttobereplaced + '<div class="testimonial"><div class="left">' + rankingstr + '<br>By: <a href="' + userlink + '">' + username + '</a><br/>' + formateddate + '</div><div class="right"><h3><a href="' + link + '">' + title + '</a></h3><p>' + long_description + '</p></div></div>';
                    }
                });
				if (texttobereplaced != ''){
					$("#reviews-wrap").html(texttobereplaced);
				}
}
initJQuery();
