function ie() {
	return (navigator.appVersion.indexOf('MSIE')==-1) ? false : true;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function Linkify(inputText) {
   
   //improved
   
    var replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
    var replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');

	if (inputText != replacedText) {
		
		return replacedText;
		exit;
	
	}
	
    
    var replacePattern2 = /(\bwww.[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
    var replacedText = replacedText.replace(replacePattern2, '<a href="http://$1" target="_blank">$1</a>');

    if (inputText != replacedText) {
		
		return replacedText;
		exit;
	
	}
	
    var replacePattern3 = /([a-zA-Z0-9\_\.\-]+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;
    var replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');

    return replacedText
}

var pri_cat
var serv_cat
var bart_cat
var lastab = 0
var allc
var ldn

$(document).ready(function()  {
	
	$("#search").autocomplete("/suggest.php",{cache: false, minChars: 2, mustMatch: false, autoFill: false, selectFirst: false});
	
	$("#promo-img").click(function() {
		$("#promo-video").html("<embed width=\"577\" height=\"400\" flashvars=\"width=577&amp;height=400\" wmode=\"opaque\" salign=\"tl\" allowscriptaccess=\"never\" allowfullscreen=\"true\" scale=\"scale\" quality=\"high\" bgcolor=\"#FFFFFF\" src=\"http://www.youtube.com/v/5FmWixDqIN8?version=3&amp;autohide=1&amp;autoplay=1\" type=\"application/x-shockwave-flash\">");
	});
	
	$('.tab-line').find('span:first').css('background-position','top left').find('a').css('color','#fe7200');
	$('.tab-line').find('li:first').css('background-position','top right');
	
	$("#categories .maincat").after("<div class=\"subhover\"></div>");
	
	$("#tab-1").click(function() {
		$("#search_type").val("sell");
		if (lastab != 1 && lastab != 2)
		$("#catbox").html(allc+pri_cat);
		lastab = 1;
	});
	
	$("#tab-2").click(function() {
		$("#search_type").val("buy");
		if (lastab != 1 && lastab != 2)
		$("#catbox").html(allc+pri_cat);
		lastab = 2;
	});
	
	$("#tab-4").click(function() {
		
		if (lastab != 4 && lastab != 5) {
			$("#search_type").val("services");
			$("#catbox").html(allc+serv_cat);
		}
		lastab = 4;
	});
	
	$("#tab-5").click(function() {
		
		if (lastab != 4 && lastab != 5) {
			$("#search_type").val("orders");
			$("#catbox").html(allc+serv_cat);
		}
		lastab = 5;
	});
	
	$("#tab-3").click(function() {
		lastab = 3;
		$("#search_type").val("barter");
		$("#catbox").html(allc+bart_cat);
	});
	

	$("#categories li").hover(function() {
		$(this).find(".subhover").show();
		$(this).find(".subnav-top").show();
		$(this).find(".subnav").show();
		//$(this).find("a:first").css('color','#fe7200');
		$(this).hover(function() {
	}, function(){	
			$(this).find(".subhover").hide();
			$(this).find(".subnav").hide();
			$(this).find(".subnav-top").hide();
		//	$(this).find("a:first").css('color','#3f4040');
		});
	});
		
	$("#subcategories li a").after("<div class=\"subsubhover\"></div>");
	$("#subcategories li").hover(function() {
		$(this).find(".subsubhover").show();
		$(this).hover(function() {
	}, function(){	
		$(this).find(".subsubhover").hide();
	});
	});
		
	$("#menu li").hover(function() {
		$(this).find(".menu-top").show();
		$(this).find(".menu-middle").show();
		$(this).find(".main-menu").css('background','url("/img/bg-menu.gif") no-repeat');
		$(this).hover(function() {
	}, function(){	
		if (!ie()) {
		$(this).find(".menu-top").hide();
		$(this).find(".menu-middle").hide(); } else {
		$(this).find(".menu-top").hide();
		$(this).find(".menu-middle").hide();		
		}
		$(this).find(".main-menu").css('background','none');
	});
	});
	
	$("#selng").click(function() {
			var lng = $(this).find("#lng-choose");
			lng.is(":hidden") ? lng.show() : lng.hide();
	});
	
  
	//if (!ie) 
	$("#lng-choose").hover(function() {
		$(this).hover(function() {
		}, function() {	
				if (!ie()) $(this).hide();
		});
	});
	
	
	$(".tab-line a").click(function() {
		$(this).parents('.tab-line').find('span').css('background-position','bottom left').find('a').css('color','#3f4040');
		$(this).parents('.tab-line').find('li').css('background-position','bottom right');
		$(this).parents('li').find('span').css('background-position','top left').find('a').css('color','#fe7200');
		$(this).parents('li').css('background-position','top right');
		if (($(this).attr("id") != 'tab-photos') && ($(this).attr("id") != 'tab-videos')) {
			var tabstype = "";
			switch($(this).attr("id").length) {
				case 5: tabstype = "search-tabs"; break;
				case 6: tabstype = "newest-tabs"; break;
				case 7: tabstype = "popular-tabs"; break;
				case 8: tabstype = "pricecut-tabs"; break;
			}
			createCookie(tabstype,$(this).attr("id"),365);
		}
	});
	
	$(".promo-vid").click(function() {
		location.href="http://youtube.com";
	});
	/*
	$(".tab-line li").click(function() {
		$(this).parents('.tab-line').find('span').css('background-position','bottom left').find('a').css('color','#3f4040');
		$(this).parents('.tab-line').find('li').css('background-position','bottom right');
		
		$(this).find('span').css('background-position','top left').find('a').css('color','#fe7200');
		$(this).css('background-position','top right');
		if (($(this).find('a').attr("id") != 'tab-photos') && ($(this).find('a').attr("id") != 'tab-videos')) {
			var tabstype = "";
			switch($(this).attr("id").length) {
				case 5: tabstype = "search-tabs"; break;
				case 6: tabstype = "newest-tabs"; break;
				case 7: tabstype = "popular-tabs"; break;
				case 8: tabstype = "pricecut-tabs"; break;
			}
			createCookie(tabstype,$(this).attr("id"),365);
		}
	});*/
	
	$('.search-button').click(function() {
		if ($(this).attr("id") != "tab-photos" && $(this).attr("id") != "tab-videos")
		$('input[type="text"]:first').focus();
	});
	
	$(".login input[type=\"textbox\"], .login input[type=\"password\"]").focus(function() {
		$(this).val('');
	});
	
	if (readCookie("search-tabs") == "null") {
		$('#tab-1').click();
	} else $('#'+readCookie("search-tabs")).click();
	
	
	$("form").submit(function() {
		$("input[type=\'submit\']").attr("disabled", "true");
	});
	
	
	$('.message').each(function() {
		var ht = $(this).html();
		$(this).html(Linkify(ht));
  });

	$('.desc').each(function() {
		var ldn = $(this).html();
		$(this).html(Linkify(ldn));
  });
  
	
	//$('input[type="text"]:first').focus();
});
