var oScribe;
var oClickedPortfolioLink, oRotatingCarousel, oLastLoadedLink;
var isiPad = navigator.userAgent.match(/iPad/i)!=null;
var isiPod = (navigator.userAgent.match(/iPod/i)!=null && navigator.userAgent.match(/iPhone/i)!=null);
var isIE = (navigator.userAgent.match(/MSIE/i))!=null;

function showScribe(sDirection) {
	if (!isiPad && !isiPod) {
		$(".scribe").eq(0).animate({opacity:1, mozOpacity:1, marginLeft: (''+sDirection+'=40px')}, 700, 'swing', function(){});
	} else {
		oScribe = $(".scribe").get(0);
		oScribe.style.opacity = 0;
		oScribe.style.webkitTransitionDuration = "700ms";
		oScribe.style.webkitTransitionProperty = "webkitTransform, opacity, mozOpacity, marginLeft";
		oScribe.style.webkitTransitionTimingFunction = "ease-out";
		setTimeout("oScribe.style.webkitTransform = 'translate3d(" + (sDirection+"40px") + ",0,0)'; oScribe.style.opacity = 1;",1);
	}
}

function switchScribe(sNewClass,sDirection) {
	if (!isiPad && !isiPod) {
		$(".scribe").animate({opacity:0, mozOpacity:0},350,'swing',function(){
				$(".scribe").css({webkitTransitionDuration:'', webkitTransitionProperty:'', webkitTransitionTimingFunction:'', marginLeft:'', webkitTransform:''});
				$(".scribe").get(0).className = "scribe " + sNewClass;
				showScribe(sDirection);
			});
	} else {
		$(".scribe").css({"opacity":1,
							"webkitTransitionDuration":"200ms",
							"webkitTransitionProperty":"webkitTransform, opacity, mozOpacity, marginLeft",
							"webkitTransitionTimingFunction":"ease-out"});
		setTimeout("$('.scribe').css({'opacity':0})",1);
		setTimeout(function(){
						$(".scribe").css({webkitTransitionDuration:'', webkitTransitionProperty:'', webkitTransitionTimingFunction:'', marginLeft:'', webkitTransform:''});
						$(".scribe").get(0).className = "scribe " + sNewClass;
						showScribe(sDirection);
					},1000);
	}
}

function initWelcomePage() {
	// tag the content and related the tags to segments
	var sInterestsBySegments = {
		"default":[],
		"healthplan":[],
		"products":[],
		"provider":[],
		"publicsector":[]
	};
	var sBannerTags = {
		"aetna_seniorportal.html":[],
		"worldcongress_healthplaninov.html":[],
		"healthplus_.html":[]
	};

	// identify the one ore more interest segments for this visitor from a cookie (the cookie is set based on recency, frequency, and duration of visits to page categories
	var aVisitorSegments = ["default"];

	// get the list of tags for this visitor
	var aTags = {};
	for (var i=0; i<aVisitorSegments.length; i++) {
		var aTagList = sInterestsBySegments[aVisitorSegments[i]];
		for (var i=0; i<aTagList; i++) {
			
		}
	}

	// get a ranked list of banners that have matching tags
	
	// load the selected banners
	$(".welcomeportfolio .rotater li").eq(0).load("welcomebanners/worldcongress_healthplaninov.html", function(){});
	$(".welcomeportfolio .rotater li").eq(1).load("welcomebanners/healthplus_chartmgr.html", function(){});
	$(".welcomeportfolio .rotater li").eq(2).load("welcomebanners/aetna_seniorcareportal.html", function(){
		setInterval("if (autoScrollPortfolio==true) portfolioflipbook.gotonext(document.getElementById('portfolionextlink'));",6000);
	});
}
function handleWelcomeBannerClick(obj,e) {
	try {
		e = e || window.event;
		e.stopPropagation();
		e.stopImmediatePropagation();
		var oA = $(obj).find("a").get(0);
		location.href = oA.href;
	} catch(err) {
	}
}



