$(document).ready(
    function () {
        //get the entry ID from the url
        var urlArray = location.href.split('/');
        var entryId = urlArray[5];

        var ratingEnabled = true;
        var ratingWidget = $('ul.entry-rating');
        var ratingWidgetWrapper = ratingWidget.parent('div');
        var instructions = ratingWidgetWrapper.find('.instructions');

        $('.entry-rating a').each(function() {
            var rating = $(this).html();  //the # contained by the anchor tag

            $(this).mouseover(function() {
                if (ratingEnabled) {
                    //switch to rating control
                    ratingWidget.removeClass('entry-rating');
                    ratingWidget.addClass('entry-control entry-control-' + rating);
                }
            });

            $(this).mouseout(function() {
                if (ratingEnabled) {
                    //switch to actual rating
                    ratingWidget.addClass('entry-rating');
                    ratingWidget.removeClass('entry-control entry-control-' + rating);
                }
            });

            $(this).click(function() {
                if (ratingEnabled) {
                    //disable rating
                    ratingEnabled = false;

                    //hide the widget, clear all ratings and control classes
                    ratingWidget.toggle();
                    ratingWidget.attr('class', 'entry-rating disabled');

                    //add a spinner
                    ratingWidgetWrapper.append('<div class="ajax-spinner"></div>');

                    //AJAX call
                    jQuery.get('/gallery/rate/' + entryId + '/' + rating + '/ajax', '', function(newRating) {
                        //add new rating
                        $('.ajax-spinner').remove();
                        ratingWidget.addClass('entry-rating-' + newRating);
                        ratingWidget.toggle();
                        instructions.html('Thank you for rating');
                    });
                    
                    // Google Tracking
                    _gaq.push(['_trackEvent', 'engagement', 'rating', entryId]);
                }

                return false;
            });
        });

        $('a.comment-flag').each(function() {
            var commentUrlArray = $(this).attr('href').split('/');
            var commentId = commentUrlArray[4];

            $(this).click(function() {
                //show spinner
                $(this).replaceWith('<div class="comment-flag ajax-spinner"></div>');

                //AJAX call
                jQuery.get('/gallery/flag-comment/' + entryId + '/' + commentId + '/ajax', '', function() {
                    $('.comment-flag.ajax-spinner').replaceWith('<p class="ajax-thankyou">This comment is flagged.</p>');
                });

                return false;
            });
        });
    }
);

// Facebook connect

FB_RequireFeatures(['XFBML'], function(){
    FB.Facebook.init('e09782a0f4238fc82bbf7fc19f046204', '/xd_receiver.htm');
});
function fbLink( href ) {
    
    FB_RequireFeatures(['XFBML'], function() {
        var message = "Check out the great deal I got from SYMS! Vote for me and I could win $200.";
        var tracking = '?campaign=social&utm_medium=social_sharing&utm_source=facebook';
        
        var generic = 'http://igotitatsyms.com' + tracking;
        
        if (href != '') {
            var link = href + tracking;
        } else {
            var link = window.location + tracking;
        };

        var attachment = {
            'name':'I got it at SYMS',
            'href':generic,
            'description':"We’re giving our customers the chance to show just how educated they are. Just send us a photo of the great deal you got at SYMS then tell all your friends. The photo with the most votes every month gets a $200 Gift Card from us. You look like a million bucks. Now you can celebrate by going on a $200 shopping spree.",
            'media': [{ 'type': 'image', 'src': 'http://igotitatsyms.com/facebook/igotitatsyms.jpg', 'href':link}]
        };
        var action_links = [{'text':"I got it at SYMS.", 'href':link}];
        FB.Connect.requireSession( function() { FB.Connect.streamPublish(message, attachment, action_links) } );
    });
}

function tweet( bitly ) {
    
    var generic = 'http://bit.ly/a3JHDs';
    
    if (bitly != '') {
        var link = bitly;
    } else {
        var link = generic;
    };

    var message = escape("Check out the great deal I got from SYMS! Vote for me and I could win $200. " + link);
    window.open('http://twitter.com/home?source=igotitatsyms.com&status=' + message, 'twitter');
}
