var pl = false;

$(document).ready(function() {
	bindViewVideoButtons();
	if ($('#login-form-comm').length > 0) {
		var options2 = {
			beforeSubmit : preLoginComm,
			success : postLoginVoteComm,
			dataType : 'json'
		};

		$('#login-form-comm').ajaxForm(options2);
	}
	
	if ($('#newPlName').length > 0) {
	
		$('#newPlName').focus(function() {
			if ($(this).val() == $('#newPlName-txt').html()) {
				$(this).val('');
			}
		});
	
		$('#newPlName').blur(function() {
			if ($(this).val() == '') {
				$(this).val($('#newPlName-txt').html());
			}
		});
	}
});

function bindViewVideoButtons() {
	$('#submit-comment').click(function() {
		addCommentVideo();
	});
}

function preLoginComm(formData, jqForm, options) {
	$('#msg-comm').hide();
	$('#msg-comm').html('');
	return true;
}

function postLoginVoteComm(respObj, statusText) {
	try {
		if (respObj.status == "ok") {
			$('#pers-menu-ctr').html(respObj.html);
			$('#cnt-login-form-comm').fadeOut('slow', function() {
				$('#comm-frm').fadeIn('fast');
			});
		} else {
		}
	} catch (e) {
	}
}

function addCommentVideo() {
	var url = '/' + $('#lang-code').html() + '/videos/axaddcomm';
	$('#c-msg').fadeOut('fast');
	$.post(url, {
		vid : $('#video-id').html(),
		body : $('#c-body').val()
	}, function(data) {
		if (data.status == 'error') {
			$('#c-msg').fadeIn('fast');
		} else {
			$('#c-body').val('');
			reloadComments();
		}
	}, 'json');
}

function reloadComments() {
	var url = '/' + $('#lang-code').html() + '/videos/axcomms';
	$.post(url, {
		vid : $('#video-id').html()
	}, function(data) {
		$('#comments-cnt').fadeOut('fast', function() {
			$('#comments-cnt').html(data);
			$('#v-c-num').html($('#c-num').html());
			$('#comments-cnt').fadeIn('fast');
			reloadShareBtns();
		});
	});
}

function reloadShareBtns() {
	var url = '/' + $('#lang-code').html() + '/videos/axbtns';
	$.post(url, {
		vid : $('#video-id').html()
	}, function(data) {
		$('#share-btns').html(data);
	});
}

function gotoCommentForm() {
	$('html,body').animate( {
		scrollTop : $("html").attr("scrollHeight")
	}, 1000);
}

function loadVideoData(id, key) {
	if (id == $('#video-id').html()) {
		return;
	}
	reloadIframes();
	var url = '/' + $('#lang-code').html() + '/videos/axvinfo';
	$('#video-id').html(id);

	$("#clip-prop-cnt").load(url, {
		vid : id,
		vk : key,
		from : 'video'
	}, function() {
		$('#video-title-cnt').html($('#video-title-cnt-new').html());
		document.title = $('#html-title').html();
		reloadComments();
		$('#choose-playlist-temp').html($('#choose-pl-hidden').html());
		if ($('#pl-cnt-menu').length > 0) {
			$('#pl-cnt-menu').html($('#pl-menu-hidden').html());
			$('#create-playlist-form').html($('#create-pl-hidden').html());
		}
		urchinTracker($('#video-url').html());
	});
}

function reloadIframes() {
	$('iframe').each(function() {
		var url = $(this).attr('src');
		var newUrl = '';
		if (url.indexOf('&cl=1') != -1) {
			newUrl = url.replace('&cl=1', '');
		} else {
			newUrl = url + '&cl=1';
		}
		$(this).attr('src', newUrl);
	});
}

function showPlayListMenu() {
	if (pl) {
		$('#choose-playlist-cnt').slideUp('fast');
		pl = false;
		$('#newPlName').val($('#newPlName-txt').html());
	} else {
		$('#choose-playlist-cnt').slideDown('fast');
		pl = true;
	}
}

function addToPlaylist(plid, vid) {
	var url = '/' + $('#lang-code').html() + '/playlist/add';
	$.post(url, {
		pid : plid,
		order : vid,
		topl : 1,
		name : 'cucurucu'
	}, function(data) {
		$('.pl-m-' + data.pid).toggle();
		showPlayListMenu();
		noticeDisplay(data.message, 'ok', true);
	}, 'json');
}

function saveAsNewPlaylist(vid) {
	if ($('#newPlName').val() == $('#newPlName-txt').html()) {
		return;
	}
	
	var url = '/' + $('#lang-code').html() + '/playlist/add';
	$.post(url, {
		name : $('#newPlName').val(),
		order : vid
	}, function(data) {
		noticeDisplay(data.message, 'ok', true);
		$('#pl-cnt-menu').html(data.html);
		showPlayListMenu();
	}, 'json');
}

function openFBWindow(url) {
	var win = window.open(url, 'fbwin','width=400,height=300');
}


