function change_ava (){
    $('#ava').ajaxfileupload({
      'action': '/upload.php'
    });   
}

function forum_add_msg( tid ){
   tinyMCE.triggerSave();
   tinyMCE.activeEditor.setContent('');
   txt = $('#add_comment').val();
   $('#add_comment').val('');
   $.notifier.broadcast({ skin:'rounded', ttl:'', msg: 'Sending...'}); 
   $.ajax({
        type: "GET",
        cache: false,
        contentType: "application/json; charset=utf-8",
        url: "/forum/addmsg",
        data: ({msg : ""+txt , ajax: 'add_msg', tid: tid}),
        dataType: "html",
        success: function(msg){
            $.notifier.broadcast({ skin:'rounded', ttl:'', msg: msg}); 
            $('#last-comments1').html( msg );
       }
    });
}

function forum_del_msg( mid ){
   $.ajax({
        type: "GET",
        cache: false,
        contentType: "application/json; charset=utf-8",
        url: "/forum/delmsg",
        data: ({mid : mid , ajax: 'del_msg'}),
        dataType: "html",
        success: function(msg){
            $.notifier.broadcast({ skin:'rounded', ttl:'', msg: msg});
            $('#last-comments1').html( msg );
       }
    });
}

function forum_format( data ){
    
}

function add_topic( dir ){
   $.ajax({
    type: "GET",
    cache: false,
    contentType: "application/json; charset=utf-8",
    url: "/forum/addtopic",
    data: ({ ajax: 'add_topic', sbj: document.getElementById("new_topic").value, dip: dir}),
    dataType: "html",
    success: function(msg){

    }
   });
}

function get_status(){
      $.ajax({
        type: "GET",
        cache: false,
        contentType: "application/json; charset=utf-8",
        url: "/site/status",
        dataType: "json",
        data: ({ajax: 'get_status'}),
        success: function(msg){
            $('#gamers_count').html( msg.cnt );
            $('#online-users').html( msg.players );
            if( msg.m ){
                $.notifier.broadcast({ skin:'rounded', ttl:'', msg: msg.m });
		$("#audioalert").jPlayer("setMedia", {mp3: "http://cs1660.vkontakte.ru/u8919929/audio/3efafd976b89.mp3"} );
		$("#audioalert").jPlayer("play");
            }
       }
     });
}

function online (){
    if ($("#online-users").is(":hidden")) {
        $("#online-users").slideDown("slow");
        $("#onlineUsersBar").attr("src", "/images/up.gif");
    } else {
        $("#online-users").slideUp("slow");
        $("#onlineUsersBar").attr("src", "/images/down.gif");
    }
};

function game_add_comment( gid ){
   txt = $('#add_comment').val();
   $('#add_comment').val('');
   $.notifier.broadcast({ skin:'rounded', ttl:'', msg: 'Sending...'}); 
   $.ajax({
    type: "GET",
    cache: false,
    contentType: "application/json; charset=utf-8",
    url: "/game/addcomment",
    data: ({comment : ""+txt , ajax: 'add_comment', gid: gid}),
    dataType: "html",
    success: function(msg){
        $.notifier.broadcast({ skin:'rounded', ttl:'', msg: msg});
        game_get_comments( gid, page );
   }
 });
}

curpic  = 0;
page = 0;
wallpage = 0;

function repeat_game_comments( gid ){
    game_get_comments( gid, page );
}

function game_get_comments_next( gid ){
    page=page+1;
    game_get_comments( gid, page );
}

function game_get_comments_prev( gid ){
    page=page-1;
    if( page < 0 ) page = 0;
    game_get_comments( gid, page );
}

function game_get_comments( gid, page ){
   $.ajax({
    type: "GET",
    cache: false,
    contentType: "application/json; charset=utf-8",
    url: "/game/getcomment",
    data: ({ gid : gid , page: page, ajax: 'get_comment' }),
    dataType: "html",
    success: function(msg){
        $('#last-comments').html( msg );
   }
 });
}

