$.extend($.modal.defaults, {
	escClose: true,
	overlayClose: true,
	opacity: 75,
	closeClass: 'close_button',
	closeHTML: '<a href="#" class="close_button"></a>',
	onOpen: function(dialog) {
		dialog.wrap.css({overflow:'hidden'});
		dialog.overlay.fadeIn(200, function () {
			dialog.container.slideDown(500, function() {
				dialog.data.show();
			});
		});
		if(jQuery.browser.msie && /^6/.test(jQuery.browser.version)) {
			$('#simplemodal-container').belated(); // IE6 PNG FIX
		}
		$('#simplemodal-container td').append('<br/><p><a class="voltar" href="javascript:void(0)"></p>');
	},
	onClose: function(dialog) {
		dialog.container.slideUp(500, function () {
			dialog.overlay.fadeOut(200, function(){
				$.modal.close();
			});
		});
	}
});

$.ajaxSetup({
	cache: false,
	dataType: 'json',
	type: 'POST',
	url: VireCelebridade.BASE_URL+'ajax/'
});

var ajaxbusy = false;

VireCelebridade.Video = function() {
	this.VOTE_GOSTEI = 1;
	this.VOTE_NAO_GOSTEI = 0;
	this.properties = {};
	var context = 'video';
	var error = null;
	var obj = this;
	this.Vote = function(value) {
		$.ajax({
			data: $.extend(this.properties,{context:context,action:'vote',voto:value}),
			success: function(data) {
				$(obj).trigger('vote_complete',{error:(data.err!=0)});
			}
		});
	}
	this.Save = function() {
		$.ajax({
			data: $.extend(this.properties,{context:context,action:'save'}),
			success: function(data) {
				$(obj).trigger('save_complete',{error:(data.err!=0)});
			}
		});
	}
};

VireCelebridade.Email = function() {
	this.properties = {};
	var context = 'email';
	var error = null;
	var obj = this;
	this.SendToFriends = function() {
		$.ajax({
			data: $.extend(this.properties,{context:context,action:'sendtofriends'}),
			success: function(data) {
				$(obj).trigger('sendtofriends_complete',{error:(data.err!=0)});
			}
		});
	}
	this.Add = function() {
		$.ajax({
			data: $.extend(this.properties,{context:context,action:'add'}),
			success: function(data) {
				$(obj).trigger('add_complete',{error:(data.err!=0)});
			}
		});
	}
};

VireCelebridade.SendMailWidget = function(elem) {
	this.el = elem;
	this.Buttons = {
		Submit: $('.submit',elem)
	};
	this.properties = {};
	var inputs = {};
	var funcs = {
		email_usuario: function(v){
			return /^(?:[a-zA-Z0-9_\-\+]+\.)*[a-zA-Z0-9_\-\+]+@(?:[a-zA-Z0-9]+)(?:\.\w{2,4})+$/.test(v);
		},
		email_amigas: function(v){
			return /^(?:(?:[a-zA-Z0-9_\-\+]+\.)*[a-zA-Z0-9_\-\+]+@(?:[a-zA-Z0-9]+)(?:\.\w{2,4})\s*,\s*)*(?:[a-zA-Z0-9_\-\+]+\.)*[a-zA-Z0-9_\-\+]+@(?:[a-zA-Z0-9]+)(?:\.\w{2,4})+$/.test(v);
		}
	}
	var obj = this;
	var email = new VireCelebridade.Email();
	$(':input',elem).each(function() {
		inputs[$(this).attr('name')] = this;
	});
	function get_data() {
		for(i in inputs) {
			var $this = $(inputs[i]);
			obj.properties[$this.attr('name')] = $.trim($this.val());
		}
	};
	function validate() {
		for(p in funcs) {
			if(!funcs[p]((obj.properties[p]))) {
				$(inputs[p]).addClass('error');
			}
			else {
				$(inputs[p]).removeClass('error');
			}
		}
	}
	this.Buttons.Submit.bind('click',function() {
		$(elem).trigger('submit');
	});
	var isBusy = false;
	$(elem).bind('submit',function() {
		if(isBusy) {
			return false;
		}
		get_data();
		validate();
		if($(':inputs.error',elem).length>0) {
			// tratar errors
			return false;
		}
		isBusy = true;
		email.properties = $.extend(email.properties,obj.properties);
		$(email).one('sendtofriends_complete',function(e,data) {
			if(!data.error) {
				$(obj.el).fadeOut(function() {
					$('#email_enviado_dialog').modal()
				});
			}
			isBusy = false;
		});
		email.SendToFriends();
		return false;
	});
};

