$(document).ready(function() {

	$('a[href=#]').click(function(){return false;});
	
	$('[rel=tipsy]').tipsy({html: true, fade: false, gravity: 'e', title: 'content'});
	$('[rel=tipsy_e]').tipsy({html: true, fade: false, gravity: 'e', title: 'content'});
	$('[rel=tipsy_w]').tipsy({html: true, fade: false, gravity: 'w', title: 'content'});
	$('[rel=tipsy_n]').tipsy({html: true, fade: false, gravity: 'n', title: 'content'});
	$('[rel=tipsy_s]').tipsy({html: true, fade: false, gravity: 's', title: 'content'});
	$('[rel=tipsy_nw]').tipsy({html: true, fade: false, gravity: 'nw', title: 'content'});
	$('[rel=tipsy_sw]').tipsy({html: true, fade: false, gravity: 'ws', title: 'content'});
	
	$('[rel=tipsy_auto_ns]').tipsy({html: true, fade: false, gravity: $.fn.tipsy.autoNS, title: 'content'});
	
	$('[rel=tipsy_del_post]').tipsy({html: true, fade: false, gravity: 'e'});
	$('[rel=tipsy_search]').tipsy({html: true, fade: false, gravity: $.fn.tipsy.autoNS, title: 'content'});
	
	try{Group.group = pm_post.group; setInterval('Group.refresh()',45000);}catch(e){}
	
});


/* GET XHR AND AJAX EVENTS */
function getXHR(){var xhr=null;if(window.XMLHttpRequest||window.ActiveXObject){if(window.ActiveXObject){try{xhr=new ActiveXObject("Msxml2.XMLHTTP")}catch(e){xhr=new ActiveXObject("Microsoft.XMLHTTP")}}else{xhr=new XMLHttpRequest()}}else{return null}return xhr}
var xhr = getXHR();

/*ASPECTS*/
function add(aspect_name)
{
	if(aspect_name && aspect_name != 'undefined')
	{
		display_hide_inline('add_aspect_loader');
		var aspect_name1 = encodeURIComponent(aspect_name);

		var xhr = getXHR();
	
		var ui = Math.random();
		
		xhr.open("POST", "/groups/add/script/", true);
		xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xhr.send("aspect_name=" + aspect_name1 + "&ui=" + ui);

		xhr.onreadystatechange = function() {
			if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
				display_hide_inline('add_aspect_loader');
				if(xhr.responseText != ''){
					var data=eval("(" + xhr.responseText + ")");
					if(data.good && data.good==1){
						if(data.aspect_id && data.aspect_name){
						
							if(document.getElementById('aspects_container_for_layout')){
									a = document.createElement('a');
									a.href = '/groups/' + data.aspect_id + '/';
									a.id = 'aspect_' + data.aspect_id;
									a.className = 'aspect_' + data.aspect_id;
									a.innerHTML = data.aspect_name;
									document.getElementById('aspects_container_for_layout').appendChild(a);
									closeDialog('aspect_box');
							}
							else{
								window.location.reload();
							}
						}
					}
				}
			}
		}
	}
}
	
/* TABS */
function update_tabs_profile(tab)
{
		if(tab==1){
			document.getElementById('tab_2').style.display="none";
			if(document.getElementById('tab_3')) {document.getElementById('tab_3').style.display="none";}
			if(document.getElementById('tab_4')) {document.getElementById('tab_4').style.display="none";}
			document.getElementById('tab_1').style.display="block";
		}
		if(tab==2){
			document.getElementById('tab_1').style.display="none";
			if(document.getElementById('tab_3')) {document.getElementById('tab_3').style.display="none";}
			if(document.getElementById('tab_4')) {document.getElementById('tab_4').style.display="none";}
			document.getElementById('tab_2').style.display="block";
		}
		if(tab==3){
			document.getElementById('tab_1').style.display="none";
			document.getElementById('tab_2').style.display="none";
			if(document.getElementById('tab_4')) {document.getElementById('tab_4').style.display="none";}
			document.getElementById('tab_3').style.display="block";
		}
		if(tab==4){
			document.getElementById('tab_1').style.display="none";
			document.getElementById('tab_2').style.display="none";
			document.getElementById('tab_3').style.display="none";
			document.getElementById('tab_4').style.display="block";
		}
}
	
/* SWITCH DIV HIDDEN-DISPLAYED */
function display_hide(id)
{if(document.getElementById(id).style.display=="none")
{document.getElementById(id).style.display="block";}
else
{document.getElementById(id).style.display="none";}}

function display_hide_inline(id)
{if(document.getElementById(id).style.display=="none")
{document.getElementById(id).style.display="inline";
}else{document.getElementById(id).style.display="none";}}

function display_hide_inline_block(id)
{
        if(document.getElementById(id).style.display=="none")
        {
                document.getElementById(id).style.display="inline-block";
        }
        else
        {
                document.getElementById(id).style.display="none";
        }
}


/* TEXT */
function hide_text_form(form, name)

{
	document.forms[form].elements(name).value='';
}

function show_text_form(form, name, val)
{
	document.forms[form].elements(name).value=val;
}

function hide_text(id, val)
{
	if($('#'+id).val()==val || $('#'+id).val()=='')
	{
		$('#'+id).val("");
		$('#'+id).css({ color: "#000" });
	}

}

function show_text(id, val)
{
	if($('#'+id).val()==val || $('#'+id).val()=='')
	{
		$('#'+id).css({ color: "#ccc" });
		$('#'+id).val(val);
	}
}
function open_box_id(id){ Dialog.open(id); }

