/**
* 
*/
function toHTML(str){
    str = str.replace(/&/g, "&amp;");
    str = str.replace(/"/g, "&quot;");
    str = str.replace(/</g, "&lt;");
    str = str.replace(/>/g, "&gt;");
    return str;
}


function showQuote(showImmediately){
   
    //check quotes are valid.
    if (showQuote.quotes.length < 2){throw Error("showQuote: Must have at least two quotes so work")}
    
    //choose a random quote
    if (showQuote.remainingQuotes.length == 0){
        showQuote.remainingQuotes = showQuote.quotes;
    }
    
    var index = 0;
    var quote = showQuote.remainingQuotes[index];
    
    var html = quote.url ? '<q cite="'+ toHTML(quote.url) +'">' : '<q>';
    html += quote.quote;
    html += "</q>";
    if (quote.name){
        html += "<br /><span>" + toHTML(quote.score) + " "
        if (quote.url){
            html += '<a href="'+ toHTML(quote.url) +'" target="_top">'+ toHTML(quote.name) +'</a>';
        }
        else {
            html += toHTML(quote.name);
        }
        html += "</span>";
    }
    
    
    if (showImmediately === true){
        $("#quotes").html(html);
    }
    else {
        $("#quotes").fadeOut(555, function(){
            $("#quotes").html(html).fadeIn();
        });
    }
    //remove the quote from the remaining quotes
    var quotes = [];
    for (var i=0; i<showQuote.remainingQuotes.length; i++){
        if (i != index){
            quotes.push(showQuote.remainingQuotes[i]);
        }
    }
    
    showQuote.remainingQuotes = quotes;
}



/**
* show random quotes
*/
//and show one right now.
$(document).ready(function(){
    if ($("#quotes").get(0)){
        showQuote(true);
        
        //show a new quote every so often
        setInterval(showQuote, showQuote.interval);
    }
});


showQuote.interval = 7000;
showQuote.remainingQuotes = [];
showQuote.quotes = [];
showQuote.quotes.push({
    //Quoted text 
    "quote" : "It took 23 years, but Arkanoid has been bested",
    
    //Name/alias of person being quoted 
    "name" : "IGN.com",
    //spacer needed if a one line review <br>, otherwise ""
    "spacer" : "<br>",
    //rating
    "score" : "9/10", 
    //url of the quote
    "url" : "http://ps3.ign.com/articles/100/1005893p1.html"
});

showQuote.quotes.push({
    "quote" : "Shatter is an absolute must-buy",
    "name" : "PSXExtreme.com",
    "spacer" : "<br>",
    "score" : "9/10",
    "url" : "http://www.psxextreme.com/ps3-reviews/267.html"
});

showQuote.quotes.push({
    "quote" : "I'll be playing this one for a long time to come",
    "name" : "DigitalChumps.com",
    "spacer" : "<br>",
    "score" : "9.2/10",
    "url" : "http://www.digitalchumps.com/game-reviews/35-ps3/3423-shatter.html"
});

showQuote.quotes.push({
    "quote" : "The game is, quite simply, phenomenal",
    "name" : "TotalPlayStation",
    "score" : "10/10",
    "url" : "http://totalplaystation.com/ps3/Shatter/reviews/8452"
});

showQuote.quotes.push({
    "quote" : "Shatter is the PSN's shining beacon in the growing \"New Arcade\" market",
    "name" : "PushSquare.com",
    "spacer" : "",
    "score" : "4/4",
    "url" : "http://www.pushsquare.com/4694/shatter-on-playstation-3-review/"
});

showQuote.quotes.push({
    "quote" : "The completely original soundtrack is one of best you'll find in any game",
    "name" : "GamerLimit.com",
    "spacer" : "",
    "score" : "9/10",
    "url" : "http://gamerlimit.com/2009/07/gamer-limit-review-shatter/#more-32471"
});

showQuote.quotes.push({
    "quote" : "Shatter offers an extremely enjoyable game with a great personality",
    "name" : "Gamer2.0",
    "score" : "9/10",
    "url" : "http://blog.gamer20.com/2009/07/review-shatter-ps3/"
});

showQuote.quotes.push({
    "quote" : "Incredibly fresh and insanely fun",
    "name" : "HookedGamers.com",
    "score" : "9/10",
    "url" : "http://www.hookedgamers.com/ps3/shatter/review/article-415.html"
});


//~ showQuote.quotes.push({
    //~ "quote" : "",
    //~ "name" : "",
    //~ "score" : "",
    //~ "url" : "https://addons.mozilla.org/en-US/firefox/reviews/display/6984#reviews"
//~ });
