var attrGlobal = 'photo';
var loader = 'зачекайте..';

$(document).ready(function(){    $('#formComment').submit(function(){
        if(!filePost)return false;

        var fomrma = $('#formComment').serialize();
         if(!$('#comment_result').text()){
           $('#formComment').after('<div id="comment_result"></div>');
         }

         $('#formComment input').attr('readonly',true).attr('disabled',true);
         $('#comment_result').html(loader).css('color','#5C7069').fadeIn();

         $.post(filePost,'ajax=true&addcomment=true&'+fomrma,function(data){
			$('#formComment input').attr('readonly',false).attr('disabled',false);
			$('#comment_result').hide();


			  if(data['status'] == 1){
                 if(!$('#commenttr'+data['id']).html()){
                  		$('#comments').append('<tr><td id="#commenttr'+data['id']+'" style="border:2px solid #9B3E1E;padding:3px;background:#9B3E1E;width:100px"><b><a href="../users_'+data['userinfo']['id']+'.html">'+data['userinfo']['name']+'</a></b><br>'+data['date']+'</td><td style="border:2px solid #9B3E1E;padding:5px" width="100%">'+data['text']+'</td></tr>');
                  		$('#comment').val('');
                    	//getComments(data['itemid']);
                 }
                 $('#comment_result').fadeOut();
              } else {
                $('#comment_result').text(data['desc']).css('color','red').fadeIn();
              }

        },'json');
    	return false;
    });
});
    //shownext
function showPhoto(type){
     //$('#comments').hide();
     $('#imageShow').css('opacity',0.5).after('<div id="resultPhotLoad">'+loader+'</div>');
     var method = (type==1)?'shownext':'showprev';
     $.post('photo_'+NowPhotoid+'.html',{ajax:true,type:method},function(data){

        if(NowPhotoid == data['id']){        	$('#imageShow').css('opacity',1);
        	$('#resultPhotLoad').remove();
        	return false;
        }
        $('#itemid').val(data['id']);
        $('#photoTitle').text(data['title']);
        NowPhotoid = data['id'];
        $('#imageShow').hide();
        $(document).attr('title',data['title']+' - Народний ансамбль пісні і танцю Черемош');
		//$(document).attr('location','http://niko.lviv.ua');
		$('#imageShow').attr('src',data['url']);
        $('#userAdd').html('<a href="../user_'+data['userid']+'.html">'+data['userinfo']['name']+'</a>');
       	$('#userDate').text(data['date']);
       	$('#imageShow').css('opacity',1).fadeIn();
        //$('#imageShow').css('opacity',1);
        $('#resultPhotLoad').fadeOut().remove();//alert(data['id']);
        getComments(data['id']);
     },'json');
     return false;
   }
function isdefined(variable){
  return (typeof(variable) == "undefined")? false: true;
}

function getComments(itemid){  if(!filePost)return false;
  if(!isdefined(attrGlobal))attrGlobal = 'photo';
  $.post(attrGlobal+'_'+itemid+'.html','ajax=true&getComments=true&id='+itemid,function(rdata){       $('#comments').children().remove();
       $('#comments').hide();


         if(!rdata[0]){            $('#comments').after('<div style="display:none" id="messageCommentLeave">Залиште Ваш коментар!</div>');
            $('#messageCommentLeave').fadeIn().css('color','red').focus();
            $('#comment').focus();
            //return false;
         }
         for(i in rdata){
				var data = rdata[i]; //alert(data['id']);
          		if(!data['userinfo'])return false;
          		$('#comments').append('<tr><td id="#commenttr'+data['id']+'" style="border:2px solid #9B3E1E;padding:3px;background:#9B3E1E;width:100px"><b><a href="../users_'+data['userinfo']['id']+'.html">'+data['userinfo']['name']+'</a></b><br>'+data['date']+'</td><td style="border:2px solid #9B3E1E;padding:5px" width="100%">'+data['text']+'</td></tr>');
                $("#commenttr"+data['id']).slideDown('slow');
         }
        $('#comments').slideDown();
        $('#messageCommentLeave').fadeOut();
   },'json');
}