var Dialog = {
	open: function(id) {
		$('#' + id).fadeIn('fast');
	},
	close: function(id) {
		$('#'+id).css({ display: "none" });
	}
};


/* posts */
var postsWidgets = {
	add: function(widget) {
		new_post = $('#post_new');
		old_text = new_post.val();
		new_post.val(old_text + widget);
		new_post.focus();
	},
	
	youtube: function() {
		text = ' http://www.youtube.com/v/';
		postsWidgets.add(text);
	}
};


var Posts = {
	add: function(type, from, to, msg, aspect, page) {
		if(msg){
			pm.show_loader();
			old_text = $('#submit_status_post').val();
			Posts.change_text(gettext('Sharing...'));
			msg = encodeURIComponent(msg);
			ui = Math.random();
			// ---------
			$.ajax({
				type: "POST",
				url: "/posts/add/script/",
				data: "type_of_post=" + type + "&msg=" + msg + "&from_id=" + from + "&to_id=" + to + "&id_aspect=" + aspect + "&page=" + page,
				success: function(data) {
					if(data!='error') {
						Posts.insert(data);
					}
				},
				error: function(){ pm.ajax_error(); },
				complete: function(){ 
					pm.hide_loader();
					Posts.change_text(old_text);
				}
			});
		}
	},
	
	insert: function(data) {
		$(data).insertBefore('.first_post');
		$('.first_post').removeClass('first_post');
		$('.posted_new').addClass('first_post').fadeIn('slow').removeClass('.posted_new');
		$('#post_new').val('').blur();
		$('#submit_status_post').css({ display:'none' });
		$('#new_post_where_to_post').css({ display:'none' });
		try{Group.json = Group.json + 1}catch(e){}
	},
	
	change_text: function(text) {
		$('#submit_status_post').val(text);
	},
	
	remove: function(post) {
			$.ajax({
				type: "POST",
				url: "/posts/delete/script/",
				data: "post_id=" + post,
				success: function(data){
					data = $.parseJSON(data)
					if(data.good){
						Posts.do_remove(post);
					}
					else{ alert('there was an error');}
				},
				error: function(){ pm.ajax_error(); }
			});
	},
	
	do_remove: function(post) {
		$('#post_' + post).animate({ opacity:0 }).hide('fast').animate({ opacity:1 });
		try{Group.json = Group.json - 1}catch(e){}
	},
	
	show_video: function(key) {
		video = $('.video_num_' + key);
		if(video.css('display')=='none') {
			video.css({opacity:0}).show('fast').animate({ opacity:1 });
		}
		else {
			video.animate({ opacity:0 }).hide('fast').animate({ opacity:1 });
		}
	}
};


var Comments = {
	add: function(id_post) {
		_submit = $('#post_' + id_post).find('input[type=submit]');
		old_val = _submit.val();
		Comments.change_text(gettext('Posting comment...'), _submit);
		$comment = $('#comment_' + id_post);
		value = $comment.val();
		value = encodeURIComponent(value);
		csrf = $('#post_' + id_post).find('input[name=csrfmiddlewaretoken]').val();
		$.ajax({
			type: 'POST',
			url: '/_comments_/add/script/',
			data: 'post=' + id_post + '&comment=' + value + '&csrf_token=' + csrf,
			success: function(data) {
				$comment.val('');
				$comment.blur();
				window.location.reload();
			},
			error: function(){
				pm.ajax_error();
				Comments.change_val(old_val, _submit);
			}
		});
	},
	
	change_text: function(text, button) {
		button.val(text);
	},
	
	remove: function(id_post, comment) {
		csrf = $('#post_' + id_post).find('input[name=csrfmiddlewaretoken]').val();
		$.ajax({
			type: 'POST',
			url: '/_comments_/remove/script/',
			data: 'comment=' + comment + '&csrf_token=' + csrf,
			success: function(data) {
				data = $.parseJSON(data);
				if(data.good) {
					$('#comment-' + comment).animate({ opacity:0 }).hide('fast').animate({ opacity:1 }).removeClass('comment__');
				}
			},
			error: function(){ pm.ajax_error(); }
		});
	}
};


$(document).ready(function(){
	$(window).bind('resize', function() {
		header_notif()
	});
	
	var notif_ = $('.notification_container');

	$('.notif').click(function() {
		header_check();
		header_notif();
		return false;
	});
	
	notif_.blur(function() {
		if(notif_.css("display") == 'block') {
			notif_.toggle();
			$('.notif').removeClass('notif_active');
		}
	});
	
	function header_notif() {
		position = $('.notif').position();
		width = $('.notif').width();
		this_width = notif_.width();
		final_ = (this_width-width)-20;
		if(position) {
			$('.notif_container').css({ left: (position.left-final_) });
		}
	}
	
	function header_show() {
		$('.notif').addClass('notif_active');
		$('.notification_container').fadeIn('fast');
	}
	function header_hide() {
		$('.notif').removeClass('notif_active');
		$('.notification_container').fadeOut('fast');
	}
	function header_check() {
		if(notif_.css("display") == 'none') {
			header_show();
		}
		else {
			header_hide();
		}
	}
	function header_check_it() {
		if(notif_.css("display") == 'none') {
			return false;
		}
		else {
			return true;
		}
	}
	
	$('body').click(function() { 
		if(header_check_it()) {
			header_check()
		}
	}); 
	$('.notification_container').click(function(event){ 
		event.stopPropagation(); 
	}); 
	
});

