/*
 * tabs-ajax.js
 * @author: Le Yang Xhtmlized.com
 * */


var tabsAjax = function($){
	var index = new Array();//current item index for each carousel

	function tabsEvent(){
		$('.box-nav li a').click(function(){
			var arg1, arg2;
			$(this).parents('.box-nav').children('li').removeClass('active');
			$(this).parent('li').addClass('active');
			ajaxEvent($(this).attr('id'), $(this).parents('.box-nav'));
			return false;
		});
	};

	function ajaxEvent(cHrefId, arg3){
		//link id encodes category and sorting: "cat3hits" or "cat1date"
		var nCategId = cHrefId.substr(3,1);
		var cSorting = cHrefId.substr(4);
		$.ajax({
			type: 'GET',
			dataType: 'xml',
			url: '?fa=app.jxGetListingsForCarousel&it='+nCategId+'&order='+cSorting,
			success: function(xml){
				//alert($(xml).find('carousel').children('info').eq(0).text());
				var elm = arg3.next().children('.carousel-content');
				reload(elm);
				elm.find('li').find('img').each(function(i){
					$(this).attr('src',$(xml).find('carousel').children('img').eq(i).text());
				});
				elm.find('li').find('p').each(function(i){
					$(this).html($(xml).find('carousel').children('info').eq(i).text());
				});
				elm.find('li').find('a').each(function(i){
					$(this).attr('href', $(xml).find('carousel').children('url').eq(i).text());
				});
				//reset navigation:
				var objNav = arg3.next().children('.carousel-nav');
				objNav.children('li.next').children('a').removeClass('disabled');
				objNav.children('li.prev').children('a').addClass('disabled');
				if(cSorting=='date')
					index[0]=1;
				else if(cSorting=='hits')
					index[1]=1;
			}
		});
	};

	function reload(elm){
		var index = 1; //start from 1
		elm.children('ul').css('left','0');
		elm.next().children('span').removeClass('active');
		for (var j = index * 4 - 4; j < index * 4; j++){
			elm.next().children('span').eq(j).addClass('active');
		}
	}

	function scrollEvent(groupNum, divLength, margin){
		var num = $('.carousel-nav .prev a').length;
		for (var n = 0; n < num; n++){
			index[n] = 1;
		}
		$('.carousel-nav .prev a').each(function(i){
			$(this).click(function(){
				var elmParent = $(this).parents('.carousel-nav');
				// handle scroll effect
				if (index[i] > 1){
					$(this).parent().siblings('.next').children('a').removeClass('disabled');
					elmParent.next().children('ul').animate({ left: (-divLength-margin-8) * (index[i] - 2) }, 500);
					elmParent.next().next().children('span').removeClass('active');

					index[i] = index[i] - 1;
					// handle 4 active dots
					for (var j = index[i] * 4 - 4; j < index[i] * 4; j++){
						elmParent.next().next().children('span').eq(j).addClass('active');
					}
				}
				if (index[i] == 1){
					$(this).addClass('disabled');
				}
				return false;
			});
		});
		$('.carousel-nav .next a').each(function(i){
			$(this).click(function(){
				var elmParent = $(this).parents('.carousel-nav');
				// handle scroll effect
				if (index[i] < groupNum){
					$(this).parent().siblings('.prev').children('a').removeClass('disabled');
					elmParent.next().children('ul').animate({ left: (-divLength-margin-8) * index[i] }, 500);
					elmParent.next().next().children('span').removeClass('active');
					index[i] = index[i] + 1;
					// handle 4 active dots
					for (var j = index[i] * 4 - 4; j < index[i] * 4; j++){
						elmParent.next().next().children('span').eq(j).addClass('active');
					}
				}
				if (index[i] == groupNum){
					$(this).addClass('disabled');
				}
				return false;
			});
		});
	};

	return {
		init : function(){
			tabsEvent();
			scrollEvent(3, 456, 23);
		}
	};
}(jQuery);
