var bootstrap = function (){
	this._initSidebar = function (){
		$('nav.nav-sidebar > ul > li').each(function (){
			if($(this).children('a').hasClass('nochild')) {
				$(this).children('a').click(function (){
					//$(this).parent().children('ul').show();
					$(this).parent().siblings('li').children('ul').hide();
					$(this).parent().addClass('active').siblings('li').removeClass('active');
					return true;
				}) 
				//if (!$(this).hasClass('active')) $(this).children('ul').hide();

			}else {
				$(this).children('a').click(function (){
                                    if ($(this).parent().hasClass('active')) {
                                        $(this).parent().children('ul').hide();
                                        $(this).parent().removeClass('active');
                                        return false;
                                    }else {
                                        $(this).parent().children('ul').show();
					$(this).parent().siblings('li').children('ul').hide();
					$(this).parent().addClass('active').siblings('li').removeClass('active');
					return false;
                                    }
					
				});
				if (!$(this).hasClass('active')) $(this).children('ul').hide();
			}     
		});
	};

	this._initHomepage = function (){
		if ($(".home-slider").length>0){
			var slide_delay = 10; //seconds
			var slide_no = 1;
			var slider_to;
			var animating = false;
			var max_slides = $(".home-slider .slide").length;
			var slideTransition = function (slide){
				clearTimeout(slider_to);
				slider_to =setTimeout(slideTransition, (slide_delay*1000+(animating ? 1000 : 0)));
				if (animating) return;
				var prev = slide_no;
				if (slide!=null){
					if (slide_no == slide+1) return;
					slide_no = slide+1;
				}else{
					slide_no++;
				}
				if (slide_no>max_slides) slide_no = 1;
				$(".home-slider .transition_slide").addClass('slide-bg-'+prev);
				$(".home-slider .transition_slide .slide_new").addClass('slide-bg-'+slide_no);
				$(".home-slider .slide:eq("+(prev-1)+")").fadeOut('normal');
				$(".home-slider .slide:eq("+(slide_no-1)+")").fadeIn('slow');
				$(".nav-home-slider ul li:eq("+(slide_no-1)+")").addClass('selected').siblings().removeClass('selected');
				animating = true;
				$(".home-slider .transition_slide").show().animate({left:-710}, 'slow', function (){
					$(".home-slider .transition_slide").css({left:0});
					$(".home-slider").addClass('slide-bg-'+slide_no);
					$(".home-slider").removeClass('slide-bg-'+prev);
					$(".home-slider .transition_slide").hide()
					$(".home-slider .transition_slide").removeClass('slide-bg-'+prev);
					$(".home-slider .transition_slide .slide_new").removeClass('slide-bg-'+slide_no);
					animating = false;
				});
			}

			$(".home-slider *").css('z-index', 10);
			$(".home-slider").append('<div class="transition_slide"><div class="slide_new"></div></div>');
			$(".nav-home-slider").append('<ul></ul>');
			for(var i=1; i<=max_slides; i++){
				$(".nav-home-slider ul").append('<li'+(i==1 ? ' class="selected"' : '')+'><a href="#"></a></li>');
			}
			$(".nav-home-slider ul li a").click(function (){slideTransition($(this).parent().index());})
			setTimeout(slideTransition, (slide_delay*1000));
		}
	};

	for(prop in this){if (prop.match(/^_init/i)) this[prop]();}
};

$(function(){
	var bs = new bootstrap();
});

