function StartOnLoad(funct) {
    //setup onload function
    if(typeof window.addEventListener != 'undefined') {
        window.addEventListener('load', funct, false);
    } else if(typeof document.addEventListener != 'undefined') {
        document.addEventListener('load', funct, false);
    } else if(typeof window.attachEvent != 'undefined') {
        window.attachEvent('onload', funct);
    } else {
        if(typeof window.onload == 'function') {
            var existing = onload;
            window.onload = function() {
                existing();
                funct();
            };
        } else {
            window.onload = funct;
        }
    }
}
function in_array (needle, haystack, argStrict) {
    // Checks if the given value exists in the array  
    // 
    // version: 1103.1210
    // discuss at: http://phpjs.org/functions/in_array    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: vlado houba
    // +   input by: Billy
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);    // *     returns 1: true
    // *     example 2: in_array('vlado', {0: 'Kevin', vlado: 'van', 1: 'Zonneveld'});
    // *     returns 2: false
    // *     example 3: in_array(1, ['1', '2', '3']);
    // *     returns 3: true    // *     example 3: in_array(1, ['1', '2', '3'], false);
    // *     returns 3: true
    // *     example 4: in_array(1, ['1', '2', '3'], true);
    // *     returns 4: false
    var key = '',        strict = !! argStrict;
 
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    } 
    return false;
}	
/*
 * KOMMENT JS-ek
 */