function initCorporatePage() {
	// When any photo or name is clicked in the list of leadership team members
	$(".leadershipteam li")
		.live("click",
			function(e){
				var $this = $(this);
				// If this isn't already the selected item
				if (!$this.hasClass("selected")) {
					// minimize the previously selected item
					$(".leadershipteam li").removeClass("selected");

					// animate the position of the photo in to place
					var $contents = $this.find(".contents");
					var iPosition = $this.index();
					$contents
						.css({position:"absolute", top:"20px", left:((119*iPosition)+"px")})
						.animate(
							{top:"245px", left:"0px"},
							500,
							function(){
								// animate the bio in to displaying
								var $bio = $this.find(".bio");
								$bio
									.css({position:"absolute", top:"0px", left:"130px", width:"460px"})
									.fadeIn(
										200,
										function(){
											// end all animations and just enable class selected
											$this.addClass("selected");
											$bio.css({position:"", top:"", left:"", width:"", display:""});
											$contents.css({position:"", top:"", left:""})
										}
									);
							});
				}
			});
}



function initCareersPage() {
	if (window.location.hash && window.location.hash!="") {
		var aArr = document.getElementById("availablelistings").getElementsByTagName("A");
		for (var i=0; i<aArr.length; i++) {
			if (aArr[i].hash == window.location.hash) {
				selectCareerListing(aArr[i]);
				break;
			}
		}
	} else {
		//selectCareerListing($("#availablelistings li a:not(.ignore)").get(Math.floor(Math.random()*8)));
		selectCareerListing($("#availablelistings li a:not(.ignore)").get(0));
	}
}
function selectCareerListing(oLink) {
	$("#availablelistings li").removeClass("selected");
	$(oLink.parentNode).addClass("selected");

	$(".maincontent").css("height",$(".maincontent").outerHeight());
	if (!isiPad && !isiPod) {
		$("#contentcontainer").fadeOut(function(){
				loadCareerListing(oLink);
			});
	} else {
		oClickedPortfolioLink = oLink;
		$("#contentcontainer").css({"opacity":1,
									"webkitTransitionDuration":"400ms",
									"webkitTransitionProperty":"all",
									"webkitTransitionTimingFunction":"ease-out"});
		setTimeout("$('#contentcontainer').css({'opacity':0})",1);
		setTimeout(function(){
						$("#contentcontainer").css({"webkitTransitionDuration":"",
													"webkitTransitionProperty":"",
													"webkitTransitionTimingFunction":""});
						loadCareerListing(oClickedPortfolioLink);
					},800);
	}
}
function loadCareerListing(oLink) {
	window.location.hash = oLink.hash;
	var sHref = oLink.hash.substring(1,10000);
	$("#contentcontainer").load(sHref, function(){
			if (!isiPad && !isiPod) {
				$("#currentproject, #contentcontainer").fadeIn();
			} else {
				$("#currentproject, #contentcontainer").css({"opacity":0,
															"webkitTransitionDuration":"400ms",
															"webkitTransitionProperty":"all",
															"webkitTransitionTimingFunction":"ease-out"});
				setTimeout("$('#currentproject, #contentcontainer').css({'opacity':1})",1);
				setTimeout(function(){
								$("#currentproject, #contentcontainer").css({"webkitTransitionDuration":"",
																			"webkitTransitionProperty":"",
																			"webkitTransitionTimingFunction":""});
							},800);
			}
			$(".maincontent").css("height","");
		})
}



