/* Client: First Pacific Advisors
 * Company: 1/0
 * Author: Andrew Henderson
 * Email: ahenderson@1over0.com
 * Twitter: twitter.com/1over0tech
*/
var startOnSlide;
$(document).ready(function() {
	
	// Load External Functions
	carousels();
	dropdown();
	team();
	
	// make variable height divs equal heights
    $.fn.sameHeights = function() {
        $(this).each(function(){
            var tallest = 0;
            $(this).children().each(function(i){
                if (tallest < $(this).height()) { tallest = $(this).height(); }
            });
            $(this).children().css({'height': tallest});
        });
        return this;
    };

	// make children divs same height
	$('.sameHeights').sameHeights();
	
  
  var loc = document.location.href;
  if (loc.indexOf('#')!=-1)
  {
    var parts = loc.split('#');
    startOnSlide = parseInt(parts[1]) - 1;
  }
	$(window).load(function() {  
    

        $('#slider').nivoSlider({
            effect:'fade', // Specify sets like: 'fold,fade,sliceDown'
            slices:1, // For slice animations
            boxCols: 1, // For box animations
            boxRows: 1, // For box animations
            animSpeed:500, // Slide transition speed
            pauseTime:5000, // How long each slide will show
            startSlide:startOnSlide || 0, // Set starting Slide (0 index)
            directionNav:false, // Next & Prev navigation
            directionNavHide:false, // Only show on hover
            controlNav:true, // 1,2,3... navigation
            controlNavThumbs:false, // Use thumbnails for Control Nav
            controlNavThumbsFromRel:false, // Use image rel for thumbs
            controlNavThumbsSearch: '.jpg', // Replace this with...
            controlNavThumbsReplace: '_thumb.jpg', // ...this in thumb Image src
            keyboardNav:true, // Use left & right arrows
            pauseOnHover:true, // Stop animation while hovering
            manualAdvance:false, // Force manual transitions
            captionOpacity:1.0, // Universal caption opacity
            prevText: 'Prev', // Prev directionNav text
            nextText: 'Next', // Next directionNav text
            beforeChange: function(){}, // Triggers before a slide transition
            afterChange: function(){}, // Triggers after a slide transition
            slideshowEnd: function(){}, // Triggers after all slides have been shown
            lastSlide: function(){}, // Triggers when last slide is shown
            afterLoad: function(){} // Triggers when slider has loaded
        }); // eo nivo
    }); // eo window load
	

  $('a.button2.dialog').click(function(){
    formDialog('chart', this.href, this.getAttribute('content'));
    return false;
  });

}); // eo doc ready

// Begin External Functions

function carousels() {

	// Splash Page Carousel
    $(".carousel").jCarouselLite({
		easing: "easeInOutQuint",
		speed: 900,
		//auto: 5000,
		visible: 1,
		scroll: 1,
		circular: false,
	    start: 0,
	    btnNext: ".carousel-nav.next",
	    btnPrev: ".carousel-nav.prev"
    });

}

function dropdown() {
	
	$("header .login").after('<div class="login-sub"><a href="https://fpafunds.olaccess.com/">Mutual Funds</a><a href="https://www.jdclark.com/i_login.shtml">Private Funds</a></div>');
	$("header .login").click(function(){
		$(this).blur().toggleClass("open");
		if($(this).hasClass("open")){
			$("header .login-sub").slideDown();
		}
		else {
			$("header .login-sub").slideUp();
		}
		return false;
	});
	
	$("body").click(function(){
		$(this).removeClass("open");
		$("header .login-sub").slideUp();
    $("header .fund-pricing-sub").slideUp(function(){
     $('.fund-pricing span').css({padding: '5px 0'}); 
     $('.fund-pricing').removeClass('open');
    });
	});



  $("header .fund-pricing").after('<div class="fund-pricing-sub"><div class="pricing-data"></div></div>');
	$("header .fund-pricing").click(function(){
		$(this).blur().toggleClass("open");
    $('.fund-pricing span').css({padding:'5px 0 8px'});
		if($(this).hasClass("open")){
      
			$("header .fund-pricing-sub").slideDown();
		}
		else {
      
			$("header .fund-pricing-sub").slideUp(function(){
       $('.fund-pricing span').css({padding:'5px 0'}); 
      });
		}
		return false;
	});
	
  var fund_pricing_date = '08/25/2011';//new Date();
 /* var html = '<div class="fund-title">DAILY NAV AS OF '+ fund_pricing_date +'</div>';
  html+='<table cellspacing="0" width="100%">';
  html+='<thead><tr><td>Fund</td><td>Ticker</td><td>Nav</td><td>Change</td></tr></thead>';
  html+='<tbody id="fund_pricing">';

  html+='<tr><td>FPA Capital Fund</td><td>FPPTX</td><td>$37.74</td><td>($0.21)</td></tr>';
  html+='<tr><td>FPA Crescent Fund</td><td>FPACX</td><td>$25.48</td><td>($0.02)</td></tr>';
  html+='<tr><td>FPA New Income</td><td>FPNIX</td><td>$10.84</td><td>$0.05</td></tr>';
  html+='<tr><td>FPA Paramount</td><td>FPRAX</td><td>$15.59</td><td>($0.13)</td></tr>';
  html+='<tr><td>FPA Perennial</td><td>FPFFX</td><td>$30.93</td><td>($0.14)</td></tr>';
  html+='<tr><td>Source Capital</td><td>SOR</td><td>$47.07</td><td>($0.10)</td></tr>';

  html+='</tbody></table>';
  html+='<div class="fund-foot">(Closing Price $56.26)</div>';*/

  $.ajax({
    type: 'get',
    url:'fund-pricing.html',
    success: function(data){

        $('.fund-pricing-sub .pricing-data').html(data);
    },
    error: function() { 
      //alert('Error loading pricing data'); 
     }
  }) 

}

