/*
	General
*/

var Menu = {
	//menu items slide
	init: function() {	
		news_menu = false;
		social_menu = false;
		personal_menu = false;
		logo_slogan = false;
		
		//slogan slide
		$("#quotelink").click(function(e){
			personal_menu = false;
			social_menu = false;
			news_menu = false;
			if(logo_slogan == false) {
				$("ul#nav-social").hide();
				$("ul#nav-personal").hide();
				$("ul#nav-categories").hide();
				$("#quote").slideDown({duration: 500, easing: 'easeOutBack'});
				logo_slogan = true;
			} else {
				$("#quote").slideUp({duration: 500, easing: 'easeInBack'});
				logo_slogan = false;
			}
			return false;
		});
		
		//Nieuws menu slide
		$("li a.more.news").click(function(e){
			personal_menu = false;
			social_menu = false;
			logo_slogan = false;
			if(news_menu == false) {
				$("ul#nav-social").hide();
				$("ul#nav-personal").hide();
				$("#quote").hide();
				$("ul#nav-categories").slideDown({duration: 500, easing: 'easeOutBack'});
				news_menu = true;
			} else {
			    $('#nav-second-categories').hide();
				$("ul#nav-categories").slideUp({duration: 500, easing: 'easeInBack'});
				news_menu = false;
			}
			return false;
		});
		
		//Community menu slide
		$("li a.more.social").click(function(e){
			news_menu = false;
			personal_menu = false;
			logo_slogan = false;
			if(social_menu == false) {
				$("ul#nav-social").slideDown({duration: 500, easing: 'easeOutBack'});
		        $("ul#nav-categories").hide();
				$("#quote").hide();
		        $("ul#nav-personal").hide();
		        $('#nav-second-categories').hide();
				social_menu = true;
			} else {
				$("ul#nav-social").slideUp({duration: 500, easing: 'easeInBack'});
				social_menu = false;
			}
			return false;
		});
		//Personal menu slide
		$("li#logged-in a.more").click(function(e){
			news_menu = false;
			social_menu = false;
			logo_slogan = false;
			if(personal_menu == false) {
				$("ul#nav-social").hide();
				$("#quote").hide();
		        $("ul#nav-categories").hide();
		        $('#nav-second-categories').hide();
			    $("ul#nav-personal").slideDown({duration: 500, easing: 'easeOutBack'});
				personal_menu = true;
			} else {
				$("ul#nav-personal").slideUp({duration: 500, easing: 'easeInBack'});
				personal_menu = false;
			}
			return false;
		});
		
	},
	//search toggle and functionalities
	search: function() {
		$("#search img.submit").click(function(e){
		
		    if($("ul#search-options:visible").length) {
		        $("ul#search-options").css('border', 'none');
		    }
		    else {
		        $("ul#search-options").css('border', '1px solid #313232');
		    }
		    
			$("ul#search-options").slideToggle({duration: 500, easing: 'easeOutBack'});
			
			return false;
		});

		$("#search-options li a").click(function() {
		    $("#search-options li").removeClass('check');
		    $(this).parent('li').addClass('check');
		    return false;
		});

		$("#search form").submit(function() {
		    var domain = $("#search-options li.check").children('a').attr('class');
		    if(domain == 'nieuws') {
		        var field_name = 's';
		        var form_action = '/zoeken';
		    }
		    if(domain == 'community') {
		        var field_name = 'q';
		        //var form_action = '/community/zoeken';
				var form_action = '/community';
		    }
		    $(this).attr('action', form_action);
			$(this).children('input[type=text]').attr('name', field_name);
		});
		
		$('body').bind('click', function() {
		    $("ul#search-options:visible").slideUp({duration: 500, easing: 'easeOutBack'});
		});
	}
};

var Popup = {
	// modal windows
    init: function() {
        jQuery('#popup')
            .css('margin-top', '-'+(jQuery('#popup .modal').height()/2)+'px')
            .css('margin-left', '-'+(jQuery('#popup .modal').width()/2)+'px');
    },
    close: function() {
		$('#login-modal').dialog('close');
		$('#register-modal').dialog('close');
		$('#resetten-modal').dialog('close');
		$('#tip-modal').dialog('close');
		
		$('#wysiwyg-image-form').dialog('close');
		$('#wysiwyg-url-form').dialog('close');
		
		jQuery('#poll_setup').dialog('close');
        jQuery('#overlay').fadeOut('fast');

        return false;
    },
	closenotification: function() {
		jQuery('.modal.alert').hide();
		jQuery('.modal.success').hide();
	},
    open: function(element, arguments) {
		jQuery(element).dialog('open', arguments);
        jQuery('.ui-widget-overlay').click(function() {
			Popup.close();
		});
        return false;
    },
    html: function(html) {
        jQuery('#overlay').html(html);
    }
};

var General = {
	//Textarea resizer
	textareas: function() {
		$("textarea.resize:not(.processed)").TextAreaResizer();
	},
	
	//Tabs
	tabs: function() {
		
		$("ul#homepage-community-tabs").idTabs();
		
		$("ul#community-tabs").idTabs();
		$("ul#members-tabs").idTabs();
		
		$("ul#online-tabs").idTabs();
		
		$("#dashboard-tabs li a, #profile-recent-tabs li a").click(function(){
			$("#dashboard-tabs li, #profile-recent-tabs li").each(function(i){
				$(this).removeClass("selected");
			});
			$(this).parent().addClass("selected");
			return false;
		});

		// Tabs (dashboard, public profiel)
		$('#dashboard-lists, #profile-recent, full-size ul#about-who').tabs({
	        select: function(event, ui) {
				$("#opened_tab").attr("value", ui.tab.hash);
	            window.location.hash = ui.tab.hash;
	        }
	    }); 
	},
	
	//over page
	over: function() {
		//wij tabs
		//var about_settings = { start:0 }; 
		//$("ul#about-who").idTabs();
		$("ul#over-tabs").idTabs();
		
		//redactie roll
		$("p.about-omt a.about-omt-redactie").click(function() {
			$.scrollTo("ul#about-who", 1200, {easing:"easeInOutCirc"});
		});
	},
	
	//magic 'selected'
	magic: function() {
		$('.magic').emptyonclick({changeClass: 'selected'});
	},
	
	//search, auto select, focus and blur
	searchinputs: function() {
		
		// Custom search url
		$('input[name=s]').parent('form').submit(function() {
		    var term = $(this).children('input[name=s]').val();
		    if (term && (0 < term.length)) {
	    		theQueryStr = '/zoeken/'+ encodeURIComponent(term).replace('%20', '+') +'/';
	    	}
	    	window.location.assign(theQueryStr);
	    	return false;
		});
		
		$('input[name=q]').parent('form').submit(function() {
			
		    var term = $(this).children('input[name=q]').val();
		    if (term && (0 < term.length)) {
	    		theQueryStr = '/community/zoeken?q='+ encodeURIComponent(term).replace('%20', '+');
	    	}
	    	window.location.assign(theQueryStr);
	    	return false;
		});

		/*$('.search-box input[name=q]').keyup(function() {
		    var term = $(this).val();

	    	$('#search_results').load('/community/search.php', 'format=js&q='+term);
	    	return false;
		});*/
		

		
		// 'selected' class 
		$('.search-box input.text').emptyonclick({changeClass: 'selected'});
		$('#search input.text').emptyonclick({changeClass: 'selected'});
		
		// add auto select js for text inputs
		$('input[type=text]:not(#search input, .search-box input, #topic-search input)').each(function() { 
			$(this).focus( function(){ 
				this.select(); 
				$(this).addClass('hilite'); 
			}).blur(function(){ 
				$(this).removeClass('hilite'); 
			}); 
		});
		
		// 'selected' class on focus		
		$("#search input.text").focus( function(){ 
			$("#search").addClass('selected'); 
		}).blur(function(){ 
			$("#search").removeClass('selected'); 
		});
			
		$(".search-box input.text").focus( function(){ 
			$(".search").addClass('selected'); 
		}).blur(function(){ 
			$(".search").removeClass('selected'); 
		});
		
		$("#topic-search input.text").focus( function(){ 
			$("#topic-search").addClass('selected'); 
		}).blur(function(){ 
			$("#topic-search").removeClass('selected'); 
		});
		
		/*$("#search input.text").focus(function(){
			$("#search").toggleClass("selected");
		});*/
	},
	
	//pagination input
	pagination: function() {
		$('.pagination input').click(function(){
		    this.select();
		});
	},
	
	//twitter footer link
	twitter: function() {
		$("li.twitter a").hover(
			function(){
				$("li.twitter").addClass("greybird");
			},
			function(){
				$("li.twitter").removeClass("greybird");
			}
		);
	}
};

