$(function(){	
	var azCScroll = {
		xmlsrc: azCScroll_fetch,
		data: new Array(),
		imgpath: '',
		width: '',
		height: '',
		visible: 5,
		scroll: 1,
		speed: 500,
		timer: null,
		total: 0
	};
	azCScroll_getRec = function(){
		$.ajax({
			url: azCScroll.xmlsrc,
			type: "get",
			dataType: "xml",
			data: "",
			error: function(a,b,c){
				$("#az_cat_scroller_pane").html('<span class="az_cat_scroller_err">Error loading XML data!</span>');
			},
			success: function(xml){
				azcsHTML = '<a id="az_cat_scroller_prev" class="az_scroller_prev">&nbsp;</a>';
				azcsHTML += '<a id="az_cat_scroller_next" class="az_scroller_next">&nbsp;</a>';
				azcsHTML += '<div id="az_cat_scroller"></div>';
				$("#az_cat_scroller_pane").html(azcsHTML);
				
				$('#az_cat_scroller').addClass("az_cat_scroller_loading")
				
				var listing = $(xml).find("categorylist");
				
				azCScroll.total = $("category", xml).length;
				azCScroll.width = listing.attr("width");
				azCScroll.height = listing.attr("height");
				azCScroll.imgpath = listing.attr("path");
				
				$("category", xml).each(function(i){
					azCScroll.data[i] = new Array();
					azCScroll.data[i]['name'] = $("name", this).text() ? $("name", this).text() : "";
					azCScroll.data[i]['image'] = $("image", this).text() ? $("image", this).text() : "";
					azCScroll.data[i]['url'] = $("url", this).text() ? $("url", this).text() : "";
					$(new Image())
						.attr({src: azCScroll.imgpath + $("image", this).text()})
						.load(function(){});
				});
				
				azCScroll_build();
			}
		});
	};	
	
	azCScroll_build = function() {
		var dimension = (azCScroll.width ? 'width="' + azCScroll.width + '" ' : '') + (azCScroll.height ? 'height="' + azCScroll.height + '" ' : '');
		var azcsHTML = '';
		
		azcsHTML = '<ul>' + "\n";
		$(azCScroll.data).each(function(i, data){
			imgpath = azCScroll.imgpath + data['image'];
			azcsHTML += '<li><a href="' + data['url'] + '" title="' + data['name'] + '">';
			azcsHTML += '<img src="' + imgpath + '" alt="' + data['name'] + '" ' + dimension + '>';
			azcsHTML += '<span class="az_trans_bg"></span><span class="az_cat_title">' + data['name'] + '</span>';
			azcsHTML += '</a></li>' + "\n";
		});
		azcsHTML += '</ul>';

		$("#az_cat_scroller").html(azcsHTML);
		
		jQuery('a', "#az_cat_scroller").css('visibility', 'hidden')
		
		$('#az_cat_scroller_prev').addClass("az_scroller_prev_inactive");
		if(azCScroll.total <= azCScroll.visible) {
			$('#az_cat_scroller_next').addClass("az_scroller_next_inactive");
		}
			
		azCScroll.timer = setTimeout(azCScroll_init, 200)
	};
	
	azCScroll_init = function() {
		var imgObj = $('img:eq(' + (azCScroll.visible-1) + ')', '#az_cat_scroller')
		
		clearTimeout(azCScroll.timer)
							 
		if( (imgObj.width() > 0 && imgObj.height() > 0) && ((imgObj.height() == azCScroll.height) && (imgObj.width() == azCScroll.width)) && (imgObj.attr( "complete" )) ) {
			if($.browser.msie && imgObj.attr( "complete" )==false) { // IE fix (for some reason IE freakout without this test)
				azCScroll.timer = setTimeout(azCScroll_init, 200)
			} else {			
				jQuery('a', "#az_cat_scroller").css('visibility', 'visible')
				
				if($('#az_cat_scroller').hasClass("az_cat_scroller_loading")) {
					$('#az_cat_scroller').removeClass("az_cat_scroller_loading")
				}
				
				jQuery("#az_cat_scroller").jCLite({
					btnNext: "#az_cat_scroller_next",
					btnPrev: "#az_cat_scroller_prev",
					circular: false,
					speed: azCScroll.speed,
					visible: azCScroll.visible,
					scroll: azCScroll.scroll,
					beforeStart: function(a) {},
					afterEnd: function(a) {
						if($('li', '#az_cat_scroller').index(a) < (azCScroll.total-azCScroll.visible)) {
							if($('#az_cat_scroller_next').hasClass("az_scroller_next_inactive")) {
								$('#az_cat_scroller_next').removeClass("az_scroller_next_inactive")
							}
						} else {
							$('#az_cat_scroller_next').addClass("az_scroller_next_inactive")
						}
						if($('li', '#az_cat_scroller').index(a) > 0) {
							if($('#az_cat_scroller_prev').hasClass("az_scroller_prev_inactive")) {
								$('#az_cat_scroller_prev').removeClass("az_scroller_prev_inactive")
							}
						} else {
							$('#az_cat_scroller_prev').addClass("az_scroller_prev_inactive")
						}
					}
				});			
			}
		}else{
			azCScroll.timer = setTimeout(azCScroll_init, 200)
		}
		
		//});
	};
		
	azCScroll_getRec();
});

