$(document).ready(function() {
	// to store it in a variable
	var s = new String();
	s = String(window.location);
	//var locStr = loc.toString():
	var finger = s.indexOf('/uk/products/location-intelligence/');
	var loc = window.location;

	swapValues = [];
    $(".swap").each(function(i){swapValues[i] = $(this).val();
        $(this).focus(function(){if ($(this).val() == swapValues[i]) {$(this).val("");}}).blur(function(){ if ($.trim($(this).val()) == "") {$(this).val(swapValues[i]);}});
	});
	//Handle offsite links
	$('#content a').filter(function() {
		var theHref = this;
		if (theHref.hostname && theHref.hostname !== location.hostname) {
			$(theHref).not(".noAutoIcon").addClass("offSite");
			$(theHref).not(".noAutoLink").bind('click keypress', function(event) {
				var code=event.charCode || event.keyCode;
				if (!code || (code && code == 13)) {
					var fixedLink = this.href;
					fixedLink = fixedLink.replace(/https?:\/\/(.*)/,"$1");
					fixedLink = '/outgoing/' + fixedLink;
					_gaq.push(['_trackEvent', 'External Link', 'External Link to', fixedLink]);
				};
			});
		};
	});
	//Automatic PDF file handling
	$("a[@href$='.pdf']:not(.noAutoIcon)").addClass("pdf");
	$("a[@href$='.pdf']").bind('click keypress', function(event) {
		var code=event.charCode || event.keyCode;
		if (!code || (code && code == 13)) {
			var fixedLink = "/downloaded-files/pdf/" + this.pathname;
			_gaq.push(['_trackEvent', 'Download', 'PDF', fixedLink]);
		}
		;
	}
	);
	//Track Downloads
	$("a.download").bind('click keypress', function(event) {
		var code=event.charCode || event.keyCode;
		if (!code || (code && code == 13)) {
			var fixedLink = "/downloaded-files/misc/" + this.pathname;
			_gaq.push(['_trackEvent', 'Download', 'Misc.', fixedLink]);
		};
	});
	//colored OL
	$('ol li').wrapInner('<span class="olcontent"></span>').addClass('olcolor');
	//Nav
	$('ul#main-nav li').hover(function() {
		$(this).addClass('active');
	},function() {
		$(this).removeClass('active');
	});
	$("body#supportDownloads ul:not(:has(li))").append("<li>No downloads in this category.</li>");
	/*var limit = 2;//desired number - 1;
	$("ul",$("#portlets")).each(function(){
		if($(this).parent("div").attr("id") == "multimedia"){limit = limit - 1;}
		currClass = $(this).parent("div").attr("class");
		//if(currClass != "standard"){
			var totalLi = $(this).children("li");
			var childCount = totalLi.size();
			if(childCount <= limit){
				$(this).next("a.all").addClass("all-hide");
			}else{
				for(i=0; i<childCount; i++){
					if(i > limit){
						$(totalLi[i]).addClass("hide");
					}
				}
			}
		//}
		if($(this).parent("div").attr("id") == "multimedia"){limit = limit + 1;}
	});*/
	$("a.all", $("#portlets")).each(function() {
		if($(this).prev("ul").parent("div").attr("id") == "multimedia") {
			limit = limit - 1;
		}
		count = $(this).prev("ul").children().length;
		if(count <= (limit+1)) {
			$(this).addClass("hide");
		}
		if($(this).prev("ul").parent("div").attr("id") == "multimedia") {
			limit = limit + 1;
		}
		$(this).toggle(function() {
			$(this).addClass("less");
			$(this).children("span.toggle").text("Less");
			$(this).prev("ul").children("li.hide").removeClass("hide");
			return false;
		}
		,function() {
			if($(this).prev("ul").parent("div").attr("id") == "multimedia") {
				limit = limit - 1;
			}
			$(this).removeClass("less");
			$(this).children("span.toggle").text("All");
			var totalLi = $(this).prev("ul").children("li");
			var childCount = totalLi.size();
			for (i=0; i<childCount; i++) {
				if(i > limit) {
					$(totalLi[i]).addClass("hide");
				}
			}
			if($(this).prev("ul").parent("div").attr("id") == "multimedia") {
				limit = limit + 1;
			}
			return false;
		}
		);
	}
	);
	//add left border
	$("table.standard thead th:first").addClass("lb");
	whereitat = window.location + "";
	splitUrl = whereitat.split("/");
	if(splitUrl[3] == "About") {
		$("li#about-pbbi").addClass("current");
	} else if(splitUrl[3] == "Communities") {
		$("li#communities").addClass("current");
	} else if(splitUrl[3] == "Partners") {
		$("li#partners").addClass("current");
	} else if(splitUrl[3] == "Products") {
		$("li#products").addClass("current");
	} else if(splitUrl[3] == "Resources") {
		$("li#resources").addClass("current");
	} else if(splitUrl[3] == "Solutions") {
		$("li#solutions").addClass("current");
	} else if(splitUrl[3] == "Support") {
		$("li#support").addClass("current");
	} else if(splitUrl[3] == "index.html") {
		$("li#home").addClass("current");
	}
	$("a.flash-pop").click(function() {
		var rel = $(this).attr("rel");
		$('#showFlash').css('display', 'block').flash( {
			swf: '/uk/flash/Player.swf', height: 400, width:550, params: {
				wmode:'transparent'
			}
			, flashvars: {
				flv:rel
			}
		}
		);
		return false;
	}
	);
	$("a.flash-popBig").click(function() {
		var rel = $(this).attr("rel");
		$('#showFlash').css('display', 'block').flash( {
			swf: '/uk/flash/Player.swf', height: 772, width:1024, params: {
				wmode:'transparent'
			}
			, flashvars: {
				flv:rel
			}
		}
		);
		return false;
	}
	);
		$("a.flash-popBig2").click(function() {
		var rel = $(this).attr("rel");
		$('#showFlash').css('display', 'block').flash( {
			swf: '/uk/flash/Player.swf', height: 500, width:750, params: {
				wmode:'transparent'
			}
			, flashvars: {
				flv:rel
			}
		}
		);
		return false;
	}
	);
	$("#content a[href$='.flv']").each(function() {
		rel = $(this).attr("href");
		$(this).addClass("thickbox").attr( {
			"rel":rel,"href":"#TB_inline?height=420&width=550&inlineId=flashPlayer"
		}
		);
		$(this).click(function() {
			$('#showFlash').css('display', 'block').flash( {
				swf: '/uk/flash/Player.swf', height: 400, width:550, params: {
					wmode:'transparent'
				}
				, flashvars: {
					flv:rel
				}
			}
			);
			return false;
		}
		);
	}
	);
	$("li.email a").each(function() {
		var rel = $(this).attr("href");
		$(this).click(function() {
			var rel = $(this).attr('href');
			$(this).attr('href', rel +  '?atUrl=' + loc);
		}
		);
	}
	);
	$('li.print a').click(function() {
		window.print();
		return false;
	}
	);
	//add left border
	$("table.standard thead th:first").addClass("lb");
	//bios
	bioArray = $("div.col1 div.bio");
	var even = 1;
	for (i=0; i<bioArray.length; i++) {
		if(i == 0) {
			$(bioArray[0]).addClass("firstBio");
		}
		if(even == 1) {
			$(bioArray[i]).addClass("nrm");
			even = 0;
		} else {
			even = 1;
		}
	}
	$("div#comments div.comment:odd").addClass("odd")	
		//Removes bottom-border from last item in secondary navigation
	$('div#secondary-nav li:last').addClass('last');
	$('div#secondary-nav ul').each(function() {
		if($(this).children("li").length == 0) {
			$(this).remove();
		}
	}
	);
	$('a.content-edit').parent("p").addClass("logged-in");
	$(".toggle").addClass("contracted").next('.toggle_content').hide();
	$(".toggle").click(function() {
		var $this = $(this);
		if( $this.is('.contracted')) {
			$(this).removeClass("contracted").addClass("expanded");
		} else {
			$(this).removeClass("expanded").addClass("contracted");
		}
		$(this).nextAll('.toggle_content:eq(0)').toggle('fast');
		return false;
	}	);
	
	
	$("div#Enews2", $("#portlets")).each(function() {
		var $this = $(this);
		$this.replaceWith("<h2 class='eNews'>Subscribe to eNews</h2>	<div id='eNews'>		<p>Sign up to receive e-mail updates and news from Pitney Bowes Business Insight</p>		<p><a href='http://gw.vtrenz.net/?I9JT0TY6VK&amp;TB_iframe=true&amp;width=620&amp;height=400&amp;webSyncID=5e910925-8ade-b78c-053a-3c7570b85713' class='thickbox more' title='Subscribe to eNews'>Subscribe Now</a></p>	</div>"	);
	}
	);
}
);