/* 
	Home
*/
var Home = {
	// homepage arrows keydown/up scrolling
	roll: function() {
		start = -1;
		/*$(document).bind('keydown', function(event){
			if(event.keyCode == 40) {
				if(start>5) {
					start = 0;
				} else {
					start = start+1;
				}	
				$.scrollTo("#omt"+start, 1000, {easing:"easeInOutCirc"});
			//	$.scrollTo( '#omt'+start, 600);	
			}
		});	
		$(document).bind('keyup', function(event){
			if(event.keyCode == 38) {
				if(start>=0){
					start = start-1;
				//	$.scrollTo( $('#omt'+start), 600);
					$.scrollTo("#omt"+start, 1000, {easing:"easeInOutCirc"});
				}		
			}
		
		});*/
	},
	
	//'Alle podcast' sidebar link roll
	allpodcast_roll : function() {
		$("#sidebar .article.sidebar-podcast ul li a:last").click(function() {
			$.scrollTo("#podcast-latest", 1200, {easing:"easeInOutCirc"});
		});
	},
	
	//latest forum discussion block : pagination
    nuindecommynity : function() {
        community_start = 0;
        community_number = 12;
        offset = community_number;

		//next button
        $('#community-next').click(function() {
            current_page = parseInt($('#community_current_page').val());
            if(current_page < parseInt( $('#community_max_pages').val() ) ) {
				
                community_start = community_start + community_number;
                current_page = parseInt($('#community_current_page').val());
                var page = current_page+1;

                $('#community_current_page').val(page);
                
                Pagination.load('.sidebar-community-act ul', 'omt_community_load_latest_discussion.php', {
                    start: community_start,
                    limit: community_number
                });
                
                // Cleanup the pagination's buttons
                Pagination.cleanup('.sidebar-community-act', page, parseInt($('#community_max_pages').val()));
                
                return false;
            } 
        });

       	//previous button
 		$('#community-prev').click(function() {
            if(community_start > 0 ) {
                community_start = community_start - community_number;
                current_page = parseInt($('#community_current_page').val());
                var page = current_page-1;
                $('#community_current_page').val(page);

                Pagination.load('.sidebar-community-act ul', 'omt_community_load_latest_discussion.php', {
                    start: community_start,
                    limit: community_number
                });
                
                // Cleanup the pagination's buttons
                Pagination.cleanup('.sidebar-community-act', page, parseInt($('#community_max_pages').val()));
            }
        });
        
        // first button
        $('#sidebar-community-first').click(function() {
            
            if(community_start > 0 ) {
                community_start = 0;
                var page = 1;
                $('#community_current_page').val(page);
                
                Pagination.load('.sidebar-community-act ul', 'omt_community_load_latest_discussion.php', {
                    start: community_start,
                    limit: community_number
                });
                
                // Cleanup the pagination's buttons
                Pagination.cleanup('.sidebar-community-act', page, parseInt($('#community_max_pages').val()));
            }
            return false;
        });

		// last button
        $('#sidebar-community-last').click(function() {
            
			current_page = parseInt($('#community_current_page').val());
            if(current_page < parseInt( $('#community_max_pages').val() ) ) {
			
                community_start = community_number * ( parseInt( $('#community_max_pages').val() ) - 1 );

                var page = parseInt($('#community_max_pages').val());

                $('#community_current_page').val(page);
                
                Pagination.load('.sidebar-community-act ul', 'omt_community_load_latest_discussion.php', {
                    start: community_start,
                    limit: community_number
                });
                
                // Cleanup the pagination's buttons
                Pagination.cleanup('.sidebar-community-act', page, parseInt($('#community_max_pages').val()));
			
                return false;
            }
        });


        
        $('.sidebar-community-act .pagination form').submit(function() {
            return false;
        });
        
        // page number button
        $('.sidebar-community-act .pagination #community_current_page').change(function() {
            var page = $(this).val();
            if(page > 0 && page <= parseInt($('#community_max_pages').val())) {
                community_start = (page-1)*community_number;
                $('#community_current_page').attr('value', page);
                
                Pagination.load('.sidebar-community-act ul', 'omt_community_load_latest_discussion.php', {
                    start: community_start,
                    limit: community_number
                });
                
                // Cleanup the pagination's buttons
                Pagination.cleanup('.sidebar-community-act', page, parseInt($('#community_max_pages').val()));
            }
            return false;
        });
    }
};

/* 
	Artikel / Artikel Comments 
*/
var Comments = {
	// on click on comments avatars
	avatarLinks: function() {
		$("a.avatar, a.topic-avatar").bind('click', function(){
            if($(this).parent().children('ul.pop.open').length || $(this).next(".comments div ul.open").length) {
                $(this).parent().children('ul.pop').slideUp({duration: 500, easing: 'easeInBack'});
            	$(this).next(".comments div ul").slideUp({duration: 500, easing: 'easeInBack'});
            	$(".comments div ul, ul.pop").removeClass('open');
            }
            else {
                $(".comments div ul, ul.pop").hide().removeClass('open');
                $(this).parent().children('ul.pop').slideDown({duration: 500, easing: 'easeOutBack'});
                $(this).next(".comments div ul").slideDown({duration: 500, easing: 'easeOutBack'});
                $(this).parent().children('ul.pop').addClass('open');
                $(this).next(".comments div ul").addClass('open');
            }
            return false;
		});
		
		$('body').bind('click', function() {
		    $(".comments div ul:visible, ul.pop:visible").hide().removeClass('open');
		});
	},
	
    // comments pagination
    pagination : function() {
        comments_start = 0;
        comments_number = 10;
        current_page = 1;

        //next button
        $('#article-single .pagination-wrapper .pagination .next').click(function() {
            current_page = parseInt($('#comments_current_page').val());

            if(current_page < parseInt( $('#comments_max_pages').val()) ) {
                comments_start = comments_start + comments_number
				current_page = parseInt($('#comments_current_page').val());
				var page = current_page+1;
				
                $('.comments_current_page_input').val(page);
		
				Pagination.load('.comments', 'omt_post_load_comments.php', {
	                    postid: $('#artikel_comment_post_ID').val(),
	                    start: comments_start,
						limit: comments_number
	            }, Comments.avatarLinks);
	
				

                // Cleanup the pagination's buttons
                Pagination.cleanup('#article-single .pagination-wrapper', parseInt($('#comments_current_page').val()), parseInt($('#comments_max_pages').val()));
				
				$.scrollTo(".top-pagination-wrapper", 500, {easing:"easeInOutCirc"});

            }

            return false;
        });

        //previous button
        $('#article-single .pagination-wrapper .pagination .prev').click(function() {
			current_page = parseInt($('#comments_current_page').val());
            if(current_page > 1 ) {
                comments_start = comments_start - comments_number;
				var page = current_page-1;
                $('.comments_current_page_input').val(page);
				
                Pagination.load('.comments', 'omt_post_load_comments.php', {
	                    postid: $('#artikel_comment_post_ID').val(),
	                    start: comments_start,
						limit: comments_number
	            }, Comments.avatarLinks);
	
				
				
                // Cleanup the pagination's buttons
                Pagination.cleanup('#article-single .pagination-wrapper', page, parseInt($('#comments_max_pages').val()));

				$.scrollTo(".top-pagination-wrapper", 500, {easing:"easeInOutCirc"});
            }

            return false;
        });

        // first button
        $('#article-single .pagination-wrapper .pagination .first').click(function() {
			current_page = parseInt($('#comments_current_page').val());
            if(current_page > 1 ) {
                comments_start = 0;
				var page = 1;
                $('.comments_current_page_input').val(page);
                
                Pagination.load('.comments', 'omt_post_load_comments.php', {
                    postid: $('#artikel_comment_post_ID').val(),
                    start: comments_start,
                    limit: comments_number
                }, Comments.avatarLinks);

                // Cleanup the pagination's buttons
                Pagination.cleanup('#article-single', page, parseInt($('#comments_max_pages').val()));
				$.scrollTo(".top-pagination-wrapper", 500, {easing:"easeInOutCirc"});
            }
            return false;
        });

		//last button
		$('#article-single .pagination-wrapper .pagination .last').click(function() {
			current_page = parseInt($('.comments_current_page_input').val());
            if(current_page < parseInt( $('#comments_max_pages').val() ) ) {

              comments_start = comments_number * ( parseInt( $('#comments_max_pages').val() ) - 1 );
			
                var page = parseInt($('#comments_max_pages').val());

                $('.comments_current_page_input').val(page);
                
				Pagination.load('.comments', 'omt_post_load_comments.php', {
	            	postid: $('#artikel_comment_post_ID').val(),
	                start: comments_start,
	                limit: comments_number
	            }, Comments.avatarLinks);
                
                // Cleanup the pagination's buttons
                Pagination.cleanup('#article-single', page, parseInt($('#comments_max_pages').val()));
				$.scrollTo(".top-pagination-wrapper", 500, {easing:"easeInOutCirc"});
			
                return false;
            }
			return false;
        });

        $('#article-single .pagination-wrapper .pagination form').submit(function() {
            return false;
        });

        // page number button
        $('#article-single .pagination-wrapper .pagination #comments_current_page').change(function() {
            var page = $(this).val();
            if(page > 0 && page <= parseInt($('#comments_max_pages').val())) {
                comments_start = (page-1)*comments_number;
                $('.comments_current_page_input').attr('value', page);
                
                Pagination.load('.comments', 'omt_post_load_comments.php', {
                    postid: $('#artikel_comment_post_ID').val(),
                    start: comments_start,
                    limit: comments_number
                }, Comments.avatarLinks);

                // Cleanup the pagination's buttons
                Pagination.cleanup('#article-single', page, parseInt($('#comments_max_pages').val()));
				$.scrollTo(".top-pagination-wrapper", 500, {easing:"easeInOutCirc"});
            }
            return false;
        });
    }
};

var Pagination = {
    cleanup: function(id, page, max) {
        if(page == max && page !=1) {
            $(id+' .pagination .next').addClass('dead');
            $(id+' .pagination .first').removeClass('dead');
			$(id+' .pagination .last').addClass('dead');
            $(id+' .prev').removeClass('dead');
        }
        else if(page > 1) {
            $(id+' .pagination .next').removeClass('dead');
            $(id+' .pagination .first').removeClass('dead');
			$(id+' .pagination .last').removeClass('dead');
            $(id+' .prev').removeClass('dead');
        }
        else if(page <= 1) {
            $(id+' .pagination .next').removeClass('dead');
            $(id+' .pagination .first').addClass('dead');
			$(id+' .pagination .last').removeClass('dead');
            $(id+' .prev').addClass('dead');
        }
    },
    load: function(target, url, params, callback) {
        $(target).load('/wp-content/plugins/omt/'+url, http_build_query(params), callback);
    }
};

