$(document).ready(function() {
	$('.expand_wall_posts_links').click(function(e) {
		e.preventDefault();
		$('#expand_wall_posts_' + parseInt($(this).attr('rel'))).toggle();
		
		if($(this).text() == "Show More") {
			$(this).text('Show Fewer');
		} else {
			$(this).text('Show More');
		}
	});
	
	
	$('.site_button').mouseenter(function(i) {
		$('#icon_tooltip').text($(this).attr('rel'));
		
		var pos = $(this).position();

		pos.left = pos.left + 25;
		
		pos.left = pos.left - ($('#icon_tooltip').width() / 2);
		
		pos.top = pos.top + 60;
		

		
		$('#icon_tooltip').css('width', 'auto');

		$('#icon_tooltip').css('left', pos.left + 'px');
		$('#icon_tooltip').css('top', pos.top + 'px');
		$('#icon_tooltip').fadeIn();
	});
	
	$('.site_button').mouseleave(function(i) {
		$('#icon_tooltip').hide();
	});
	
	$('.overlay').overlay({target: '#overlay', top: '5%', 
                             
                             	mask: {
                        	color: '#3A3A3A',
                        	loadSpeed: 200,
                            	opacity: 0.9
	},    
                                onBeforeLoad: function() {
                                var el = this.getTrigger();
       
                                }
                             }); //handling notifications
	var overapi = $('.overlay').data('overlay');
	$('#notification-form').submit(function(e){
		var f = $(this);
		var s = f.serialize();
		e.preventDefault();
		$.post('/profile/notification', s, function(){
			overapi.close();
			$('.skip20:last').html('<p class="message-flash message-flash-notice">Message was sent</p>');
			f.find('input,textarea').not(':submit').val('');
			});
		});
	
	$('#bio').NobleCount('#count', {	on_negative: 'go_red',
	on_positive: 'go_green',
max_chars: 1024});
	
    $('#vcommentsForm').submit(function(e) {
        $(document).mask({color: '#999999', closeSpeed: 'normal'});
        e.preventDefault();
        $('#vcommentsForm button').attr('disabled', 'disabled');
        $.post(this.action, $(this).serialize(), function(data){
           $('.comments-wrapper .single-comment').removeClass('last');
	   $('.no-comments-yet').remove();
	   $('#vcommentsForm textarea').val('');
           $('.comments-wrapper .single-comment:last').after(data);
           $('#vcommentsForm button').attr('disabled', '');
           $.mask.close();
        })     
    });	

	if ($('.add-site,.add-social').length>0) {
		$('.add-site').live('click', function() {
			var parent = $(this).parent();
			parent.after('<div>'+parent.html()+'</div>');
			parent.next().find('input').attr('value', '');
			$(this).removeClass('add').removeClass('add-site').addClass('remove').addClass('remove-site').html('- Remove');		
		});
		
		$('.remove-site').live('click', function() {
			$(this).parent().remove();		
		});
		
		$('.add-social').live('click', function() {
			var parent = $(this).parents(".input-wrapper:first");
			parent.after('<table class="input-wrapper">'+parent.html()+'</table>');
			parent.next().find('input').attr('value', '');
			parent.next().find("select option[value='0']").attr('selected', 'selected');
			parent.next().find("input:radio").removeAttr('checked');
			parent.next().find("input:radio").attr('value', parseInt(parent.find("input:radio").val())+1);
			parent.next().find(".site-example-td:first").html('');
	
			$(this).removeClass('add').removeClass('add-social').addClass('remove').addClass('remove-social').html('- Remove');		
		});
		
		$('.remove-social').live('click', function() {
			$(this).parents(".input-wrapper:first").remove();		
		});
		
		$('.site-selector').livequery('change', function() {
			var value = $(this).parents(".input-wrapper").find("input.example-value:first").attr('value');
			$(this).parents(".input-wrapper").find(".site-example-td:first").html(siteExamples[this.value].replace(/USERNAME/ig, '<span class="example-username">'+value+'</span>'));
		});
		
		$("input.example-value").live('keyup', function() {
			var siteId = $(this).parents(".input-wrapper").find(".site-selector:first").attr('value');
			if (siteId!=0)
				$(this).parents(".input-wrapper").find(".site-example-td:first").html(siteExamples[siteId].replace(/USERNAME/ig, '<span class="example-username">'+this.value+'</span>'));
		});
		
		$("input.example-value").keyup();
		
		if ($("#signup-two-form").length>0) {
			$("#signup-two-form").validate({
				rules: {
					name: {
						required: true,
						minlength: 5
					},
					'photo[]': {
						required: true
					},
					genre_id: {
						required: true
					},
					bio: {
						required: true
					},
					agree: {
						required: true
					},
					password: {
						required: true,
						minlength: 5
					},
					confirm_password: {
						required: true,
						minlength: 5,
						equalTo: "#password"
					}
				},
				messages: {
					name: {
						required: "Please enter your name",
						minlength: "5+ chars"
					},
					'photo[]': {
						required: "Provide photo"
					},
					genre_id: {
						required: "Please select genre"
					},
					bio: {
						required: "Write something here"
					},
					agree: {
						required: "Please check if you agree"
					},
					password: {
						required: "Please enter password",
						minlength: "5+ chars"
					},
					confirm_password: {
						required: "Confirm your password here",
						minlength: "5+ chars",
						equalTo: "Passwords don't match"
					}
				}
			});
			$("#password").blur(function() {
				$("#confirm_password").valid();
			});
		}
	}
	
	if ($('.fancybox').length>0) {
		$('.fancybox').fancybox();
	}
	
});

function callIframe(url, affiliate) {
    if (affiliate) {
		$(document.body).append('<iframe id="affiliate_frame" width="1" height="1" style="display:none;">');
	    $('iframe#affiliate_frame').attr('src', affiliate);
	
	    $('iframe#affiliate_frame').load(function() 
	    {
	        location.href = url;
	    });
    } else {
    	location.href = url;
    }
}
