/**
 * Star Rating - jQuery plugin
 *
 * Copyright (c) 2007 Wil Stuckey
 * Modified by John Resig
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 */

/**
 * Create a degradeable star rating interface out of a simple form structure.
 * Returns a modified jQuery object containing the new interface.
 *   
 * @example jQuery('form.rating').rating();
 * @cat plugin
 * @type jQuery 
 *
 */

    function submitVote(id_ocena, wartosc) {
        ajaxObj.call('action=ocena,submit_vote&op=vote&id_ocena='+id_ocena+'&wartosc='+wartosc, showResults);
    }

    function showResults(resp) {
        __setCookie(resp.cookieName, resp.cookieVal, resp.cookieDomain);
    }

    function __setCookie(cookieName, cookieVal, cookieDomain) {
        var now = new Date();
        fixDate(now);
        now.setTime(now.getTime() + 1000 * 365 * 24 * 60 * 60 * 1000);
        setCookie(cookieName, cookieVal, now, '/', cookieDomain);
    }jQuery.fn.rating = function( voteId ){
    return this.each(function(){
        var div = jQuery("<div/>").attr({
            title: this.title,
            className: this.className
        }).insertAfter( this );

        jQuery(this).find("select option").each(function(){
            div.append( this.value == "0" ?
                "<div class='cancel'><a title='Oceń'>Oceń</a></div>" :
                "<div class='star'><a title="+this.value+">" + this.value + "</a></div>" );
        });

        var averageRating = this.title.split(/:\s*/)[1].split("."),
            url = this.action,
            averageIndex = averageRating[0],
            averagePercent = averageRating[1];

        // hover events and focus events added
        var cookie = getCookie('ocena_'+voteId);

        if( !cookie ) {
            var stars = div.find("div.star")
                .mouseover(drainFill).focus(drainFill)
                .mouseout(drainReset).blur(drainReset)
                .click(click);

            // cancel button events
                div.find("div.cancel")
                .mouseover(drainAdd).focus(drainAdd)
                .mouseout(resetRemove).blur(resetRemove)
                .click(click);
        }
        else {

            var stars = div.find("div.star")
                .mouseover(drainReset).focus(drainReset)
                .mouseout(drainReset).blur(drainReset)
                ;            // cancel button events
                div.find("div.cancel")
                .mouseover(resetRemove).focus(resetRemove)
                .mouseout(resetRemove).blur(resetRemove)
                ;

        }
            

        reset();

        function empty() {}

        function drainFill(){ drain(); fill(this); }
        function drainReset(){ drain(); reset(); }
        function resetRemove(){ reset(); jQuery(this).removeClass('on'); }
        function drainAdd(){ drain(); jQuery(this).addClass('on'); }

        function click(){

            //alert(stars);

            var cookie = getCookie('ocena_'+voteId);
            if( cookie ) return false;

            averageIndex = stars.index(this) + 1;
            averagePercent = 0;
            
            //alert(averageIndex);

            if ( averageIndex == 0 )
                drain();

            submitVote(voteId, averageIndex);

            /*
            jQuery.post(url,{
                rating: jQuery(this).find('a')[0].href.slice(1)
            });
            */

            stars = div.find("div.star")
                .mouseover(drainReset).focus(drainReset)
                .mouseout(drainReset).blur(drainReset)
                ;            // cancel button events
                div.find("div.cancel")
                .mouseover(resetRemove).focus(resetRemove)
                .mouseout(resetRemove).blur(resetRemove)
                ;

            reset();

            return false;
        }
        // fill to the current mouse position.
        function fill( elem ){
            stars.find("a").css("width", "100%");
            stars.lt( stars.index(elem) + 1 ).addClass("hover");
        }
    
        // drain all the stars.
        function drain(){
            stars.removeClass("on hover");
        }

        // Reset the stars to the default index.
        function reset(){
            stars.lt(averageIndex).addClass("on");

            var percent = averagePercent ? averagePercent * 10 : 0;
            if (percent > 0)
                stars.eq(averageIndex).addClass("on").children("a").css("width", percent + "%");
        }
    }).remove();
};

// fix ie6 background flicker problem.
if ( jQuery.browser.msie == true )
    document.execCommand('BackgroundImageCache', false, true);


