/* File: js/main.js, Ratio: 37.1% */

$(function(){$('select').selectmenu({style:'dropdown',transferClasses:true,width:200});$('span[title]').tooltip();});window.alert=function(t,head){if(typeof($)!='undefined'){$('#warn').text(t).dialog({autoOpen:false,title:(typeof(head)!='undefined')?head:'System message',modal:true,position:['center',10],buttons:{'OK':function(){$(this).dialog('close');}}}).dialog('open');}else{alert(t);}}
jQuery.fn.toggle=function(){if(!$(this).is(':visible')){$(this).slideDown();}else{$(this).slideUp();}}
function isFriend(id){var ret=false;if(window.friends&&typeof(window.friends)!='undefined'&&window.friends.length>0){for(x in window.friends){var f=window.friends[x];ret=(parseInt(f.Network.user_id)==parseInt(id)||parseInt(f.Network.friend_id)==parseInt(id));if(ret){break;}}}
return ret;}
function getFriend(id){var ret={};if(typeof(window.friends)!='undefined'&&window.friends.length>0){for(x in window.friends){var f=window.friends[x];ret=(parseInt(f.Network.user_id)==parseInt(id)||parseInt(f.Network.friend_id)==parseInt(id));if(ret){ret=f;break;}}}
return ret;}
function isOnline(id){var ret=false;if(typeof(window.online)!='undefined'&&window.online.length>0){for(x in window.online){var f=window.online[x];if(typeof(f.User)!='undefined'){ret=(parseInt(f.User.id)==parseInt(id));if(ret){break;}}}}
return ret;}
function addFriend(id){if(!window.me){alert('You must sign in first');return;}
$.ajax({url:window.webroot+'networks/add/'+id,cache:false,success:function(){if($('#addFriend').is('p')||$('#addFriend').is('div')||$('#addFriend').is('span')){$('#addFriend').fadeOut('slow');}
alert('The user will be notified about your request');}});}
function profileActions(action,el,pos){var id=$(el).attr('id').replace(/[^\d]+/g,'');if(parseInt(id)==parseInt(window.me)){alert('You cannot execute this action on yourself');return;}
if(!window.me){alert('You must sign in first');return;}
switch(action){case'add':if(isFriend(id)){alert('This user is already part of your network');}else{addFriend(id);}
break;case'chat':if(!isOnline(id)){alert('This user is not online');}else if(!isFriend(id)){alert('This user does not belong to your network');}else{var f=getFriend(id);chat(escape(window.user.username)+'-'+escape(f.username));}
break;case'view':location.href=window.webroot+'users/view/'+id;break;case'block':$.ajax({url:window.webroot+'networks/toggle_block/'+window.me+'/'+id,cache:false,dataType:'json',success:function(ret){var m=(ret)?'blocked':'unblocked';alert('The user has been '+m);}});break;case'im':if(!isFriend(id)){alert('This user does not belong to your network');}else{$.ajax({url:window.webroot+'messages/add/'+id,cache:false,type:'GET',success:function(ret){$('#dialog').dialog('destroy').html(ret).dialog({height:'auto',title:'Send message',width:300}).dialog('open');}});}
break;}}
function addComment(sha){if(!window.me){alert('You must sign in first');return;}
$('#CommentAddForm-'+sha+' #CommentContent').attr('value',window.comment);var fdata=$('#CommentAddForm-'+sha).serialize();$.ajax({url:window.webroot+'comments/add',type:'POST',cache:false,data:fdata,success:function(){alert('Your comment has been submitted','Comments');}})}
function sendIM(sha){if(!window.me){alert('You must sign in first');return;}
var fdata=$('#MessageAddForm-'+sha).serialize();$.ajax({url:window.webroot+'messages/add',cache:false,dataType:'json',data:fdata,type:'POST',success:function(ret){if(ret){alert('Your message has been succesfully delivered');$('#dialog').dialog('close');openIM();}
else{alert('Your message cannot be delivered. Do not leave the input fields empty.');$('#dialog').dialog('close');openIM();}}});}
function openIM(){if(!window.me){alert('You must sign in first');return;}
$('#dialog').dialog('destroy');$.ajax({url:window.webroot+'messages/index',cache:false,async:false,success:function(ret){$('#dialog').empty().html(ret).dialog({height:'auto',title:'Mail',width:500,position:'center',open:function(){$('#messages').tabs({spinner:'Loading messages...'});}}).dialog('open');}});}
function read(id,update){$.ajax({url:window.webroot+'messages/mark_read/'+id,cache:false,dataType:'json',success:function(ret){$('.content-'+id).toggle(400);}})}
function reply(recipient_id,parent_id){$('#messages').tabs('add',window.webroot+'messages/add/'+recipient_id+'/'+parent_id,'New reply',3).tabs('select',3);}
function deleteIM(message_id){$.ajax({url:window.webroot+'messages/delete/'+message_id,cache:false,dataType:'json',success:function(ret){if(typeof(ret)!='undefined'){if(ret){alert('The message was deleted');$('#dialog').dialog('close');$('.message-'+message_id).remove();openIM();}else{alert('The message cannot be deleted');$('#dialog').dialog('close');openIM();}}}});}
function action(href){href=href.replace(/^\/(.*)$/gi,'$1');if(window.logged){$.ajax({url:window.webroot+href,cache:false,success:function(ret){$('#picturePlayer').empty().html(ret).dialog({autoOpen:false,title:'Picture',width:(window.app.MAX_PIC_WIDTH+85),height:'auto',modal:true}).dialog('open');}});}
else{$('#picturePlayer').empty().html('You are not allowed to watch a picture without sign up').dialog({autoOpen:false,title:'Picture',width:250,height:65,modal:true}).dialog('open');}
return false;}
function actionVideo(href){href=href.replace(/^\/(.*)$/gi,'$1');if(window.logged){$.ajax({url:window.webroot+href,cache:false,success:function(ret){$('#videoPlayer').empty().html(ret).dialog({autoOpen:false,title:'Video',width:(window.app.VIDEO_WIDTH+85),height:'auto',modal:true}).dialog('open');}});}else{$('#videoPlayer').empty().html('You are not allowed to watch a video without sign up').dialog({autoOpen:false,title:'Video',width:250,height:65,modal:true}).dialog('open');}}
