var intervalId = 0;
jQuery(function(){
	jQuery('#slide1_content').cycle({fx:'fade',delay:-3000,pause:1,next:'.slide_next_btn',prev:'.slide_pre_btn'});jQuery('#slide2_content').cycle({fx:'fade',delay:-3000,pause:1,next:'.slide_next_btn',prev:'.slide_pre_btn'});
	// get store from ajax when click two button 
	jQuery("[rel*='ajax_gianhang']").click(function(){
				loadRandomStore();
				resetTimer();
				return false;
			});
	loadRandomStore();
	startTimer();
	
	// Get product by category (ajax) =======================================
	
	/*
	 * Random filter function
	 * Usage:
 	 * $.random(int);
 	 * $.randomBetween(min, max);
	 */
	jQuery.extend
	({
		random: function(X) {
		    return Math.floor(X * (Math.random() % 1));
		},
		randomBetween: function(MinV, MaxV) {
		  return MinV + jQuery.random(MaxV - MinV + 1);
		}
	});

	
	/*
	 * Load random category
	 */
	var tour = jQuery('a.tour_ajax');
	var tour_random = jQuery.randomBetween(0,tour.length-1);
	tour_random = tour[tour_random];
	jQuery(tour_random).addClass('selected');
	getProductByCatAjax(jQuery(tour_random).attr('rel'),jQuery('#tour'));
	
	var onlydalatvip = jQuery('a.onlydalatvip_ajax');
	var onlydalatvip_random = jQuery.randomBetween(0,onlydalatvip.length-1);
	onlydalatvip_random = onlydalatvip[onlydalatvip_random];
	jQuery(onlydalatvip_random).addClass('selected');
	getProductByCatAjax(jQuery(onlydalatvip_random).attr('rel'),jQuery('#onlydalatvip'));

	/*
	 * Handler onclick event
	 */
	jQuery('a.tour_ajax').click(function(){
		jQuery('a.tour_ajax').each(function(){
			if (jQuery(this).hasClass('selected'))
			{
				jQuery(this).removeClass('selected');
				return;
			}
		});
		jQuery(this).addClass('selected');
		//jQuery.cookie('tour_ajax',this.rel);
	    getProductByCatAjax(this.rel,jQuery('#tour'));
		return false;
	});
	
	jQuery('a.onlydalatvip_ajax').click(function(){
		jQuery('a.onlydalatvip_ajax').each(function(){
			if (jQuery(this).hasClass('selected'))
			{
				jQuery(this).removeClass('selected');
				return;
			}
		});
		jQuery(this).addClass('selected');
		//jQuery.cookie('onlydalatvip_ajax',this.rel);
	    getProductByCatAjax(this.rel,jQuery('#onlydalatvip'));
		return false;
	});
	
	/*
	var current_tour = jQuery.cookie('tour_ajax');
	if (current_tour != null)
	{
		jQuery('a.tour_ajax').each(function(){
				if (this.rel == current_tour)
				{
					jQuery(this).addClass('selected');
					getProductByCatAjax(this.rel,jQuery('#tour'));
				}
				else
				{
					jQuery(this).removeClass('selected');
				}
			});
	}
	else
	{
		jQuery('a.tour_ajax').each(function(){
			if (jQuery(this).hasClass('selected'))
			{
				getProductByCatAjax(this.rel,jQuery('#tour'));
				return;
			}
		});
	}
	
	var current_onlydalatvip = jQuery.cookie('onlydalatvip_ajax');
	if (current_onlydalatvip != null)
	{
		jQuery('a.onlydalatvip_ajax').each(function(){
				if (this.rel == current_onlydalatvip)
				{
					jQuery(this).addClass('selected');
					getProductByCatAjax(this.rel,jQuery('#onlydalatvip'));
				}
				else
				{
					jQuery(this).removeClass('selected');
				}
			});
	}
	else
	{
		jQuery('a.onlydalatvip_ajax').each(function(){
			if (jQuery(this).hasClass('selected'))
			{
				getProductByCatAjax(this.rel,jQuery('#onlydalatvip'));
				return;
			}
		});
	}
	*/
	// End Get product by category (ajax) =======================================
});

function startTimer()
{
	intervalId = setInterval('loadRandomStore()',10000);
}

function resetTimer()
{
	clearInterval(intervalId);
	startTimer();
}

function loadRandomStore()
{
	jQuery('#content').empty().html('<img src="http://dalatvip.com/skin/frontend/default/dalatvip/images/slideshow/loading.gif" />');
				jQuery.post('loader.php','',function(data){
						jQuery('#content').empty().fadeIn('fast',function(){jQuery('#content').html(data)});
				});
}

function getProductByCatAjax(catid, container)
{
	var loading_animate = jQuery('#loading-mask');
	if (loading_animate.css('display') == 'none')
		loading_animate.fadeIn('slow');
	jQuery.get('productsbycatid',{'catid': catid}, function(data){
		jQuery(container).empty().html(data);
		loading_animate.hide();
	});
}