var RATING_LEVELS = 5;

function newRatingsInit(){
    // Test if the browser is safari 2.x and return imediately.
    // Safari 2.x and jQuery have some major problems, resulting in a crash of Safari when doing some actions
    // This might be removed in future, when Safari 2.x is not around anymore or jQuery has fixed those
    // cases.
    if ($.browser.safari) {
        var safariVersionString = $.browser.version;
        var safariMajorVersion = safariVersionString.substr(0, safariVersionString.indexOf('.'));
        if (parseInt(safariMajorVersion) < 500) {
            return;		// Bad! This is a Safari Version older than 3.x! We do not offer rating here.
        }
    }
    
    // test if cookies are enabled
    if (testCookie() == true) {
        init_rating();
    }
}

function testCookie(){
    if ($.cookie('ArticleRating')) {
        cookieContent = decodeURIComponent($.cookie('ArticleRating'));
        // console.log('Current Cookie: ' + cookieContent);
        
        cookieArticleIds = cookieContent.split('=');
        // console.log('cookieArticleIds.length: ' + cookieArticleIds.length);
        for (var i = 0; i <= cookieArticleIds.length; i++) {
            // console.log('cookieID: ' + cookieArticleIds[i] + ':' + rating_article_id);
            if (cookieArticleIds[i] == rating_article_id) {
                // console.log('User already rated article: ' + rating_article_id);
                // console.log("Returing false!");
                var count = $(".starRatingSrd .ratesCount").html();
                $(".starRatingSrd p").html("Sie haben diesen Artikel bereits bewertet. "+count+" Bewertung(en)");
                
            	$(".star-rating-live").each(function(){
            		//unbind all events from divs with stars.
            		$(this).unbind();
            	});
                return false;
            }
        }
        return true;
    }
    else {
        // try to set cookie
        // console.log('Trying to set cookie!');
        $.cookie('ArticleRating', '=',{expires: 30});
        // console.log('cookie set!');
        
        if ($.cookie('ArticleRating')) {
            // console.log('cookie read successfully');
            
            return true;
        }
    }
    return false;
}

function init_rating(){
    // Debug output to see if the data gets here.
    // console.log("Rating initialized for: " + rating_article_id);
    
    var userHasRated = 0;

    function submitRating(event){
    	// console.log(event.type);
         
        if (userHasRated != 1) {
        	// tmp = article_id:star_num
            var tmp = $(this).attr("id");
         
            var widgetId = tmp.substr(0, tmp.indexOf(':'));
            var starNbr = tmp.substr(tmp.indexOf(':') + 1);

            $.get("/wub/j/rating/submitRating.jsp", {
                ratingID: widgetId,
                value: starNbr
            });
            //
            var oldCookie = $.cookie('ArticleRating');
            var cookieArticleIds = oldCookie.split('=');
            var newCookie = widgetId+"=";
//            console.log('new cookie: ' + newCookie);
//            console.log('length :'+ cookieArticleIds.length);
            //keep max 100 cookie i<98 - at the end we have == that are splitted to two empty values
            for (var i = 0; i < (cookieArticleIds.length-1) && i<98; i++) {
            	newCookie = newCookie+cookieArticleIds[i]+"=";
//            	console.log('new cookie: ' + newCookie);
            }
            
            // console.log('Oldcookie: ' + oldCookie);
            $.cookie('ArticleRating', newCookie,{expires: 30});
            userHasRated = 1;
            //disable rating plugin logic.
            $(".star-rating-live").each(function(){
            	//unbind all events from divs with stars.
            	$(this).unbind();
            });
            // render thank you for voting. 
            $(".starRatingSrd p").html("Vielen Dank für Ihre Bewertung für diesen Artikel.");
        }
    }

    $(".starRatingSrd .star-rating").bind('click', submitRating);
}
