$(document).ready(function(){
	
	$.ajaxSetup ({  
         cache: false  
    });
	
	// HOME PAGE TAKEOVERS - FIVE LINKS
	
	var link0 = new Object();
	var link1 = new Object();
	var link2 = new Object();
	var link3 = new Object();
	var link4 = new Object();
	
	link0.name="Listen";
	link0.bgColor = "#e41c35";
	link1.name="Learn";
	link1.bgColor = "#e6322f";
	link2.name="Connect";
	link2.bgColor = "#ea5126";
	link3.name="Influence";
	link3.bgColor = "#ee6d1e";
	link4.name="Optimize";
	link4.bgColor = "#f18517";
	
	var links = Array(link0,link1,link2,link3,link4);
	
	
	$(".five-link").click(function() {
		
		var idx = $(".five-link").index($(this));
		
		var obj = links[idx];
		
		var div = $("#takeover");
		
		var wh = $(document).height()-60;
		$(div).css({
			"height":wh+"px",
			"background-color":obj.bgColor
			
		});
		
		// call new content via ajax
		var id = $("#indicator");
		$(id).show();
		$(id).activity({color: '#990000'});
   
 		//  load() functions
		var slide = obj.name.toLowerCase();
		var loadUrl = "/wp-content/themes/accent/five_links_slides.php?slide="+slide;
		 
		$(div).fadeIn('fast', function() { 
			$(div).load(loadUrl, null, function(responseText){ 
			
				$(id).activity(false);
				$(id).hide();
				positionContainer();
				
				$(".container").fadeIn('fast');
				
				$(".closex").click(function() {
					
					$(".container").hide();
					$(div).hide();
				});
				
				var currTitle = $(".hdr-title span").filter(":visible");
				var currCopy = $(".copy").filter(":visible");
				var currImg = $(".img img").filter(":visible");
				
				$(".menu-slide").click(function() {
					
					// if not active
					if(!$(this).hasClass('active')) {
			
						var menu = $("#menu ul li");
						var midx = $(menu).index($(this));
					
						// turn off active state of previous button
						$(".menu-slide").each(function() {
							if($(this).hasClass('active')) $(this).removeClass('active');
						});
						
						// apply active state to this button
						$(this).addClass('active');
						
						// switch titles
						var newTitle = $(".hdr-title span").get(midx);
						$(currTitle).fadeOut('fast',function() { $(newTitle).fadeIn('fast'); } );
						currTitle = $(newTitle);
						
						// switch images
						var newImg = $(".img img").get(midx);
						$(currImg).fadeOut('fast',function() { $(newImg).fadeIn('fast'); } );
						currImg = $(newImg);
						
						// switch copy
						var newCopy = $(".copy").get(midx)
						$(currCopy).fadeOut('fast',function() { $(newCopy).fadeIn('fast'); } );
						currCopy = $(newCopy);
						
						// animate background color
						var obj = links[midx];
						$(div).animate({ backgroundColor: obj.bgColor }, 'fast');
						
						// swap class of menu
						var newClass = "menu-color"+(midx+1);
						var oldClass = "menu-color"+(idx+1);
						var ul = $("#menu ul");
						$(ul).removeClass(oldClass);
						$(ul).addClass(newClass);

						// update idx
						idx = midx;
						
					}
					
				});
				
			})
		});
		
		positionTakeover();
		
		function positionTakeover(){
			var wh = $(document).height() - 60;
			$("#takeover").css("height",wh+"px");
			
			// center the indicator
			var id = $("#indicator");
			var ctr = $(document).width()/2;
			$(id).css("left",ctr-20+"px");
			
			positionContainer();
		
		}
		
		$(window)
		.scroll(positionTakeover)
		.resize(positionTakeover);
		
		
		function positionContainer(){
			// center the container
			var ctr = $(document).width()/2;
			var ct = $(".container");
			var cw = 895/2;
			$(ct).css("left",ctr-cw+"px");
		}
		
		
		
		
	
		
		
	} );
	
	
	/// END HOME PAGE TAKEOVERS
	
	
	
	/// TOP NAVIGATION EFFECTS
	/*var $el, leftPos, newWidth;
	$("#top_navigation ul.nav li.current_page_item a").css("color","#FFF");
	$topNav = $("#top_navigation ul.nav");

	$topNav.append("<li id='magic-line'></li>");
    
    var $magicLine = $("#magic-line");
    
    $magicLine
        .width($(".current_page_item").width())
        .height($topNav.height())
        .css("left", $(".current_page_item a").position().left)
		.css("top", "20px")
		.css("height","22px")
        .data("origLeft", $(".current_page_item a").position().left)
        .data("origWidth", $magicLine.width())
                
    $("#top_navigation ul.nav li a").hover(function() {
        $el = $(this);
        leftPos = $el.position().left;
        newWidth = $el.parent().width();
		
		//alert($(".current_page_item a").css("color"));
		
		if(!$el.parent().hasClass("current_page_item") && $(".current_page_item a").css("color") != "rgb(255,255,255)") $(".current_page_item a").css("color","#666");
		
		
        $magicLine.stop().animate({
            left: leftPos,
            width: newWidth
        }, function() { $el.css("color","#FFF") } )
    }, function() {
		$el = $(this);
		$el.css("color","#666")
		if(!$el.parent().hasClass("current_page_item")) $(".current_page_item a").css("color","#FFF");
        $magicLine.stop().animate({
            left: $magicLine.data("origLeft"),
            width: $magicLine.data("origWidth")
        });    
    });
    
    /* Kick IE into gear 
    $(".current_page_item a").mouseenter();*/

	
	
	
	
	// CASE STUDIES
	
	// CASE STUDY BUBBLE UP LG
	
	// preload images
	preload([ '/wp-content/themes/accent/images/ico_Jane-sm.png',
		'/wp-content/themes/accent/images/ico_Rachel-sm.png',
		'/wp-content/themes/accent/images/ico_Bill-sm.png',
		'/wp-content/themes/accent/images/ico_Jake-sm.png',
		'/wp-content/themes/accent/images/ico_Darryl-sm.png',
		'/wp-content/themes/accent/images/bg_bubble-lg_right-cap.png',
		'/wp-content/themes/accent/images/bg_bubble-lg_ctr.png',
		'/wp-content/themes/accent/images/bg_bubble-lg_left-cap.png',
		'/wp-content/themes/accent/images/bg_bubble-lg_point.png']);
	
	// if this is the home page, start the timed reveal of case studies
	var studies, hover;
	var studyIndex = 0;
	if($(".case-study").get(studyIndex)) var startCS = setTimeout(startCaseStudies,8000);
	
	function startCaseStudies() {
		studies = setInterval(function(){
				// if a case study is visible, remove it first
				if($("#bubble-lg").is(":visible")) {
					$("#bubble-lg").remove();
					//clearTimeout(hover);
					studyIndex = (studyIndex>=4)? 0:studyIndex+1;
				}
				
				// reveal case study bubble
				var box = $(".case-study").get(studyIndex);
			
				//$(box).parent().append(makeBubbleLg(studyIndex));
				$(box).append(makeBubbleLg(studyIndex));
				
				// make it display with 0 opacity
				$("#bubble-lg").css('opacity:0');
				$("#bubble-lg").fadeIn('fast');
				
				// get width of copy block
				var copyWidth = document.getElementById("copyStr").offsetWidth;
				
				// position bubble to the right or left of the cursor
				var p = $("#bubble-lg").position();
				var b = $(box).offset(); //console.log(b.left + " -> " + b.top);
	
				var left = (b.left< $(document).width()/2)? p.left - 26:p.left - copyWidth - 26;
				var top = p.top - 88 + 1;
				//var left = (b.left< $(document).width()/2)? b.left - 182:b.left - copyWidth - 182;
				//var top = b.top - 215;
				
				$("#bubble-lg").css( { "left":left+"px", "top":top+"px" } ); 
				$("#bubble-lg-arrow").css( { "left":(left*-1)-5+"px", "top":"72px" } );
				
				//var arrowLeft = (b.left< $(document).width()/2)? 15:15+copyWidth;
				//$("#bubble-lg-arrow").css( { "left":arrowLeft+"px", "top":"72px" } );
				
				$("#bubble-lg").hover(
					function() { stopCaseStudies() },
					function() { 
						var p = $(this).position();
						$(this).fadeOut('fast',function() { $(this).remove() });
						studyIndex++; 
						startCaseStudies(); 
					}
				);
				
				
				$(".open-case-study").click( function() {
			
					$("#bubble-lg").remove();
					openCaseStudy(studyIndex);
					
				});
				
			
		}, 8000);
	}
	
	function stopCaseStudies() {
		clearInterval(studies);
	}
	
	
	// case study on hover
	jQuery.fn.isChildOf = function(b){ 
		return (this.parents(b).length > 0); 
	};
	
	$(".case-study").hover(
		function(e) {
			
			if(!$(this).children().length) {
		
				stopCaseStudies();
				
				if($("#bubble-lg").is(":visible")) {
					$("#bubble-lg").remove();
				}
				
				var idx = $(".case-study").index($(this));
				studyIndex = idx;
				$(this).append(makeBubbleLg(idx));
				
				// make it display with 0 opacity
				$("#bubble-lg").css('opacity:0');
				$("#bubble-lg").fadeIn('fast');
				
				// get width of copy block
				var copyWidth = document.getElementById("copyStr").offsetWidth;
				
				// position bubble to the right or left of the cursor
				if($("#bubble-lg").hasClass('adj')){
				}else{
				var p = $("#bubble-lg").position();
				var left = (e.pageX< $(document).width()/2)? p.left - 26:p.left - copyWidth - 26;
				$("#bubble-lg").addClass('adj')
				}
				var top = p.top - 88 + 1;
				
				$("#bubble-lg").css( { "left":left+"px", "top":top+"px" } );
				$("#bubble-lg-arrow").css( { "left":(left*-1)-5+"px", "top":"72px" } );
				
				/*hover = setTimeout(function(){ 
					//$("#bubble-lg").fadeIn('fast');
					//$("#bubble-lg").animate({ opacity: 1 }, 300);
					//$("#bubble-lg").css('opacity',1);
					$("#bubble-lg").css('display','block');
				}, 500);*/
				
				$(".open-case-study").click( function() {
			
					$("#bubble-lg").remove();
					//clearTimeout(hover);
					openCaseStudy(idx);
					
				});
				
			}

		},
		function() { 
			
			$("#bubble-lg").remove();
			//clearTimeout(hover);
			stopCaseStudies();
			startCaseStudies();
			
		}
	);
	
	$("#bubble-lg").hover(
		function(e) { e.stopPropagation(); /*clearTimeout(hover),*/ stopCaseStudies() },
		function(e) { e.stopPropagation(); startCaseStudies(); }
	);
	
	
	// for links from Success Stories
	$(".success_ct .content a").click(function() { 
	
		var idx = $(".success_ct .content a").index($(this));
		
		openCaseStudy(idx)
	
	});
	
	// for quick links to launch Success Stories
	$(".learn-more-case-study").click(function() {
		
		var idx = 0;
		
		// if idx is a string (from a wordpress link)
		switch($(this).attr("rel")) {
			case 'jane': idx = 0;
			break;
			case 'rachel': idx = 1;
			break;
			case 'bill': idx = 2;
			break;
			case 'jake': idx = 3;
			break;
			case 'darryl': idx = 4;
			break;
		}
		
		openCaseStudy(idx);
		
	});
	
	
	function makeBubbleLg(idx) {
		
		// get the bubble content
		
		var bubble0 = new Object();
		var bubble1 = new Object();
		var bubble2 = new Object();
		var bubble3 = new Object();
		var bubble4 = new Object();
		
		bubble0.img = 'ico_Jane-sm.png';
		bubble0.img_w = 49;
		bubble0.img_h = 103;
		bubble0.str = "<span>JANE</span><br />Learn how we converted Jane from a<br />vocal critic into a brand advocate.";
		
		bubble4.img = 'ico_Darryl-sm.png';
		bubble4.img_w = 40;
		bubble4.img_h = 103;
		bubble4.str = "<span>DARRYL</span><br />Learn how we converted Darryl from a disappointed<br />customer into a satisfied brand advocate.";
		
		bubble1.img = 'ico_Rachel-sm.png';
		bubble1.img_w = 49;
		bubble1.img_h = 103;
		bubble1.str = "<span>RACHEL</span><br />Learn how we converted Rachel from an anxious new<br />customer into an enthusiastic lifetime member.";
		
		bubble3.img = 'ico_Jake-sm.png';
		bubble3.img_w = 47;
		bubble3.img_h = 103;
		bubble3.str = "<span>JAKE</span><br />Learn how we converted Jake from a<br />brand defector into a loyal customer.";
		
		bubble2.img = 'ico_Bill-sm.png';
		bubble2.img_w = 52;
		bubble2.img_h = 103;
		bubble2.str = "<span>BILL</span><br />Learn how we converted Bill from a causal<br />shopper into an engaged brand enthusiast.";
		
		var bubble = eval("bubble"+idx);
		
		var html = "<div id='bubble-lg'>";
		html+="<div class='photo-icon'><img src='/wp-content/themes/accent/images/"+bubble.img+"' width='"+bubble.img_w+"' height='"+bubble.img_h+"' border='0' /></div>";
		html+="<div id='copy'>";
		html += "<ul><li id='copyStr'>"+bubble.str+"<br /><a class='open-case-study'>SEE CASE STUDY &raquo;</a></li>";
		html += "</ul></div><div class='cap-right'></div><div id='bubble-lg-arrow'></div></div>";
		
		return html;	
		
	}
	
	// end CASE STUDY BUBBLE UP LG
	
	var openSlide = 0;
	
	function openCaseStudy(idx) {
		
		// reset openSlide
		openSlide = 0;
		
		// open the modal popup yaay!
		var div = $("#blanket");
		
		// call new content via ajax
		var id = $("#indicator");
		$(id).show();
		$(id).activity({color: '#fff'});
   
 		//  load() functions
		var loadUrl = "/wp-content/themes/accent/case_study_slides.php?slide=slide"+idx;
		 
		$(div).fadeIn('fast', function() {
			$(div).load(loadUrl, null, function(responseText){  
			
				$(id).activity(false);
				$(id).hide();
				positionBlanket();
				
				$(".closex").click(function() {
					
					var temp = $(".case-study_ct").filter(":visible");
					$(temp).hide();
					$(div).hide();
					startCaseStudies();
					
				});
				
				$(".img-swap").hover(
		
					function(){ if(!$(this).hasClass('active')) this.src = this.src.replace("_off","_over") },
					function(){ if(!$(this).hasClass('active')) this.src = this.src.replace("_over","_off")
					
				});
				
				
				
				var className;
				switch(idx) {
					case 0: className = 'jane'; $(".groceries").css("top","158px");
					break;
					case 4: className = 'darryl';
					break;
					case 1: className = 'rachel';
					break;
					case 3: className = 'jake';
					break;
					case 2: className = 'bill';
					break;
				}
				
				$(".case-study_ct .right img").click( function() {
					
					if(!$(this).hasClass("active")) {
			
						// de-activate the active link
						$(".case-study_ct .right img").each(function() {
							if($(this).hasClass("active")) {
								$(this).removeClass("active");
								$(this).css("cursor","pointer");
								this.src = this.src.replace("_over","_off")
							}
						});
						
						
						///////// DETERMINE WHICH CASE STUDY /////////
						var buttonEl;
						var graphicEl;
						var textEl;
						
						switch(true) {
							case $(this).hasClass('jane'): 
								buttonEl = $('.case-study_ct .right img.jane');
								//graphicEl = $(".mobile-layer");
								textEl = $(".cs-jane-text");
							break;
							
							case $(this).hasClass('darryl'): 
								buttonEl = $('.case-study_ct .right img.darryl');
								graphicEl = $(".dvd-layer");
								textEl = $(".cs-darryl-text");
							break;
							
							case $(this).hasClass('rachel'): 
								buttonEl = $('.case-study_ct .right img.rachel');
								graphicEl = $(".scale-layer");
								textEl = $(".cs-rachel-text");
							break;
							
							case $(this).hasClass('jake'): 
								buttonEl = $('.case-study_ct .right img.jake');
								graphicEl = $(".mobile-layer");
								textEl = $(".cs-jake-text");
							break;
							
							case $(this).hasClass('bill'): 
								buttonEl = $('.case-study_ct .right img.bill');
								//graphicEl = $(".mobile-layer");
								textEl = $(".cs-bill-text");
							break;
						}
						
						// index of the button image
						var idx = $(buttonEl).index($(this));
						
						// the visible panel
						var p1 = "";
						$(".right-ct-content").each(function() {
							if( $(this).is(":visible") && $(this).hasClass(className) ) p1 = $(this);
						});
						
						// if switching from first panel (challenge)
						if(!openSlide) {
							// default slide // fade out the slide and the next one in
							var p2 = $('.right-ct-content').get(1);
							$(p1).fadeOut('fast',function() { $(p2).fadeIn('fast') } );
							openSlide = 1;
						}
						
						// if switching from the last panel (results)
						if(openSlide==2) {
							var p2 = $('.right-ct-content').get(1);
							$(p1).fadeOut('fast',function() { $(p2).fadeIn('fast') } );
							openSlide = 1;
						}
						
						// change the spot graphic overlay // MAKE EXCEPTIONS FOR BILL and JANE
						if(className!="bill" && className!="jane" && className!="rachel") {
							var l1 = "";
							var visibleIndex = "";
							
							$(graphicEl).each(function() {
								if( $(this).is(":visible") ) {
									l1 = $(this);
									visibleIndex = $(graphicEl).index($(this));
								}
							});
							
							var l2 = $(graphicEl).get(idx);
							
							if(idx > visibleIndex) {
								$(l2).fadeIn('slow', function() { /*$(l1).hide()*/ } );
								$(l1).fadeOut('fast')
							} else {
								$(l2).show(function() {  } );	
								$(l1).fadeOut('fast')
							}
						}
						
						// spot graphic script for BILL
						if(className=='bill') {
							// animate the height of the mask according to which slide we're on
							var new_height;
							switch(idx) {
								case 0: new_height = 185;
								break;
								case 1: new_height = 138;
								break;
								case 2: new_height = 92;
								break;
								case 3: new_height = 46;
								break;
								case 4: new_height = 1;
								break;
							}
							
							$('.mask').animate({
							  height: new_height+"px"
							}, 350, function() {
							  // Animation complete.
							});
						}
						
						// spot graphic script for JANE
						if(className=='jane') {
							// animate the top of the groceries according to which slide we're on
							var new_top;
							switch(idx) {
								case 0: new_top = 158;
								break;
								case 1: new_top = 120;
								break;
								case 2: new_top = 85;
								break;
								case 3: new_top = 45;
								break;
								case 4: new_top = 5;
								break;
							}
							
							$('.groceries').animate({
							  top: new_top+"px"
							}, 350, function() {
							  // Animation complete.
							});
						}
						
						// spot graphic script for RACHEL
						if(className=='rachel') {  
							// animate the word going by the center of the scale
							var new_left;
							switch(idx) {
								case 0: new_left = 131;
								break;
								case 1: new_left = 106;
								break;
								case 2: new_left = 81;
								break;
								case 3: new_left = 56;
								break;
								case 4: new_left = 31;
								break;
							}
							
							if($('.lifetime').css('left') != '0px') {
							
								$('.lifetime').animate({
								  left: new_left+"px"
								}, 350, function() {
								  // Animation complete.
								});
								
							} else {
								$('.lifetime').css('left','131px');
							}
						}
							
						// swap the text
						var t1 = "";
						
						$(textEl).each(function() {
							if( $(this).is(":visible") ) t1 = $(this);
						});
						
						var t2 = $(textEl).get(idx);
						
						if(t1!="") {
							$(t1).fadeOut('fast',function() { $(t2).fadeIn('fast') } );
						} else {
							$(t2).fadeIn('fast');
						}
					
						this.src = this.src.replace("_off","_over"); // swap the image in case this is a scripted click
						$(this).addClass("active");
						$(this).css("cursor","default");
						
					} // end if not has active class
					
				}); // end case-study_ct .right img click
				
				
				$("a.lg").click( function() { /// for clicks coming from the text links rather than the buttons
					
					var idx = $("a.lg").index($(this));
					var obj, btn, text, graphic;

					switch(className) { 
						case 'jane': obj = $('.case-study_ct .right img.jane'); text = $(".cs-jane-text"); graphic = $(".mobile-layer");
						break;
						case 'darryl': obj = $('.case-study_ct .right img.darryl'); text = $(".cs-darryl-text"); graphic = $(".dvd-layer");
						break
						case 'rachel': obj = $('.case-study_ct .right img.rachel'); text = $(".cs-rachel-text"); graphic = $(".mobile-layer");
						break;
						case 'jake': obj = $('.case-study_ct .right img.jake'); text = $(".cs-jake-text"); graphic = $(".mobile-layer");
						break;
						case 'bill': obj = $('.case-study_ct .right img.bill'); text = $(".cs-bill-text"); graphic = $(".mobile-layer");
						break;
					}
					
					btn = $(obj).get(idx);
					
					if($(btn)) $(btn).click();
					
					if(!btn) { // show RESULTS
					
						var p1 = $('.right-ct-content').get(1);
						var p2 = $('.right-ct-content').get(2);
						$(p1).fadeOut('fast',function() { $(p2).fadeIn('fast') } );
						
						// turn off the optimize button
						var ob = $(obj).get(4);
						$(ob).removeClass('active');
						ob.src = ob.src.replace("_over","_off");
						
						// hide the visible text
						var p3;
						
						$(text).each(function() {
							if( $(this).is(":visible") ) p3 = $(this);
						});
						
						$(p3).fadeOut('fast');
						
						// hide the visible graphic element
						var p4;
						$(graphic).each(function() {
							if( $(this).is(":visible") ) p4 = $(this);
						});
						
						$(p4).fadeOut('fast');
						
						openSlide = 2;
						
					}
					
				});

				
				
			}) // end .load
			
		});
		
		positionBlanket();
		
		function positionBlanket(){
			var wh = $(document).height();
			$("#blanket").css("height",wh+"px");
			
			// center the indicator
			var id = $("#indicator");
			var ctr = $(document).width()/2;
			$(id).css("left",ctr-20+"px");
			
			positionCaseStudy();
		
		}
		
		$(window)
		.scroll(positionBlanket)
		.resize(positionBlanket);
		
		
		function positionCaseStudy(){
			// center the container
			var ctr = $(document).width()/2;
			var ct = $(".case-study_ct");
			var cw = 948/2;
			$(ct).css( {"left":ctr-cw+"px","top":"110px"});
		}
		
	}
	
	// 
	
	
	// img swap
	$(".img-swap").hover(
		
			function(){ if(!$(this).hasClass('active')) this.src = this.src.replace("_off","_over") },
			function(){ if(!$(this).hasClass('active')) this.src = this.src.replace("_over","_off")
			
			});
			
	// image preload
	function preload(arrayOfImages) {
		$(arrayOfImages).each(function(){
			$('<img/>')[0].src = this;
			// Alternatively you could use:
			// (new Image()).src = this;
		});
	}

	
	
	
	// disable right-click on logo
	$(".img-logo").bind("contextmenu", function(e) {
		e.stopPropagation()
    	e.preventDefault();
    });
	
	
});