function initPortfolioPage() {
	if (window.location.hash && window.location.hash!="") {
		var aArr = document.getElementById("availableprojects").getElementsByTagName("A");
		for (var i=0; i<aArr.length; i++) {
			if (aArr[i].hash == window.location.hash) {
				selectPortfolioProject(aArr[i]);
				break;
			}
		}
	} else {
		selectPortfolioProject($("#availableprojects li a").get(Math.floor(Math.random()*5)));
	}
}
function selectPortfolioProject(oLink) {
	$("#availableprojects li").removeClass("selected");
	$(oLink.parentNode.parentNode).addClass("selected");

	$(".maincontent").css("height",$(".maincontent").outerHeight());
	if (!isiPad && !isiPod) {
		$("#currentproject, #contentcontainer").fadeOut(function(){
				$("#currentproject ul").eq(0).css("marginLeft","0px");
				$(".gotoindices a").removeClass("selected");
				$(".gotoindices a").eq(0).addClass("selected");
				oClickedPortfolioLink = oLink;
				loadPortfolioProject(oLink);
			});
	} else {
		oClickedPortfolioLink = oLink;
		$("#currentproject, #contentcontainer").css({"opacity":1,
													"webkitTransitionDuration":"400ms",
													"webkitTransitionProperty":"all",
													"webkitTransitionTimingFunction":"ease-out"});
		setTimeout("$('#currentproject, #contentcontainer').css({'opacity':0})",1);
		setTimeout(function(){
						$("#currentproject, #contentcontainer").css({"webkitTransitionDuration":"",
																	"webkitTransitionProperty":"",
																	"webkitTransitionTimingFunction":""});
						$("#currentproject ul").eq(0).css("marginLeft","0px");
						$(".gotoindices a").removeClass("selected");
						$(".gotoindices a").eq(0).addClass("selected");
						loadPortfolioProject(oClickedPortfolioLink);
					},1000);
	}
}
function loadPortfolioProject(oLink) {
	window.location.hash = oLink.hash;
	var sHref = oLink.hash.substring(1,10000);
	$("#contentcontainer").load(sHref, function(){
			if (oLastLoadedLink != oLink) {
				oLastLoadedLink = oLink;
				var $Instruction = $("#contentcontainer #animationinstruction");
				var sInstruction = $Instruction.eq(0).html();
				setTimeout("switchScribe('"+sInstruction.split(",")[0]+"', '"+sInstruction.split(",")[1]+"')",50);
			}

			$("#currentproject .carousel").html("");
			$("#contentcontainer ul.slides").appendTo("#currentproject .carousel");
			$("#currentproject .projecttitle").html("");
			$("#contentcontainer #projecttitlecontents").appendTo("#currentproject .projecttitle");
			if ($("#contentcontainer .projector").length>0) {
				$(".projectillustrations .frame").css("display","block");
			} else {
				$(".projectillustrations .frame").css("display","none");
			}	
			try { portfoliocarousel.init(document.getElementById("portfolionextlink"));  } catch(err) {}
			
			if (!isiPad && !isiPod) {
				$("#currentproject, #contentcontainer").fadeIn();
			} else {
				$("#currentproject, #contentcontainer").css({"opacity":0,
															"webkitTransitionDuration":"400ms",
															"webkitTransitionProperty":"all",
															"webkitTransitionTimingFunction":"ease-out"});
				setTimeout("$('#currentproject, #contentcontainer').css({'opacity':1})",1);
				setTimeout(function(){
								$("#currentproject, #contentcontainer").css({"webkitTransitionDuration":"",
																			"webkitTransitionProperty":"",
																			"webkitTransitionTimingFunction":""});
							},800);
			}
			$(".maincontent").css("height","");
			
			setTimeout("$('.screenshot').animate({opacity:1, mozOpacity:1}, 700, 'swing', function(){});",700);
		})
}


