var currentURL = String(document.location);
// Check if in inner directory or directory page
if (currentURL.indexOf("/",jsBaseURL.length) >0)
	var checkURL = currentURL.substring(0,(currentURL.indexOf("/",jsBaseURL.length)+1));
else
	var checkURL = currentURL;

$(document).ready(function() {
	$("a.topTab[href='"+checkURL+"']").each(function() {
		$(this).toggleClass("topTabSelected");
	});
	
	if(checkURL!=jsBaseURL){ // check if we're in inner page
		
		// get all index products
		$.ajax({
		   type: "POST",
		   url: jsBaseURL+'includes/product_popup.php?method=get_product_to_popup',
		   data: '',
		   success: function(msg){
		   		ids = msg.split(',');
		   		$("a[id][href*='/server/adv.php?gid=']").each(function () {
					var originalLink = $(this).attr('href');
					var linkDetails = /gid=(.*)/i.exec(originalLink);
					var id = $(this).attr('id');
					id = /id(.*)_([0-9]+)/i.exec(id); // extract link's id
					var obj = $(this);
					if(jQuery.inArray(id[2], ids)!=-1){ // check if the link's id is in the popup product's list
						obj.bind('click', function() { // attach the showOffer function
							showOffer (id[2],linkDetails[1]); 
							return false;
						});
					}
				}); // end of .each()
		   }
		}); // end of $.ajax
	}
	
	function showOffer(id,Link){
		if (!$("#dimmer").length > 0){
			$("#main").append('<div id="dimmer" style="display:none; position:absolute; top:0px; left:0px; z-index:100; width:100%; background-color:black; opacity: 0.6; -moz-opacity:0.60; filter: alpha(opacity=60);"></div>');
		}
		$("#dimmer").css("height", $(document).height() + "px");
		$("#dimmer").fadeIn("slow");
		$.ajax({
		   type: "GET",
		   url: jsBaseURL+'includes/product_popup.php?method=get_products_popup',
		   data: 'id='+id+'&link='+Link,
		   success: function(msg){
		   		$("#ajaxloader").hide();
				$("#ajaxloader").addClass("popOffer");
				$("#ajaxloader").css("top", ( $(window).height() - 400 ) / 2+$(window).scrollTop() + "px");
				$("#ajaxloader").css("left", ( $(window).width() - 640 ) / 2+$(window).scrollLeft() + "px");
				$("#ajaxloader").html(msg);
				$("#ajaxloader").fadeIn("slow");
		   }
		}); // end of $.ajax
	}
	
});
