var rewriteUrlCount = 0;
var amIDoingARequest = false;

if(typeof hasPluckComponent == 'undefined' || hasPluckComponent == null){
	hasPluckComponent = false;
}

function rewriteUrls(){
    var urlAttributes = {}
	var queryString = window.location.search.substring(1);
	var params = queryString.split('&');
	for (var i=0; i<params.length; i++) {
		var position = params[i].indexOf('=');
		if (position > 0) {
			var key = params[i].substring(0,position);
			var val = params[i].substring(position+1);
			if(key=="storeId"){
			    urlAttributes.storeId = val;
			} else if(key=="langId"){
			    urlAttributes.langId = val;
			} else if (key == "catalogId"){
			    urlAttributes.catalogId = val;
			} else if (key == "categoryId"){
			    urlAttributes.categoryId = val;
			} else if (key == "categoryParentId"){
			    urlAttributes.categoryParentId = val;
			} else if (key == "productId"){
			    urlAttributes.productId = val;
			}
		}
	}
	$(".PluckCMS a,.topFivePluck a").each(function(){
	    hasStoreId = false;
	    hasLangId = false;
	    hasCatalogId = false;
	    hasCategoryId = false;
	    hasCategoryParentId = false;
	    hasProductId = false;
	    var linkPath = $(this).attr("href");
	    if(linkPath !== undefined){
			var queryString = linkPath.substring(linkPath.indexOf("?")+1);
			hasQueryString = linkPath.indexOf("?");
			var params = queryString.split('&');
			for (var i=0; i<params.length; i++) {
				var position = params[i].indexOf('=');
				if (position > 0) {
					var key = params[i].substring(0,position);
					var val = params[i].substring(position+1);
					if(key=="storeId"){
					    hasStoreId = true;
					} else if(key=="langId"){
					    hasLangId = true;
					} else if (key == "catalogId"){
					    hasCatalogId = true;
					} else if (key == "categoryId"){
					    hasCategoryId = true;
					} else if (key == "categoryParentId"){
					    hasCategoryParentId = true;
					} else if (key == "productId"){
					    hasProductId = true;
					}
				}
			}
			var isFirst = false;
			function isItFirst(){
			    if(isFirst){
			        isFirst = false;
			    } else {
			        linkPath += "&";
			    }
			}
			if(hasQueryString<0){
			    linkPath += "?";
			    isFirst = true;
			}
			if(!hasStoreId && (urlAttributes.storeId !== undefined)){
			    isItFirst();
			    linkPath += "storeId="+urlAttributes.storeId;
			}
			if(!hasLangId && (urlAttributes.langId !== undefined)){
			    isItFirst();
			    linkPath += "langId="+urlAttributes.langId;
			}
			if(!hasCatalogId && (urlAttributes.catalogId !== undefined)){
			    isItFirst();
			    linkPath += "catalogId="+urlAttributes.catalogId;
			}
			if(!hasCategoryId && (urlAttributes.categoryId !== undefined)){
			    isItFirst();
			    linkPath += "categoryId="+urlAttributes.categoryId;
			}
			if(!hasCategoryParentId && (urlAttributes.categoryParentId !== undefined)){
			    isItFirst();
			    linkPath += "categoryParentId="+urlAttributes.categoryParentId;
			}
			if(!hasProductId && (urlAttributes.productId !== undefined)){
			    isItFirst();
			    linkPath += "productId="+urlAttributes.productId;
			}
			//console.log(linkPath);
			linkPath = linkPath.replace(/\/\/webapp/g,"\/webapp");
			//console.log(linkPath);
			$(this).attr({href:linkPath});
		}
	});
	rewriteUrlCount++;
	if(rewriteUrlCount<15){
		window.setTimeout(isPluckLoaded,1000)
	} else {
		addStyles();
	}
}
function trimUserNames(){
	var allUserNames = $(".slComments .slUserInfo .slUserName," + 
		".slGroupDiscussionPosts .slUserInfo .slUserName," + 
		".slGroupBlogPosts .slUserInfo .slUserName");
	
	var maxItems = allUserNames.length;
	for(var i=0; i<maxItems; i++){
		var tempText = allUserNames[i].innerHTML;
		if(tempText.length >16){
			allUserNames[i].innerHTML = tempText.substring(0,16);
		}
	}
}
function addStyles(){
	$(".PersonaHeader_PublicContent a:first").addClass("leaveMe");
	$(".PersonaHeader_PublicContent a:last").addClass("addMe");
}
function isPluckLoaded(){
	var PluckCMSText = $(".PluckCMS").text();
	var pluckText = $("#slGroupLanding").text();
	var pluckPanelText = $(".topFivePluck div ul").text();
	if((pluckText.length+pluckPanelText.length+PluckCMSText.length) <= 0){
		window.setTimeout(isPluckLoaded,1000)
	} else {
		rewriteUrls();
		trimUserNames();
	}
}

if(window['boots.pluck.rewriteurls.loaded'] != true){
	window['boots.pluck.rewriteurls.loaded'] = true;
	$(document).ready(function(){
		if(hasPluckComponent){
			isPluckLoaded();
		}
	});
}
