$(document).ready(
  
  function () {
  
    var rating_clicked = false;
    
    $('#rate_site input:checked').each( function() {
        var idname = $(this).attr('id');
        $(this).parent().parent().toggleClass(idname);
    });
    
    $('#rate_site label').hover(
      function() {
        if (!rating_clicked) {
          var idname = $(this).find('input').attr('id');
          $(this).parent().toggleClass(idname);
        }
      },
      function() {
        if (!rating_clicked) {
          var idname = $(this).find('input').attr('id');
          $(this).parent().toggleClass(idname);
        }
      }
    );
    
    $('#rate_site label').click( function() {
        rating_clicked = true;
        var element = $(this).find('input').attr("checked", true); ;
        var idname = element.attr('id');
        
        var p = $(this).parent();
        p.removeClass('rating-none');
        p.removeClass('rating-1');
        p.removeClass('rating-2');
        p.removeClass('rating-3');
        p.removeClass('rating-4');
        p.removeClass('rating-5');
        p.addClass(idname);
    });
   
    $('div.usefulBar li.action a').click(
       function() {
				  var helpful = $(this).attr('class');
          var message_box = $(this).parent().parent().parent().find('.feedback-thanks'); // div's can't be inside of ul's, doesn't work in IE. might be a better way to traverse. - nh
          var num = $(this).parent().parent().parent().find('.num');
					var tot = $(this).parent().parent().parent().find('.total');
					var li = $(this).parent().parent().find('li');
					//message_box.show();
          
					var rid = $(this).attr('rid');
          var value = $(this).attr('class');
          $.post("/write/helpfulness?random=" + Math.round(Math.random()*21474836747) , { helpful: value, rid: rid } );
          if (message_box.hasClass('success')) {
						li.removeClass('check');
						$(this).parent().addClass('check');
						/* change numbers?
						number = num.text();
						total = tot.text();
						makenum = number*1;
						maketotal = total*1;
						
						if (tot.hasClass('voted')==false) {
							tot.text(maketotal+1);
						}
						
						if (helpful=='yes') {
							num.text(makenum+1);
						} else {
							num.text(makenum-1);
						}
						
						tot.addClass('voted');
            */
						//message_box.fadeOut(3000);
          }	else {
						message_box.show();
					}
					
					message_box.find('.close').click(
						function() {
							$(this).parent().hide();
							$(this).parent().parent().find('ul').show();	
						}
					)
						
			}
    )
    $('#sort').change(
			function() {
				 $("#sort option:selected").each(function () {
					//alert($(this).val());
					$.cookie('sort', $(this).val(), { path: '/', expires: 365});  
			   })
				 location.reload();
			}
		)
  }
);

var _wpObj = {
    star: null,
    site: null,
    text: null,
    title: null,
    load: function(st, si, te, ti, au)
    {
        this.star   = st;
        this.site   = si;
        this.text   = te;
        this.title  = ti;
        this.author = au;
    },
    reset: function()
    {
        this.star   = null;
        this.site   = null;
        this.text   = null;
        this.title  = null;
        this.author = null;
    },
    genObj: function()
    {
        var obj = {
            'images' : [],
            'stars'  : '',
            'site'   : '',
            'text'   : '',
            'title'  : '',
            'author' : ''
        };

	if (templateID == 282649415426)
	      obj['images'] = [{'src':'http://traffic.alexa.com/graph?&w=400&h=220&o=f&c=1&y=t&b=ffffff&r=1m&u='+this.site,'href':'http://www.alexa.com/siteinfo/' + this.site}];
	if (templateID == 271781750426)
	      obj['images'] = [{'src':'http://www.alexa.com/images/reviews/star-'+this.star+'.png','href':'http://www.alexa.com'}];      
	
        if (this.star)
            obj['stars']  = this.star;
        if (this.site)
            obj['site']   = this.site;
        if (this.text)   
            obj['text']   = this.text;
        if (this.title)
            obj['title']  = this.title;
        if (this.author)
            obj['author']  = this.author;
        return obj;
    }
};

var templateID = null;
function _wallposting( callback )
{
    FB.ensureInit(
        function() 
        {
            var obj = _wpObj.genObj();
            if (callback)
                facebook_publish_feed_story(templateID, obj, callback);
            else
                facebook_publish_feed_story(templateID, obj);
        }
    );
}

function _onload() {
  FB.ensureInit(function() {
      FB.Facebook.get_sessionState().resetChange();
      FB.Facebook.get_sessionState().waitUntilReady(function(session) {
            _wallposting();
        });
      facebook_onload(true);
    });
}

function _wallpostingwithlogin()
{
    FB.ensureInit( 
        function() 
        { 
            _onload(); 
            
            FB.Connect.requireSession(
            ); 
        } 
    );
}
function wallposting( star, site, title, text, author, wallposttype) 
{
    _wpObj.load(star, site, text, title, author);
    if (wallposttype == 'review')
        templateID = 271781750426;
    if (wallposttype == 'profile')
        templateID = 271717930426;//265222045426;
    if (wallposttype == 'siteinfo')
        templateID = 282649415426;
	
    if (templateID != 282649415426)
      $.get("/data/social.php?target=facebook:"+site);
    //FB.Connect.ifUserConnected(_wallposting, _wallpostingwithlogin);
    FB.Connect.get_status().waitUntilReady( function( status ) {
      switch ( status ) {
	case FB.ConnectState.connected:
	  _wallposting();
	  break;
	case FB.ConnectState.userNotLoggedIn:
	  _wallpostingwithlogin();
	  break;
	}}); 
    
    //_wpObj.reset()
}

function twitterShare(url)
{
   var url_target = "http://www.alexa.com/siteinfo/" + url + "#reviews";

   $.post("/write/social/twitter" , { url : url_target }, 
        function(data)
        {
           var twitterurl = "http://twitter.com/home?status=Review of " + url + " on Alexa: " + data[0].sUrl;
           window.open(twitterurl);
        }, "json" ); 
}
