var openedFormId = '';
var url = '';	// selecteaza controllerul la care trimitem datele pt votare

$(document).ready( function() {
	$('#videoAction_01 a').click(function () {
		openForm('videoActionForm_01');
		return false;
	});
	
	$('#videoAction_02 a').click(function () {
		$('#Mediafavorite').submit();
		return false;
	});
	
	$('#videoAction_03 a').click(function () {
		openForm('videoActionForm_03');
		return false;
	});
	
	$('#videoAction_04 a').click(function () {
		openForm('videoActionForm_04');
		return false;
	});
	
	$("#videoActionForm_04").simpletabs();
	$('#videoRatingStars').rating();
	$('#videoPicks').videoPicks();
	
	$('#videoCommentsHiddenRecords').toggle();
	$('#videoCommentsHSLink').click(function () {
		$('#videoCommentsHiddenRecords').toggle();
		return false;
	});
	$('#commentFormSLink').click(function () {
		$('#commentAddForm').slideDown();
		$('#commentAddForm').animate({
			opacity: 1
		});
		return false;
	});
	$('#commentFormHLink').click(function () {
		$('#commentAddForm').animate({
			opacity: 0
		});
		$('#commentAddForm').slideUp();
		return false;
	});
	$('#MediacommentBody').data('limit', Number($('#commentFormCharcount span').text()));
	var MediacommentBodyText = $('#MediacommentBody').val();
	$('#MediacommentBody').keyup(function (e) {
		var _ta = $('#MediacommentBody'); 
		var t = _ta.val();
		var limit = $('#MediacommentBody').data('limit');
		if (t.length > limit) {
			_ta.val(MediacommentBodyText);
		} else {
			if (t.length == limit) {
				$('#commentFormCharcount').addClass('red');
			} else {
				$('#commentFormCharcount').removeClass('red');	
			}
			MediacommentBodyText = t;
		}
		$('#commentFormCharcount span').text(limit -_ta.val().length);
	});
	$('#MediacommentAddForm').ajaxForm({
		'dataType': 'json',
		'success':	function (data) {
			if (data.success) {
				showMessage( 'Comentariul a fost adăugat și va apărea pe site după moderare', true );
				$('#MediacommentBody').val('');
	        } else if (data.errors) {
				showMessage( data.errors.Mediacomment.body, false);
	        } else if (data.redirect) {
				window.location = data.redirect;
			}
		}
	});
	
	$('#Mediarating').ajaxForm({
		'dataType': 'json',
		'success':	function (data) {
			if (data.success >= 0) {
//				$('#videoRatingStars').rating.setRateClass(data.success);
				showMessage( 'Imaginea a fost notată', true );
	        } else if (data.errors) {
				showMessage( data.errors.Mediarating.media_id, false);
	        } else if (data.redirect) {
				window.location = data.redirect;
			}
		}
	});
	
	$('#Mediafavorite').ajaxForm({
		'dataType': 'json',
		'success':	function (data) {
			if (data.success) {
				showMessage( 'Imaginea a fost adăugată în lista ta de favorite', true );
	        } else if (data.errors) {
				showMessage( data.errors.Mediafavorite.media_id, false);
	        } else if (data.redirect) {
				window.location = data.redirect;
			}
		}
	});
	
	$('#UsermediaTellafriend').ajaxForm({
		'dataType': 'json',
		'beforeSubmit': function () {
			openForm('');
		},
		'success':	function (data) {
			if (data.success) {
				showMessage( 'Mesajul către prietenul tău a fost trimis', true );
	        } else if (data.errors) {
				showMessage( 'Mesajul către prietenul tău nu a fost trimis. Te rugăm să încerci mai târziu', false);
	        } else if (data.redirect) {
				window.location = data.redirect;
			}
		}
	});
	
	$('#UsermediaVote1').ajaxForm({
		'dataType': 'json',
		'success':	function (data) {
			if (data.success) {
				showMessage( 'Votul tău a fost înregistrat', true );
	        } else if (data.errors) {
				showMessage( data.errors.Contestvote.id, false);
	        } else if (data.redirect) {
				window.location = data.redirect;
			}
		}
	});
	
	$('#UsermediaVote2').ajaxForm({
		'dataType': 'json',
		'success':	function (data) {
			if (data.success) {
				showMessage( 'Votul tău a fost înregistrat', true );
	        } else if (data.errors) {
				showMessage( data.errors.Contestvote.id, false);
	        } else if (data.redirect) {
				window.location = data.redirect;
			}
		}
	});
	
	$('#UsermediaVote3').submit(function() { 
        if (url!=''){
        	if (url == '/usermedia/ajax_vote_minicontest'){
        		$('#UsermediaVote4').ajaxSubmit({
        			'dataType': 'json',
        			'success':	function (data) {
        				if (data.success) {
        					showMessage( 'Votul tău a fost înregistrat', true );
        		        } else if (data.errors) {
        					showMessage( data.errors.Minicontestvote.id, false);
        		        } else if (data.redirect) {
        					window.location = data.redirect;
        				}
        			}
        		}); 	
        	}else if (url == '/usermedia/ajax_vote'){
        		$('#UsermediaVote3').ajaxSubmit({
        			'dataType': 'json',
        			'success':	function (data) {
        				if (data.success) {
        					showMessage( 'Votul tău a fost înregistrat', true );
        					$('span#UserBalance').text(data.success.balance);
        		        } else if (data.errors) {
        					showMessage( data.errors.Contestvote.id, false);
        		        } else if (data.redirect) {
        					window.location = data.redirect;
        				}
        			}
        		});
        	}
        	//url='';
        	return false;
        }
        $('#UsermediaVote3').ajaxSubmit({
			'dataType': 'json',
			'success':	function (data) {
				if (data.success) {
					showMessage( 'Votul tău a fost înregistrat', true );
					$('span#UserBalance').text(data.success.balance);
		        } else if (data.errors) {
					showMessage( data.errors.Contestvote.id, false);
		        } else if (data.redirect) {
					window.location = data.redirect;
				}
			}
		});
        return false;
    }); 

	$('#UsermediaVote4').submit(function() { 
        if (url!=''){
        	if (url == '/usermedia/ajax_vote_minicontest'){
        		$('#UsermediaVote4').ajaxSubmit({
        			'dataType': 'json',
        			'success':	function (data) {
        				if (data.success) {
        					showMessage( 'Votul tău a fost înregistrat', true );
        		        } else if (data.errors) {
        					showMessage( data.errors.Minicontestvote.id, false);
        		        } else if (data.redirect) {
        					window.location = data.redirect;
        				}
        			}
        		}); 	
        	}else if (url == '/usermedia/ajax_vote'){
        		$('#UsermediaVote3').ajaxSubmit({
        			'dataType': 'json',
        			'success':	function (data) {
        				if (data.success) {
        					showMessage( 'Votul tău a fost înregistrat', true );
        					$('span#UserBalance').text(data.success.balance);
        		        } else if (data.errors) {
        					showMessage( data.errors.Contestvote.id, false);
        		        } else if (data.redirect) {
        					window.location = data.redirect;
        				}
        			}
        		});
        	}
        	//url='';
        	return false;
        }
        $('#UsermediaVote4').ajaxSubmit({
			'dataType': 'json',
			'success':	function (data) {
				if (data.success) {
					showMessage( 'Votul tău a fost înregistrat', true );
		        } else if (data.errors) {
					showMessage( data.errors.Minicontestvote.id, false);
		        } else if (data.redirect) {
					window.location = data.redirect;
				}
			}
		});
        return false;
    }); 

	$('a.lightboxed').lightBox({
		overlayBgColor: '#000000',
		overlayOpacity: 0.25,
		imageBlank: '/img/lightbox/lightbox-blank.gif',
		imageLoading: '/img/lightbox/lightbox-ico-loading.gif',
		imageBtnClose: '/img/lightbox/lightbox-btn-close.gif',
		imageBtnPrev: '/img/lightbox/lightbox-btn-prev.gif',
		imageBtnNext: '/img/lightbox/lightbox-btn-next.gif',
		containerResizeSpeed: 350,
		txtImage: '',
		txtOf: ''
	});
});