var portfolioOldObj, portfolioNewObj;
var portfolioflipbook = 
(function(){
	var j=false;
	var k;
	var l;
	var m=-1;
	var n=0;
	var o=60;
	var p=new Array(0,20,40,60,80,100);

	var q = function(a){
				var b=a.parentNode;
				k=b.getElementsByTagName("UL")[0];
				var c=k.getElementsByTagName("LI");
				n=c.length;
				l=b.getElementsByTagName("DIV");
				l=l[0];
				for (var i=0; i<c.length;i++) {
					if (c[i].style.display=="block") {
						m=i;
						continue
					}
				}
				portfolioflipbook.recenter()
			};
	var r = function(a,b){
				if (!isiPad && !isiPod) {
					for (var i=0; i<p.length; i++) {
						setTimeout("portfolioflipbook.updateopacity("+a+","+(p[i]/100)+","+b+","+((100-p[i])/100)+");",((i*o)+50))
					}
					setTimeout("portfolioflipbook.recenter();",((i*o)+50))
				} else {
					var aEls = k.getElementsByTagName("LI");
					portfolioNewObj = aEls[a];
					portfolioOldObj = aEls[b];
					var h=l.getElementsByTagName("DIV")[b];
					var i=l.getElementsByTagName("DIV")[a];
					h.className=h.className.split(" portfolioselectedindex").join("");
					i.className=i.className.split(" portfolioselectedindex").join("")+" portfolioselectedindex"
					var iDelay = (((p.length-1)*o)+50);
					portfolioOldObj.style.opacity = 1;
					portfolioNewObj.style.opacity = 0;
					portfolioNewObj.style.display = "block";
					portfolioOldObj.style.webkitTransitionDuration = iDelay + "ms";
					portfolioOldObj.style.webkitTransitionProperty = "all";
					portfolioOldObj.style.webkitTransitionTimingFunction = "ease-out";
					portfolioNewObj.style.webkitTransitionDuration = iDelay + "ms";
					portfolioNewObj.style.webkitTransitionProperty = "all";
					portfolioNewObj.style.webkitTransitionTimingFunction = "ease-out";
					setTimeout(function(){
									portfolioOldObj.style.opacity = 0;
									portfolioNewObj.style.opacity = 1;
								},1);
					setTimeout(function(){
									portfolioOldObj.style.webkitTransitionDuration = "";
									portfolioOldObj.style.webkitTransitionProperty = "";
									portfolioOldObj.style.webkitTransitionTimingFunction = "";
									portfolioNewObj.style.webkitTransitionDuration = "";
									portfolioNewObj.style.webkitTransitionProperty = "";
									portfolioNewObj.style.webkitTransitionTimingFunction = "";
									portfolioOldObj.style.display = 'none';
									portfolioflipbook.recenter();
								},(iDelay+30))
				}
			};

	return {
		gotonext : function(a){
						if (!j) {
							q(a);
							j=true;
							var b=m+1;
							if(b>=n)b=0;
							r(b,m)
						}
					},
		gotoprev : function(a){
						if (!j) {
							q(a);
							j=true;
							var b=m-1;
							if(b<0)b=n-1;
							r(b,m)
						}
					},
		gotoindex : function(a,b){
						if (!j && a.className.indexOf("portfolioselectedindex")<0) {
							q(a.parentNode);
							j=true;
							var c=b-1;
							r(c,m)
						}
					},
		updateopacity : function(a,b,c,d){
							var e=k.getElementsByTagName("LI");
							var f=e[a];
							f.style.opacity=b;
							f.style.mozOpacity=b;
							f.style.filter="alpha(opacity="+(b*100)+")";
							var g=e[c];
							g.style.opacity=d;
							g.style.mozOpacity=d;
							g.style.filter="alpha(opacity="+(d*100)+")";
							if(b==0){
								f.style.display="block"
							}
							if(b==1){
								g.style.display="none";
								var h=l.getElementsByTagName("DIV")[c];
								var i=l.getElementsByTagName("DIV")[a];
								h.className=h.className.split(" portfolioselectedindex").join("");
								i.className=i.className.split(" portfolioselectedindex").join("")+" portfolioselectedindex"
							}
						},
		recenter : function(){
						j=false
					}
	}
}
)();