var Artikel = {
	// reageer / reacties scrollto
	scrolls: function() {
		//reageer scrollto
		$("#now-what ul li a:first").click(function() {
			$.scrollTo(".reageer-form", 1200, {easing:"easeInOutCirc"});
		});
		//reacties scrollto
		$('.total-comments').click(function() {
			if($("#comments").length != 0) {
				$.scrollTo("#comments", 1200, {easing:"easeInOutCirc"});
			} else if($("#login-form").length != 0) {
				$.scrollTo("#login-form", 1200, {easing:"easeInOutCirc"});
			} else if($("#wysiwyg-form").length != 0) {
				$.scrollTo("#wysiwyg-form", 1200, {easing:"easeInOutCirc"});
			}
			
		});
	}
}

/* 
	User Profile & Account
*/
var Profile = {
	// Account / Flickr input
	flickr: function() {
		$('#show-flickr').click(function() {
			$('#li-edit-flickr').show();
			$('#li-flickr').hide();
			return false;
		});
	},
	
	// Account / Flickr input
	password: function() {
		$('#show-password').click(function() {
			$('#li-edit-password').show();
			$('#li-password').hide();
			return false;
		});
	},
	
	// Acount, apple products selector dynamic width !
	appleselector : function() {
		/*$(".apple-product-items").each(function(i){ 
		    var new_width = 0;
		    $(this).children('.apple-product-item').each(function(i, elem){
		        new_width += 200;//$(elem).width();
		        console.log($(elem).length);
		    });
			console.log($(this).children('.apple-product-item').length);
			$(this).css('width', new_width);
		});*/
		
		// Box toggle
		$("ul#apple-products li a").each(function(i){
			var toSlide = $(this).next(".apple-product-items-scroll");
			$(this).toggle(
			    function(){
				    //$(this).addClass("open").next(toSlide).slideDown({duration: 500, easing: 'easeOutBack'});
				    $(this).addClass("open").next(toSlide).show();
			    },
			    function(){
					//$(this).next(toSlide).slideUp({duration: 500, easing: 'easeInBack'});
					$(this).removeClass("open").next(toSlide).hide();
			});
		});
	},	
	
	// Profile / Recente Reacties pagination
	forumposts: function() {
		user_forumposts_start = 0 ;
		user_forumposts_number = 5 ;

		if($('#user_recent_posts_max_pages').attr('value')<=1){
			$('#profile-recent .pagination .next').addClass('dead');	
			$('#profile-recent .pagination .last').addClass('dead');
		}
		
		// next button
		$('#profile-recent .pagination .next').click(function() {
			current_page = parseInt($('#user_recent_posts_current_page').val());
	
			if(current_page < parseInt( $('#user_recent_posts_max_pages').val() ) ) {
				user_forumposts_start = user_forumposts_start + user_forumposts_number;
				current_page = parseInt($('#user_recent_posts_current_page').val());
	            var page = current_page+1;
				
				$('#user_recent_posts_current_page').val(page);
				
				Pagination.load('#recent-comments', 'omt_profile_load_user_forumposts.php', {
                    userid: $('#user_id').val(),
                    start: user_forumposts_start,
					limit: user_forumposts_number
                });
				
				// Cleanup the pagination's buttons
                Pagination.cleanup('#profile-recent ', page, parseInt($('#user_recent_posts_max_pages').val()));
			}
	
			return false;
		});
		
		//previous button
		$('#profile-recent .pagination .prev').click(function() {
			if(user_forumposts_start > 0 ) {
				
				current_page = parseInt($('#user_recent_posts_current_page').val());
				user_forumposts_start = user_forumposts_start - user_forumposts_number;
				var page = current_page-1;
				$('#user_recent_posts_current_page').val(page);

				Pagination.load('#recent-comments', 'omt_profile_load_user_forumposts.php', {
	                    userid: $('#user_id').val(),
	                    start: user_forumposts_start,
						limit: user_forumposts_number
	            });
	
				// Cleanup the pagination's buttons
                Pagination.cleanup('#profile-recent', page, parseInt($('#user_recent_posts_max_pages').val()));
			}

			return false;
		});
		
		// first button
		$('#profile-recent .pagination .first').click(function() {
			
			if(user_forumposts_start > 0 ) {
				user_forumposts_start = 0;
				var page = 1;
				$('#user_recent_posts_current_page').val(page);
				
				Pagination.load('#recent-comments', 'omt_profile_load_user_forumposts.php', {
                    userid: $('#user_id').val(),
                    start: user_forumposts_start,
                    limit: user_forumposts_number
                });
				
				// Cleanup the pagination's buttons
	            Pagination.cleanup('#profile-recent', page, parseInt($('#user_recent_posts_max_pages').val()));
			}
			return false;
		});
		
		//last button
		$('#profile-recent .pagination .last').click(function() {
			current_page = parseInt($('#user_recent_posts_current_page').val());
            if(current_page < parseInt( $('#user_recent_posts_max_pages').val() ) ) {

                user_forumposts_start = user_forumposts_number * ( parseInt( $('#user_recent_posts_max_pages').val() ) - 1 );
			
                var page = parseInt($('#user_recent_posts_max_pages').val());

                $('#user_recent_posts_current_page').val(page);
                
                Pagination.load('#recent-comments', 'omt_profile_load_user_forumposts.php', {
					userid: $('#user_id').val(),
                    start: user_forumposts_start,
                    limit: user_forumposts_number
                });
                
                // Cleanup the pagination's buttons
                Pagination.cleanup('#profile-recent', page, parseInt($('#user_recent_posts_max_pages').val()));
			
                return false;
            }
        });
		
		
		$('#profile-recent .pagination form').submit(function() {
            return false;
        });

        // page number button
        $('#profile-recent .pagination #user_recent_posts_current_page').change(function() {
            var page = $(this).val();
            if(page > 0 && page <= parseInt($('#user_recent_posts_max_pages').val())) {
                user_forumposts_start = (page-1)*user_forumposts_number;
                $('#user_recent_posts_current_page').attr('value', page);
                
                Pagination.load('#recent-comments', 'omt_profile_load_user_forumposts.php', {
                    userid: $('#user_id').val(),
                    start: user_forumposts_start,
                    limit: user_forumposts_number
                });
                
                // Cleanup the pagination's buttons
                Pagination.cleanup('#profile-recent', page, parseInt($('#user_recent_posts_max_pages').val()));
            }
            return false;
        });
	}, 
	
	// Profile / Recente topics pagination
	forumtopics : function() {
		user_forumposts_start = 0 ;
		user_forumposts_number = 5 ;
		
		/*
		if($('#user_recent_topics_max_pages').attr('value')>1){
			$('.remaining').html('van '+ $('#user_recent_topics_max_pages').attr('value'));
			$('#profile-recent .pagination .next').removeClass('dead');
			$('#profile-recent .pagination .last').removeClass('dead');
		} else{
			$('.remaining').html('van 1');
			$('#profile-recent .pagination .next').addClass('dead');
			$('#profile-recent .pagination .last').addClass('dead');	
		}*/
		
		//next button
		$('#profile-recent .pagination .next').click(function() {
			current_page = parseInt($('#user_recent_posts_current_page').val());
	
			if(current_page < parseInt( $('#user_recent_topics_max_pages').val() ) ) {
				user_forumposts_start = user_forumposts_start + user_forumposts_number;
				current_page = parseInt($('#user_recent_posts_current_page').val());
				var page = current_page+1;
				$('#user_recent_posts_current_page').val(page);
				
				Pagination.load('#recent-topics', 'omt_profile_load_user_forumtopics.php', {
                    userid: $('#user_id').val(),
                    start: user_forumposts_start,
					limit: user_forumposts_number
                });

				// Cleanup the pagination's buttons
                Pagination.cleanup('#profile-recent ', page, parseInt($('#user_recent_topics_max_pages').val()));
			}
	
			return false;
		});

		//previous button
		$('#profile-recent .pagination .prev').click(function() {
			if(user_forumposts_start > 0 ) {
				current_page = parseInt($('#user_recent_posts_current_page').val());
				user_forumposts_start = user_forumposts_start - user_forumposts_number;
				var page = current_page-1;
				$('#user_recent_posts_current_page').val(page);

				Pagination.load('#recent-topics', 'omt_profile_load_user_forumtopics.php', {
	                    userid: $('#user_id').val(),
	                    start: user_forumposts_start,
						limit: user_forumposts_number
	            });
				
				// Cleanup the pagination's buttons
                Pagination.cleanup('#profile-recent', page, parseInt($('#user_recent_topics_max_pages').val()));
			}

			return false;
		});
		
		// first button
		$('#profile-recent .pagination .first').click(function() {
			
			if(user_forumposts_start > 0 ) {
				user_forumposts_start = 0;
				var page = 1;
				$('#user_recent_posts_current_page').val(page);
				
				Pagination.load('#recent-topics', 'omt_profile_load_user_forumtopics.php', {
	                userid: $('#user_id').val(),
	                start: user_forumposts_start,
	                limit: user_forumposts_number
	            });

				// Cleanup the pagination's buttons
	            Pagination.cleanup('#profile-recent', page, parseInt($('#user_recent_topics_max_pages').val()));		
			}
			return false;
		});
		
		//last button
		$('#profile-recent .pagination .last').click(function() {
			current_page = parseInt($('#user_recent_posts_current_page').val());
            if(current_page < parseInt( $('#user_recent_topics_max_pages').val() ) ) {

                user_forumposts_start = user_forumposts_number * ( parseInt( $('#user_recent_topics_max_pages').val() ) - 1 );
			
                var page = parseInt($('#user_recent_topics_max_pages').val());

                $('#user_recent_posts_current_page').val(page);
                
                Pagination.load('#recent-topics', 'omt_profile_load_user_forumtopics.php', {
					userid: $('#user_id').val(),
                    start: user_forumposts_start,
                    limit: user_forumposts_number
                });
                
                // Cleanup the pagination's buttons
                Pagination.cleanup('#profile-recent', page, parseInt($('#user_recent_topics_max_pages').val()));
			
                return false;
            }
        });
		
		$('#profile-recent .pagination form').submit(function() {
            return false;
        });

        // page number button
        $('#profile-recent .pagination #user_recent_posts_current_page').change(function() {
            var page = $(this).val();
            if(page > 0 && page <= parseInt($('#user_recent_topics_max_pages').val())) {
                user_forumposts_start = (page-1)*user_forumposts_number;
                $('#user_recent_posts_current_page').attr('value', page);
                
                Pagination.load('#recent-topics', 'omt_profile_load_user_forumtopics.php', {
                    userid: $('#user_id').val(),
                    start: user_forumposts_start,
                    limit: user_forumposts_number
                });
                
                // Cleanup the pagination's buttons
                Pagination.cleanup('#profile-recent', page, parseInt($('#user_recent_topics_max_pages').val()));
            }
            return false;
        });
	},
	
	// Profile / Apple products Carousel
	applecarousel : function() {
		$(".apple-carousel").jCarouselLite({
	    	btnNext: "#look-at-my-stuff .prev",
	      	btnPrev: "#look-at-my-stuff .next",
			visible: 3,
			scroll:2,
			easing: 'easeOutQuad'
	  	});
	},
	
	//Profile / if carousel empty (old products ids for example)
	empty_applecarousel : function() {
		if($("#look-at-my-stuff ul li").length <1) {
			$("#look-at-my-stuff").hide();
		}
	},
	
	// Profile / Apple products Tooltip
	appletooltip : function() {
	    if($("#look-at-my-stuff ul li a").length) {
	        $("#look-at-my-stuff ul li a").tooltip({
	           track: true,
	           top: -35,
	           left: -50
	        });
	    }
	},
	
	// Pro pages d'or
	pro : function() {
		$('#enable_page').click(function() {
			$('#pro-page').toggle();
		});
	}
};

