function image_show(url) {
	window.open(url, "bigimage", "status=0,toolbar=0");
}

function urlDecode(string, overwrite){
	if(!string || !string.length){
		return {};
	}
	var obj = {};
	var pairs = string.split('&');
	var pair, name, value;
	var lsRegExp = /\+/g;
	for(var i = 0, len = pairs.length; i < len; i++){
		pair = pairs[i].split('=');
		name = unescape(pair[0]);
		value = unescape(pair[1]).replace(lsRegExp, " ");
		//value = decodeURIComponent(pair[1]).replace(lsRegExp, " ");
		if(overwrite !== true){
			if(typeof obj[name] == "undefined"){
				obj[name] = value;
			}else if(typeof obj[name] == "string"){
				obj[name] = [obj[name]];
				obj[name].push(value);
			}else{
				obj[name].push(value);
			}
		}else{
			obj[name] = value;
		}
	}
	return obj;
}

function jsGet(param)
{
	var wl = window.location.href;
	var params = urlDecode(wl.substring(wl.indexOf("?")+1));
	return(params[param]);
}

$(document).ready(function() {
	var AJAX = 0;
	var arr = location.href.split('/');
	arr.shift(); arr.shift(); arr.shift();
	var lang = 'en';
	switch (arr[0]) {
		case 'en':
		case 'es':
		case 'pt':
			lang = arr[0];
			arr.shift();
	}
	var qs = arr.join('/');
	$("#select_language option[value="+lang+"]").attr("selected", "selected");
	$("#select_language").keypress(function() {
		$(this).change();
	});
	$("a[href$='.jpg']").click(function() {
		image_show($(this).attr("href"));
		return false;
	});
	if (!$("input[value='Others']").attr("checked")) {
		$("input[name='payothers']").hide();
	}
	$("input[value='Others']").change(function() {
		if ($(this).attr("checked")) {
			$("input[name='payothers']").show();
		} else {
			$("input[name='payothers']").hide();
		}
	});
	$(".del").click(function() {
		return confirm("Are you sure?");
	});
	$("#select_language").change(function() {
		if ($(this).attr("value") == "gt") {
			location.href="http://translate.google.com/translate?js=y&ie=UTF-8&sl=en&tl=fr&swap=1&u="+"http://www.eurekaclick.com/en/"+qs;
		} else {
			location.href="/"+$(this).attr("value")+"/"+qs;
		}
	});
	$("#search #question img").hover(function() {
		$("#search #help").show();
	},function() {
		$("#search #help").hide();
	});
	var type = "products";
	if (jsGet("type") != undefined && (jsGet("type") == "products" || jsGet("type") == "companies" || jsGet("type") == "buyleads")) {
		type = jsGet("type");
	}
	$("#search a[name='"+type+"']").parent().addClass("active");
	$("#search input[name='type']").attr("value", type);
	$("#search ul#types a").click(function() {
		$("#search ul#types li").removeClass("active");
		$(this).parent().addClass("active");
		$("#search input[name='type']").attr("value", $(this).attr("name"));
		return false;
	});
	$("ul.tabs a").click(function() {
		$("ul.tabs li").removeClass("active");
		$(this).parent().addClass("active");
		var type = $(this).attr("name");
		$(".listcat a").attr("href", function() {
			return $(this).attr("href").replace(/category\/[^\/]*\//, 'category/'+type+'/');
		});
		return false;
	});
	$("a.simple").click(function() {
		window.open($(this).attr("href"), "simple", "status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=1,width=400,height=500");
		return false;
	});
	$(".cat select").live("keypress", function() {
		$(this).change();
	});
	$(".cat select").live("click", function() {
		$(this).change();
	});
	$(".cat select").live("change", function() {
		var n = parseInt($(this).attr("id").replace(/c/, ''));
		var m = $(this).find("option:selected").attr("value");
		for (var k = n+1; $("#c"+k).size() != 0; k++) {
			$("#c"+k).remove();
		}
		$(this).attr("name", "cat");
		if ($(this).find("option:selected").html() != '-' && window.catgraph[m].length > 0) {
			n++;
			$(this).attr("name", "");
			var s = "<select id=\"c"+n+"\" name=\"cat\">\n";
			s+= "<option value=\""+m+"\">-</option>";
			for (var i = 0; i < window.catgraph[m].length; i++) {
				s+= "<option value=\""+window.catgraph[m][i]+"\">";
				s+= window.catnames[window.catgraph[m][i]];
				s+= "</option>\n";
			}
			s+= "</select>\n";
			$(this).parent().append(s);
			if ($.browser.msie) {
					$("#c"+n).selecteSizer();
			}
		}
	});
	/*$(".value").prev().example(function() {
		return $(this).next().html();
	});*/
	$("input.numeric").keypress(function (e) {
		if (document.all) {
			t = e.keyCode;
		} else {
			t = e.which;
		}
		if (48 <= t && t <= 57) {
			return true;
		}
		if (t == 8 || t == 0) {
			return true;
		}
		return false;
	});
	$("input.permalink").keypress(function (e) {
		if (document.all) {
			t = e.keyCode;
		} else {
			t = e.which;
		}
		if (48 <= t && t <= 57) {
			return true;
		}
		if (97 <= t && t <= 122) {
			return true;
		}
		if (t == 45 || t == 46 || t == 8 || t == 0) {
			return true;
		}
		return false;
	});
	$("li.multilang textarea").css("display", "none");
	$("li.multilang textarea[lang='en']").css("display", "inline");
	$("li.multilang .buttons a[lang='en']").addClass("active");
	$("li.multilang .buttons a").click(function() {
		li = $(this).parent().parent().parent();
		li.find("textarea").css("display", "none");
		li.find(".buttons a").removeClass("active");
		li.find("textarea[lang='"+$(this).attr("lang")+"']").css("display", "inline");
		$(this).addClass("active");
		return false;
	});
	$(".product p[lang]").hide();
	$(".product").each(function() {
		$(this).find("p[lang]:first").show();
		$(this).find(".langlinks a:first").addClass("active");
	});
	$(".product .langlinks a").click(function() {
		var p = 1;
		if (location.href.match(/certificate/) || location.href.match(/search/)) {
			p = 0;
		}
		if (p) {
			$(this).parent().parent().parent().parent().find("p[lang]").hide();
			$(this).parent().parent().parent().parent().find("p[lang='"+$(this).attr("lang")+"']").show();
		} else {
			$(this).parent().parent().parent().find("p[lang]").hide();
			$(this).parent().parent().parent().find("p[lang='"+$(this).attr("lang")+"']").show();
		}
		$(this).parent().find("a").removeClass("active");
		$(this).addClass("active");
		return false;
	});
	$("img.bookmark").click(function() {
		var un = "un";
		if ($(this).attr("src").match(/stard/)) {
			un = "";
		}
		var iid = $(this).parent().find("input[type=hidden]").attr("value");
		var type = 'c';
		if (location.href.match(/\/products/)) {
			type = 'p';
		} else if (location.href.match(/\/buyleads/)) {
			type = 'b';
		}
		var th = $(this);
		$.get("/"+un+"favorite/"+type+"/"+iid, function(data) {
			if (data == "ok") {
				if (un == "un") {
					th.attr("src", th.attr("src").replace(/star/, 'stard'));
				} else {
					th.attr("src", th.attr("src").replace(/stard/, 'star'));
				}
			}
		});
	});
	$(".ibox img").hide();
	$(".iimg a:first-child img").show();
	$(".iimg > img:first-child").show();
	$(".ilinks a:first-child").addClass("active");
	$(".ilinks a").click(function() {
		$(this).parent().find("a").removeClass("active");
		$(this).parent().parent().find("img").hide();
		var n = $(this).attr("href").replace(/#/, '');
		$(this).parent().parent().find("img[alt='"+n+"']").show();
		$(this).addClass("active");
		return false;
	});
	$("a input.submit").click(function() {
		location.href = $(this).parent().attr("href");
		return false;
	});
	jQuery.each(jQuery.browser, function(i) {
		if ($.browser.mozilla){
			$(".loginbox input.text").css("padding-top", "3px");
			$(".changepass input.text").css("padding-top", "3px");
		}
	});

	$(".scard a").each(function() {
		var hr = $(this).attr("href").replace(/^/, '/count');
		if (!hr.match(/#/) && !$(this).attr("href").match(/^skype:/)) {
			$(this).attr("href", hr);
		}
	});

	$(".cpmessage .bluebox").css("display", "none");
	$(".cpmessage .langlinks [lang=en]").addClass("sel");
	$(".cpmessage .bluebox[lang=en]").css("display", "block");
	$(".cpmessage .langlinks a").click(function() {
		$(".cpmessage .langlinks a").removeClass("sel");
		$(this).addClass("sel");
		var l = $(this).attr("lang");
		$(".cpmessage .bluebox").css("display", "none");
		$(".cpmessage .bluebox[lang="+l+"]").css("display", "block");
		return false;
	});

	if (location.href.match(/products\/show/)) {
		$(".iimg img").css("cursor", "pointer");
		$(".iimg img").click(function() {
			image_show($(this).attr("src").replace(/upload\/o/, "upload/b"));
		});
	}

	if (AJAX) {
		$(".tnav a").click(function() {
			$(".tnav li").removeClass("active");
			$(this).parent().addClass("active");
			$(".tcontent").html('<p class="loading">Loading...</p>');
			$.get($(this).attr("href")+"/text", function(data) {
				$(".tcontent").html(data);
			});
			return false;
		});
	}

	if ($.browser.msie) {
		$(".multilang.small").css("width", "354px");
		$(".multilang.small").css("height", "120px");
		$(".multilang.small .buttons").css("right", "4px");
		$(".multilang.small textarea").css("width", "344px");
		$(".multilang.small textarea").css("height", "90px");
		$(".prod.addphoto").css("width", "238px");
		$(".info.addphoto").css("display", "block");
		$(".info.addphoto").css("clear", "both");
		$(".prod.addphoto").css("float", "right");
		$(".prod.addphoto").css("text-align", "center");
		$(".prod.addphoto input").css("margin-top", "3px");
		$(".scard.active").css("border", "solid 2px #0284c9");
		$(".scard.active").css("padding", "9px 24px");
	}
});