function repeat_wall_comments( uid ){
    wall_get_comments( uid, wallpage );
}

function wall_get_comments_next( uid ){
    page=page+1;
    wall_get_comments( uid, page );
}

function wall_get_comments_prev( uid ){
    page=page-1;
    if( page < 0 ) page = 0;
    wall_get_comments( uid, page );
}

function wall_get_comments( uid, page ){
   $.ajax({
    type: "GET",
    cache: false,
    contentType: "application/json; charset=utf-8",
    url: "/user/getwall",
    data: ({ uid : uid , page: page, ajax: 'get_wall' }),
    dataType: "html",
    success: function(msg){
        $('#last-comments-read').html( msg );
   }
 });
}

function del_msg_wall( wid ) {
   $.ajax({
    type: "GET",
    cache: false,
    contentType: "application/json; charset=utf-8",
    url: "/user/delwall",
    data: ({ wid : wid , page: page, ajax: 'del_wall' }),
    dataType: "json",
    success: function(msg){
	$.notifier.broadcast({skin:'rounded', ttl:'', msg: msg.msg});
        wall_get_comments( msg.uid, page );
   }
 });
}

function repeat_read( rid ){
   $.ajax({
    type: "GET",
    cache: false,
    contentType: "application/json; charset=utf-8",
    url: "/msg/getmsg",
    data: ({ rid : rid , ajax: 'get_msg' }),
    dataType: "html",
    success: function(msg){
        $('#read-last-comments').html( msg );
   }
 });
}

function msg_send( rid ){
   tinyMCE.triggerSave();
   tinyMCE.activeEditor.setContent('');
   txt = $('#add_comment').val();
   $('#add_comment').val('');
   $.notifier.broadcast({skin:'rounded', ttl:'', msg: 'Sending...'}); 
   $.ajax({
    type: "GET",
    cache: false,
    contentType: "application/json; charset=utf-8",
    url: "/msg/addmsg",
    data: ({txt : ""+txt , ajax: 'add_msg', rid: rid}),
    dataType: "html",
    success: function(msg){
        $.notifier.broadcast({skin:'rounded', ttl:'', msg: msg}); 
        repeat_read( rid );
   }
 });
}


function get_smiles() {
   $.ajax({
    type: "GET",
    cache: false,
    contentType: "application/json; charset=utf-8",
    url: "/game/getsmiles",
    data: ({ ajax: 'get_smiles' }),
    dataType: "html",
    success: function(msg){
        $('#smiles').html( msg );
   }
 });
}

function smiles_box( i ){
    if ($("#smile"+i+"div").is(":hidden")) {
        $("#smile"+i+"div").slideDown("slow");
    } else {
        $("#smile"+i+"div").slideUp("slow");
    }
    return false;
}

function game_vote(gid) {
   $.ajax({
    type: "GET",
    cache: false,
    contentType: "application/json; charset=utf-8",
    url: "/game/vote",
    data: ({ ajax: 'vote', gid: gid, vote: document.getElementById("mystr").value }),
    dataType: "html",
    success: function(msg){
        $.notifier.broadcast({skin:'rounded', ttl:'', msg: msg}); 
   }
 });
}

function add2fav( gid ) {
  $.ajax({
    type: "GET",
    cache: false,
    contentType: "application/json; charset=utf-8",
    url: "/user/addgame2fav",
    data: ({ ajax: 'addfav', gid: gid }),
    dataType: "html",
    success: function(msg){
        $.notifier.broadcast({skin:'rounded', ttl:'', msg: msg}); 
   }
 });
}

function delfav( gid ) {
  $.ajax({
    type: "GET",
    cache: false,
    contentType: "application/json; charset=utf-8",
    url: "/user/delfav",
    data: ({ ajax: 'delfav', gid: gid }),
    dataType: "html",
    success: function(msg){
        $.notifier.broadcast({skin:'rounded', ttl:'', msg: msg}); 
   }
 });
}