/* 
	User Dashboard 
*/
var Dashboard = {
	// Gestarte Topics pagination
	gestarte : function() {
	
		$('#subs-more .previous-gestarte').click(function() {
	
			current_page = parseInt($('#gestarte_current_page').attr('value'));
	
			gestarte_topics_start = gestarte_topics_start + gestarte_topics_number;

			$('#gestarte_current_page').attr('value', current_page+1);

			$('#gestarte-topics').load('/wp-content/plugins/omt/omt_dashboard_load_user_started_topics.php?userid='+$('#user_id').attr('value')+'&current_page='+$('#gestarte_current_page').attr('value')+'&start='+gestarte_topics_start+'&limit='+gestarte_topics_number, function() {
				Dashboard.gestarte();
			});

			return false;
		});
		
		$('#subs-more .next-gestarte').click(function() {
			if(gestarte_topics_start > 0 ) {
				gestarte_topics_start = gestarte_topics_start - gestarte_topics_number;
				current_page = parseInt($('#gestarte_current_page').attr('value'));
				
				$('#gestarte_current_page').attr('value', current_page-1);

				$('#gestarte-topics').load('/wp-content/plugins/omt/omt_dashboard_load_user_started_topics.php?userid='+$('#user_id').attr('value')+'&current_page='+$('#gestarte_current_page').attr('value')+'&start='+gestarte_topics_start+'&limit='+gestarte_topics_number, function() {
					Dashboard.gestarte();
				});
			}
			
			return false;
		});
	},
	
	// Subscribed topics pagination and "select all" button
	subscribed : function() {
		
		// "select all" checkbox
		$("#dash_subscribed_selectall").click(function()				
		{ 
			var checked_status = this.checked;
			$("input[name='dash_subscribed_check[]']").each(function()
			{
				this.checked = checked_status;
			});
		});
		
		//previous button
		$('#subs-more .previous-subscribed').click(function() {

			current_page = parseInt($('#subscribed_current_page').attr('value'));

			subscribed_topics_start = subscribed_topics_start + subscribed_topics_number;

			$('#subscribed_current_page').attr('value', current_page+1);

			$('#subscriptions').load('/wp-content/plugins/omt/omt_dashboard_load_user_subscribed_topics.php?userid='+$('#user_id').attr('value')+'&current_page='+$('#subscribed_current_page').attr('value')+'&start='+subscribed_topics_start+'&limit='+subscribed_topics_number, function() {
				Dashboard.subscribed();
			});

			return false;
		});
		
		// next button
		$('#subs-more .next-subscribed').click(function() {
			if(subscribed_topics_start > 0 ) {
				subscribed_topics_start = subscribed_topics_start - subscribed_topics_number;
				current_page = parseInt($('#subscribed_current_page').attr('value'));

				$('#subscribed_current_page').attr('value', current_page-1);

				$('#subscriptions').load('/wp-content/plugins/omt/omt_dashboard_load_user_subscribed_topics.php?userid='+$('#user_id').attr('value')+'&current_page='+$('#subscribed_current_page').attr('value')+'&start='+subscribed_topics_start+'&limit='+subscribed_topics_number, function() {
					Dashboard.subscribed();
				});
			}

			return false;
		});
	},
	
	recent : function() {
		$('#subs-more .previous-recente').click(function() {
	
			current_page = parseInt($('#recent_current_page').attr('value'));
			recent_topics_start = recent_topics_start + recent_topics_number;
			$('#recent_current_page').attr('value', current_page+1);
			$('#recente-reacties').load('/wp-content/plugins/omt/omt_dashboard_load_user_recent_posts.php?userid='+$('#user_id').attr('value')+'&current_page='+$('#recent_current_page').attr('value')+'&start='+recent_topics_start+'&limit='+recent_topics_number, function() {
				Dashboard.recent();
			});

			return false;
		});
		
		$('#subs-more .next-recente').click(function() {
			if(recent_topics_start > 0 ) {
				recent_topics_start = recent_topics_start - recent_topics_number;
				current_page = parseInt($('#recent_current_page').attr('value'));
				
				$('#recent_current_page').attr('value', current_page-1);

				$('#recente-reacties').load('/wp-content/plugins/omt/omt_dashboard_load_user_recent_posts.php?userid='+$('#user_id').attr('value')+'&current_page='+$('#recent_current_page').attr('value')+'&start='+recent_topics_start+'&limit='+recent_topics_number, function() {
					Dashboard.recent();
				});
			}
			
			return false;
		});
		
	},
	
	// incoming messages pagination and "select all" button
	incoming : function() {
		
		// "select all" checkbox
		$("#dash_incoming_selectall").click(function()				
		{ 
			var checked_status = this.checked;
			$("input[name='dash_incoming_check[]']").each(function()
			{
				this.checked = checked_status;
			});
		});
		
		// previous button
		$('#subs-more .previous-incoming').click(function() {

			current_page = parseInt($('#incoming_current_page').attr('value'));

			incoming_messages_start = incoming_messages_start + incoming_messages_number;

			$('#incoming_current_page').attr('value', current_page+1);

			$('#berichten').load('/wp-content/plugins/omt/omt_dashboard_load_user_incoming_messages.php?userid='+$('#user_id').attr('value')+'&current_page='+$('#incoming_current_page').attr('value')+'&start='+incoming_messages_start+'&limit='+incoming_messages_number, function() {
				Dashboard.incoming();
			});

			return false;
		});
		
		//next button
		$('#subs-more .next-incoming').click(function() {
			if(incoming_messages_start > 0 ) {
				incoming_messages_start = incoming_messages_start - incoming_messages_number;
				current_page = parseInt($('#incoming_current_page').attr('value'));

				$('#incoming_current_page').attr('value', current_page-1);

				$('#berichten').load('/wp-content/plugins/omt/omt_dashboard_load_user_incoming_messages.php?userid='+$('#user_id').attr('value')+'&current_page='+$('#incoming_current_page').attr('value')+'&start='+incoming_messages_start+'&limit='+incoming_messages_number, function() {
					Dashboard.incoming();
				});
			}

			return false;
		});
	},
	
	// sent messages
	sent : function() {
		$('#subs-more .previous-sent').click(function() {

			current_page = parseInt($('#sent_current_page').attr('value'));
			sent_messages_start = sent_messages_start + sent_messages_number;

			$('#sent_current_page').attr('value', current_page+1);

			$('#verzonden').load('/wp-content/plugins/omt/omt_dashboard_load_user_sent_messages.php?userid='+$('#user_id').attr('value')+'&current_page='+$('#sent_current_page').attr('value')+'&start='+sent_messages_start+'&limit='+sent_messages_number, function() {
				Dashboard.sent();
			});

			return false;
		});
		
		$('#subs-more .next-sent').click(function() {
			if(sent_messages_start > 0 ) {
				sent_messages_start = sent_messages_start - sent_messages_number;
				current_page = parseInt($('#sent_current_page').attr('value'));

				$('#sent_current_page').attr('value', current_page-1);

				$('#verzonden').load('/wp-content/plugins/omt/omt_dashboard_load_user_sent_messages.php?userid='+$('#user_id').attr('value')+'&current_page='+$('#sent_current_page').attr('value')+'&start='+sent_messages_start+'&limit='+sent_messages_number, function() {
					Dashboard.sent();
				});
			}

			return false;
		});
	},
	
	// Created vacatures
	vacatures : function() {
		
		// "select all" checkbox
		$("#dash_vacatures_selectall").click(function()				
		{ 
			var checked_status = this.checked;
			$("input[name='dash_vacaturecheck[]']").each(function()
			{
				this.checked = checked_status;
			});
		});
		
		//previous button
		$('#subs-more .previous-vacatures').click(function() {

			current_page = parseInt($('#vacatures_current_page').attr('value'));
			vacatures_start = vacatures_start + vacatures_number;

			$('#vacatures_current_page').attr('value', current_page+1);

			$('#vacatures').load('/wp-content/plugins/omt/omt_dashboard_load_user_created_vacatures.php?userid='+$('#user_id').attr('value')+'&current_page='+$('#vacatures_current_page').attr('value')+'&start='+vacatures_start+'&limit='+vacatures_number, function() {
				Dashboard.vacatures();
			});

			return false;
		});
		
		//next button
		$('#subs-more .next-vacatures').click(function() {
			if(vacatures_start > 0 ) {
				vacatures_start = vacatures_start - vacatures_number;
				current_page = parseInt($('#vacatures_current_page').attr('value'));

				$('#vacatures_current_page').attr('value', current_page-1);

				$('#vacatures').load('/wp-content/plugins/omt/omt_dashboard_load_user_created_vacatures.php?userid='+$('#user_id').attr('value')+'&current_page='+$('#vacatures_current_page').attr('value')+'&start='+vacatures_start+'&limit='+vacatures_number, function() {
					Dashboard.vacatures();
				});
			}

			return false;
		});
	},
	
	// Created events
	events : function() {
		
		// "select all" checkbox
		$("#dash_events_selectall").click(function()				
		{ 
			var checked_status = this.checked;
			$("input[name='dash_eventcheck[]']").each(function()
			{
				this.checked = checked_status;
			});
		});
		
		//previous button
		$('#subs-more .previous-events').click(function() {

			current_page = parseInt($('#events_current_page').attr('value'));
			events_start = events_start + events_number;

			$('#events_current_page').attr('value', current_page+1);

			$('#evenementen').load('/wp-content/plugins/omt/omt_dashboard_load_user_created_events.php?userid='+$('#user_id').attr('value')+'&current_page='+$('#events_current_page').attr('value')+'&start='+events_start+'&limit='+events_number, function() {
				Dashboard.events();
			});

			return false;
		});
		
		//next button
		$('#subs-more .next-events').click(function() {
			if(events_start > 0 ) {
				events_start = events_start - events_number;
				current_page = parseInt($('#events_current_page').attr('value'));

				$('#events_current_page').attr('value', current_page-1);

				$('#evenementen').load('/wp-content/plugins/omt/omt_dashboard_load_user_created_events.php?userid='+$('#user_id').attr('value')+'&current_page='+$('#events_current_page').attr('value')+'&start='+events_start+'&limit='+events_number, function() {
					Dashboard.events();
				});
			}

			return false;
		});
	}
};