function show_comment(record,story_id) {
    jQuery('.show_comments').css('opacity','0.2');
    jQuery.ajax({
        type: "POST",
        url: "/Story/getCommentList",
        data: "record="+record+"&story_id="+story_id,
        success: function(data){
            if(data.error==0) {
                jQuery('.show_comments').html(data.html);
            }
            jQuery('.show_comments').css('opacity','1');
            var offs = jQuery('.show_comments').offset();
            jQuery('html, body').animate({scrollTop:offs.top}, 'fast');
        }
    });
}
function show_blog_comment(record,blog_id) {
    jQuery('.show_comments').css('opacity','0.2');
    jQuery.ajax({
        type: "POST",
        url: "/Blog/getCommentList",
        data: "record="+record+"&blog_id="+blog_id,
        success: function(data){
            if(data.error==0) {
                jQuery('.show_comments').html(data.html);
            }
            jQuery('.show_comments').css('opacity','1');
            var offs = jQuery('.show_comments').offset();
            jQuery('html, body').animate({scrollTop:offs.top}, 'fast');
        }
    });
}
function show_message(record,forum_id) {
    jQuery('.message_list').css('opacity','0.2');
    jQuery.ajax({
        type: "POST",
        url: "/Forum/getMessageList",
        data: "record="+record+"&forum_id="+forum_id,
        success: function(data){
            if(data.error==0) {
                jQuery('.message_list').html(data.html);
            }
            jQuery('.message_list').css('opacity','1');
            var offs = jQuery('.message_list').offset();
            jQuery('html, body').animate({scrollTop:offs.top}, 'fast');
        }
    });
}
function comment_submit() {
    jQuery('.new_comment').css('opacity','0.2');
    if(ajax!=true){
        jQuery('#story_comment_form').validator({
            url: '/Story/commentSave',
            errorplace: 'comment_error',
            onSuccess: function(){
                location.reload();
            }
        });
    }
    jQuery('.new_comment').css('opacity','1');
}
/*function comment_submit() {
    jQuery('.new_comment').css('opacity','0.2');
    if(ajax!=true){
        var params = jQuery('#story_comment_form').serialize()+'&type=1';
        jQuery('#story_comment_form').validator({
            url: '/Comment/commentSave',
            errorplace: 'comment_error',
            params: params,
            onSuccess: function(){
                location.reload();
            }
        });
    }
    jQuery('.new_comment').css('opacity','1');
}*/
function blog_comment_submit() {
    jQuery('.new_comment').css('opacity','0.2');
    if(ajax!=true){
        jQuery('#story_comment_form').validator({
            url: '/Blog/commentSave',
            errorplace: 'comment_error',
            onSuccess: function(){
                location.reload();
            }
        });
    }
    jQuery('.new_comment').css('opacity','1');
}
function message_submit() {
    jQuery('.new_message').css('opacity','0.2');
    if(ajax!=true){
        jQuery('#forum_message_form').validator({
            url: '/Forum/messageSave',
            errorplace: 'message_error',
            onSuccess: function(){
                location.reload();
            }
        });
    }
    jQuery('.new_message').css('opacity','1');
}
function add_reply(comment_id){
    var id = comment_id;
    var user_name = jQuery('#'+id+'_user_name').html();
    var number = jQuery('#'+id+'_number').html();
    jQuery('#reply_id').val(comment_id);
    jQuery('#reply_container').html('Válasz erre:  <a href="#item-'+id+'">('+number+') '+user_name+'</a> (<a href="javascript:cancel_reply()">mégse</a>)');
}    
function cancel_reply() {
    jQuery('#reply_id').val(0);
    jQuery('#reply_container').html('');
}
function show_comment_dialog(comment_id) {
    jQuery.ajax({
        type: 'POST',
        url: '/Story/getComment',
        data: 'comment_id='+comment_id,
        success: function(ret) {
            if(ret.error==0) {
                jQuery('#comment_dialog').html(ret.html);
                jQuery('#comment_dialog').dialog({
                    title: ret.title
                });
                jQuery('#comment_dialog').dialog('open');
            }
        }
    });    
}
function show_blog_comment_dialog(comment_id) {
    jQuery.ajax({
        type: 'POST',
        url: '/Blog/getComment',
        data: 'comment_id='+comment_id,
        success: function(ret) {
            if(ret.error==0) {
                jQuery('#comment_dialog').html(ret.html);
                jQuery('#comment_dialog').dialog({
                    title: ret.title
                });
                jQuery('#comment_dialog').dialog('open');
            }
        }
    });    
}
function show_message_dialog(message_id) {
    jQuery.ajax({
        type: 'POST',
        url: '/Forum/getMessage',
        data: 'message_id='+message_id,
        success: function(ret) {
            if(ret.error==0) {
                jQuery('#comment_dialog').html(ret.html);
                jQuery('#comment_dialog').dialog({
                    title: ret.title
                });
                jQuery('#comment_dialog').dialog('open');
            }
        }
    });
    
}
function toggle_emoticon_list(){
    jQuery('.comment_emoticon').toggle();
    jQuery('#emoticon_arrow').toggleClass('arrow_up');
}
function addsmile(id) {
    var text = jQuery('#text').val();
    text = text+" [smiley "+id+"] ";
    jQuery('#text').val(text);
}
function mod_user_data() {
    if(ajax!=true){
        jQuery('#user_data_form').validator({
            url: '/User/modUserData',
            errorplace: 'user_error',
            onSuccess: function(){
                location.reload();
            }
        });
    }
}
function add_favorite(story_id) {
    if(ajax!=true){
        ajax = true;
        jQuery.ajax({
            type: 'POST',
            url: '/Story/addFavorite',
            data: 'story_id='+story_id,
            success: function(ret) {
                ajax = false;
                if(ret.status==0) {
                    jQuery('#addfavorite').html('Kedvencek közé');
                } else {
                    jQuery('#addfavorite').html('Kiveszem a kedvencek közül');
                }
            }
        });
    }
}

function searchLength() {
    jQuery('.search_error').html('');
    var search = jQuery('#user_name').val();
    if(search.length<3) {
        jQuery('.search_error').html('Minimum 3 karaktert kell megadni!');
        return false;
    }
    else return true;    
}

function forum_submit() {
    if(ajax!=true){
        ajax = true;
        jQuery('#new_forum_form').validator({
            url: '/Forum/forumSave',
            errorplace: 'new_forum_error',
            onSuccess: function(){
                location.href = '/forum';
            }
        });
    }
}