function addtowall( uid ) {
   tinyMCE.triggerSave();
   tinyMCE.activeEditor.setContent('');
   txt = $('#addtowall').val();
   $('#addtowall').val('');

  $.ajax({
    type: "GET",
    cache: false,
    contentType: "application/json; charset=utf-8",
    url: "/user/addwall",
    data: ({ ajax: 'add_wall', uid: uid, txt: txt }),
    dataType: "html",
    success: function(msg){
	repeat_wall_comments(uid);
        $.notifier.broadcast({skin:'rounded', ttl:'', msg: msg});
   }
 });
}

function pic_get_next( ){
  $.ajax({
    type: "GET",
    cache: false,
    contentType: "application/json; charset=utf-8",
    url: "/alboms/next",
    data: ({ ajax: 'get_next', pic: document.getElementById("pic").value }),
    dataType: "json",
    success: function(msg){
	//repeat_wall_comments(uid);
        $('#pic').attr( 'value' , msg.pid );
	$('#picpath').attr( 'src' , '/img/alboms/'+msg.src );
	$('#last-comments').html( msg.comments );
   }
 });
}

function photo_add_comment( gid ){
   txt = $('#add_comment').val();
   $('#add_comment').val('');
   $.notifier.broadcast({ skin:'rounded', ttl:'', msg: 'Sending...'});
   $.ajax({
    type: "GET",
    cache: false,
    contentType: "application/json; charset=utf-8",
    url: "/alboms/addcomment",
    data: ({comment : ""+txt , ajax: 'add_comment', pic: document.getElementById("pic").value}),
    dataType: "json",
    success: function(msg){
        $('#last-comments').html( msg.comments );
   }
 });
}

function msg_del( uid ){

   $.ajax({
    type: "GET",
    cache: false,
    contentType: "application/json; charset=utf-8",
    url: "/msg/delmsgtree",
    data: ({ ajax: 'delmsgtree', uid:uid}),
    dataType: "html",
    success: function(msg){
        $('#msg' + uid ).html( msg );
   }
 });

}

function msg_set_as_read( uid ){

   $.ajax({
    type: "GET",
    cache: false,
    contentType: "application/json; charset=utf-8",
    url: "/msg/setasread",
    data: ({ ajax: 'setasread', uid:uid}),
    dataType: "html",
    success: function(msg){
        $('#msg' + uid + ' div p' ).css( 'background-color', '#C6D7F9' );
   }
 });
}

function add_carma( uid ) {
    carma( uid, 0 );
}

function del_carma( uid ) {
    carma( uid, 1 );
}


function carma( uid, carma ) {
  $.ajax({
    type: "GET",
    cache: false,
    contentType: "application/json; charset=utf-8",
    url: "/user/addcarma",
    data: ({ ajax: 'carma', uid: uid, carma: carma }),
    dataType: "html",
    success: function(msg){
        $.notifier.broadcast({skin:'rounded', ttl:'', msg: msg});
   }
 });
}

function del_from_friends( uid ) {
  $.ajax({
    type: "GET",
    cache: false,
    contentType: "application/json; charset=utf-8",
    url: "/friend/del",
    data: ({ ajax: 'friend', uid: uid }),
    dataType: "html",
    success: function(msg){
        $.notifier.broadcast({skin:'rounded', ttl:'', msg: msg});
   }
 });
}

function approve_friends( uid ) {
  $.ajax({
    type: "GET",
    cache: false,
    contentType: "application/json; charset=utf-8",
    url: "/friend/approve",
    data: ({ ajax: 'approve', uid: uid }),
    dataType: "html",
    success: function(msg){
        $.notifier.broadcast({skin:'rounded', ttl:'', msg: msg});
   }
 });
}

