/**** Add application wide javascripts below this point  ******/
$(document).ready(function(){
  // $(document).pngFix();
  
  // remove search text when click
  $('#q').click(function(){
    $(this).val('');
  });
  
  // Arrow points down if preopen
  if(location.href.search(/preopen/)>0) {
    var url = location.href.split("=");
    var preopen = url[url.length-1];
    $("#"+preopen+"_arrow").css('background', 'url(/images/down-arrow.jpg) no-repeat');  
  }
  
  // Fund Literature arrows
    $('.download_title').click(function(){
      if($('.arrow', this).css('background')=='transparent url(/images/down-arrow.jpg) no-repeat scroll 0% 0%') {
        $('.arrow', this).css('background', 'url(/images/right-arrow.jpg) no-repeat');
      }else if($('.arrow', this).css('background')=='transparent url(/images/right-arrow.jpg) no-repeat scroll 0% 0%') {
        $('.arrow', this).css('background', 'url(/images/down-arrow.jpg) no-repeat');      
      }else{
        $('.arrow', this).css('background', 'url(/images/down-arrow.jpg) no-repeat');            
      }
    });

  
  $('li.sub_menu_headlink').hover(
    function() { $('ul', this).show(); },
    function() { $('ul', this).hide(); });
    // Header carousel
      $("#mycarousel").jCarouselLite({
          easing: "easein",/*http://gsgd.co.uk/sandbox/jquery.easing.old.php*/
          visible: 1,
          vertical:true,
          auto: 5500,
          speed: 2500
      });

  // // latest news and insight
  // //initially show item 1, hide the others, and ensure background is correct.
  // 
  if ($.browser.msie && $.browser.version.substr(0,1)<7) {
    $('#first-item').css('background', 'url(/images/home/news-item-box-selected.gif) no-repeat'); 
    $('#first-item').css('width', '296px'); 

    $('.latest-news #item-2').hide();
    $('.latest-news #item-3').hide();
    $('.latest-news #item-4').hide();

    $('.insight-menu .item').hover(
     function() { 
       var url = location.href;
       if(url.search(/intermediaries/)>0) $('.insight-menu .item').css('background', 'url(/images/home/news-item-box.gif) no-repeat'); 
       if(url.search(/investors/)>0) $('.insight-menu .item').css('background', 'url(/images/home/news-item-box-investors.jpg) no-repeat'); 
       if(url.search(/institutions/)>0) $('.insight-menu .item').css('background', 'url(/images/home/news-item-box-institutions.jpg) no-repeat'); 
       $('.insight-menu .item').css('width', '241px'); 

       $(this).css('cursor', 'pointer');
       $(this).css('background', 'url(/images/home/news-item-box-selected.gif) no-repeat'); 
       $(this).css('width', '296px'); 
       // show this item
       var current = "#"+$(this).attr('rel')+"";
       $('.latest-news #item-1').hide();
       $('.latest-news #item-2').hide();
       $('.latest-news #item-3').hide();
       $('.latest-news #item-4').hide();     
       $(current).show();
     },
     function() { 
     //  $(this).css('background', 'url(images/home/news-item-box.gif) no-repeat'); 
     //  $(this).css('width', '241px'); 
     });
  }else{
    $('#first-item').css('background', 'url(/images/home/news-item-box-selected.png) no-repeat'); 
    $('#first-item').css('width', '296px'); 

    $('.latest-news #item-2').hide();
    $('.latest-news #item-3').hide();
    $('.latest-news #item-4').hide();

    $('.insight-menu .item').hover(
     function() { 
       // 3 types of image for the 3 sections
       //alert(location.href);
       var url = location.href;
       if(url.search(/intermediaries/)>0) $('.insight-menu .item').css('background', 'url(/images/home/news-item-box.gif) no-repeat'); 
       if(url.search(/investors/)>0) $('.insight-menu .item').css('background', 'url(/images/home/news-item-box-investors.jpg) no-repeat'); 
       if(url.search(/institutions/)>0) $('.insight-menu .item').css('background', 'url(/images/home/news-item-box-institutions.jpg) no-repeat'); 

       
       $('.insight-menu .item').css('width', '241px'); 

       $(this).css('cursor', 'pointer');
       $(this).css('background', 'url(/images/home/news-item-box-selected.png) no-repeat'); 
       $(this).css('width', '296px'); 
       // show this item
       var current = "#"+$(this).attr('rel')+"";
       $('.latest-news #item-1').hide();
       $('.latest-news #item-2').hide();
       $('.latest-news #item-3').hide();
       $('.latest-news #item-4').hide();     
       $(current).show();
     },
     function() { 
     //  $(this).css('background', 'url(images/home/news-item-box.gif) no-repeat'); 
     //  $(this).css('width', '241px'); 
     });
  }

  $('li.headlink').hover(
    function() {  },
   function() { $('ul', this).css('display', 'none'); $('#contact-map').css('display','inline'); $('#contact-map-embed').css('display','inline'); });

 $('li.headlink').click(
  function() { $('ul', this).css('display', 'block'); $('#contact-map').css('display','none'); $('#contact-map-embed').css('display','none'); });


  // $('li.headlink').hover(
  //  function() { $('ul', this).css('display', 'block'); },
  //  function() { $('ul', this).css('display', 'none'); });
   
  $('#cssdropdown ul li a').hover(
   function() { $(this).css('color', '#f9c663');  },
   function() { $(this).css('color', '#fff'); });
   
   $('#cssdropdown ul li').hover(
    function() { $(this).css('list-style-image', 'url(/images/yellow_bullet.gif)');  },
    function() { $(this).css('list-style-image', 'url(/images/orange_bullet.gif)'); }); 
   
  // redirect user via url
  $("#search_fund_finder").change(function() 
  { 
      //alert($("#search_fund_finder").val());
      var url = $("#search_fund_finder").val();
      window.location = url;
      return false;
  });
  
  // open external links into new window.  
  function externalLinks(base_url) {
    return $('a[href^=http]:not("[href^=' + base_url + ']"):not(":has(\'img\')")');
  }
  
  function externalLinksPdf() {
    return $('a[href$=pdf]');
  }
  
  externalLinks('this.hostname').addClass("external");

  // add external class so open in new window and pdf so can style with pdf image...
  externalLinksPdf().addClass("external");
  externalLinksPdf().attr("id","pdf");

  $('.external').click(function(){
  this.target = "_blank";
  });
  
  
  $("#help").hide();
  $("#morehelp").click(function(){
    $("#help").toggle('slow');
    return false;
  });  

  $('#terms').children().hide();
  $('#terms_submit').hide();

  $("#fund_box_main").accordion({
    header: 'a.fundtitle',
    active: false,
    alwaysOpen: false, 
    autoHeight: false
  });

  $("#fund_docs").accordion({
      header: 'a.download_title',
      active: '.selected_sheet',
      alwaysOpen: false, 
      autoHeight: false
    });
  
  $("#quickfund_list").change(function(){
    toUrl = $(this).attr('value');
    window.location = toUrl;
  });
  
  $("#search_fund_finder").change(function(){
    toUrl = $(this).attr('value');
    window.location = toUrl;
  });
  
  $(".featuredfund_holder").jCarouselLite({
      btnNext: ".next",
      btnPrev: ".prev",
      vertical: true,
      scroll: 3,
      visible: 3
  });    
  
  $('#terms1').show();// first is always shown
  $('select').change(function() {
    var sel = $(this).val().toString(); 
    $('.selectcountry').children(0).attr('selected', '');
    $('.selectcountry').val(sel).attr('option', '0');  
    $('#terms').children().hide();
    $('#'+sel).show('slow');
    //$('#terms_submit').show('slow');

  });
  
});


function mycarousel_initCallback(carousel) {
// alert(dump(carousel));    
		jQuery('.jcarousel-control a').bind('click', function() {
      	carousel.scroll(jQuery.jcarousel.intval(jQuery(this).attr('rel')));
				// change background image.
				$('.text-select').css('background', 'url(images/header-select.gif)'); 
				$(this).children().css('background', 'url(images/header-selected.gif)'); 
        // carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        return false;
    });

    jQuery('.jcarousel-scroll select').bind('change', function() {
        $('.text-select').css('background', 'url(images/header-select.gif)'); 
				carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
        return false;
    });

    jQuery('#mycarousel-next').bind('click', function() {
				$('.text-select').css('background', 'url(images/header-select.gif)'); 
				carousel.next();
        return false;
    });

    jQuery('#mycarousel-prev').bind('click', function() {
        $('.text-select').css('background', 'url(images/header-select.gif)'); 
				carousel.prev();
        return false;
    });
};