$(document).ready(function () {
  
    var rating_clicked = false,
    	rating_selected = null,
    	rating_pairs =  { 
		'subStar-rating-1': 'I did not like this site',
		'subStar-rating-2': 'Eh, the site was alright',
        'subStar-rating-3': 'This site is OK',
        'subStar-rating-4': 'Pretty cool site',
        'subStar-rating-5': 'This site is fantastic!'
    };
 
    $('#rate_site input:checked').each( function() {
        rating_selected = $(this).attr('id');
        $('#rate_site').toggleClass(rating_selected);
    });
    
    $('#rate_site label').hover(
		function() {
			var idname = $(this).find('input').attr('id');

			for (var key in rating_pairs) { 
				$(this).parent().toggleClass(key, false);
				if (idname == key) {$('#star-help').text( rating_pairs[key] );}
			}
			$(this).parent().toggleClass(idname, true);
		},
		function() {
			for (var key in rating_pairs) { 
				$(this).parent().toggleClass(key, false);
			}
			$(this).parent().toggleClass(rating_selected, true);
			$('#star-help').text( rating_pairs[rating_selected] );
		}
    );
    
    $('#rate_site label').click( function() {
        rating_clicked = true;
        var element = $(this).find('input').attr("checked", true); ;
        rating_selected = element.attr('id');
        
        for (var key in rating_pairs) { 
          $(this).parent().toggleClass(key, false);
          $('#previewStars').toggleClass(key, false);
        }
        
        $('#previewStars').toggleClass(rating_selected, true);
        $(this).parent().toggleClass(rating_selected, true);
        $('#star-help').text( rating_pairs[rating_selected] );
    });
   
    $('div.usefulBar li.report a').click(
        function() {
            $(this).parent().addClass('flag');
            var rid = $(this).attr('rid');
            var rrer = $(this).attr('rrer');
            $.post("/write/flag?random=" + Math.round(Math.random()*21474836747) , { rid: rid, rrer: rrer } );
	    }
    );
    $('div.usefulBar li.action a').click(function() {
    	var helpful = $(this).attr('class'),
    		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
    		num = $(this).parent().parent().parent().find('.num'),
    		tot = $(this).parent().parent().parent().find('.total'),
    		li = $(this).parent().parent().find('li'),
    		rid = $(this).attr('rid'),
    		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();
		}
    );
});

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" 
   ); 
}