function add_friends( uid ) {
  $.ajax({
    type: "GET",
    cache: false,
    contentType: "application/json; charset=utf-8",
    url: "/friend/add",
    data: ({ ajax: 'add', uid: uid }),
    dataType: "html",
    success: function(msg){
        $.notifier.broadcast({skin:'rounded', ttl:'', msg: msg});
   }
 });
}

function add_gift(giftid, uid) {
  $.ajax({
    type: "GET",
    cache: false,
    contentType: "application/json; charset=utf-8",
    url: "/user/addgift",
    data: ({ ajax: 'gift', uid: uid, giftid: giftid }),
    dataType: "html",
    success: function(msg){
        $.notifier.broadcast({skin:'rounded', ttl:'', msg: msg});
   }
 });
}




function save_profile(){
  $.ajax({
    type: "GET",
    cache: false,
    contentType: "application/json; charset=utf-8",
    url: "/user/saveprofile",
    data: ({ ajax: 'profile', sex: $('#sex').val(), city: $('#city').val() }),
    dataType: "html",
    success: function(msg){
        $.notifier.broadcast({skin:'rounded', ttl:'', msg: msg});
   }
 });
}


function add_music( id ){
  $.ajax({
    type: "GET",
    cache: false,
    contentType: "application/json; charset=utf-8",
    url: "/music/export",
    data: ({ ajax: 'export', id: id }),
    dataType: "json",
    success: function(msg){
        $.notifier.broadcast({skin:'rounded', ttl:'', msg: msg.msg});
   }
 });
}

function new_albom(  ){
  if( !$('#newalbom').val() ){
      alert("name is empty");
      return false;
  }
  $.ajax({
    type: "GET",
    cache: false,
    contentType: "application/json; charset=utf-8",
    url: "/alboms/new",
    data: ({ ajax: 'new', name: $('#newalbom').val() }),
    dataType: "json",
    success: function(msg){
        $.notifier.broadcast({skin:'rounded', ttl:'', msg: msg.msg});
        $('#albom_list').html( msg.alboms );
   }
 });
}



function del_albom( aid ){
  $.ajax({
    type: "GET",
    cache: false,
    contentType: "application/json; charset=utf-8",
    url: "/alboms/del",
    data: ({ ajax: 'del', aid: aid }),
    dataType: "json",
    success: function(msg){
       window.location.href="/alboms/";
    }
   }
 );
}

function save_status(){
  $.ajax({
    type: "GET",
    cache: false,
    contentType: "application/json; charset=utf-8",
    url: "/status/update",
    data: ({ ajax: 'update', status: $("#status").val() }),
    dataType: "html",
    success: function(msg){
       $.notifier.broadcast({skin:'rounded', ttl:'', msg: msg});
    }
   }
 );
}

function join_to_clan( cid ){
  $.ajax({
    type: "GET",
    cache: false,
    contentType: "application/json; charset=utf-8",
    url: "/clans/addtoclan",
    data: ({ ajax: 'addtoclan', cid: cid }),
    dataType: "html",
    success: function(msg){
       $.notifier.broadcast({skin:'rounded', ttl:'', msg: msg});
    }
   }
 );
}

function go_from_clan( cid ){
  $.ajax({
    type: "GET",
    cache: false,
    contentType: "application/json; charset=utf-8",
    url: "/clans/fromclan",
    data: ({ ajax: 'fromclan', cid: cid }),
    dataType: "html",
    success: function(msg){
       $.notifier.broadcast({skin:'rounded', ttl:'', msg: msg});
    }
   }
 );
}

function add_clan( ){
  $.ajax({
    type: "GET",
    cache: false,
    contentType: "application/json; charset=utf-8",
    url: "/clans/addclan",
    data: ({ ajax: 'addclan', name: $('#clanname').val() }),
    dataType: "html",
    success: function(msg){
       $.notifier.broadcast({skin:'rounded', ttl:'', msg: msg});
       $('#new_clan').val( msg );
    }
   }
 );
}

