var BlogAjax = function(){
    var sid;
    var frontendUrl;
    var cfg;
    var favouriteAction;
    return{
        init : function (sessionId,frontend){
            BlogAjax.sid = sessionId;
            BlogAjax.frontendUrl = frontend;
            //alert(BlogAjax.sid);
        },
        
        like : function (postId,cbsuccess){
            try{
                BlogAjax.cfg = null;
                $.ajax({
                    url : BlogAjax.frontendUrl + '/ajax/like/postid/'+postId+'/',
                    data : 'PHPSESSIONID='+BlogAjax.sid,
                    dataType: 'json',
                    success: cbsuccess,
                    error: BlogAjax.ajaxerror
                });
            }catch(e){
               BlogAjax.error(e.message);
            }
            return false;
        },
        likeByUrl : function (url,id,cbsuccess){
            try{
                BlogAjax.cfg = null;
                $.ajax({
                    url : BlogAjax.frontendUrl + '/ajax/like/postid/'+id+'/?url='+url+'/',
                    data : 'PHPSESSIONID='+BlogAjax.sid,
                    dataType: 'json',
                    success: cbsuccess,
                    error: BlogAjax.ajaxerror
                });
            }catch(e){
               BlogAjax.error(e.message);
            }
            return false;
        },
        dislikeByUrl : function (url,id,cbsuccess){
            try{
                BlogAjax.cfg = null;
                $.ajax({
                    url : BlogAjax.frontendUrl + '/ajax/dislike/postid/'+id+'/?url='+url+'/',
                    data : 'PHPSESSIONID='+BlogAjax.sid,
                    dataType: 'json',
                    success: cbsuccess,
                    error: BlogAjax.ajaxerror
                });
            }catch(e){
               BlogAjax.error(e.message);
            }
            return false;
        },
        dislike : function (postId,cbsuccess){
            try{
                BlogAjax.cfg = null;
                $.ajax({
                    url : BlogAjax.frontendUrl + '/ajax/dislike/postid/'+postId+'/',
                    data : 'PHPSESSIONID='+BlogAjax.sid,
                    dataType: 'json',
                    success: cbsuccess,
                    error: BlogAjax.ajaxerror
                });
            }catch(e){
               BlogAjax.error(e.message);
            }
            return false;
        },
        cbLike : function (data){
        	
        	if( data.type == 1 ){	
	            $('#like_count_'+data.data.postId).html(data.data.likeCount);
	            $('#dislike_count_'+data.data.postId).html(data.data.dislikeCount);
        	}else{
	            alert(data.data);
        	}
        },
        reblog : function (postId){
            BlogAjax.errorpopup();
            return false;
        },
        favourite : function (action,blogId,config,cbsuccess){
            BlogAjax.cfg = null;
            BlogAjax.cfg = config;
            if(BlogAjax.favouriteAction != undefined){
                action = BlogAjax.favouriteAction;
            }
            if(action == 'add'){
                try{
                    $.ajax({
                        url : BlogAjax.frontendUrl + '/ajax/addfavourite/blogid/'+blogId+'/',
                        data : 'PHPSESSIONID='+BlogAjax.sid,
                        dataType: 'json',
                        success: cbsuccess,
                        error: BlogAjax.ajaxerror
                    });
                }catch(e){
                   BlogAjax.error(e.message);
                }
            }
            if(action == 'delete'){
                try{
                    $.ajax({
                        url : BlogAjax.frontendUrl + '/ajax/deletefavourite/blogid/'+blogId+'/',
                        data : 'PHPSESSIONID='+BlogAjax.sid,
                        dataType: 'json',
                        success: cbsuccess,
                        error: BlogAjax.ajaxerror
                    });
                }catch(e){
                   BlogAjax.error(e.message);
                }
            }
            return false;
        },
        cbFavourite : function (data){
            BlogAjax.favouriteAction = data.data;
            if(data.data == 'add'){
                $('#favourite').html(BlogAjax.cfg.addText);
            }
            if(data.data == 'delete'){
                $('#favourite').html(BlogAjax.cfg.deleteText);
            }
        },
        ActivityCreekMore : function (url,index){
            $('#activitycreek_more'+index).html('<img src="/images/loader.gif" width="16" height="16">');
            $('#activitycreek_more'+index).load(url,null,function (responseText, textStatus, XMLHttpRequest) {
                if (textStatus == 'error') {
                    if (responseText == 'Noauth') {
                        $(location).attr('href','/');
                    }
                }
            });
            return false;
        },
        ajaxcall : function(callurl,calldata,cbsuccess){
            try{
                $.ajax({
                    url : callurl,
                    data : calldata,
                    success: cbsuccess,
                    error: BlogAjax.ajaxerror
                });
            }catch(e){
               BlogAjax.error(e.message);
            }
        },
        errorpopup : function(){
            alert('Ehhez a funkcióhoz be kell lépni');
        },
        error : function(msg){
            alert('Hiba:'+msg);
        },
        alert : function(msg){
            alert(msg);
        },
        ajaxerror : function(req){
            if(req.responseText == 'Noauth'){
                BlogAjax.errorpopup();
            } else {
                try{
                    var json = jQuery.parseJSON(req.responseText);
                    BlogAjax.alert(json.data);
                }catch(e){
                    BlogAjax.error(req.responseText);
                }
            }
        },
        iwiwshare_click : function() {
            u=location.href;
            t=document.title;
            window.open('http://iwiw.hu/pages/share/share.jsp?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'iwiwshare','toolbar=0,status=0,location=1, width=650,height=600,scrollbars=1');
            return false;
        },
        fbshare_click :function () {
            u=location.href;
            t=document.title;
            window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'fbshare','toolbar=0,status=0,location=1, width=650,height=600,scrollbars=1');
            return false;
        },
        twittershare_click :function () {
            u=location.href;
            window.open('http://twitter.com/home?status=Currentlyreading'+encodeURIComponent(u),'twittershare','toolbar=0,status=0,location=1, width=650,height=600,scrollbars=1')
            ;return false;
        }
        
    }
}();
