// JavaScript Document
var starRatingStore;
window.addEvent('domready', function()
{
  var shareTag = $('video_share_tag');
  var ratingTag = $('video_rating_tag');
  var viewTag = $('video_views_tag');
  var shareFix = $('share_tag_fix');
  var starState = [];
  var ratingMessage = $('video_rating_stars_login_message');
  var stars = $$('.star_button');
  var ratingFormBoxClose = $('rating_form_box_close');
  var ratingFormBox = $('rating_form_box'),
	ratingFormBoxSlide = new Fx.Slide(ratingFormBox);
  ratingFormBoxSlide.hide();
  ratingMessage.fade(0);
  
if(!$('lock_video')) 
  stars.each(function(star, i){
    
    if($('video_rating_star'+i).hasClass('half_full_star')) {
        starState[i] = 'half_full_star';
    } else if($('video_rating_star'+i).hasClass('full_star')) {
        starState[i] = 'full_star';
    } else {
        starState[i] = 'empty_star';
    }
    
    
    star.addEvents({
      'mouseenter':function() {  
        for(var n=0; n<=4; n++)
        {
          $('video_rating_star'+n).removeClass('full_star');
          $('video_rating_star'+n).removeClass('half_full_star');
          $('video_rating_star'+n).addClass('empty_star');  
        }
              
        for(var n=0; n<=i; n++)
        {
          $('video_rating_star'+n).removeClass('empty_star');
          $('video_rating_star'+n).removeClass('half_full_star');
          $('video_rating_star'+n).addClass('full_star');
        }
      },
      'mouseleave':function() {
        for(var n=0; n<=4; n++)
        {
          $('video_rating_star'+n).removeClass('full_star');
          $('video_rating_star'+n).removeClass('half_full_star');
          $('video_rating_star'+n).addClass('empty_star');
        }
      },
      'click':function(){
        var userId = 0;
        
        if($defined($('right_user_log_name_label'))) {
            userId = $('right_user_log_name_label').get('text');
        }
        //alert(userId);
        
        var flashMessage = function() {
            var msgMorph = new Fx.Morph(ratingMessage, {duration:1200});            
            stars.setStyle('display', 'none');
            stars.fade(0);
            ratingMessage.setStyle('display', 'inline');
            msgMorph.start({'opacity':1}).chain(function() {
                msgMorph.start({'opacity':0}).chain(function(){
                    ratingMessage.setStyle('display', 'none');
                    stars.setStyle('display', 'inline');
                    stars.fade(1);
                });
            });
        };
        if(userId != 0){
            if(userId.length > 3) {
            
            var targetContentId = $('video_content_id_holder').get('class');
            var ratingUrl = '/ajax/userrating/content/'+targetContentId+'/rating/'+(i+1);            
            var rateContent =  
            new Request({
                url:ratingUrl,
                onSuccess:function(txt){
                    if(txt.length > 3)
                    {
                        var upAvgRate = txt.substring(txt.indexOf(';')+1, 3);
                        var upCountRate = txt.substring(0, txt.indexOf(';'));
                        var averageRating =  $('average_rating');
                        var countRating = $('count_rating');
                        averageRating.set('text', 'Average Rating: '+upAvgRate);
                        countRating.set('text', upCountRate+' votes');
                        
                        for(var n=0; n<=4; n++)
                        {
                            //alert(upAvgRate - n);
                          if( (upAvgRate - n) >= 1) {
                            $('video_rating_star'+n).removeClass('empty_star');
                            $('video_rating_star'+n).removeClass('half_full_star');
                            $('video_rating_star'+n).addClass('full_star');
                            starState[n] = 'full_star';
                          } else if( (upAvgRate - n) >= 0.5) {
                            $('video_rating_star'+n).removeClass('empty_star');
                            $('video_rating_star'+n).addClass('half_full_star');
                            $('video_rating_star'+n).removeClass('full_star');
                            starState[n] = 'half_full_star';
                          } else {
                            $('video_rating_star'+n).addClass('empty_star');
                            $('video_rating_star'+n).removeClass('half_full_star');
                            $('video_rating_star'+n).removeClass('full_star');
                             starState[n] = 'empty_star';
                          }
                        }
                        
                        ratingMessage.set('text', 'Vote Recorded');
                        flashMessage.run();
                        
                    } else {
                        ratingMessage.set('text', 'Already Voted');
                        flashMessage.run();
                    }
                }
            });
            rateContent.send();
            
            } 
        } else {
            ratingMessage.set('text', 'Please Login');
            // Add dropdown here. Need to maintain STAR STATE.
            starRatingStore = i;
            var cloneStars = $('video_rating_tag').getElements('div.star_button');
            var html = '';
            cloneStars.each(function(v,k){
            	var star = v.get('class').substring(12);
            	star = star.split('_')[0];
            	html += '<img src="/css/'+channelName+'/i/ratingstar_'+star+'.png" />';
            });
            $('store_star1').set('html',html);
            initRatingFormBox();
			ratingFormBoxSlide.slideIn();
            flashMessage.run();            
        }
        
      }
    });
  });
  
  if(ratingFormBoxClose) {
  	ratingFormBoxClose.addEvent('click',function(){
  		ratingFormBoxSlide.toggle();
  		hideTalkBox();
  	});
  	
  	var initRatingFormBox = function(){
		$('register_form').addEvent('submit',function(e){
			e.stop();
			this.getElements('input').each(function(v,k){
				if(v.get('alt') == v.get('value'))
					v.set('value','');
			});
			this.set('send',{
				onComplete:function(response) {
					if(response.indexOf('complete') > 0) {
				  		ratingFormBoxSlide.toggle();
				  		hideTalkBox();
				  		successDoLogin(response.split('-')[0]);
					} else 
						showTalkBox($('btnRegister'),response);
				}
			});
			this.send();
			this.getElements('input').each(function(v,k){
				if(v.get('alt') && v.get('value') == '')
					v.set('value',v.get('alt'));
			});
		});
		
		$('login_form').addEvent('submit',function(e){
			e.stop();
			this.getElements('input').each(function(v,k){
				if(v.get('alt') == v.get('value'))
					v.set('value','');
			});
			this.set('send',{
				onComplete:function(response) {
					if(response.indexOf('success') > 0) {
				  		ratingFormBoxSlide.toggle();
				  		hideTalkBox();
				  		successDoLogin(response.split('-')[0]);
					} else 
						showTalkBox($('btnLogin'),response);
				}
			});
			this.send();
			this.getElements('input').each(function(v,k){
				if(v.get('alt') && v.get('value') == '')
					v.set('value',v.get('alt'));
			});
		});  		
  	}, successDoLogin = function(u) {
	        var topRewrite = '<label id="right_user_log_name_label">Hi '+u+'!</label><a href="/user/myprofile\">My Account</a><a href="/user/profile/view/'+u+'">My Profile</a><a id="right_user_log_out" href="#">Logout</a>';
	        $('right_user_login_line').set('html', topRewrite);
	        $$('.user_comment_status_message_group').set('html', 'Hi '+u+'!');
	        rightLogout.run();	
  			stars[starRatingStore].fireEvent('click'); //starRatingStore)
  	}
  }
  
  shareTag.addEvents({
      'mouseenter':function(){
        
        shareTag.setStyle('z-index', 15);
      },
      'mouseleave':function(){
        shareTag.setStyle('z-index', 3);
      }
  });
  
if(!$('lock_video'))   
  ratingTag.addEvents({
      'mouseenter':function(){
        ratingTag.setStyle('z-index', 15);
        shareFix.setStyle('z-index', 10);
      },
      'mouseleave':function(){
        
        for(var n=0; n<=4; n++)
        {
            $('video_rating_star'+n).morph({'opacity':[0.2, 1]});
            $('video_rating_star'+n).addClass(starState[n]);
            
        }
        
        ratingTag.setStyle('z-index', 3);
        shareFix.setStyle('z-index', 20);
      }
  });
  shareFix.addEvents({
    'mouseenter':function(){
        ratingTag.setStyle('z-index', 15);
        shareFix.setStyle('z-index', 10);
      },
      'mouseleave':function(){
        ratingTag.setStyle('z-index', 3);
        shareFix.setStyle('z-index', 20);
      }
  
  });
  
  $('share_tag_url').addEvent('click', function(){
    this.select();
  });
  
  if($('share_tag_embed'))
  $('share_tag_embed').addEvent('click', function(){
    this.select();
  });
  
  $('share_email').addEvent('click', function(){

  });
  
  viewTag.addEvents({
      'mouseenter':function(){
        viewTag.setStyle('z-index', 15);
      },
      'mouseleave':function(){
        viewTag.setStyle('z-index', 3);
      }
  });
  
});