//  export


OPENED_WINDOWS={};
WINDOW_TEMPLATES={};

function openWindow(id,tpl,modal,style,variables){
	if (OPENED_WINDOWS[id]){
		return false;
	}
	if (typeof modal == 'undefined'){
		modal=true;
	}

	if (modal && !document.getElementById('modal_window_night')){
		$('<div id="modal_window_night"></div>').css({
			background:'#000000',
			opacity:0.5,
			height:'700%',
			width:'100%',
			position:'absolute',
			top:0,
			left:0,
			zIndex:500
		}).appendTo($(document.body));
	}


	if (typeof style == 'undefined'){
		style={
			width:'500px',
			marginLeft:'-250px',
			height:'200px',
			marginTop:'-100px'
		};
	}
	if (!WINDOW_TEMPLATES[tpl]){
		$.ajax({
			async:false,
			url:'/tpls/popup_windows/'+tpl+'.php',
            data:{},
			success:function(data,status){
				WINDOW_TEMPLATES[tpl]=data;
			}
		});
		if (!WINDOW_TEMPLATES[tpl]){
			if (modal){
				tryCloseWindowNight();
			}
			return false;
		}
	}

	var t = WINDOW_TEMPLATES[tpl];

	if (typeof(variables) == 'object') {
		for (var key in variables) {
			t = t.replace(RegExp('%' + key + '%', 'g'), (variables[key] + '').replace(RegExp('"', 'g'), '&quot;'));
		}
	}
	t=t.replace(/<%id%>/g,id);

	$w = $('<div class="popup" id="'+id+'_popup_div"></div>').html(t);
	$w.css(style);
	if ($.browser.msie && $.browser.version != 8){
		$w.css('top',document.documentElement.clientHeight/2+'px');
		$w.css('marginTop',(style.marginTop.replace('px','')-0+document.documentElement.scrollTop-0)+'px');
	}
	$w.appendTo($(document.body));
	OPENED_WINDOWS[id]={'modal':modal,'style':style};
}

//функция эмуляции position fixed для окошек в ие
function ieWindowOnScroll(){
	var margin;
	for (var i in OPENED_WINDOWS){
		//alert(document.body.clientHeight-document.body.offsetHeight);
		margin = OPENED_WINDOWS[i].style.marginTop;
		margin = margin.replace('px', '')-0;
		margin = (document.documentElement.scrollTop-0+margin-0)+'px';
		$('#'+i+'_popup_div').css('marginTop',margin);
		if ($.browser.msie){
			$('#modal_window_night').css('marginTop',document.documentElement.scrollTop);
		}

	}
}

function closeWindow(id){
	if (!OPENED_WINDOWS[id]){
		return false;
	}
	$('#'+id+'_popup_div').remove();
	var modal = OPENED_WINDOWS[id].modal;
	delete(OPENED_WINDOWS[id]);
	if (modal)
		tryCloseWindowNight();
	return true;
}

function tryCloseWindowNight(){
	var lastmodal = true;

	for (var i in OPENED_WINDOWS){
		if (OPENED_WINDOWS[i].modal){
			lastmodal=false;
			break;
		}
	}
	if (lastmodal){
		$('#modal_window_night').remove();
	}
}


if ($.browser.msie){
	document.documentElement.onscroll=ieWindowOnScroll;
}