function showMessage (msg, success ) {
	_insert = $('#videoFlashMessages');
	_insert.dialog("destroy");
	if (!_insert.length) {
		_insert = $(document.createElement('div')).attr('id', 'videoFlashMessages');
		_insert.attr('title','Alertă');
	}
	if (success) {
		_insert.html('<div class="feedbackOk" id="flashMessage">'+ msg+ '</div>');
	} else {
		_insert.html('<div class="feedbackError" id="flashMessage">'+ msg+ '</div>');
	}
	
	$('#videoActionForms').prepend(_insert);
	_insert.dialog({
        modal: true,
        draggable: false,
        resizable: false
	});
}

function openForm (fId) {
	if (openedFormId != '') {
		var openedForm = $('#' + openedFormId);
		openedForm.animate({
			opacity: 0
		});
		openedForm.slideUp();
	}
	if (openedFormId == fId) {
		openedFormId = '';
		return;
	}
	openedFormId = fId;
	if (openedFormId == '') return;
	var f = $('#' + openedFormId);
	f.delay(500);
	f.slideDown();
	f.animate({
		opacity: 1
	});
}

function voteInChanged(nr){
	var cb = "votein"+nr;
	if (document.getElementById(cb).selectedIndex!=0 || document.getElementById("isContestEntry").value==false){
		url = '/usermedia/ajax_vote_minicontest';
		document.getElementById('UsermediaMinicontestentryId').value = document.getElementsByName('optids')[document.getElementById(cb).selectedIndex].value;
		document.getElementById('voteInMinicontestAdditionalInfo').innerHTML='<p>Votează imaginea în mini concursul: <b>'+document.getElementById(cb)[document.getElementById(cb).selectedIndex].innerHTML+'</b>. Poți vota o singură imagine într-un mini concurs.</p>';
		document.getElementById('voteInContestAdditionalInfo').innerHTML='<p>Votează imaginea în mini concursul: <b>'+document.getElementById(cb)[document.getElementById(cb).selectedIndex].innerHTML+'</b>. Poți vota o singură imagine într-un mini concurs.</p>';
	}else if (document.getElementById('isContestEntry').value=='true'){
		url = '/usermedia/ajax_vote';
		document.getElementById('UsermediaContestentryId').value = document.getElementsByName('optids')[0].value;
		document.getElementById('voteInMinicontestAdditionalInfo').innerHTML='<p>Un vot va folosi un <strong>zibar</strong> din contul tău Zibo.</p>';
		document.getElementById('voteInContestAdditionalInfo').innerHTML='<p>Un vot va folosi un <strong>zibar</strong> din contul tău Zibo.</p>';
	}
}