VireCelebridade.AddMailWidget = function(elem) {
	this.el = elem;
	this.Form = $('form',elem);
	this.Container = $('.msg_container',elem);
	this.Buttons = {
		Submit: $('.submit',elem)
	};
	this.properties = {};
	var inputs = {};
	var funcs = {
		email: function(v){
			return /^(?:[a-zA-Z0-9_\-\+]+\.)*[a-zA-Z0-9_\-\+]+@(?:[a-zA-Z0-9]+)(?:\.\w{2,4})+$/.test(v);
		}
	}
	var obj = this;
	var email = new VireCelebridade.Email();
	$(':input',elem).each(function() {
		inputs[$(this).attr('name')] = this;
	});
	function get_data() {
		for(i in inputs) {
			var $this = $(inputs[i]);
			obj.properties[$this.attr('name')] = $.trim($this.val());
		}
	};
	function validate() {
		for(p in funcs) {
			if(!funcs[p]((obj.properties[p]))) {
				$(inputs[p]).addClass('error');
			}
			else {
				$(inputs[p]).removeClass('error');
			}
		}
	}
	this.Buttons.Submit.bind('click',function() {
		$(elem).trigger('submit');
	});
	var isBusy = false;
	$(elem).bind('submit',function() {
		if(isBusy) {
			return false;
		}
		get_data();
		validate();
		if($(':inputs.error',elem).length>0) {
			return false;
		}
		isBusy = true;
		email.properties = $.extend(email.properties,obj.properties);
		$(email).one('add_complete',function(e,data) {
			if(!data.error) {
				obj.Form.hide();
				obj.Container.text('Seu e-mail foi cadastrado com sucesso!');
			}
			isBusy = false;
		});
		email.Add();
		return false;
	});
}

VireCelebridade.UploadWidget = function(elem) {
	this.el = elem;
	this.Buttons = {
		Submit: $('.submit',elem)
	};
	this.properties = {};
	var inputs = {};
	var funcs = {
		apelido: function(v) {
			return /^[0-9a-zA-Z_\-]{3,}$/.test(v);
		},
		embed: function(v) {
			return /http:\/\/www\.youtube\.com\/v\/[^&"]+/.test(v);
		}
	}
	var obj = this;
	var video = new VireCelebridade.Video();
	$(':input',elem).each(function() {
		inputs[$(this).attr('name')] = this;
	});
	function get_data() {
		for(i in inputs) {
			var $this = $(inputs[i]);
			obj.properties[$this.attr('name')] = $.trim($this.val());
		}
	};
	function validate() {
		for(p in funcs) {
			if(!funcs[p]((obj.properties[p]))) {
				$(inputs[p]).addClass('error');
			}
			else {
				$(inputs[p]).removeClass('error');
			}
		}
	}
	this.Buttons.Submit.bind('click',function() {
		$(elem).trigger('submit');
	});
	var isBusy = false;
	$(elem).bind('submit',function(){
		if(isBusy) {
			return false;
		}
		get_data();
		validate();
		if($(':inputs.error',elem).length>0) {
			return false;
		}
		isBusy = true;
		video.properties = $.extend(video.properties,obj.properties);
		$(video).one('save_complete',function(e,data) {
			if(!data.error) {
				window.location.reload(true);
			}
			else {
				$('#emuso_dialog').modal();
			}
			isBusy = false;
		});
		video.Save();
		return false;
	});
};

VireCelebridade.VideoWidget = function(elem) {
	this.Buttons = {
		Gostei: $('a.gostei',elem),
		NaoGostei: $('a.naogostei',elem)
	};
	this.Votes = {
		NaoGostei: 0,
		Gostei: 1
	};
	var obj = this;
	var video = new VireCelebridade.Video();
	function vote(value) {
		$(video).bind('vote_complete',function(e,data) {
			if(!data.error) {
				$('#confirm_vote_dialog').modal();
			}
			else {
				$('#javotou_dialog').modal();
			}
			isBusy = false;
		});
		video.properties = $.extend(video.properties,{id_video:$('input[name="id_video"]',elem).val()});
		video.Vote(value);
		$(elem).hide();
	}
	var isBusy = false;
	this.Buttons.Gostei.bind('click',this,function(e) {
		if(isBusy) {
			return false;
		}
		isBusy = true;
		vote(video.VOTE_GOSTEI);
		return false;
	});
	this.Buttons.NaoGostei.bind('click',this,function(e) {
		if(isBusy) {
			return false;
		}
		isBusy = true;
		vote(video.VOTE_NAO_GOSTEI);
		return false;
	});
};

$(document).ready(function() {
	$('#logo').belated();

	$('textarea.defaultvalue, input.defaultvalue').defaultvalue();

	$('input[name="permalink"]').autoselect();

	$('textarea[name="mensagem_twitter"]').limit(140,$('#contagem_caracteres_twitter')[0]);

	VireCelebridade.VideoWidget('#meuvideo .botoes');

	VireCelebridade.SendMailWidget('#envioEmail');

	VireCelebridade.UploadWidget('form#novo_video')

	$('#envioTwitter').submit(function() {
		var mensagem = $('textarea[name="mensagem_twitter"]').val();
		window.open('http://twitter.com/home/?status='+encodeURIComponent(mensagem));
		return false;
	});
	$('table.modal td a.voltar').live('click', function(){
		$.modal.close();
	});
});

