jQuery(document).ready(function(){
	// AJAX tabs
	var _dataParsing = [];
	var _select = jQuery('div.pages select');
	var _curent = 1;
	var _itemLength;
	
	jQuery('ul.tabset a').click(function(){
		if (!jQuery(this).is('.active')) {
			var _this = jQuery(this);
			var _href = jQuery(this).attr('href');
			ajaxSend(_href, _this)
		}
		return false;
	});
	
	ajaxSend(jQuery('ul.tabset a:eq(0)').attr('href'), false);
	function ajaxSend(_href, _this) {
		jQuery.ajax({
			type: "GET",
			url: _href,
			dataType: (jQuery.browser.msie) ? 'text' : 'xml',
			beforeSend: function() {
				if (_this) {
					jQuery('ul.tabset a').removeClass('active');
					_this.addClass('active');
				}
				jQuery('div.xml-holder').css({opacity:0.2});
			},
			success: function(msg){
				var data;
				if (typeof msg == 'string') {
					data = new ActiveXObject( 'Microsoft.XMLDOM');
					data.async = false;
					data.loadXML(msg);
				} else {
					data = msg;
				}
				
				// parsing XML
				_itemLength = Math.ceil(jQuery('ITEM', data).length / 9);
				_select.empty();
				var _options = '';
				for (var i=0; i<_itemLength; i++) {
					_options += '<option value="'+(i+1)+'">'+(i+1)+'</option>'
				}
				jQuery(_select).html(_options);
				
				jQuery('ITEM', data).each(function(i, _item){
					var _itemlink = jQuery(_item).find('ITEMLINK').text();
					var _preview = jQuery(_item).find('PREVIEW').text();
					var _image = jQuery(_item).find('IMAGE').text();
					var _title = jQuery(_item).find('TITLE').text();
					var _price = jQuery(_item).find('PRICE').text();
					var _addtocartlink = jQuery(_item).find('ADDTOCARTLINK').text();
					_dataParsing[i] = '\
						<li> \
							<div class="item"> \
								<div class="photo"> \
									<a class="tooltip" href="'+_itemlink+'" title="'+_title+' '+_price+'"> \
										<img height="140" width="145" alt="" src="'+_preview+'"/> \
									</a> \
									<div style="color:#666666">'+_title+' <span style="color:#66cc33;">'+_price+'</span></div>\
								</div> \
								<a class="lightview zoomicon" href="'+_image+'">zoom</a> \
					';
					if(_addtocartlink!=''){
						_dataParsing[i] += '\
											<a class="cart" href="'+_addtocartlink+'">cart</a> \
											';	
					}
					_dataParsing[i] += '\
							</div> \
						</li> \
					';
					/* do't  implement
						<PICEXISTS ID="48887">1</PICEXISTS>
						<CURRENCYCODE>USD</CURRENCYCODE>
						<DESCRIPTION>Made in Japan this vintage mug is a real treasure.&#38;nbsp; No chips</DESCRIPTION>
						<STORE ID="9069">Anne's Attic</STORE>
						<LOCATION><![CDATA[Whiteville,&nbsp;Tennessee, United States]]></LOCATION>
						<CONDITION><![CDATA[Excellent]]></CONDITION>
						<PICKUP><![CDATA[]]></PICKUP>
						<LISTEDON><![CDATA[01/08/2009]]></LISTEDON>
					*/
				});
				showItems(1);
			}
		});
	}
	
	function showItems(_page) {
		_curent = _page;
		var _lastItem = 9 * _page;
		var _firstItem = _lastItem - 9;
		var _html = '';
		var _n = _firstItem;
		for (var i=0; i<3; i++) {
			_html += '<ul class="product">'
			for (var j=0; j<3; j++) {
				if (_dataParsing[_n]) {
					_html += _dataParsing[_n];
					_n++
				}
			}					
			_html += '</ul>'
		}
		
		jQuery('div.xml-holder').html(_html)
			.animate({opacity:1}, {duration:1000});
		
		try {
			jQuery('.tooltip', jQuery('div.tab')).tooltip();
		} catch (e) { return false; }
		try {
			var _images = [];
			// preload
			jQuery('a.zoom').each(function(i, _el) {
				_images[i] = new Image();
				_images[i].src = jQuery(this).attr('href');
			});
			jQuery('a.zoom').simpleLightbox({
				lightboxContentBlock:'div.img-popup',
				closeLink:'div.popup-foot a, a.close'
			});
		} catch (e) { return false; }
	}

	_select.change(function(){
		var _selected = jQuery(this).val();
		if (jQuery(this).val() != _curent) {
			jQuery('div.xml-holder').css('opacity',.2);
			_curent = parseInt(_selected);
			_select.find('option').removeAttr('selected');
			_select.find('option').eq(_selected-1).attr('selected','selected');
			showItems(_curent);
		}
	})
	jQuery('div.page a.prev').click(function(){
		if (_curent - 1 > 0) {_curent = _curent - 1;}
		else _curent = _itemLength;
		jQuery('div.xml-holder').css('opacity',.2);
		_select.find('option').removeAttr('selected');
		_select.find('option').eq(_curent-1).attr('selected','selected');
		showItems(_curent);
		return false;
	});
	jQuery('div.page a.next').click(function(){
		if (_curent + 1 <= _itemLength) {_curent = _curent + 1;}
		else _curent = 1;
		jQuery('div.xml-holder').css('opacity',.2);
		_select.find('option').removeAttr('selected');
		_select.find('option').eq(_curent-1).attr('selected','selected');
		showItems(_curent);
		return false;
	});
	
	// parsing Classified_XML
	ajaxSend_Classified_XML();
	function ajaxSend_Classified_XML() {
		var _href = window.location.toString();
		_href = _href.substr( 0,_href.lastIndexOf('/') ) +'/shopajax_xml_items.php?tab=Classifieds&limit=25';
		jQuery.ajax({
			type: "GET",
			url: _href,
			dataType: (jQuery.browser.msie) ? 'text' : 'xml',
			success: function(msg){
				var data;
				if (typeof msg == 'string') {
					data = new ActiveXObject( 'Microsoft.XMLDOM');
					data.async = false;
					data.loadXML(msg);
				} else {
					data = msg;
				}
				
				// parsing XML
				var classifiedXMLdata = '';
				jQuery('CLASSIFIED', data).each(function(i, _item){
					var _price = jQuery(_item).find('PRICE').text();
					var _condition = jQuery(_item).find('CONDITION').text();
					var _title = jQuery(_item).find('TITLE').text();
					var _location = jQuery(_item).find('LOCATION').text();
					var _classifiedlink = jQuery(_item).find('CLASSIFIEDLINK').text();
					var _picexists = parseInt(jQuery(_item).find('PICEXISTS').text());
					
					if (_picexists) _picexists = '<span><a href="#">pic</a></span>';
					else _picexists = '';
					classifiedXMLdata += '\
						<li> \
							<p> \
								'+_condition+' - \
								<a href="'+_classifiedlink+'">'+_title+' - '+_price+'</a> \
								- ('+_location+') \
								'+_picexists+' \
							</p> \
						</li> \
					';
					/* do't  implement
						<CLASSIFIED_HEADING>none</CLASSIFIED_HEADING>
						<LISTEDON><![CDATA[12/13/2008]]></LISTEDON>
					*/
				});
				jQuery('ul.scrolling').html(classifiedXMLdata);
				jQuery('ul.scrolling li:odd').addClass('light');
				if (typeof(initScrollbars) == 'function') {
					initScrollbars();
				}
			}
		});
	};
});