/*
    WebAds Advertisements
*/

var Ads = {
    places: {
        home: {
            tbase:'SITE=ONEMORETHING/AREA=ONEMORETHING_HOME',
            iframe:'/wp-content/themes/omt_theme/javascripts/ads/frame.html',
            stir:'onemorething_stir.htm',
            wl:'onemorething/1main',
            cc:'TU/247/3736/13564',
            BannerPreSize:'true'
        },
        forum: {
            tbase:'SITE=ONEMORETHING/AREA=ONEMORETHING_FORUM',
            iframe:'/wp-content/themes/omt_theme/javascripts/ads/frame.html',
            stir:'onemorething_stir.htm',
            wl:'onemorething/forum',
            cc:'TU/247/3736/13565',
            BannerPreSize:'true'
        },
        pro: {
            tbase:'SITE=ONEMORETHING/AREA=ONEMORETHING_ZAKELIJK',
            iframe:'/wp-content/themes/omt_theme/javascripts/ads/frame.html',
            stir:'onemorething_stir.htm',
            wl:'onemorething/1main',
            cc:'TU/247/3736/13564',
            BannerPreSize:'true'
        },
        other: {
            tbase:'SITE=ONEMORETHING/AREA=ONEMORETHING_OTHER',
            iframe:'/wp-content/themes/omt_theme/javascripts/ads/frame.html',
            stir:'onemorething_stir.htm',
            wl:'onemorething/1main',
            cc:'TU/247/3736/13564',
            BannerPreSize:'true'
        }
    },
    place_set: false,
    initialized: false,
    show: function(resolution, position) {
        try {
            wbds_ads.qWrite(resolution, position.toUpperCase());
        } catch(e){}
    },
    leaderboard: function(position) {
        this.show('728x90', position);
    },
    skyscraper: function(position) {
        this.show('120x600', position);
    },
    rectangle: function(position) {
        this.show('336x280', position);
    },
    init: function(page) {
        if(!Ads.place_set) {
            Ads.setPlace(page);
        }

        try {
            wbds_ads.load();
        } catch(e){}
        
    },
    setPlace: function(name) {
        var place = Ads.places[name];
        if(!place) {
            var place = Ads.places['other'];
        }
        try {
            //wbds_PlaceInit(place);
            var i=0,url = 'http://smartinit.webads.nl/Bin/SmartInit.dll?';
            for(var k in place){ if(isS(place[k]) && place[k] != ''){ if(i == 0) url += k+'='+place[k];  else            url += '&'+k+'='+place[k];  i++;  } }
            var browser = new wbds_GetBrowser();
            if ((document.getElementsByTagName('base').length==0 || !browser.is_ie) && (!browser.is_webkit || browser.is_webkit && browser.version > 3) && (!browser.is_safari)) {
                wbds_Include(url); 
            } 
            else {  
                wbds_Include(url,'direct');  
            }
            Ads.place_set = true;
        }
        catch(e){}
    }
};

/*
	Evenementen
*/
var Evenementen = {
	// add an event : google maps link toggle
	add_googlemaps : function() {
		$('#add-gmap').click(function() {
			$('#addevent-li-gmap').toggle();
			/*if($('#add-gmap').attr("checked") == false) {
				$('#events_mapslink').attr("value", "");
			}*/
		});
	},
	
	date_pick : function() {
	    if($('.date-pick').length) {
	        $('.date-pick').datePicker();
	    }
	}
}