function jsTitleIn(title, elm, style, chase){

	if (!elm.id){
		elm.id=uniqueId();
	}
	if (typeof chase == 'undefined'){
		chase=false;
	}
	if (TITLES_TIMEOUTS[elm.id]){
		window.clearTimeout(TITLES_TIMEOUTS[elm.id]);
	}

	if ($('#jsTitle'+elm.id).get(0)){
		return;
	}
	if (!style){
		style='small';
	}

	var styles = {
		'small':{
			'width':'112px','height':'81px','paddingBottom':'0px','paddingLeft':'10px',
			'paddingRight':'27px','marginTop':'-59px','marginLeft':'-145px',
			'backgroundImage':'url(/images/clouds/small.png)','textAlign':'center'
		},
		'medium':{
			'width':'186px','height':'142px','paddingBottom':'20px','paddingLeft':'20px',
			'paddingRight':'20px','marginTop':'-150px','marginLeft':-175+elm.offsetWidth,
			'backgroundImage':'url(/images/clouds/medium.png)'
		}
	}

	if ($.browser.msie && $.browser.version<7){
		styles.small.backgroundImage='none';
		styles.small.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="/images/clouds/small.png", sizingMethod="crop");';
		styles.medium.backgroundImage='none';
		styles.medium.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="/images/clouds/medium.png", sizingMethod="crop");';
	}
	//alert(style.marginLeft);
	var d = $('<div class="jsTitle" id="jsTitle'+elm.id+'" style="position:absolute;z-index:1000;cursor:pointer;"></div>');
	d.appendTo($(document.body));
	d.css(styles[style]);
	var offset = $(elm).offset();

	d.css({'top':offset.top,'left':offset.left,'backgroundRepeat':'no-repeat'});

	d.click(function(){
		if (typeof elm.onclick == 'function'){
			elm.click();
		}
		if (typeof elm.href !='undefined'){
			window.location.href=elm.href;
		}
	});
	if (!chase) {
		d.mouseover(function(){
			if (TITLES_TIMEOUTS[elm.id]){
				window.clearTimeout(TITLES_TIMEOUTS[elm.id]);
			}
		});
		d.mouseout(function(){
			jsTitleOut(elm);
		});
	}
	d.html('<table height="100%" width="100%"><tr><td valign="middle">'+title+'</td></tr></table>');

}
TITLES_TIMEOUTS={};
/*
document.documentElement.onmousemove=function(){
	document.write('asd');
}
//*/
function jsTitleOut(elm,e){
	if (typeof e == 'undefined'){
		e=window.event;
	}
	//alert(e.clientX+':'+e.clientY);
	var body=document.getElementsByTagName('body')[0];
	try{
		var mouseY=e.clientY+$(document).scrollTop();
		var mouseX=e.clientX+$(document).scrollLeft();
		var width = $(elm).width();
		var height = $(elm).height();
		//alert(document.documentElement.onmousemove);
		var offset = $(elm).offset();

	}catch(err){
		if (($.browser.msie || $.browser.safari)){
			document.documentElement.onmousemove=null;
			//window.onmousemove=null;
		}else{
			$(body).attr('onmousemove','');
		}
		TITLES_TIMEOUTS[elm.id]=window.setTimeout(function(){
			$('#jsTitle'+elm.id).remove();
		},5);
		return;
	}

	//мышка на самом деле все еще в пределах элемента
	//alert('before if');
	//alert($(document.body).attr('onmousemove'));

	if (mouseX>=offset.left
		&& mouseX<=offset.left+width
		&& mouseY>=offset.top
		&& mouseY<=offset.top+height
		){
		//alert('set attr');
		if (($.browser.msie || $.browser.safari) && !document.documentElement.onmousemove){
			document.documentElement.onmousemove=function(){

				jsTitleOut(elm);
			};
		}else{
			//alert(window.event);
			$(body).attr('onmousemove',"jsTitleOut($('#"+elm.id+"').get(0), event);");
		}
	}else{
		//alert('clear attr');
		if (($.browser.msie || $.browser.safari)){
			document.documentElement.onmousemove=null;
		}else{
			$(body).attr('onmousemove','');
		}

		TITLES_TIMEOUTS[elm.id]=window.setTimeout(function(){
			$('#jsTitle'+elm.id).remove();
		},5);
	}
	//alert(offset.left+':'+offset.top+':'+offset.w+':'+offset.h);
}