function team() {
	
	$(".cols .col ul.style2 li a[style='color:#12595e;']").removeAttr("style").addClass("active");
	
}




var formDialog = function(type,pdfs,content,terms) {
  
  var cw = window.innerWidth || document.body.clientWidth || document.documentElement.clientWidth,
  ch = window.innerHeight || document.body.clientHeight || document.documentElement.clientHeight,
  
  modal = $('<div id="formDialogModal">').css({
    position:'fixed',
    top:0, right:0, bottom:0, left:0,
    background:'rgba(0,0,0,0.6)',
    zIndex:10
  }),

  dlg = $('<div id="formDialog">').css({
    position:'fixed',
    width:600,
    minHeight:300,
    left: ( ( cw / 2 ) - 300 ),
    background:'#fff',
    boxShadow:'rgba(0,0,0,0.3) 0 0 5px',
    border:'1px solid #eee',
    zIndex:11,
    visibility:'hidden'
  }),

  dlghead = $('<div>').css({
    background:'#0b595e',
    color:'#FFFFFF',
    padding:17
  }).append('<img width="170" height="79" alt="First Pacific Advisors" src="img/skin/logo.png">');

  dlg.append(dlghead);

  if (type== 'non-retirement')
  {
    dlg.append(contentNonRetirement(pdfs,terms))
  }
  else if (type == 'ira')
  {
    dlg.append(contentIraForms(pdfs,terms))
  }
  else if (type == '403b')
  {
    dlg.append(content403bForms())
  }
  else if (type == 'chart')
  {
    dlg.append(contentChartForms(pdfs,content));
  }
  else if (type == 'custom')
  {
    dlg.append(contentCustomForm(content));
  }
  else {
    // dialog has no content. dont show it
    return;
  }


  $('body').append(modal).append(dlg);
  dlg.css({
    top:( (window.innerHeight || $(window).height()) / 2 ) - (dlg.height() / 2),
    visibility:'visible'
    });

  if (!content)
  {
    $.ajax({
      type:'get',
      url:'terms.txt',
      success: function(data){
        $('#text_terms').html(data).attr('scrollTop',0);
      }
    });
  }
  else {
    $('#text_terms').html(content).attr('scrollTop',0);
  }

  $('#formDialog ul li:first-child input[type="radio"]').attr('checked','checked');

  if (terms==false)
  {
    $('#btnsubmit').removeAttr('disabled').click(function(){
        var pdfloc = $('#pdf_list input[type="radio"]:checked').val();
        if (pdfloc && pdfloc !='')
        {
          window.open('http://www.fpafunds.com/'+pdfloc,'pdfdownload');
          setTimeout(closeFormDialog, 1000);
        }
      });
  }

  $('#terms_agreement').click(function(){
    if (this.checked)
    {
      $('#btnsubmit').removeAttr('disabled').click(function(){
        var pdfloc = $('#pdf_list input[type="radio"]:checked').val();
        if (pdfloc && pdfloc !='')
        {
          window.open('http://www.fpafunds.com/'+pdfloc,'pdfdownload');
          /*
          var frmpdf = $('<iframe>').css({
            width:1,
            height:1,
            position:'absolute'
          })
          frmpdf.attr('src', pdfloc);
          $('body').append(frmpdf);
         */
          setTimeout(closeFormDialog, 1000);
          
        }
      });
    } 
    else {
      $('#btnsubmit').attr('disabled','disabled');
    }
  });
}





function closeFormDialog() {
  $('#formDialog').remove();
  $('#formDialogModal').remove();
}







