if ($) {
	var tab = $(".tab-vrmsearch");

	var xhr;
	var getVRMfeed = function($search, $last){
		if(xhr){
			xhr.abort();
		}
		xhr = $.post("", {'vrm_action': 'getfeed','last': $last, 'search': $search}, function(json){
			if(json.status){
				if('' !== json.html){
					$(".vrmsearch-timelines-ajax").append(json.html);
				} else{
					alert("No result found!");	
				}
				tab.find(".tab-container").height("auto");
			} else{
				alert("Something wonrg!");
			}
		}, 'json');
	};
	getVRMfeed();

	$(document).ready(function () {
		$('.u-SearchVrmsearch')
		/*.on('keyup', function () {
			var $this = $(this),
					$search = $this.parents(".control-search"),
					tab_container = $this.parents(".tab-container"),
					feeditems = tab,
					$val = $this.val();
			
			feeditem = feeditems.find(".vrmsearch-timeline-items");		
			feeditem.alterClass('found_*');
					
			var ele_feedsummary = feeditem.find(".vrmsearch-timeline-summary"),
					ele_feedresult = feeditem.find(".vrmsearch-timeline-result");

			if ("" !== $val) {
				//console.log("val", $val);
				var explodedval = $val.split(",");
				$this.parents(".option-container").find(".c-BtnClear").show();
				feeditem.removeClass("c-contact-found");

				feeditem.hide();
				
				console.log("explodedval", explodedval);
				if(explodedval.length > 1){
					var feedsummary = new Array();
					var feedresult = new Array();
					var $classFounded = "";
					$.each(explodedval, function(i, val){
						var $val = val.trim().replace(/ +/g, "").toLowerCase();
						if("" !== $val){
							$class = "found_"+i;
							$classFounded += "."+$class;
							console.info("val",val.trim().replace(/ +/g, "").toLowerCase());
							feedsummary[i] = ele_feedsummary.filter(function () {
								var string = $(this).text().trim().replace(/ +/g, "").toLowerCase();
								if (string.indexOf($val) > -1) {
									$(this).parents(".vrmsearch-timeline-items").addClass($class);
									return $(this).addClass("found");
								}
							});
							feedresult[i] = ele_feedresult.filter(function () {
								var string = $(this).text().trim().replace(/ +/g, "").toLowerCase();
								if (string.indexOf($val) > -1) {
									$(this).parents(".vrmsearch-timeline-items").addClass($class);
									return $(this).addClass("found");
								}
							});
						}
						
					});

					console.log("$classFounded",$classFounded);
					console.log(tab_container.find(".vrmsearch-timeline-items"+$classFounded));
					tab_container.find(".vrmsearch-timeline-items"+$classFounded).show();

					
				} else{
					var feedsummary = ele_feedsummary.filter(function () {
						var string = $(this).text().trim().replace(/ +/g, "").toLowerCase();
						if (string.indexOf($val.trim().replace(/ +/g, "").toLowerCase()) > -1) {
							return $(this);
						}
					});
					var feedresult = ele_feedresult.filter(function () {
						var string = $(this).text().trim().replace(/ +/g, "").toLowerCase();
						if (string.indexOf($val.trim().replace(/ +/g, "").toLowerCase()) > -1) {
							return $(this);
						}
					});
					
					
					if (feedsummary.length) {
						feedsummary.parents(".vrmsearch-timeline-items").show();
					}
					if (feedresult.length) {
						feedresult.parents(".vrmsearch-timeline-items").show();
					}
				}
			} else {
				tab_container.find(".vrmsearch-timeline-items").show();
			}
			tab_container.height("auto");
		});*/
		.on('keyup', function () {
			var $this = $(this),
					$search = $this.parents(".control-search"),
					tab_container = $this.parents(".tab-container"),
					feeditems = tab,
					$val = $.trim($this.val());

			$(".vrmsearch-timelines-ajax").html('');
			if ("" !== $val) {
				getVRMfeed($val);
			} else {
				getVRMfeed();
			}
			tab_container.height("auto");
		});


		$(".tab-vrmsearch")
				.on("click", ".vrmsearch-timeline-item", function () {
					var $this = $(this),
							$parents = $this.parents(".vrmsearch-timeline-items"),
							$icon = $parents.find(".vrmsearch-timeline-item-open");
					
					if($parents.hasClass("action-open")){
						$icon.removeClass("icon-dropdown2").addClass("icon-dropdown");
						$parents.find(".vrmsearch-timeline-action").slideUp(function(){
							$parents.removeClass("action-open");
							tab.find(".tab-container").height("auto");
							soothScroll($parents, -220);
						});
					} else {
						$icon.removeClass("icon-dropdown").addClass("icon-dropdown2");
						$parents.find(".vrmsearch-timeline-action").slideDown(function(){
							$parents.addClass("action-open");
							tab.find(".tab-container").height("auto");
							soothScroll($parents, -220);
						});
					}
					return false;
				})
				.on("click", ".vrmsearch-timeline-action-button", function () {
					var $this = $(this),
							$parents = $this.parents(".vrmsearch-timeline-items");
					$parents.find(".vrmsearch-timeline-item").trigger("click");
					return false;
				})
				.on("click", ".u-More-VrmTimelines", function () {
					var $timeline = $(".vrmsearch-timeline"),
						$search = $.trim($('.u-SearchVrmsearch').val());
					if($timeline.length){
						var item = $timeline.last().find(".vrmsearch-timeline-items").last();
						if(item.length){
							var $lastid = item.attr("data-id");
							getVRMfeed($search, $lastid);
						} else{
							getVRMfeed($search);
						}
					} else{
						alert("No vrm timeline found!");
					}
					return false;
				})
				.on("click", ".vrmsearch-timeline-items-new-button", function () {
					if($arrVRM.items.length > 0){
						var items = $arrVRM.items;
						$arrVRM.items = [];
						$.each(items, function(i, item) {
					      	addItem(item);
					    });
					}
					return false;
				});
	});


	$arrVRM = [];
	$arrVRM.items = [];
	$arrVRM.latest = false;
	var xhrLatest;

	var getlatest = function(){
		if(xhrLatest){
			xhrLatest.abort();
		}

		$latest = 0;
		//console.log($arrVRM);
		if(false !== $arrVRM.latest){
			$latest = $arrVRM.latest;
		} else {
			$latestItem = $(".vrmsearch-timeline").first().find(".vrmsearch-timeline-items").first();
			if($latestItem.length){
				$latest = $latestItem.attr("data-id");
			}
		}
		//console.log("$latest", $latest);

		xhrLatest = $.post("", {'vrm_action': 'getlatest','id': $latest}, function(json){
			if(json.status){
				$.each(json.item, function(i, item) {
					//console.log('push',i);
			        $arrVRM.items.push(item);
			    });
			    if(json.latest > 0){
					$arrVRM.latest = json.latest;
				}

				$(".u-NewVRM-Numbers").html($arrVRM.items.length);
				if($arrVRM.items.length > 0){
					$(".vrmsearch-timeline-items-new").show();
				} else {
					$(".vrmsearch-timeline-items-new").hide();
				}
			} else {
				//$(".vrmsearch-timeline-items-new").hide();
				//$(".u-NewVRM-Numbers").html("0");
			}
		}, 'json');
	};

	setInterval(function(){
		getlatest();
	}, 5000);


	var addItem = function($append){
		$(".u-NewVRM-Numbers").html($arrVRM.items.length);
		if($arrVRM.items.length > 0){
			$(".vrmsearch-timeline-items-new").show();
		} else {
			$(".vrmsearch-timeline-items-new").hide();
		}

		var $timeline = tab.find(".vrmsearch-timeline").first(),
			$timelineBlock = $timeline.find(".vrmsearch-timeline-items-block"),
			$template = $append;

			var $tmp = $($template).addClass("new-added"),
				$id = $tmp.attr("data-id");
			//console.info('add',$id);	
			$timelineBlock.prepend($tmp);

			var $element = $timelineBlock.find("[data-id="+$id+"]");
			
			height = 100;
			if($(window).width() <= 430){
				height = 73;
			}

			$element
				.css({'opacity': 0, 'height': 0})
				.animate(
					{height:height}, 
					{duration: 500, easing: 'easeOutQuad'})
				.animate({ 'height': '100%'})
				.animate(
				    { 'opacity': 1},
				    { duration: 'slow' });
	};

	setInterval(function(){
		if($arrVRM.items.length > 0){
			var lastEl = $arrVRM.items.shift();
			addItem(lastEl);
		}
	}, 3000);
}
