$(document).ready(function() {
						   
	$(".slider").hide();
	
	$("#table p a").click(function() {
		var p = $(this).closest("p");
		$(p).find("span").toggle();
		return false
	});
	
	$("#table p span").hide();
	
	$(".scale li p").hide();
	$(".scale li a").hide();
	$(".scale_wrapper .directions").hide();
	$(".scale").hide();
	
	$(".show_scale").click(function() {
		$(".scale_wrapper .button").hide();
		$(".scale_wrapper .directions").fadeIn("slow");
		$(".scale").fadeIn("slow");
		show_scale();
	});
	
	function show_scale(){
		$("[rel^='s"+15+"'] p").fadeIn("slow");
		setTimeout ( '$("[rel^=\'s"+14+"\'] p").fadeIn("normal");', 250 ); 
		setTimeout ( '$("[rel^=\'s"+13+"\'] p").fadeIn("normal");', 500 ); 
		setTimeout ( '$("[rel^=\'s"+12+"\'] p").fadeIn("normal");', 750 ); 
		setTimeout ( '$("[rel^=\'s"+11+"\'] p").fadeIn("normal");', 1000 ); 
		setTimeout ( '$("[rel^=\'s"+10+"\'] p").fadeIn("normal");', 1250 ); 
		setTimeout ( '$("[rel^=\'s"+9+"\'] p").fadeIn("normal");', 1500 ); 
		setTimeout ( '$("[rel^=\'s"+8+"\'] p").fadeIn("normal");', 1750 ); 
		setTimeout ( '$("[rel^=\'s"+7+"\'] p").fadeIn("normal");', 2000 ); 
		setTimeout ( '$("[rel^=\'s"+6+"\'] p").fadeIn("normal");', 2250 ); 
		setTimeout ( '$("[rel^=\'s"+5+"\'] p").fadeIn("normal");', 2500 ); 
		setTimeout ( '$("[rel^=\'s"+4+"\'] p").fadeIn("normal");', 2750 ); 
		setTimeout ( '$("[rel^=\'s"+3+"\'] p").fadeIn("normal");', 3000 ); 
		setTimeout ( '$("[rel^=\'s"+2+"\'] p").fadeIn("normal");', 3250 ); 
		setTimeout ( '$("[rel^=\'s"+1+"\'] p").fadeIn("normal");', 3500 );
		
		$("[rel^='s"+15+"'] a").fadeIn("slow");
		setTimeout ( '$("[rel^=\'s"+14+"\'] a").fadeIn("normal");', 250 ); 
		setTimeout ( '$("[rel^=\'s"+13+"\'] a").fadeIn("normal");', 500 ); 
		setTimeout ( '$("[rel^=\'s"+12+"\'] a").fadeIn("normal");', 750 ); 
		setTimeout ( '$("[rel^=\'s"+11+"\'] a").fadeIn("normal");', 1000 ); 
		setTimeout ( '$("[rel^=\'s"+10+"\'] a").fadeIn("normal");', 1250 ); 
		setTimeout ( '$("[rel^=\'s"+9+"\'] a").fadeIn("normal");', 1500 ); 
		setTimeout ( '$("[rel^=\'s"+8+"\'] a").fadeIn("normal");', 1750 ); 
		setTimeout ( '$("[rel^=\'s"+7+"\'] a").fadeIn("normal");', 2000 ); 
		setTimeout ( '$("[rel^=\'s"+6+"\'] a").fadeIn("normal");', 2250 ); 
		setTimeout ( '$("[rel^=\'s"+5+"\'] a").fadeIn("normal");', 2500 ); 
		setTimeout ( '$("[rel^=\'s"+4+"\'] a").fadeIn("normal");', 2750 ); 
		setTimeout ( '$("[rel^=\'s"+3+"\'] a").fadeIn("normal");', 3000 ); 
		setTimeout ( '$("[rel^=\'s"+2+"\'] a").fadeIn("normal");', 3250 ); 
		setTimeout ( '$("[rel^=\'s"+1+"\'] a").fadeIn("normal");', 3500 );
	}
	
		if($("#slider").length>0){
			var int = setInterval(start_show,5000);
		}
		 $("#slider").mouseover(function(){
			clearInterval(int);
		});
		 
		 $("#slider").mouseout(function(){
			int = setInterval(start_show,5000);
		});
		
		$("#slider ul a").click(function(){
			var href = $(this).attr("href");
			$(".slider").fadeOut(1200);
			$(href).fadeIn(1200);
			$("#current").attr("id","");
			$(this).closest("li").attr("id","current");
			return false;
		});
	
	
function start_show(){
	var curr = $("#current");
	var href= $("#current a").attr("href");
	$(href).fadeOut(1200);
	if($(curr).hasClass("last")) { //loop around to first image
		$(curr).attr("id",""); //remove id from current gallery image
		$(".first").attr("id","current");
	}
	else
	{
		$(curr).attr("id",""); //remove id from current gallery image
		$(curr).next().attr("id","current");
	}
	var href= $("#current a").attr("href");
	$(href).fadeIn(1200);
}

	if($("#slider1").length > 0){
	var href= $("#current a").attr("href");
	$(href).show();
	}


	$("#search label").inFieldLabels();
	$("#community_login label").inFieldLabels();
	
	var body_id = $("body").attr("id");
	var thehash=window.location.hash;
	if(thehash == "#loyalty"){
	$("#li_loyalty_"+body_id).addClass("current");
	}
	else {
	$("#li_"+body_id).addClass("current");
	}
	
	
	if($("#sub_nav li li li.current").length>0){
		$("#sub_nav li li li.current").parent().closest("li").parent().closest("li").addClass("active");
	}
	if($("#sub_nav li li.current").length>0){
		$("#sub_nav li li.current").parent().closest("li").addClass("active");
	}
	
	$(".active ul ul").hide();
	$(".active ul ul ul").hide();
	if($(".active ul:first").length > 0){
		$(".active ul:first").show();
	}
	if($(".current ul:first").length > 0){
		$(".current ul:first").show();
	}
	if($("#sub_nav .current").closest("ul").length > 0){
		$("#sub_nav .current").closest("ul").show();
	}
	
	if($("a[rel^='prettyPhoto']").length>0){
		$("a[rel^='prettyPhoto']").prettyPhoto({theme:'dark_square'});
	}
	
	if($("#sub_nav li:first").length >0){
		$("#sub_nav li:first").addClass("round_top_200");
	}
	if($("#sub_nav li:last").length > 0){
		$("#sub_nav li:last").addClass("round_bottom_200");
	}
	if($("#tertiary_nav li:first").length >0){
		$("#tertiary_nav li:first").addClass("round_top_200");
	}
	if($("#tertiary_nav li:last").length > 0){
		$("#tertiary_nav li:last").addClass("round_bottom_200");
	}

//business issues
	if($("#tabs").length>0){
		$("#tabs div").hide();
		var hash=window.location.hash;
		if(hash == ""){
			hash = "#d1";
		}
		$("#tabs "+hash).show();
		
		$(".issue_tabs a").click(function(){
			$("#tabs div").hide();
			var href=$(this).attr("href");
			$("#tabs "+href).show();
			return false;
		});
		
	}
	
	
	if($.cookie("issues")){
		$("#issues_download").hide();
		if($("#issues_view").length>0){
			$("#issues_view").show();
		}
	}
	else
	{
		$("#issues_view").hide();
	}
	if($.cookie("adi_demo")){
		$("#adi_demo_click").hide();
		$("#adi_demo_view").show();
	}

	if(jQuery.url.segment(2)){
		if($.cookie(jQuery.url.segment(2))){
			//if form cookie - hide form link
			//if form cookie - show links
			$(".podcast_link").show();
			$("#podcasts object").show();
			$(".whitepaper_link").show();
			$(".suite_form").hide();
		}
	}
	
//scales	
	$(".scale_questions div").hide();
	$(".scale_questions .scale_part").hide();
	if($(".scale").length>0){
		scale($(".scale").attr("id"));
	}
	

	//hide scale
	$(".scale li").hide();
	//show scale
	$(".show_scale").click(function(){
		$(".scale li").slideDown("normal");
	});
	
	$(".scale_questions li p a").click(function(){
		scales(this.id, this.id.substr(1,1), $(this).closest("div").attr("id"));
		return false;
	});
	
	$(".scale a").click(function(){
								 
		if($(this).hasClass("delete")){
			var rel = $(this).closest("li").attr("rel");
			$("li[rel='"+rel+"'] a").removeClass("delete");
			listSubtract(rel.substr(1,2), $(this).closest("ul").attr("id"));
		}
		else{
			var rel = $(this).closest("li").attr("rel");
			$("li[rel='"+rel+"'] a").addClass("delete");
			listAdd(rel.substr(1,2), $(this).closest("ul").attr("id"));
		}
		return false;
	});
	
	$(".scale_part a").click(function(){
		if($(this).hasClass("delete")){
			var rel = $(this).closest("li").attr("rel");
			$("li[rel='"+rel+"'] a").removeClass("delete");
			
			listSubtract(rel.substr(1,2), $(".scale").attr("id"));
		}
		else{
			var rel = $(this).closest("li").attr("rel");
			$("li[rel='"+rel+"'] a").addClass("delete");
			listAdd(rel.substr(1,2), $(".scale").attr("id"));
		}
		return false;
	});
	
	//tooltips
	if($(".scale p").length>0){
		$(".scale p").tooltip({
			track: true,
			delay: 0,
			showURL: true,
			showBody: " - ",
			fade: 250
			});
	}
	if($(".scale_part p").length>0){
		$(".scale_part p").tooltip({
			track: true,
			delay: 0,
			showURL: true,
			showBody: " - ",
			fade: 250
			});
	}
	
	if($("#proof_list").length > 0){
		List();
	}
	
	
function scales(question, talk, scale) { //shows/hides videos 
		$(".scale_questions #t" + talk).slideToggle("normal");
		$(".scale_questions #u" + talk).slideToggle("normal");
}

function scale(scale){ //marks scale elements that are in list
	if($.cookie(scale)){
		var cookie = $.cookie(scale);
		var ar = cookie.split(','); //put in array
		for( var i=0, len=ar.length; i<len; ++i){
			//mark list items that are in list	
			$("li[rel='s"+ar[i]+"'] a").addClass("delete");
		}
		listCount(scale);
	}
}

function listCount(scale){
		if($.cookie(scale)){
			var str = $.cookie(scale);
			str = str.replace("undefined,","");
			str = str.replace("null,","");
			//update cookie after stripping undefined and nulls
			$.cookie(scale, str, { path: '/', expires: 365 });
			var ar = $.cookie(scale).split(','); //put in array
			$("#scale_my_list a").html("My List ["+ar.length+"]");
		}
}

function listAdd(id, scale){ //add items to list
	var add="yes";
	if($.cookie(scale)){
		var ar = $.cookie(scale).split(','); //put in array
		
		for( var i=0, len=ar.length; i<len; ++i){
			if(ar[i] == id){
				add="no";
			}
		}
	}
	
	if(add == "yes"){
		$.cookie(scale, $.cookie(scale) + ","+id, { path: '/', expires: 365 });
	}
	listCount(scale);
	
	
}

function listSubtract(id, scale){
	if($.cookie(scale)){
		var cookie = $.cookie(scale); //get existing cookie
		var ar = $.cookie(scale).split(','); //put in array
		
		for( var i=0, len=ar.length; i<len; ++i){
			if(ar[i] == id){
				ar.splice(i,1);
			}
		}
		cookie = ar.toString();
	$.cookie(scale, cookie, { path: '/', expires: 365 });
	listCount(scale);
	}
}

//Display List
function List(){
	$.get("/scale.php" ,function(response) {
		$("#proof_list").html(response);
		
		$("#proof_list li p").tooltip({
			track: true,
			delay: 0,
			showURL: true,
			showBody: " - ",
			fade: 250
		});
		
		$(".scale a").click(function(){
								 
		
		if($(this).hasClass("delete")){
			var rel = $(this).closest("li").attr("rel");
			$(this).closest("li").hide();
			scale = $(this).closest("ul").attr("id");
			var id = rel.substr(scale.length,2);
			if($.cookie(scale)){
			var cookie = $.cookie(scale); //get existing cookie
			var ar = $.cookie(scale).split(','); //put in array
			
			for( var i=0, len=ar.length; i<len; ++i){//look thru array for value
				if(ar[i] == id){
					ar.splice(i,1);
				}
			}
			cookie = ar.toString();
			$.cookie(scale, cookie, { path: '/', expires: 365 });//update cookie
		}
		
		
		
	
	}
		
		
		
		
		
		return false;
	});
	})
		
}

	
});