var contentNonRetirement = function(pdfs,terms){
		var html='<div style="padding:20px;font:13px arial, sans-serif;">';
		html+='<h2>Non-Retirement</h2>';
		html+='<ul id="pdf_list" style="margin:20px;list-style:none">';
		if (pdfs != 'undefined') {
			for(var i=0; i<pdfs.length;i++){
				html+='<li><input type="radio" id="pdf_'+i+'" name="pdf_download" value="'+pdfs[i][1]+'"> <label for="pdf_'+i+'">'+pdfs[i][0]+'</label></li>';
			}
		}
		html+='</ul>';
    
    if (terms==true)
    {
      html+='<textarea spellcheck="false" readonly id="text_terms" style="width:100%;height:90px;outline:none;resize:none;"></textarea>';
      html+='<p style="padding-top:10px;font-size:13px;color:#111;">';
      html+='<input type="checkbox" id="terms_agreement" name="terms_agreement" value="1" style="float:left;margin:3px 6px 0 0">';
      html+='<label for="terms_agreement">I agree to the Terms and Conditions</label>';
      html+='</p>';
    }
    


		html+='<div class="controls" style="text-align:right">';
    if (terms==true)html+='<input id="btnsubmit" type="button" value="Download" disabled="disabled">';
    else html+='<input id="btnsubmit" type="button" value="Download">';
    

		html+='<input type="button" value="Close" onclick="closeFormDialog()">';
		html+='</div>';
		html+='</div>';
		return html;
},

contentIraForms = function(pdfs,terms) {
  var html='<div style="padding:20px;font:13px arial, sans-serif;">';
  html+='<h2>IRA</h2>';
  html+='<ul id="pdf_list" style="margin:20px;list-style:none">';
  if (pdfs != 'undefined') {
    
    for(var i=0; i<pdfs.length;i++){
      html+='<li><input type="radio" id="pdf_'+i+'" name="pdf_download" value="'+pdfs[i][1]+'"> <label for="pdf_'+i+'">'+pdfs[i][0]+'</label></li>';
    }
  }
  html+='</ul>';

  
  if (terms==true)
  {
    html+='<textarea spellcheck="false" readonly id="text_terms" style="width:100%;height:90px;outline:none;resize:none;"></textarea>';
    html+='<p style="padding-top:10px;font-size:13px;color:#111;">';
    html+='<input type="checkbox" id="terms_agreement" name="terms_agreement" value="1" style="float:left;margin:3px 6px 0 0">';
    html+='<label for="terms_agreement">I agree to the Terms and Conditions</label>';
    html+='</p>';
  }
  


  html+='<div class="controls" style="text-align:right">';
  if (terms==true)
  {
    html+='<input id="btnsubmit" type="button" value="Download" disabled="disabled">';
  }
  else {
    html+='<input id="btnsubmit" type="button" value="Download">';
  }
  html+='<input type="button" value="Close" onclick="closeFormDialog()">';
  html+='</div>';
  html+='</div>';
  return html;
},

content403bForms = function() {
  var html='<div style="padding:20px;font:13px arial, sans-serif;">';
  html+='<h2>403b Forms</h2>';
  html+='<p style="font-size:16px; color:#111; text-align:center; padding:30px 60px 80px">';
  html+='For the 403b application package, please contact FPA at mfa@fpafunds.com or at 800-982-4372, Ext. 425 or 426';
  html+='</p>';
  html+='<div class="controls" style="text-align:right">';
  //html+='<input type="button" value="Submit" disabled="disabled">';
  html+='<input type="button" value="Close" onclick="closeFormDialog()">';
  html+='</div>';
  html+='</div>';
  return html;
},

contentChartForms = function(pdfs,content){
  var html='<div style="padding:20px;font:13px arial, sans-serif;overflow:hidden">';
  html+='<img src="'+pdfs+'" style="margin-left:-30px" alt />';
  html+='<p style="font-size:11px; line-height:13px; margin-bottom:20px">';
  if (content)html+=content
  html+='</p>';
  html+='<div class="controls" style="text-align:right">';
  //html+='<input type="button" value="Submit" disabled="disabled">';
  html+='<input type="button" value="Close" onclick="closeFormDialog()">';
  html+='</div>';
  html+='</div>';
  return html;
},

contentCustomForm = function(content) {
  var html='<div style="padding:20px;font:13px arial, sans-serif;overflow:hidden">';
  //html+='<img src="'+pdfs+'" style="margin-left:-30px" alt />';
  html+='<p style="color: #111111; font-size: 16px; padding: 30px 60px 80px; text-align: center; margin-bottom:20px">';
  if (content)html+=content
  html+='</p>';
  html+='<div class="controls" style="text-align:right">';
  //html+='<input type="button" value="Submit" disabled="disabled">';
  html+='<input type="button" value="Close" onclick="closeFormDialog()">';
  html+='</div>';
  html+='</div>';
  return html; 
};