/*
	Community	
*/
var Community = {
	//forum post form
	postform : function() {
		//emoticons
		$("div#wysiwyg ul li#ctrl-emoticons a").click(function(){
			$("div#wysiwyg #ctrl-emoticon-overlay").show();
			return false;
		});

		$("div#wysiwyg #ctrl-emoticon-overlay div a").click(function(){
			$("div#wysiwyg #ctrl-emoticon-overlay").hide();
			return false;
		});
		
		//return false on forms wysiwyg
		jQuery('#wysiwyg ul li a').click(function(){
			return false;
		});
	}, 
	
	forumpagination : function() {
		$('#topic-list-top-nav .pagination form, #topic-list-bottom-nav .pagination form, #thread .pagination form, #thread-info .pagination form, .advanced-search-pagination form').submit(function() {
			return false;
        });
        // page number button
        $('#topic-list-top-nav .pagination #forum_list_current_page, #topic-list-bottom-nav .pagination #forum_list_current_page, #thread .pagination #forum_list_current_page, #thread-info .pagination #forum_list_current_page').change(function() {
           	var page = $(this).val();

            if(page > 0 && page <= parseInt($('#forum_list_total_page').val())) {
				var url = $('#forum_url').val()+"/page/"+page;
				window.location.replace(url);
        	}
            return false;
        });

		$('.advanced-search-pagination #supersearch_current_page').change(function() {
           	var page = $(this).val();

            if(page > 0 && page <= parseInt($('#supersearch_total_page').val())) {
				var url = $('#supersearch_url').val()+"&page="+page;
				window.location.replace(url);
        	}
            return false;
        });
	},
	
	// page /community : tabs pagination
	community_newtopics : function() {
		communityfront_start = 0;
        communityfront_number = 20;
        frontoffset = communityfront_number;

		if($('#community_front_max_pages').attr('value')<=1){
			$('#community-front-next').addClass('dead');	
			$('#community-front-last').addClass('dead');
		}

		//next button
        $('#community-front-next').click(function() { 
            communityfront_current_page = parseInt($('#community_front_current_page').val());
            if(communityfront_current_page < parseInt( $('#community_front_max_pages').val() ) ) {
				
                communityfront_start = communityfront_start + communityfront_number;
                communityfront_current_page = parseInt($('#community_front_current_page').val());
                var page = communityfront_current_page+1;

                $('#community_front_current_page').val(page);
                
                Pagination.load('#nieuwe-topics', 'omt_community_load_last_topics.php', {
                    start: communityfront_start,
                    limit: communityfront_number
                });
                
                // Cleanup the pagination's buttons
                Pagination.cleanup('.front-community-act', page, parseInt($('#community_front_max_pages').val()));
                
                return false;
            } 
        });

       	//previous button
 		$('#community-front-prev').click(function() {
            if(communityfront_start > 0 ) {
                communityfront_start = communityfront_start - communityfront_number;
                communityfront_current_page = parseInt($('#community_front_current_page').val());
                var page = communityfront_current_page-1;
                $('#community_front_current_page').val(page);

                Pagination.load('#nieuwe-topics', 'omt_community_load_last_topics.php', {
                    start: communityfront_start,
                    limit: communityfront_number
                });
                
                // Cleanup the pagination's buttons
                Pagination.cleanup('.front-community-act', page, parseInt($('#community_front_max_pages').val()));
            }
        });
        
        // first button
        $('#community-front-first').click(function() {
            
            if(communityfront_start > 0 ) {
                communityfront_start = 0;
                var page = 1;
                $('#community_front_current_page').val(page);
                
                Pagination.load('#nieuwe-topics', 'omt_community_load_last_topics.php', {
                    start: communityfront_start,
                    limit: communityfront_number
                });
                
                // Cleanup the pagination's buttons
                Pagination.cleanup('.front-community-act', page, parseInt($('#community_front_max_pages').val()));
            }
            return false;
        });

		// last button
        $('#community-front-last').click(function() {
            
			communityfront_current_page = parseInt($('#community_front_current_page').val());
            if(communityfront_current_page < parseInt( $('#community_front_max_pages').val() ) ) {
			
                communityfront_start = communityfront_number * ( parseInt( $('#community_front_max_pages').val() ) - 1 );

                var page = parseInt($('#community_front_max_pages').val());

                $('#community_front_current_page').val(page);
                
                Pagination.load('#nieuwe-topics', 'omt_community_load_last_topics.php', {
                    start: communityfront_start,
                    limit: communityfront_number
                });
                
                // Cleanup the pagination's buttons
                Pagination.cleanup('.front-community-act', page, parseInt($('#community_front_max_pages').val()));
			
                return false;
            }
        });

        
       	$('.front-community-act .pagination form').submit(function() {
            return false;
        });
        
        // page number button
        $('.front-community-act .pagination #community_front_current_page').change(function() {
            var page = $(this).val();
            if(page > 0 && page <= parseInt($('#community_front_max_pages').val())) {
                communityfront_start = (page-1)*communityfront_number;
                $('#community_front_current_page').attr('value', page);
                
                Pagination.load('#nieuwe-topics', 'omt_community_load_last_topics.php', {
                    start: communityfront_start,
                    limit: communityfront_number
                });
                
                // Cleanup the pagination's buttons
                Pagination.cleanup('.front-community-act', page, parseInt($('#community_front_max_pages').val()));
            }
            return false;
        });
	},
	
	// page /community : tabs pagination
	community_lastposts : function() {
		communityfront_start = 0;
        communityfront_number = 20;
        frontoffset = communityfront_number;

		//next button
        $('#community-front-next').click(function() { 
            communityfront_current_page = parseInt($('#community_front_current_page').val());
            if(communityfront_current_page < parseInt( $('#community_front_last_posts_max_pages').val() ) ) {
				
                communityfront_start = communityfront_start + communityfront_number;
                communityfront_current_page = parseInt($('#community_front_current_page').val());
                var page = communityfront_current_page+1;

                $('#community_front_current_page').val(page);
                
                Pagination.load('#laatste-posts', 'omt_community_load_last_posts.php', {
                    start: communityfront_start,
                    limit: communityfront_number
                });
                
                // Cleanup the pagination's buttons
                Pagination.cleanup('.front-community-act', page, parseInt($('#community_front_last_posts_max_pages').val()));
                
                return false;
            } 
        });

       	//previous button
 		$('#community-front-prev').click(function() {
            if(communityfront_start > 0 ) {
                communityfront_start = communityfront_start - communityfront_number;
                communityfront_current_page = parseInt($('#community_front_current_page').val());
                var page = communityfront_current_page-1;
                $('#community_front_current_page').val(page);

                Pagination.load('#laatste-posts', 'omt_community_load_last_posts.php', {
                    start: communityfront_start,
                    limit: communityfront_number
                });
                
                // Cleanup the pagination's buttons
                Pagination.cleanup('.front-community-act', page, parseInt($('#community_front_last_posts_max_pages').val()));
            }
        });
        
       	// first button
        $('#community-front-first').click(function() {
            
            if(communityfront_start > 0 ) {
                communityfront_start = 0;
                var page = 1;
                $('#community_front_current_page').val(page);
                
                Pagination.load('#laatste-posts', 'omt_community_load_last_posts.php', {
                    start: communityfront_start,
                    limit: communityfront_number
                });
                
                // Cleanup the pagination's buttons
                Pagination.cleanup('.front-community-act', page, parseInt($('#community_front_last_posts_max_pages').val()));
            }
            return false;
        });

		// last button
      	$('#community-front-last').click(function() {
            
			communityfront_current_page = parseInt($('#community_front_current_page').val());
            if(communityfront_current_page < parseInt( $('#community_front_last_posts_max_pages').val() ) ) {
			
                communityfront_start = communityfront_number * ( parseInt( $('#community_front_last_posts_max_pages').val() ) - 1 );

                var page = parseInt($('#community_front_last_posts_max_pages').val());

                $('#community_front_current_page').val(page);
                
                Pagination.load('#laatste-posts', 'omt_community_load_last_posts.php', {
                    start: communityfront_start,
                    limit: communityfront_number
                });
                
                // Cleanup the pagination's buttons
                Pagination.cleanup('.front-community-act', page, parseInt($('#community_front_last_posts_max_pages').val()));
			
                return false;
            }
        });

        
       	$('.front-community-act .pagination form').submit(function() {
            return false;
        });
        
        // page number button
        $('.front-community-act .pagination #community_front_current_page').change(function() {
            var page = $(this).val();
            if(page > 0 && page <= parseInt($('#community_front_last_posts_max_pages').val())) {
                communityfront_start = (page-1)*communityfront_number;
                $('#community_front_current_page').attr('value', page);
                
                Pagination.load('#laatste-posts', 'omt_community_load_last_posts.php', {
                    start: communityfront_start,
                    limit: communityfront_number
                });
                
                // Cleanup the pagination's buttons
                Pagination.cleanup('.front-community-act', page, parseInt($('#community_front_last_posts_max_pages').val()));
            }
            return false;
        });
	},
	
	// page /community : tabs pagination
	community_unansweredtopics : function() {
		communityfront_start = 0;
        communityfront_number = 20;
        frontoffset = communityfront_number;

		//next button
        $('#community-front-next').click(function() { 
            communityfront_current_page = parseInt($('#community_front_current_page').val());
            if(communityfront_current_page < parseInt( $('#community_front_unanswered_topics_max_pages').val() ) ) {
				
                communityfront_start = communityfront_start + communityfront_number;
                communityfront_current_page = parseInt($('#community_front_current_page').val());
                var page = communityfront_current_page+1;

                $('#community_front_current_page').val(page);
                
                Pagination.load('#onbeantwoord', 'omt_community_load_unanswered_topics.php', {
                    start: communityfront_start,
                    limit: communityfront_number
                });
                
                // Cleanup the pagination's buttons
                Pagination.cleanup('.front-community-act', page, parseInt($('#community_front_unanswered_topics_max_pages').val()));
                
                return false;
            } 
        });

       	//previous button
 		$('#community-front-prev').click(function() {
            if(communityfront_start > 0 ) {
                communityfront_start = communityfront_start - communityfront_number;
                communityfront_current_page = parseInt($('#community_front_current_page').val());
                var page = communityfront_current_page-1;
                $('#community_front_current_page').val(page);

                Pagination.load('#onbeantwoord', 'omt_community_load_unanswered_topics.php', {
                    start: communityfront_start,
                    limit: communityfront_number
                });
                
                // Cleanup the pagination's buttons
                Pagination.cleanup('.front-community-act', page, parseInt($('#community_front_unanswered_topics_max_pages').val()));
            }
			return false;
        });
     
       	// first button
        $('#community-front-first').click(function() {
            
            if(communityfront_start > 0 ) {
                communityfront_start = 0;
                var page = 1;
                $('#community_front_current_page').val(page);
                
                Pagination.load('#onbeantwoord', 'omt_community_load_unanswered_topics.php', {
                    start: communityfront_start,
                    limit: communityfront_number
                });
                
                // Cleanup the pagination's buttons
                Pagination.cleanup('.front-community-act', page, parseInt($('#community_front_unanswered_topics_max_pages').val()));
            }
            return false;
        });
 
		// last button
      	$('#community-front-last').click(function() {
            
			communityfront_current_page = parseInt($('#community_front_current_page').val());
            if(communityfront_current_page < parseInt( $('#community_front_unanswered_topics_max_pages').val() ) ) {
			
                communityfront_start = communityfront_number * ( parseInt( $('#community_front_unanswered_topics_max_pages').val() ) - 1 );

                var page = parseInt($('#community_front_unanswered_topics_max_pages').val());

                $('#community_front_current_page').val(page);
                
                Pagination.load('#onbeantwoord', 'omt_community_load_unanswered_topics.php', {
                    start: communityfront_start,
                    limit: communityfront_number
                });
                
                // Cleanup the pagination's buttons
                Pagination.cleanup('.front-community-act', page, parseInt($('#community_front_unanswered_topics_max_pages').val()));
			
                return false;
            }
        });

       	$('.front-community-act .pagination form').submit(function() {
            return false;
        });
        
        // page number button
        $('.front-community-act .pagination #community_front_current_page').change(function() {
            var page = $(this).val();
            if(page > 0 && page <= parseInt($('#community_front_unanswered_topics_max_pages').val())) {
                communityfront_start = (page-1)*communityfront_number;
                $('#community_front_current_page').attr('value', page);
                
                Pagination.load('#onbeantwoord', 'omt_community_load_unanswered_topics.php', {
                    start: communityfront_start,
                    limit: communityfront_number
                });
                
                // Cleanup the pagination's buttons
                Pagination.cleanup('.front-community-act', page, parseInt($('#community_front_unanswered_topics_max_pages').val()));
            }
            return false;
        });
	},
	
	// page /community : online members tabs pagination
	community_online_members : function() {
		communityfront_members_start = 0;
        communityfront_members_number = 27;

		if($('#community_front_members_max_pages').attr('value')<=1){
			$('#community-front-members-next').addClass('dead');	
			$('#community-front-members-last').addClass('dead');
		}

		//next button
        $('#community-front-members-next').click(function() { 
            communityfront_members_current_page = parseInt($('#community_front_members_current_page').val());
            if(communityfront_members_current_page < parseInt( $('#community_front_members_max_pages').val() ) ) {
				
                communityfront_members_start = communityfront_members_start + communityfront_members_number;
                communityfront_members_current_page = parseInt($('#community_front_members_current_page').val());
                var page = communityfront_members_current_page+1;

                $('#community_front_members_current_page').val(page);
                
                Pagination.load('#online-members', 'omt_community_load_online_members.php', {
                    start: communityfront_members_start,
                    limit: communityfront_members_number
                }, Comments.avatarLinks);
                
                // Cleanup the pagination's buttons
                Pagination.cleanup('.members', page, parseInt($('#community_front_members_max_pages').val()));
            } 
			return false;
        });

		//previous button
 		$('#community-front-members-prev').click(function() {
            if(communityfront_members_start > 0 ) {
                communityfront_members_start = communityfront_members_start - communityfront_members_number;
                communityfront_members_current_page = parseInt($('#community_front_members_current_page').val());
                var page = communityfront_members_current_page-1;
                $('#community_front_members_current_page').val(page);

                Pagination.load('#online-members', 'omt_community_load_online_members.php', {
                    start: communityfront_members_start,
                    limit: communityfront_members_number
                }, Comments.avatarLinks);
                
                // Cleanup the pagination's buttons
                Pagination.cleanup('.members', page, parseInt($('#community_front_members_max_pages').val()));
            }
			return false;
        });

		// first button
        $('#community-front-members-first').click(function() {
            
            if(communityfront_members_start > 0 ) {
                communityfront_members_start = 0;
                var page = 1;
                $('#community_front_members_current_page').val(page);
                
                Pagination.load('#online-members', 'omt_community_load_online_members.php', {
                    start: communityfront_members_start,
                    limit: communityfront_members_number
                }, Comments.avatarLinks);
                
                // Cleanup the pagination's buttons
                Pagination.cleanup('.members', page, parseInt($('#community_front_members_max_pages').val()));
            }
            return false;
        });

		// last button
      	$('#community-front-members-last').click(function() {
            
			communityfront_members_current_page = parseInt($('#community_front_members_current_page').val());
            if(communityfront_members_current_page < parseInt( $('#community_front_members_max_pages').val() ) ) {
			
                communityfront_members_start = communityfront_members_number * ( parseInt( $('#community_front_members_max_pages').val() ) - 1 );

                var page = parseInt($('#community_front_members_max_pages').val());

                $('#community_front_members_current_page').val(page);
                
                Pagination.load('#online-members', 'omt_community_load_online_members.php', {
                    start: communityfront_members_start,
                    limit: communityfront_members_number
                }, Comments.avatarLinks);
                
                // Cleanup the pagination's buttons
                Pagination.cleanup('.members', page, parseInt($('#community_front_members_max_pages').val()));
            }
			return false;
        });

		$('.members .pagination form').submit(function() {
            return false;
        });
        
        // page number button
        $('.members .pagination #community_front_members_current_page').change(function() {
            var page = $(this).val();
            if(page > 0 && page <= parseInt($('#community_front_members_max_pages').val())) {
                communityfront_members_start = (page-1)*communityfront_members_number;
                $('#community_front_members_current_page').attr('value', page);
                
                Pagination.load('#online-members', 'omt_community_load_online_members.php', {
                    start: communityfront_members_start,
                    limit: communityfront_members_number
                }, Comments.avatarLinks);
                
                // Cleanup the pagination's buttons
                Pagination.cleanup('.members', page, parseInt($('#community_front_members_max_pages').val()));
            }
            return false;
        });
	},
	
	community_online_crew : function() {
		communityfront_members_start = 0;
        communityfront_members_number = 27;

		//next button
        $('#community-front-members-next').click(function() { 
            communityfront_members_current_page = parseInt($('#community_front_members_current_page').val());
            if(communityfront_members_current_page < parseInt( $('#community_front_crew_max_pages').val() ) ) {
				
                communityfront_members_start = communityfront_members_start + communityfront_members_number;
                communityfront_members_current_page = parseInt($('#community_front_members_current_page').val());
                var page = communityfront_members_current_page+1;

                $('#community_front_members_current_page').val(page);
                
                Pagination.load('#online-crew', 'omt_community_load_online_crew.php', {
                    start: communityfront_members_start,
                    limit: communityfront_members_number
                }, Comments.avatarLinks);
                
                // Cleanup the pagination's buttons
                Pagination.cleanup('.members', page, parseInt($('#community_front_crew_max_pages').val()));
            } 
			return false;
        });

		//previous button
 		$('#community-front-members-prev').click(function() {
            if(communityfront_members_start > 0 ) {
                communityfront_members_start = communityfront_members_start - communityfront_members_number;
                communityfront_members_current_page = parseInt($('#community_front_members_current_page').val());
                var page = communityfront_members_current_page-1;
                $('#community_front_members_current_page').val(page);

                Pagination.load('#online-crew', 'omt_community_load_online_crew.php', {
                    start: communityfront_members_start,
                    limit: communityfront_members_number
                }, Comments.avatarLinks);
                
                // Cleanup the pagination's buttons
                Pagination.cleanup('.members', page, parseInt($('#community_front_crew_max_pages').val()));
            }
			return false;
        });

		// first button
        $('#community-front-members-first').click(function() {
            
            if(communityfront_members_start > 0 ) {
                communityfront_members_start = 0;
                var page = 1;
                $('#community_front_members_current_page').val(page);
                
                Pagination.load('#online-crew', 'omt_community_load_online_crew.php', {
                    start: communityfront_members_start,
                    limit: communityfront_members_number
                }, Comments.avatarLinks);
                
                // Cleanup the pagination's buttons
                Pagination.cleanup('.members', page, parseInt($('#community_front_crew_max_pages').val()));
            }
            return false;
        });
		
		// last button
      	$('#community-front-members-last').click(function() {
            
			communityfront_members_current_page = parseInt($('#community_front_members_current_page').val());
            if(communityfront_members_current_page < parseInt( $('#community_front_crew_max_pages').val() ) ) {
			
                communityfront_members_start = communityfront_members_number * ( parseInt( $('#community_front_crew_max_pages').val() ) - 1 );

                var page = parseInt($('#community_front_crew_max_pages').val());

                $('#community_front_members_current_page').val(page);
                
                Pagination.load('#online-crew', 'omt_community_load_online_crew.php', {
                    start: communityfront_members_start,
                    limit: communityfront_members_number
                }, Comments.avatarLinks);
                
                // Cleanup the pagination's buttons
                Pagination.cleanup('.members', page, parseInt($('#community_front_crew_max_pages').val()));
            }
			return false;
        });

		$('.members .pagination form').submit(function() {
            return false;
        });
        
        // page number button
        $('.members .pagination #community_front_members_current_page').change(function() {
            var page = $(this).val();
            if(page > 0 && page <= parseInt($('#community_front_crew_max_pages').val())) {
                communityfront_members_start = (page-1)*communityfront_members_number;
                $('#community_front_members_current_page').attr('value', page);
                
                Pagination.load('#online-crew', 'omt_community_load_online_crew.php', {
                    start: communityfront_members_start,
                    limit: communityfront_members_number
                }, Comments.avatarLinks);
                
                // Cleanup the pagination's buttons
                Pagination.cleanup('.members', page, parseInt($('#community_front_crew_max_pages').val()));
            }
            return false;
        });
	}
	
}

