/* Author: Tribal DDB

*/

var navInterval;

function rtrim(str, chars) {
	if(str!=null){
		chars = chars;
		return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
	}
}

$(document).ready(function(){
	$.fn.placeholder = function(){

            // quit if there's support  for html5 placeholder
            if (this[0] && 'placeholder' in document.createElement('input')) return;

            return this
            .live('focusin',function(){
                if ($(this).val() === $(this).attr('placeholder')) {
                    $(this).val('');
                }
            }).live('focusout',function(){
                if ($(this).val() === ''){
                    $(this).val( $(this).attr('placeholder') );
                    if($(this).attr('type')==='password') {
                        $(".password-field label").removeClass('hidden');
                        $(".password-field").addClass('needed');
                    }
                }
            }).focusout();
    }
    $('input[placeholder]').placeholder();
	
	//Header dropdowns
	$('.hat ul ul').css({'opacity':'0','overflow':'hidden'});
	$('.hat a').click(function(e){
		if($(this).attr('href')=='#'){
			e.preventDefault();
			if($(this).next().outerHeight()==0){
				var toHeight = $(this).next().children().outerHeight();
				var children= $(this).next().children();
				$(this).next().stop().animate({'height':children.length*toHeight,'padding-bottom':'5px','opacity':'1'},500);
			} else {
				$(this).next().stop().animate({'height':'0px',"padding-bottom":'0px','opacity':'0'},500);
			}
		}
	});
	$('.tool-bar ul ul').animate({'height':0},0).removeClass('preload');
	$('.tool-bar a').click(function(e){
		if($(this).attr('href')=='#'){
			e.preventDefault();
			if($(this).next().outerHeight()<150){
				$(this).next().addClass('open').stop().animate({'height':150},500,function(){
					$(this).addClass('scrolling');
					$('#container').click(function(){
						$('.language').next().removeClass('scrolling').stop().animate({'height':0},500,function(){
							$(this).removeClass('open');
							$('#container').unbind('click');
						});
					});
				});
			} else {
				$('#container').unbind('click');
				$(this).next().removeClass('scrolling').stop().animate({'height':0},500,function(){
					$(this).removeClass('open');
				});
			}
		}
	});
	
	//Home page intro animations for browsers w/o CSS animations.  Switch animationCheck to "" for all browsers.
	var animationCheck = ".no-cssanimations ";
	$(animationCheck + '.home h1.home').stop().delay(500).animate({'left':'0px'},1000,'easeInOutQuad');
	//$(animationCheck + '.home nav.left').stop().delay(1500).animate({'left':'0px'},1000,'easeInOutQuad');
	$(animationCheck + '.home .intro .first').stop().delay(1500).animate({'left':'0px'},1000,'easeInOutQuad');
	if($('html').hasClass('ie7')){
		$(animationCheck + '.home .persona .feature-button .button').css('opacity',0).stop().delay(2500).animate({'opacity':'1'},1000,'easeInOutQuad');
	} else if (!$('html').hasClass('ie8')) {
		$(animationCheck + '.home .persona .feature-button').css('opacity',0).stop().delay(2500).animate({'opacity':'1'},1000,'easeInOutQuad');
	} else {
		$(animationCheck + '.home .persona .feature-button .button').addClass('button-0');
		setTimeout(function(){
			var ie8Loop = 0;
			var ie8LoopInterval;
			ie8LoopInterval = setInterval(function(){
				ie8Loop++;
				if(ie8Loop==21){
					$(animationCheck + '.home .persona .feature-button .button').attr('class','button');
					clearInterval(ie8LoopInterval);
				} else {
					$(animationCheck + '.home .persona .feature-button .button').addClass('button-'+ie8Loop);
				}
			},40);	
		},2500);	
	}
	$('.persona .feature-button').removeClass('preload');
	setTimeout(function(){
		$('div.home').removeClass('home');
	},3500);
	
	//Home page event animations
	var ieButtonFixSee;
	var ieButtonFixReturn;
	$('.intro .first a').live('click',function(e){
		e.preventDefault();
		$('nav.left').addClass('open-nav').stop().animate({'left':'-180px'},1000,'easeInOutQuad');
		if (!$('html').hasClass('ie8')) {
			$('.persona .feature-button').stop().animate({'opacity':'0'},1000,'easeInOutQuad');
		} else {
			$('.persona .button').addClass('button-20');
			setTimeout(function(){
				var ie8Loop = 20;
				var ie8LoopInterval;
				ie8LoopInterval = setInterval(function(){
					ie8Loop--;
					if(ie8Loop==-1){
						$('.persona .button').addClass('button-0');
						clearInterval(ie8LoopInterval);
					} else {
						$('.persona .button').addClass('button-'+ie8Loop).removeClass('button-'+(ie8Loop+1));
					}
				},40);	
			},0);	
		}
		if($('html').hasClass('ie')){
			ieButtonFixSee=$(".intro .first .button-bkg").html();
			$(".intro .first .button-bkg").remove();
			ieButtonFixReturn=$(".intro .second .button-bkg").html();
			$(".intro .second .button-bkg").remove();
		}
		$('.intro .first').stop().animate({'opacity':'0'},1000,'easeInOutQuad',function(){
			$('.intro .first').addClass("preload");
			$('.intro .second').css('opacity',0).removeClass('preload').stop().animate({'opacity':'1'},1000,'easeInOutQuad',function(){
				if($('html').hasClass('ie')){
					$(".intro .first").attr('style','left:0;').append('<div class="button-bkg png_bg6"/>');
					$(".intro .first .button-bkg").html(ieButtonFixSee);
					$(".intro .second").attr('style','left:0;').append('<div class="button-bkg png_bg6"/>');
					$(".intro .second .button-bkg").html(ieButtonFixReturn);
					if($('html').hasClass('ie6')){
						DD_belatedPNG.fix('.png_bg6');
					}
				}	
			});
			if (!$('html').hasClass('ie8')) {
				$('.blister').css('opacity',0).removeClass('preload').stop().delay(500).animate({'opacity':'1'},1000,'easeInOutQuad');
			} else {
				$('.blister').addClass('blister-1').removeClass('preload');
				$('.blister .trial').css('opacity',0).stop().delay(500).animate({'opacity':'1'},1000,'easeInOutQuad');
				setTimeout(function(){
					var ie8Loop = 0;
					var ie8LoopInterval;
					ie8LoopInterval = setInterval(function(){
						ie8Loop++;
						if(ie8Loop==21){
							$('.blister').attr('class','blister');
							clearInterval(ie8LoopInterval);
							$('.ie .blister .button-bkg, .ie .blister .button-left, .ie .blister .button-right').css('position','relative');
						} else {
							$('.blister').attr('class','blister').addClass('blister-'+ie8Loop);
						}
					},40);	
				},500);
			}
		});
		$('.intro').stop().animate({'left':'70px'},1000,'easeInOutQuad');
		$('.persona .bkg').stop().animate({'left':'-1240px'},1000,'easeInOutQuad',function(){
			$('nav.left').addClass('show-open');
		});
	});
	$('.intro .second a').live('click',function(e){
		e.preventDefault();
		$('.blister').removeClass('blister-open');
		$('nav.left').removeClass('open-nav');
		if (!$('html').hasClass('ie8')) {		
			$('.blister').stop().animate({'opacity':'0'},1000,'easeInOutQuad',function(){
				$(this).addClass('preload');	
			});
		} else {
			$('.blister').addClass('blister-20');
			$('.blister .trial, .blister .get-trial').stop().animate({'opacity':'0'},1000,'easeInOutQuad');
			setTimeout(function(){
				var ie8Loop = 20;
				var ie8LoopInterval;
				$('.ie .blister .button-bkg, .ie .blister .button-left, .ie .blister .button-right').css('position','static');
				ie8LoopInterval = setInterval(function(){
					ie8Loop--;
					if(ie8Loop==0){
						$('.blister').attr('class','preload').addClass('blister');
						clearInterval(ie8LoopInterval);
					} else {
						$('.blister').attr('class','blister').addClass('blister-'+ie8Loop);
					}
				},40);	
			},0);	
		}
		$('nav.left').removeClass('show-open');
		if($('html').hasClass('ie')){
			ieButtonFixSee=$(".intro .first .button-bkg").html();
			$(".intro .first .button-bkg").remove();
			ieButtonFixReturn=$(".intro .second .button-bkg").html();
			$(".intro .second .button-bkg").remove();
		}
		$('.intro .second').stop().delay(500).animate({'opacity':'0'},1000,'easeInOutQuad',function(){
			$(this).addClass('preload');
			$('.intro .first').css({'opacity':'0'}).removeClass("preload").stop().animate({'opacity':'1'},1000,'easeInOutQuad');
			if (!$('html').hasClass('ie8')) {
				$('.persona .feature-button').stop().delay(500).animate({'opacity':'1'},1000,'easeInOutQuad');
			} else {
				$('.persona .button').addClass('button-0');
				setTimeout(function(){
					var ie8Loop = 0;
					var ie8LoopInterval;
					ie8LoopInterval = setInterval(function(){
						ie8Loop++;
						if(ie8Loop==21){
							$('.persona .button').attr('class','button');
							clearInterval(ie8LoopInterval);
						} else {
							$('.persona .button').addClass('button-'+ie8Loop);
						}
					},40);	
				},500);	
			}
			$('nav.left').stop().animate({'left':'0px'},1000,'easeInOutQuad', function(){					
				if($('html').hasClass('ie')){
					$(".intro .first").attr('style','left:0;').append('<div class="button-bkg png_bg6"/>');
					$(".intro .first .button-bkg").html(ieButtonFixSee);
					if($('html').hasClass('ie6')){
						DD_belatedPNG.fix('.png_bg6');
					}
				}
			});
			$('.intro').stop().animate({'left':'595px'},1000,'easeInOutQuad');
			$('.persona .bkg').stop().animate({'left':'0px'},1000,'easeInOutQuad');
		});
	});
	
	$('.open-nav').live('mouseover',function(){
		$('nav.left').removeClass('show-open').stop().animate({'left':'0px'},1000,'easeInOutQuad');
	}).live('mouseout',function(){
		$('nav.left').removeClass('show-open').stop().animate({'left':'-180px'},1000,'easeInOutQuad');
	})
	
	$('.ie6 .feature-button, .ie7 .feature-button').hover(function(){
		$(this).toggleClass("feature-button-hover");
	},function(){
		$(this).toggleClass("feature-button-hover");
	})
	
	$('.blister .trial').click(function(e){
		e.preventDefault();
		$('.blister').addClass('blister-open');
	});
	
	//Nav hover event animations
	var preventClick = 0;
	$('nav.left ul.nav .nav-element').hover(function(){
		preventClick=1;
		clearInterval(navInterval);
		var thisChild = $(this).children('.panel');
		var panelCount = $('nav.left ul.nav .nav-element').index(this);
		var panelSmall = $('.panel:eq('+panelCount+') .small');
		var panelMedium = $('.panel:eq('+panelCount+') .medium');
		var panelLarge = $('.panel:eq('+panelCount+') .large');
		navInterval = setInterval(function() {
			var openTo = panelSmall.length*124+panelMedium.length*248+panelLarge.length*372;
			$('.panel:eq('+panelCount+') .panel-columns').css('width',openTo+'px');
			$('.panel:eq('+panelCount+') .panel-holder').css('width',openTo+27+'px');
			var beThis = thisChild.children('.panel-holder').outerHeight();
			$('nav.left .current,nav.left .pointer').css('z-index',1).removeClass('pointer').removeClass('current').stop().animate({'width':0},500);
			thisChild.addClass('pointer').css({'z-index':2,"height":beThis}).stop().animate({'width':openTo+27+'px'},500,function(){
				$(this).addClass('current');
			});
			clearInterval(navInterval);
		}, 100);
	},function(){
		clearInterval(navInterval);
		$('open-this').removeClass('pointer');
		navInterval = setInterval(function() {
			$('nav.left .panel').css('z-index',1).removeClass('pointer').removeClass('current').stop().animate({'width':0},500);
			clearInterval(navInterval);
		}, 100);
	});
	
	$('.eye-bottom a').hover(function(){
		$('.eye-bkg').addClass('eye-hover');
		preventClick=1;
		clearInterval(navInterval);
		var thisChild = $('.panel:last');
		var panelCount = $('nav.left ul.nav .nav-element');
		panelCount=panelCount.length-1;
		var panelSmall = $('.panel:eq('+panelCount+') .small');
		var panelMedium = $('.panel:eq('+panelCount+') .medium');
		var panelLarge = $('.panel:eq('+panelCount+') .large');
		navInterval = setInterval(function() {
			var openTo = panelSmall.length*124+panelMedium.length*248+panelLarge.length*372;
			$('.panel:eq('+panelCount+') .panel-columns').css('width',openTo+'px');
			$('.panel:eq('+panelCount+') .panel-holder').css('width',openTo+27+'px');
			var beThis = thisChild.children('.panel-holder').outerHeight();
			$('nav.left .current,nav.left .pointer').css('z-index',1).removeClass('pointer').removeClass('current').stop().animate({'width':0},500);
			thisChild.addClass('pointer').css({'z-index':2,"height":beThis}).stop().animate({'width':openTo+27+'px'},500,function(){
				$(this).addClass('current');
			});
			clearInterval(navInterval);
		}, 100);
	},function(){
		$('.eye-bkg').removeClass('eye-hover');
		clearInterval(navInterval);
		$('open-this').removeClass('pointer');
		navInterval = setInterval(function() {
			$('nav.left .panel').css('z-index',1).removeClass('pointer').removeClass('current').stop().animate({'width':0},500);
			clearInterval(navInterval);
		}, 100);
	});
	
	//Touch Fall Back
	$('nav.left ul.nav .nav-element a.nav-link').mouseover(function(e){
		e.preventDefault();	
		var thisChild1 = $(this).next('.panel');
		if(preventClick==0){
			if($(thisChild1).css('width')!='0px'){
				$('open-this').removeClass('pointer');
				$('nav.left .panel').css('z-index',1).removeClass('pointer').removeClass('current').stop().animate({'width':0},500);		
				$('#container').unbind('click');
			} else {
				var openTo1 = thisChild1.children('.panel-holder').outerWidth();
				var beThis1 = thisChild1.children('.panel-holder').outerHeight();
				$('nav.left .current,nav.left .pointer').css('z-index',1).removeClass('pointer').removeClass('current').stop().animate({'width':0},500);
				thisChild1.addClass('pointer').css({'z-index':2,"height":beThis1}).stop().animate({'width':openTo1},500,function(){
					$(this).addClass('current');	
					$('#container').click(function(){
						$('open-this').removeClass('pointer');
						$('nav.left .panel').css('z-index',1).removeClass('pointer').removeClass('current').stop().animate({'width':0},500);		
						$('#container').unbind('click');
					});
				});
			}
		}
	});
	$('nav.left ul.nav .nav-element a.nav-link').click(function(e){
		e.preventDefault();	
		var thisChild1 = $(this).next('.panel');
		if(preventClick==0){
			if($(thisChild1).css('width')!='0px'){
				$('open-this').removeClass('pointer');
				$('nav.left .panel').css('z-index',1).removeClass('pointer').removeClass('current').stop().animate({'width':0},500);		
				$('#container').unbind('click');
			} else {
				var openTo1 = thisChild1.children('.panel-holder').outerWidth();
				var beThis1 = thisChild1.children('.panel-holder').outerHeight();
				$('nav.left .current,nav.left .pointer').css('z-index',1).removeClass('pointer').removeClass('current').stop().animate({'width':0},500);
				thisChild1.addClass('pointer').css({'z-index':2,"height":beThis1}).stop().animate({'width':openTo1},500,function(){
					$(this).addClass('current');	
					$('#container').click(function(){
						$('open-this').removeClass('pointer');
						$('nav.left .panel').css('z-index',1).removeClass('pointer').removeClass('current').stop().animate({'width':0},500);		
						$('#container').unbind('click');
					});
				});
			}
		}
	});
	
	//Filmstrip Navigation hover
	/*
	var originState;
	$('.filmstrip .three-cells a').hover(function(){
		originState=$('.filmstrip').attr('class');
		var current=originState.split(' ');
		if(current[0]=='filmstrip'){
			current=current[1];
		} else {
			current=current[0];
		}
		if(current=='filmstrip-toric') {
			if($('.filmstrip a').index(this)==0){
				$('.filmstrip').css('background-position','0 -87px');
			} else if($('.filmstrip a').index(this)==2){
				$('.filmstrip').css('background-position','0 -522px');
			} else if($('.filmstrip a').index(this)==3){
				$('.filmstrip').css('background-position','0 -609px');
			}
		}
		if(current=='filmstrip-aqua') {
			if($('.filmstrip a').index(this)==1){
				$('.filmstrip').css('background-position','0 -87px');
			} else if($('.filmstrip a').index(this)==2){
				$('.filmstrip').css('background-position','0 -174px');
			} else if($('.filmstrip a').index(this)==3){
				$('.filmstrip').css('background-position','0 -261px');
			}
		}
	}, function() {
		$('.filmstrip').attr('style','');
	});
	*/
	// USE ONLY WITH 5 CELL FILM STRIP
	
	$('.filmstrip a').hover(function(){
		originState=$('.filmstrip').attr('class');
		var rotateOn=originState.split(' ');
		if(rotateOn[0]=='filmstrip'){
			rotateOn=rotateOn[1];
		} else {
			rotateOn=rorateOn[0];
		}
		if(rotateOn=='filmstrip-toric')	{
			if($('.filmstrip a').index(this)==0){
				$('.filmstrip').css('background-position','0 -87px');
			} else if($('.filmstrip a').index(this)==2){
				$('.filmstrip').css('background-position','0 '+(7*-87)+'px');
			} else if($('.filmstrip a').index(this)==3){
				$('.filmstrip').css('background-position','0 '+(8*-87)+'px');
			} else if($('.filmstrip a').index(this)==4){
				$('.filmstrip').css('background-position','0 '+(9*-87)+'px');
			}else if($('.filmstrip a').index(this)==5){
				$('.filmstrip').css('background-position','0 '+(16*-87)+'px');
			}
		} else if(rotateOn=='filmstrip-aqua')	{
			if($('.filmstrip a').index(this)==1){
				$('.filmstrip').css('background-position','0 -87px');
			} else if($('.filmstrip a').index(this)==2){
				$('.filmstrip').css('background-position','0 '+(2*-87)+'px');
			} else if($('.filmstrip a').index(this)==3){
				$('.filmstrip').css('background-position','0 '+(3*-87)+'px');
			} else if($('.filmstrip a').index(this)==4){
				$('.filmstrip').css('background-position','0 '+(4*-87)+'px');
			}else if($('.filmstrip a').index(this)==5){
				$('.filmstrip').css('background-position','0 '+(15*-87)+'px');
			}
		} else if(rotateOn=='filmstrip-total')	{
			if($('.filmstrip a').index(this)==0){
				$('.filmstrip').addClass(rotateOn+"-1");
			} else if($('.filmstrip a').index(this)==2){
				$('.filmstrip').addClass(rotateOn+"-2");
			} else if($('.filmstrip a').index(this)==3){
				$('.filmstrip').addClass(rotateOn+"-3");
			} else if($('.filmstrip a').index(this)==4){
				$('.filmstrip').addClass(rotateOn+"-4");
			}
		}
	},function(){
		$('.filmstrip').removeAttr('style');	
	});
	
	//Button Hover
	$('.button-bkg a').live('mouseover mouseout', function(event) {
	  if (event.type == 'mouseover') {
		$(this).parent().parent().parent().addClass('button-bkg-hover');
		if($(this).children('img')){
			var buttonImage = $(this).children('img').attr('src');
			buttonImage = rtrim(buttonImage,'.gif');
			buttonImage = buttonImage + '-hover.gif';
			$(this).children('img').attr('src',buttonImage);
		}
	  } else {
		$('.button-bkg-hover').removeClass('button-bkg-hover');
		if($(this).children('img')){
			var buttonImage = $(this).children('img').attr('src');
			buttonImage = rtrim(buttonImage,'-hover.gif');
			buttonImage = buttonImage + '.gif';
			$(this).children('img').attr('src',buttonImage);
		}
	  }
	});
	
	
	//Inner page acordion
	$("nav a").click(function(event) {
		var destination = $(this).attr('href');
		var url = location.href;
		if(destination.search('#')!=-1){
			destination=destination.split("/");
			destination=destination[destination.length-1];
			destination=destination.split('#');
			var accordion = $('.accordion');
			if(url.search(destination[0])!=-1 && accordion.length>0){
				$('dt.'+destination[1]+' a').click();
				$('open-this').removeClass('pointer');
				$('nav.left .panel').css('z-index',1).removeClass('pointer').removeClass('current').stop().animate({'width':0},500);
			}
			var goTo = rtrim(destination[1],'trial');
			var goTo = rtrim(goTo,'-');
			$('#freeTrialForm input').removeAttr('CHECKED');
			$('#'+goTo).attr('CHECKED','checked');
		}
	}); 
	$('.accordion dd').wrapInner('<div class="accordion-content" />');
	$('.accordion dd').stop().animate({'height':'1px'},0);	
	var url = window.location.href;
	var urlParts = url.split('#');
	var accordions = $('.accordion');
	if(accordions.length>0 && (!$('.accordion').hasClass('faq') || (urlParts.length!=1 && urlParts[1]!=''))){
		if(urlParts.length==1 || urlParts[1]=='') {
			$('.accordion dd:eq(0),.accordion dt:eq(0)').addClass('current');
		} else {
			$('.accordion dt.'+urlParts[1]).addClass('current').next().addClass('current');
		}
		$('.suggestion ul').html($('dd.current .suggestions').html());
		var checkHeight = $('.current .accordion-content').outerHeight();
		if(checkHeight>=289) {
			$('.current .accordion-content').addClass('accordion-content-scroll');
			checkHeight=290;
		}
		$('dd.current').stop().animate({'height':checkHeight},0);
	}
	$('.accordion dt a').click(function(e){
		e.preventDefault();
		if(!$(this).parent().next().children('.accordion-content').hasClass('current')){
			$('.accordion dt.current').removeClass('current');
			$(this).parent().addClass('current');
			$('.accordion dd.current').removeClass('current').stop().animate({'height':'1px'},500,function(){
				$(this).children('.accordion-content').removeClass('accordion-content-scroll');	
			});
			var toHeight = $(this).parent().next().children('.accordion-content').outerHeight();
			if(toHeight>=289) { 
				toHeight='290px';
				$(this).parent().next().children('.accordion-content').addClass('accordion-content-scroll');
			}	
			$(this).parent().next().addClass('current').stop().animate({'height':toHeight},500);
			$('.suggestion ul').html($('dd.current .suggestions').html());
		}
	})
	$('.expand-all a').live('click',function(e){
		e.preventDefault();
		if($(this).hasClass('hide')){
			$(this).parent().html('Click <a href="#" title="Hide All" class="expand">here</a> to expand all answers.');	
			$('.accordion dt.current').removeClass('current');
			$('.accordion dd.current').removeClass('current').stop().animate({'height':'1px'},500,function(){
				$(this).children('.accordion-content').removeClass('accordion-content-scroll');	
			});
		} else {
			$(this).parent().html('Click <a href="#" title="Hide All" class="hide">here</a> to hide all answers.');
			var answers = $('.accordion dd');
			for (i=0;i<answers.length;i++){
				var toHeight = $('.accordion dd:eq('+i+')').children('.accordion-content').outerHeight();
				if(toHeight>=289) { 
					toHeight='290px';
					$('.accordion dd:eq('+i+')').children('.accordion-content').addClass('accordion-content-scroll');
				}
				$('.accordion dt:eq('+i+')').addClass('current')
				$('.accordion dd:eq('+i+')').addClass('current').stop().animate({'height':toHeight},500);
			}
			
		}
	});
	
	var isHome = $('.home');
	if(isHome.length==0){
		$('body').addClass("inner");
		$('#main').css('overflow','visible');
		$('.inner #main').children().wrapAll('<div class="inner-container" />');
	}
	
	var withTrial = $('#freeTrialForm');
	if(withTrial.length==1){
		var url = window.location.href;
		var urlParts = url.split('#');
		var goTo = rtrim(urlParts[1],'trial');
		var goTo = rtrim(goTo,'-');
		$('#'+goTo).attr('CHECKED','false');
	}
});

 
