var categoriesTimeOut;
$(document).ready(function(){
	$('#tab_menu li a').click(function(){
		$(this).tabchange();
		return false;
	});
	
	//set position for the category list
	$('#categories').each(function(){
		var menu_left = $('#show_cat').offset().left,
			offset_top = $('#show_cat').offset().top,
			list_width = $('#categories').width(),
			pos_left = $('.header_main').offset().left;
			pos_right = $('.header_main').offset().left + 998,
			list_position = menu_left - ( list_width /2 );
			
		if ( list_position < pos_left )
			list_position = pos_left;
			
		if ( (list_position + list_width) > pos_right )
			list_position = pos_right - list_width;
			
		$(this).css('left', list_position).css('top', offset_top + 53);
	});
	
	$.AutoSearch({
		form: '#searchform',
		textbox: '#key',
		category: '#cate',
		button: '#submit_search',
		resultWrap: '#autosearch_result',
		resultItemClass: '.publish',
		ajaxUrl: jQuery('meta[name=termUrl]').attr('content')
	});
	
	//slider hover	
	if ( jQuery( '.slide-item' ).length > 0 )
	{
		jQuery('.slide-item').hover(function(){
			jQuery(this).find('.slide-desc').animate({height : '+=33'});
		}, function(){
			jQuery(this).find('.slide-desc').animate({height : '-=33'});
		});
	}
	
	// category list toogle
	jQuery('#show_cat .red_left').hover(function(){
		var categoryid = '#categories';
		jQuery(categoryid).fadeIn();
		$('#arrow_cat').fadeIn();
		jQuery(categoryid).each(function(){
			var element = this;
			
			jQuery(this).fadeIn();
			$('#arrow_cat').fadeIn();
			
		});
	}, function(){
		categoriesTimeOut = setTimeout( function(){
				$('#categories').fadeOut();
				$('#arrow_cat').fadeOut();
			}, 2000);
	} );
	
	jQuery('#categories').hover(function(){
		clearTimeout(categoriesTimeOut );
	}, function(){
		$(this).fadeOut();
		$('#arrow_cat').fadeOut();
	});
	
	
});

var slider = function( target, distance ) {	
	var sld = {
		current: jQuery( target ),
		distance: 230,
		init: function(){
			var elements  = sld.current.find('li');
			sld.current.css('width', elements.length * 230);
		},
		moveLeft: function(){
			var elements  = sld.current.find('li'),
				last = sld.current.find('li:last');
			sld.current.prepend(last);
			sld.current.css('left', sld.current.position().left - sld.distance);
			sld.current.animate( { left : '+=' + sld.distance } );			
		},
		moveRight: function(){
			var elements  = sld.current.find('li'),
				first = sld.current.find('li:first');
			sld.current.animate( { left : '-=' + sld.distance } , {
				complete: function(){
					sld.current.append(first);
					sld.current.css('left', sld.current.position().left + sld.distance);
				}
			} );
		}
	}
	return sld;
}

$.fn.priceDisplay = function(options){
	
};

$.fn.sliceAds = function(options){
	var def = { direct: 'left', distance : '230' },
	opt = jQuery.extend( def, options ),
	current = jQuery(this),
	move = '+=' + opt.distance ;
	
	if ( opt.direct == 'left')
		move = '-=' + opt.distance;
	else
		move = '+=' + opt.distance;
	
	current.animate({left: move});
}

$.fn.moveLeft = function(dist){
	$(this).sliceAds( {direct : 'left', distance: dist} );
	return false;
}

$.fn.moveRight = function(dist){
	$(this).sliceAds( {direct : 'right', distance: dist} );
	return false;
}

$.fn.tabchange = function(){
	var current = $(this),
		container = current.parent(),
		menu = $('#tab_menu'),
		sections = $('.content_box'),
		target = current.attr('href');
	var value = current.attr( 'rel' );
	
	menu.find('li').removeClass('selected');
	container.addClass('selected');
	sections.hide();
	$(target).show();
}

$.validate = function( input , type ){
	switch ( type )
	{
		case 'email':
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			if ( reg.test(input) == false )
				return false;
			break;
		
		case 'empty':
			if (input == '')
				return false;
			break;
	}
	return true;
}

$.statictisc = function(){
	var graph = {
		category : [''],
		data : [0],
		init: function(arg){
			var def = { category: [''], data: [0] },
			options = $.extend( def, arg );
			category = options.category;
			data = options.data;
		},
		showGraph: function(){
			var graph = new Highcharts.Chart({
			});
		}
	}
	return graph;
}