$(document).ready(function(){
	
	//nu in de community input field fix when "back" button fro browser
	$('#community_current_page').attr("value", 1);
	
	/* 
		General
	*/
	Menu.init();
	Menu.search();
	General.tabs();
	General.over();
	General.magic();
	General.textareas();
	General.searchinputs();
	General.pagination();
	General.twitter();
	
	/* 
		Homepage
	*/
	// podcast carousel
	$(".mycarousel").jCarouselLite({
      	    btnNext: ".prev",
      	    btnPrev: ".next",
			visible: 4,
			scroll:4,
			easing: 'easeOutQuad'
  	});

	Home.roll();
	Home.allpodcast_roll();
	Home.nuindecommynity();

	/*
	 	Single page
	*/
	Artikel.scrolls();
	Comments.pagination();
	Comments.avatarLinks();
	
	/* 
		Dashboard
	*/
	gestarte_topics_start = 0 ;
	gestarte_topics_number = 20 ;
	Dashboard.gestarte();
	
	subscribed_topics_start = 0 ;
	subscribed_topics_number = 20 ;	
	Dashboard.subscribed();
	
	recent_topics_start = 0 ;
	recent_topics_number = 20 ;	
	Dashboard.recent();
	
	incoming_messages_start = 0 ;
	incoming_messages_number = 20 ;	
	Dashboard.incoming();
	
	sent_messages_start = 0 ;
	sent_messages_number = 20 ;	
	Dashboard.sent();
	
	vacatures_start = 0 ;
	vacatures_number = 20 ;	
	Dashboard.vacatures();
	
	events_start = 0 ;
	events_number = 20 ;	
	Dashboard.events();
	
	Popup.init();
	Ads.init();
	
	/*
	 	Evenementen
	*/
	// Add event
	Evenementen.add_googlemaps();
	Evenementen.date_pick();
	
	/*
	 	Community
	*/
	Community.postform();
	Community.forumpagination();
	Community.community_newtopics();
//	Community.community_lastposts();
	Community.community_online_members();	
	
	
	//reset pagination when change tab for New Topics
	$('#tab-nieuwe-topics').click(function() {
		$('#community_front_current_page').attr('value', 1);
	
		Pagination.cleanup('.front-community-act', 1, parseInt($('#community_front_max_pages').val()));
		
		if($('#community_front_max_pages').attr('value')>0){
			$('.front-community-act .pagination .remaining').html('van '+ $('#community_front_max_pages').attr('value'));
		}else{
			$('.front-community-act .pagination .remaining').html('van 1');	
		}
		$("#community-front-next").unbind("click");
		$("#community-front-last").unbind("click");
		$("#community-front-prev").unbind("click");
		$("#community-front-first").unbind("click");
		$('#community_front_current_page').unbind("change");
		
		Pagination.load('#laatste-posts', 'omt_community_load_last_posts.php', {
            start: 0,
            limit: 20
        });

		Pagination.load('#onbeantwoord', 'omt_community_load_unanswered_topics.php', {
            start: 0,
            limit: 20
        });

		Community.community_newtopics();
	});
	
	//reset pagination when change tab for Last Posts
	$('#tab-laatste-posts').click(function() {
		$('#community_front_current_page').attr('value', 1);
		
		Pagination.cleanup('.front-community-act', 1, parseInt($('#community_front_last_posts_max_pages').val()));
		
		if($('#community_front_last_posts_max_pages').attr('value')<=1){
			$('#community-front-next').addClass('dead');	
			$('#community-front-last').addClass('dead');
		}

		if($('#community_front_last_posts_max_pages').attr('value')>0){
			$('.front-community-act .pagination .remaining').html('van '+ $('#community_front_last_posts_max_pages').attr('value'));
		}else{
			$('.front-community-act .pagination .remaining').html('van 1');	
		}
		$("#community-front-next").unbind("click");
		$("#community-front-last").unbind("click");
		$("#community-front-prev").unbind("click");
		$("#community-front-first").unbind("click");
		$('#community_front_current_page').unbind("change");
		
		Pagination.load('#nieuwe-topics', 'omt_community_load_last_topics.php', {
            start: 0,
            limit: 20
        });

		Pagination.load('#onbeantwoord', 'omt_community_load_unanswered_topics.php', {
            start: 0,
            limit: 20
        });
		
		Community.community_lastposts();
	});

	//reset pagination when change tab for Unanswered Topics
	$('#tab-onbeantwoord').click(function() {
		$('#community_front_current_page').attr('value', 1);
		Pagination.cleanup('.front-community-act', 1, parseInt($('#community_front_unanswered_topics_max_pages').val()));
		
		if($('#community_front_unanswered_topics_max_pages').attr('value')<=1){
			$('#community-front-next').addClass('dead');	
			$('#community-front-last').addClass('dead');
		}
		
		if($('#community_front_unanswered_topics_max_pages').attr('value')>0){
			$('.front-community-act .pagination .remaining').html('van '+ $('#community_front_unanswered_topics_max_pages').attr('value'));
		}else{
			$('.front-community-act .pagination .remaining').html('van 1');	
		}
		
		$("#community-front-next").unbind("click");
		$("#community-front-last").unbind("click");
		$("#community-front-prev").unbind("click");
		$("#community-front-first").unbind("click");
		$('#community_front_current_page').unbind("change");
		
		Pagination.load('#nieuwe-topics', 'omt_community_load_last_topics.php', {
	    	start: 0,
	    	limit: 20
	   	});
	
		Pagination.load('#laatste-posts', 'omt_community_load_last_posts.php', {
            start: 0,
            limit: 20
        });

		Community.community_unansweredtopics();
	});
	
	//reset pagination when change tab for Online Members
	$('#tab-online-members').click(function() {
		$('#community_front_members_current_page').attr('value', 1);
	
		Pagination.cleanup('.members', 1, parseInt($('#community_front_members_max_pages').val()));
		
		if($('#community_front_members_max_pages').attr('value')>0){
			$('.members .pagination .remaining').html('van '+ $('#community_front_members_max_pages').attr('value'));
		}else{
			$('.members .pagination .remaining').html('van 1');	
		}
		$("#community-front-members-next").unbind("click");
		$("#community-front-members-last").unbind("click");
		$("#community-front-members-prev").unbind("click");
		$("#community-front-members-first").unbind("click");
		$('#community_front_members_current_page').unbind("change");
		
		//unbind the Comments.avatarLinks
		$("a.avatar, a.topic-avatar").unbind('click');
		$('body').unbind('click');
		
		Pagination.load('#online-crew', 'omt_community_load_online_crew.php', {
            start: 0,
            limit: 27
        }, Comments.avatarLinks);

		Community.community_online_members();
	});
	
	//reset pagination when change tab for Online Crew
	$('#tab-online-crew').click(function() {
		$('#community_front_members_current_page').attr('value', 1);
	
		Pagination.cleanup('.members', 1, parseInt($('#community_front_crew_max_pages').val()));
		
		
		if($('#community_front_crew_max_pages').attr('value')<=1){
			$('#community-front-members-next').addClass('dead');	
			$('#community-front-members-last').addClass('dead');
		}

		if($('#community_front_crew_max_pages').attr('value')>0){
			$('.members .pagination .remaining').html('van '+ $('#community_front_crew_max_pages').attr('value'));
		}else{
			$('.members .pagination .remaining').html('van 1');	
		}
		$("#community-front-members-next").unbind("click");
		$("#community-front-members-last").unbind("click");
		$("#community-front-members-prev").unbind("click");
		$("#community-front-members-first").unbind("click");
		$('#community_front_members_current_page').unbind("change");
		
		//unbind the Comments.avatarLinks
		$("a.avatar, a.topic-avatar").unbind('click');
		$('body').unbind('click');
		
		Pagination.load('#online-members', 'omt_community_load_online_members.php', {
            start: 0,
            limit: 27
        }, Comments.avatarLinks);
		Community.community_online_crew();
	});
	
	/*
		PROFILE
	*/
	Profile.flickr();
	Profile.password();
	Profile.forumposts();
	Profile.applecarousel();
	Profile.appletooltip();
	Profile.appleselector();
	Profile.pro();

	
	$('#tab-recent-posts').click(function() {
		$('#user_recent_posts_current_page').attr('value', 1);
		Pagination.cleanup('#profile-recent', 1, parseInt($('#user_recent_posts_max_pages').val()));
		
		if($('#user_recent_posts_max_pages').attr('value')>0){
			$('.remaining').html('van '+ $('#user_recent_posts_max_pages').attr('value'));
		}else{
			$('.remaining').html('van 1');	
		}
		
		$("#profile-recent .pagination .next").unbind("click");
		$("#profile-recent .pagination .last").unbind("click");
		$("#profile-recent .pagination .prev").unbind("click");
		$("#profile-recent .pagination .first").unbind("click");
		$('#profile-recent .pagination #user_recent_posts_current_page').unbind("change");
		
		Pagination.load('#recent-topics', 'omt_profile_load_user_forumtopics.php', {
            userid: $('#user_id').val(),
            start: 0,
			limit: 5
        });

		Profile.forumposts();
	});
		
	$('#tab-recent-topics').click(function() {
		$('#user_recent_posts_current_page').attr('value', 1);
		Pagination.cleanup('#profile-recent', 1, parseInt($('#user_recent_topics_max_pages').val()));
		
		if($('#user_recent_topics_max_pages').attr('value')<=1){
			$('#profile-recent .pagination .next').addClass('dead');	
			$('#profile-recent .pagination .last').addClass('dead');
		}

		if($('#user_recent_topics_max_pages').attr('value')>0){
			$('.remaining').html('van '+ $('#user_recent_topics_max_pages').attr('value'));
		}else{
			$('.remaining').html('van 1');	
		}

		$("#profile-recent .pagination .next").unbind("click");
		$("#profile-recent .pagination .last").unbind("click");
		$("#profile-recent .pagination .prev").unbind("click");
		$("#profile-recent .pagination .first").unbind("click");
		$('#profile-recent .pagination #user_recent_posts_current_page').unbind("change");
		
		Pagination.load('#recent-comments', 'omt_profile_load_user_forumposts.php', {
	            userid: $('#user_id').val(),
	            start: 0,
				limit: 5
		});
		
		
		Profile.forumtopics();
	});

});

