/*
* author	isunja
* req		Prototype >= 1.6.0.3
*    		script.aculo.us >= 1.8.2 
*/

function _shareSuccess() {
	$('shareBox').insert({
		after: new Element('div',{id: 'shareSuccess'})
	});
	$('shareSuccess').setStyle({
		border: '1px solid #ccc',
		fontSize: '14px',
		margin: '10px 0px 10px 0px',
		display: 'none'
	})
	.update('Uspjesno ste pozvali vase prijatelje!');
	Effect.BlindUp('shareBox',{duration: 1.5});
	setTimeout("$('shareSuccess').appear(); $('shareBox').remove(); ",1500);
}
	 
function sharePage(page) {
	new Ajax.Request('/ajax/share.php?p=' + page, {
		onSuccess: function(tr) {
			data = tr.responseText;
			$('friends').update(data);
		}
	});
}
 
function shareIgnore(el) {
	var btn = $(el);
	new Ajax.Request('/ajax/share.php', {
		method: 'post',
		parameters: {ignore: btn.readAttribute('rel')},
		onSuccess: function(tr) {			 
			btn.setValue('Uspjesno si blokirao notifikacije od korisnika');
		}	 
	});
}

function shareInvite(el) {   
	 var profiles = $('profiles').immediateDescendants();
	 var flag     = 0;
	 profiles.each(function(item) {    
		 if(item.readAttribute('rel') === el.readAttribute('rel')) flag = 1; return;
	 });
	 $(el).down(5).checked = true;
	 if(flag === 0) {
		 var profile = $(el).clone(true);
		 profile.writeAttribute({onclick: 'remove(this); if($("'+ $(el).id +'")) $("'+ $(el).id +'").down(5).checked = false;'})
		 .setStyle({
			 position: '',
			 top: '',
			 left: ''
		 })
		 .id = 'profile-' + profile.readAttribute("rel");
		 $('profiles').insert(profile);
	 }else {
		 $('profile-' + el.readAttribute("rel")).remove();
		 $(el).down(5).checked = false;
	 }
} 

function ignoreInvite(el) {
	var parent 	= el.ancestors().first();
	var box 	= parent.select('input').first();

	if(box.readAttribute('disabled') == '') {
		box.disabled = '';
		box.store('invite',0);
	} else {
		box.disabled = 'disabled';
		box.checked  = false;
		box.store('invite',-1);
	}
}

function addInvite(el) {
	var parent 	= el.up('span');
	var box 	= parent.select('input').first();	
	if(el.readAttribute('src') != undefined) {
		box.checked = true;
		box.disabled = '';
	}
	if(box.checked.toString() === 'true') 
		if(box.retrieve('invite') == undefined || box.retrieve('invite') == 0) box.store('invite',1);		
	else box.store('invite',0);
}

function submitInvite(el) {
	el.disabled = 'disabled';
	$('friends').select('.friendz').each(function(item) {
		var box = item.select('input').first();
		var foo = box.retrieve('invite');		
		if(foo != 0 && (foo == 1 || foo == -1)) {
			var input = new Element('input',{type: 'hidden', value: item.readAttribute('rel')});
			if(foo == 1) input.writeAttribute({name:'invites[]'});			
			else if(foo == -1) input.writeAttribute({name:'ignores[]'});
			$('invite').insert(input);
		}		
	});
	
	new Ajax.Request('/invite.php', {
		method: 'post',
		parameters: $('invite').serialize(),
		onSuccess: _successInvite
	});
	
	$(el).remove();
}

function _successInvite() {
	$('friends').update('<h2 style="font-size:16px">Uspjesno si pozvao/la svoje prijatelje!</h2>')
				.setStyle({border:''});
}

document.observe('dom:loaded',function() {
	if($('friends')) {
		$('friends').immediateDescendants().each(function(item){
			item.down(5).checked = false;
		});
	}	

	if($('share')) {
		$('share').observe('submit',function(e) {
			var profiles = [];
			e.stop();
			$('profiles').immediateDescendants().each(function(item) {    
				profiles.push(item.readAttribute('rel'));
			});
			if(profiles.length <= 0) return false;
			this.down(0).setValue(profiles);
			new Ajax.Request('/share',{
				method: 'post',
				parameters: this.serialize(true),
				onSuccess: _shareSuccess
			});	
		});
	}
});