// webkitTransform = "translate3d(0,300px,0)";
var portfoliocarousel = 
(function(){
	var bIsRotating = false;
	var oRotater, oIndeces;
	var iPixelsPerItem = 0;
	var iNumberOfItems = 0;
	var iNumberOfVisibleItems = 0;
	var iSpeed = 40;
	var aSlideSteps = new Array(0,2,6,20,40,60,80,88,94,97,99,100);
	
	var getRotater = function(el){
			var oSizer = null
			while (oSizer==null && el.tagName.toLowerCase()!="body") {
				if (el.className=="projectillustrations") {
					oSizer = el;
				}
				el = el.parentNode;
			}
			oRotater = oSizer.getElementsByTagName("UL")[0];
			var liArr = oRotater.getElementsByTagName("LI");
			iPixelsPerItem = liArr[0].offsetWidth;
			iNumberOfItems = liArr.length;
			iNumberOfVisibleItems = Math.round((oSizer.offsetWidth/iPixelsPerItem));
			oRotater.style.width = (iPixelsPerItem*iNumberOfItems) + "px";
			oIndeces = oSizer.getElementsByTagName("SPAN");
			oIndeces = oIndeces[oIndeces.length-1];
			if (oIndeces.getElementsByTagName("A").length!=iNumberOfItems) {
				var sHTML = "";
				for (var i=0; i<iNumberOfItems; i++) {
					sHTML += "<a href=\"#\" onclick=\"portfoliocarousel.gotoindex(this," + (i+1) + "); return false;\">" + (i+1) + "</a>";
				}
				oIndeces.innerHTML = sHTML;
			}
			portfoliocarousel.recenter();
		};
	var doAnimation = function(iPixelDistance){
			var iOriginalPosition = parseFloat(oRotater.style.marginLeft);
			if (isNaN(iOriginalPosition)) iOriginalPosition = 0;
			if (!isiPad && !isiPod) {
				for (var i=0; i<aSlideSteps.length; i++) {
					setTimeout("portfoliocarousel.updateposition(" + (iOriginalPosition + (iPixelDistance * (aSlideSteps[i]/100))) + ");",((i*iSpeed)+50))
				}
				setTimeout("portfoliocarousel.recenter();",((i*iSpeed)+50));
			} else {
				oRotatingCarousel = oRotater;
				oRotater.style.webkitTransitionDuration = "600ms";
				oRotater.style.webkitTransitionProperty = "all";
				oRotater.style.webkitTransitionTimingFunction = "ease-out";
				setTimeout("oRotatingCarousel.style.marginLeft = " + (iOriginalPosition + iPixelDistance) + " + 'px';",1);
				setTimeout("oRotatingCarousel.style.webkitTransitionDuration = '';\
							oRotatingCarousel.style.webkitTransitionProperty = '';\
							oRotatingCarousel.style.webkitTransitionTimingFunction = '';\
							portfoliocarousel.recenter();",800);
			}
		};

	return {
		init: function(el){
				if (!bIsRotating) {
					getRotater(el);
				}
			},
		gotonext: function(el,iOptionalDistance){
				if (!bIsRotating) {
					getRotater(el);
					bIsRotating = true;
					doAnimation((-1 * iOptionalDistance * iPixelsPerItem));
				}
			},
		gotoprev: function(el,iOptionalDistance){
				if (!bIsRotating) {
					getRotater(el);
					bIsRotating = true;
					doAnimation((iOptionalDistance * iPixelsPerItem));
				}
			},
		gotoindex: function(el,iIndex){
				if (!bIsRotating) {
					getRotater(el);
					var liArr = oRotater.getElementsByTagName("LI");
					var iCurrCenterIndex = liArr[parseInt((iNumberOfItems/2)-0.01)].id.split("c")[1];
					if (iIndex>iCurrCenterIndex) {
						portfoliocarousel.gotonext(el.parentNode.parentNode,1);
					} else if (iIndex<iCurrCenterIndex) {
						portfoliocarousel.gotoprev(el.parentNode.parentNode,1);
					}
				}
			},
		updateposition: function(iPos){
				oRotater.style.marginLeft = iPos + "px";
			},
		recenter: function(){
				var iCurrentPosition = parseFloat(oRotater.style.marginLeft);
				if (isNaN(iCurrentPosition)) iCurrentPosition = 0;
				var iDesireablePosition = iPixelsPerItem * Math.round(((iNumberOfVisibleItems - iNumberOfItems)/2));
				var iNumberOfMisplacedItems = Math.abs(Math.round((iDesireablePosition-iCurrentPosition)/iPixelsPerItem));
				if (iCurrentPosition < iDesireablePosition) {
					var liArr = oRotater.getElementsByTagName("LI");
					var aObjsToMove = new Array();
					for (var i=0; i<iNumberOfMisplacedItems; i++) { aObjsToMove[aObjsToMove.length] = liArr[i]; }
					for (var i=0; i<aObjsToMove.length; i++) { oRotater.insertBefore(aObjsToMove[i],null); }
					portfoliocarousel.updateposition(iDesireablePosition);
				} else if (iCurrentPosition > iDesireablePosition) {
					var liArr = oRotater.getElementsByTagName("LI");
					var aObjsToMove = new Array();
					for (var i=0; i<iNumberOfMisplacedItems; i++) { aObjsToMove[aObjsToMove.length] = liArr[liArr.length-iNumberOfMisplacedItems+i]; }
					var oOriginalRoot = liArr[0];
					for (var i=0; i<aObjsToMove.length; i++) { oRotater.insertBefore(aObjsToMove[i],oOriginalRoot); }
					portfoliocarousel.updateposition(iDesireablePosition);
				}
				var liArr = oRotater.getElementsByTagName("LI");
				var iCurrCenterIndex = liArr[parseInt((iNumberOfItems/2)-0.01)].id.split("c")[1];
				var aArr = oIndeces.getElementsByTagName("A");
				for (var i=0; i<aArr.length; i++) {
					aArr[i].className="";
					if (i==(iCurrCenterIndex-1)) aArr[i].className="selected";
				}
				bIsRotating = false;
			}
	};
})();