function http_build_query (formdata, numeric_prefix, arg_separator) {
    var value, key, tmp = [];
 
    var _http_build_query_helper = function (key, val, arg_separator) {
        var k, tmp = [];
        if (val === true) {
            val = "1";
        } else if (val === false) {
            val = "0";
        }
        if (val !== null && typeof(val) === "object") {
            for (k in val) {
                if (val[k] !== null) {
                    tmp.push(_http_build_query_helper(key + "[" + k + "]", val[k], arg_separator));
                }
            }
            return tmp.join(arg_separator);
        } else if (typeof(val) !== "function") {
            return this.urlencode(key) + "=" + this.urlencode(val);
        } else {
            throw new Error('There was an error processing for http_build_query().');
        }
    };
 
    if (!arg_separator) {
        arg_separator = "&";
    }
    for (key in formdata) {
        value = formdata[key];
        if (numeric_prefix && !isNaN(key)) {
            key = String(numeric_prefix) + key;
        }
        tmp.push(_http_build_query_helper(key, value, arg_separator));
    }
 
    return tmp.join(arg_separator);
}

function urlencode (str) {
  	str = (str+'').toString();
    
    // Tilde should be allowed unescaped in future versions of PHP (as reflected below), but if you want to reflect current
    // PHP behavior, you would need to add ".replace(/~/g, '%7E');" to the following.
    return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
                                                                    replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
}