function like(value,story_id) {
    if(ajax!=true){
        jQuery('#vote_container').css('opacity','0.2');
        jQuery('#vote_container_'+story_id).css('opacity','0.2');
        ajax = true;
        jQuery.ajax({
            type: 'POST',
            url: '/Story/like',
            data: {
                value: value, 
                story_id: story_id
            },
            success: function(ret) {
                jQuery('#vote_container').css('opacity','1');
                jQuery('#vote_container_'+story_id).css('opacity','1');
                if(ret.error == 0) {
                    jQuery('#vote_container').html(ret.html);
                    jQuery('#vote_container_'+story_id).html(ret.html);
                }
                ajax = false;
            }
            
        });
    }
}

function togglepublic(story_id) {
    if(ajax!=true){
        ajax = true;
        jQuery.ajax({
            type: 'POST',
            url: '/Story/togglepublic',
            data: {
                story_id: story_id
            },
            success: function(ret) {
                if(ret.error == 0) {
                    if(ret.status == 1) jQuery('#toggle_'+story_id).html('Publikus')
                    if(ret.status == 0) jQuery('#toggle_'+story_id).html('Nem publikus')
                }
                ajax = false;
            }

        });
    }
}

function get_comment_list(record) {
    if(ajax!=true){
        jQuery('.forum_comment_list').css('opacity','0.2');
        ajax = true;
        jQuery.ajax({
            type: 'POST',
            url: '/Story/gettoplistacomment',
            data: {
                record: record
            },
            success: function(ret) {
                if(ret.error == 0) {
                    jQuery('.forum_comment_list').html(ret.html);
                }
                jQuery('.forum_comment_list').css('opacity','1');
                ajax = false;
            }

        });
    }
}
function moderate_forum_topic(id) {
    if(ajax!=true) {
        ajax = true;
        jQuery.ajax({
            type: 'POST',
            url: '/Forum/moderateforumtopic',
            data: {
                forum_id: id  
            },
            success: function(ret) {
                if(ret.error==0) {
                    alert('Fórum téma moderálása sikeres');
                    location.href='/forum'
                } else {
                    alert('Valami hiba történt');
                }
                ajax = false;
            }
            
        })
    }
}
function blog_moderate_cancel(id) {
    if(ajax!=true) {
        ajax = true;
        jQuery.ajax({
            type: 'POST',
            url: '/Blog/moderatecancel',
            data: {
                comment_id: id  
            },
            success: function(ret) {
                if(ret.error==0) {
                    location.reload();
                }
                ajax = false;
            }
            
        })
    }
}
function story_moderate_cancel(id) {
    if(ajax!=true) {
        ajax = true;
        jQuery.ajax({
            type: 'POST',
            url: '/Story/moderatecancel',
            data: {
                comment_id: id  
            },
            success: function(ret) {
                if(ret.error==0) {
                    location.reload();
                }
                ajax = false;
            }
            
        });
    }
}

function removefavorite(id) {
        if(ajax!=true) {
        ajax = true;
        jQuery.ajax({
            type: 'POST',
            url: '/Story/removefavorite',
            data: {
                id: id  
            },
            success: function(ret) {
                if(ret.error==0) {
                    location.reload();
                }
                ajax = false;
            }
            
        });
    }
}

function fontSize(dir) {
    var act_size = jQuery('.show_content').css('font-size');
    var act_line = jQuery('.show_content').css('line-height');
    act_size = parseInt(act_size);
    act_line = parseInt(act_line);
    if(dir == 1 && act_size > 9) {
        var new_size = act_size-2;
        var new_line = act_line-2;
    }
    if(dir == 2 && act_size < 21) {
        var new_size = act_size+2;
        var new_line = act_line+2;
    }
    jQuery('.show_content').css({
        'font-size': new_size+'px',
        'line-height': new_line+'px'
    });
}    
