
// cookie functions
function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

// doc ready
$(document).ready(function(){
	var browseOpen=false;
	var topCurrent=1;
	var totalTopVideoPages=3;
	var topListItem=1;
	var videoOptionSelected='send';
	var sendingRating=false;
	var sendingFriend=false;
	var sendingComment=false;
	
	// toggle browse menu
	$('.browse_list').click(function(){
		$('#head .browseList').slideDown('slow',function(){
			browseOpen=true;
		});
	});
	
	// close browse menu
	$(document).click(function(){
		if(browseOpen==true){
			$('#head .browseList').slideUp('fast');
			browseOpen=false;
		}
	});
	
	//  featured video paging
	$('.top_video_nav').click(function(){
		var set = $(this).attr('title');
		if(set!=topCurrent && set <= totalTopVideoPages && set > 0){
			$('.set_'+topCurrent).fadeOut('fast',function(){
				topCurrent=set;
				$('.set_'+set).slideDown('slow');
				nextId = Math.ceil(set)+1;
				prevId = Math.ceil(set)-1;
				$('#p1').html(topCurrent);
				$('#prev_top').attr('title',prevId);
				$('#next_top').attr('title',nextId);
			});
		}
		
		if(set > 1){
			$('#prev_top').removeClass('off');
		} else {
			$('#prev_top').addClass('off');
		}
		
		if(set < totalTopVideoPages){
			$('#next_top').removeClass('off');
		} else {
			$('#next_top').addClass('off');
		}
	});
	
	// top video toggle
	$('.top_list').click(function(){
		var file = $(this).attr('alt');
		if(file != topListItem){
			$('.selected .more').html('<a href="javascript:void(0);">Watch Video</a>');
			$('#top_video_list li').removeClass('selected');
			topListItem = $(this).attr('alt');
			videoId = $(this).attr('name');
			var videoHTML = '<embed width="560" height="452" flashvars="auto_play=true&token='+file+'&dev_url=http://media.vmixcore.com/vmixcore" allowscriptaccess="always" src="http://media.vmixcore.com/player/'+file+'/player.swf?player_id=01365b921506ddde1962bb28ea8dfa9d"/>';
			$('#video_player').html(videoHTML);
			$('#'+topListItem).addClass('selected');
			$('#'+topListItem+' .more').html('<a href="javascript:void(0);">Watch Video</a>');
			$('.selected .more').html('<a href="?section=video&id='+videoId+'">Comment on this video</a>');
		}
	});
	
	// toggle video options
	$('.toggle').click(function(){
		var optionClicked = $(this).attr('title');
		if(optionClicked!=videoOptionSelected){
			$('#videoOptions li a').removeClass('selected');
			$(this).addClass('selected');
			$('#'+videoOptionSelected).fadeOut('fast',function(){
				$('#'+optionClicked).fadeIn('fast');
				videoOptionSelected = optionClicked;
			});
		}
	});
	
	// submit ratings
	$('#ratingStars li a').click(function(){
		var vidRating = $(this).attr('title');
		var vidId = $(this).attr('name');
		var rateCookie = vidId;
		var cookieSet = readCookie(vidId);
		
		if(cookieSet){
			alert('You already voted!');
			return false;
			
		} else {
			if(!sendingRating){
				sendingRating=true;
				$.post('./app/post_actions.php',{ id:vidId,rating:vidRating },
					function(response){
						if(response==1){
							createCookie(rateCookie, vidRating, 365);
							$('#ratingStars').fadeOut('fast',function(){
								$('#ratingStars').html('<p>Thanks for voting!</p>');
								$('#ratingStars').fadeIn('slow');
							});
						} else {
							alert('An Error Occured - '+response);
							sendingRating=false;
						}
	
				});
			} else {
				alert('Please Wait!');
			}
		}
	});
	
	
	// send to friend
	$('#send_friend').submit(function(){
		var send = $('#email_id').attr('value');
		var y_n = $('#your_name').attr('value');
		var y_e = $('#your_email').attr('value');
		var f_n = $('#friends_name').attr('value');
		var f_e = $('#friends_email').attr('value');
		var m = $('#message').attr('value');
		
		if(!sendingFriend){
			sendingFriend=true;
			$.post('./app/post_actions.php',{ send_to_friend:send,your_name:y_n,your_email:y_e,friends_name:f_n,friends_email:f_e,message:m },
				function(response){
					if(response==1){
						$('#send_friend').fadeOut('fast',function(){
							$('#send_friend').html('<p>Your message was sent!</p>');
							$('#send_friend').fadeIn('slow');
						});
					} else {
						alert(response);
						sendingFriend=false;
					}

				});
		} else {
			alert('Please Wait!');
		}
		return false;
	});	
	
	// post comment
	$('#postComment').submit(function(){
		var media_id = $('#media_id').attr('value');
		var cUsername = $('#comment_username').attr('value');
		var cComment = $('#comment_comment').attr('value');
		var md5_sum = $('#md5').attr('value');
		var cCode = $('#captcha').attr('value');
		
		if(!sendingComment){
			sendingComment=true;
			if(cUsername && cComment && cCode){
				$.post('./app/post_actions.php',{ id:media_id,username:cUsername,comment:cComment,md5:md5_sum,captcha:cCode },
					function(response){
						if(response==1){
							$('#postComment').fadeOut('fast',function(){
								$('#commentsList li:nth-child(1)').after('<li><dl><dt>'+cUsername+'</dt><dd>'+cComment+'</dd><dd class="date">1 sec ago</dd></dl>');
								$('#postComment').html('<div class="insertedComment"><h3>Thanks!</h3><p>Your comment was posted.</p></div>');
								$('#postComment').fadeIn('slow');
							});
						} else {
							alert(response);
							sendingComment=false;
						}
	
					});
			} else {
				alert('Username, Comment & Security Code Required!');
				sendingComment=false;
			}
		} else {
			alert('Please Wait!');
		}
		return false;
	});
	// end doc ready
});