jQuery(document).ready(function($){

	$('.widget h4').click(function() {
		if (
			$(this.parentNode).children(!!$(this.parentNode).children('ul').length ? 'ul' : 'div').is(":hidden")
		)
			$(this.parentNode).children(!!$(this.parentNode).children('ul').length ? 'ul' : 'div').slideDown();	
		else
			$(this.parentNode).children(!!$(this.parentNode).children('ul').length ? 'ul' : 'div').slideUp();
	});

	$('.widget_categories .children').parent().children('a').after('<span class=\'jquery-accordion\'>&nbsp;</span>');
	$('.widget_categories .jquery-accordion').click(function(){
		_this = this;
		$(this).next().slideToggle('slow',function(){
			$(_this).toggleClass('active');
		});
	});
	
	all_most_views_grinds = $('.yui-g.view');
	$('.box-nav li').click(function (){
		_this = this;
		txtCon = $(this).text();
		var id = $('.yui-g.view.active')[0].id;

		if(parseInt(txtCon) != parseInt(/[0-9]/.exec(id))){
			grind = $('.yui-g.view.active');
			grind.fadeOut(
				'slow',
				function (){
					all_most_views_grinds.removeClass('active');
					var view = $('#most-viewed-' + txtCon);
					view.addClass('active');
					$('.box-nav li.active').removeClass('active');
					$(_this).addClass('active');
					view.fadeIn('slow');
					var active = $('.box-nav li.active');
					var first = $('.box-nav li:first');
					var last = $('.box-nav li:last');
					$('.box-button-left').removeClass('active');
					$('.box-button-right').removeClass('active');
					if( active[0] !== first[0] || active[0] !== last[0] )
						if( active[0] !== first[0])
							$('.box-button-left').addClass('active');
						if( active[0] !== last[0])
							$('.box-button-right').addClass('active');
				}
			);
		}
	});
	$('.box-button-left').click(function(){
		_this = this;
		txtCon = $('.box-nav li.active').text();
		txtCon_fi = $('.box-nav li:first').text();
		var id = $('.yui-g.view.active')[0].id;
		
		if (parseInt(txtCon) - 1 > parseInt(txtCon_fi) - 1) {
			num = parseInt(txtCon);
			index = num-1;
			alt = $('#most-viewed-' + num);
			alt.animate(
				{
					marginLeft: parseInt(alt.css('marginLeft'), 10) == 0 ? parseInt(alt.outerWidth()) : 0 
				},
				'slow',
				function (){
					all_most_views_grinds.removeClass('active');
					alt.css('marginLeft',0);
					neu = $('#most-viewed-' + parseInt(num-1));
					neu.css('marginLeft', '-40em');
					neu.addClass('active');
					var first = $('.box-nav li:first');
					$('.box-nav li.active').removeClass('active');
					$($('.box-nav').children()[index-1]).addClass('active');
					if(parseInt(/[0-9]/.exec(neu[0].id))==parseInt(first.text()))
						$(_this).removeClass('active').css({cursor:'auto'});
					else
						$('.box-button-right').css({cursor:'pointer'}).addClass('active');
					neu.animate({marginLeft: parseInt(neu.css('marginLeft'), 10) == 0 ? parseInt(0-neu.outerWidth()) : 0},'slow');
				}
			);
		}
	});
	$('.box-button-right').click(function(){
		_this = this;
		txtCon = $('.box-nav li.active').text();
		txtCon_la = $('.box-nav li:last').text();
		var id = $('.yui-g.view.active')[0].id;
		
		if (parseInt(txtCon) + 1 < parseInt(txtCon_la) + 1) {
			num = parseInt(txtCon);
			index = num-1;
			alt = $('#most-viewed-' + num);
			alt.animate(
				{
					marginLeft: parseInt(alt.css('marginLeft'), 10) == 0 ? parseInt(0 - alt.outerWidth())+'px' : 0 
				},
				'slow',
				function (){
					all_most_views_grinds.removeClass('active');
					alt.css('marginLeft',0);
					neu = $('#most-viewed-' + parseInt(num+1));
					neu.css('marginLeft', '40em');
					neu.addClass('active');
					var last = $('.box-nav li:last');
					$('.box-nav li.active').removeClass('active');
					$($('.box-nav').children()[index+1]).addClass('active');
					if(parseInt(/[0-9]/.exec(neu[0].id))==parseInt(last.text()))
						$(_this).removeClass('active').css({cursor:'auto'});
					else
						$('.box-button-left').addClass('active');
					neu.animate({marginLeft: parseInt(neu.css('marginLeft'), 10) == 0 ? neu.outerWidth() : 0 },'slow');
				}
			);
		}
	});
});
