$(function(){
  stylize_page_title();
  setup_side_menus();
  setup_accordions();
  investor_tools_module();
	setup_rotating_home_banners();
  $('.breadcrumb .grid-inner a:last').addClass('last-crumb');
	
 
	filter_link_types();
  setup_shadowtext('#keyword', 'Keyword Search...');
  setup_tool_tips();
	setup_printable_links();
	setup_lightbox();
	  
});

// controls the rotating images on the home page
var rotatingImages = new Array(
	'images/banners/front/homebanner-1.jpg',
	'images/banners/front/homebanner-2.jpg',
	'images/banners/front/homebanner-3.jpg',
	'images/banners/front/homebanner-4.jpg',
	'images/banners/front/homebanner-5.jpg'
)


// sets up any accordions on the page
function setup_accordions(){
		format_accordions($('.accordion'));
}

function format_accordions($allAccordions){
	
	
	$allAccordions.each(function(element){
		$currentAccordion = $allAccordions.get(element);
		if(!$($currentAccordion).hasClass('show-all-disabled')) setup_accordion_showall($currentAccordion);
		accordion_hide_all($currentAccordion );// hide all by default
		$allTitles = $($currentAccordion).find('h3');
		$allTitles.addClass('ui-accordion-header ui-state-default').next().addClass('ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active');
		$allTitles.find('a').click(function(target){
			target.preventDefault();
			$(target).parent().click();
		});
		$allTitles.click(function(element){
			if($(element.currentTarget).hasClass('ui-state-default')){
				
				accordion_hide_all($(element.currentTarget).parent());
				
				accordion_expand(element.currentTarget);
			}else{
				accordion_close(element.currentTarget);
			}
			
		});
		
		
		accordion_expand( $($currentAccordion).find('h3').first() );
		
	});
}

function accordion_expand(headerElement){
	$(headerElement).addClass('ui-state-active').removeClass('ui-state-default').next().slideDown('fast');
}

function accordion_close(headerElement){
	//console.log('close');
	$(headerElement).addClass('ui-state-default').removeClass('ui-state-active').next().slideUp('fast');
}

function accordion_hide_all(parentElement){
	$currentAccordion = parentElement;
	$allTitles = $($currentAccordion).find('h3');

	$allTitles.each(function(elementID){
		currentH3 = $allTitles.get(elementID);
		//console.log(currentH3);
		accordion_close(currentH3);
	});
}



function setup_accordion_showall(accordionObject) {
	$(accordionObject).prepend('<a href="#" class="accordion-show-all show-all">Show all</a>');
	$('.accordion-show-all').click(function(event){
		event.preventDefault();
		accordion_show_all(this);
	});
}


function accordion_show_all(target) {
	var $parentList = $(target).parent();
	var $link = $(target);

	if($link.html() == 'Show all'){
		// show all
		$link.html('Hide all').removeClass('show-all').addClass('hide-all');
		$allH3 = $parentList.find('h3.ui-accordion-header');
		$allH3 .each(function(element){
			accordion_expand($allH3.get(element));
		});
	}else{
		// hide all
		$link.html('Show all').removeClass('hide-all').addClass('show-all');
		accordion_hide_all($parentList);
	}
}


// use to place default text in a text area
function setup_shadowtext(textfieldSelector, defaultText){
  
  var targetFormElement = $(textfieldSelector);
  targetFormElement.addClass('default-text');
  targetFormElement.val(defaultText);
  jQuery.data( targetFormElement, 'defaultText', defaultText );
  
  // setup event binding
  targetFormElement.focus(function(){
    if( jQuery.data( targetFormElement, 'defaultText') == $(this).val() ) $(this).val('');
    targetFormElement.removeClass('default-text');
  });
  targetFormElement.blur(function(){
    if($(this).val() == '') $(this).val(jQuery.data( targetFormElement, 'defaultText'));
    targetFormElement.addClass('default-text');
  });
}


// setup default tool-tips for any link with a .has-tool-tip class
// Don't forget to set the title attribute on your link
function setup_tool_tips() {
  $('#main a.has-tool-tip').poshytip({
      className: 'tip-yellowsimple',
      showTimeout: 1,
      alignTo: 'target',
      alignX: 'center',
      offsetY: 5,
      allowTipHover: false
  });
}

function setup_side_menus() {
      $("#side-nav").treeview({
        collapsed: true,
        animated: "fast",
        persist: "location"
	  });
}

function investor_tools_module() {
      $("#investor-tools").treeview({
        collapsed: true,
        animated: "fast"
	  });
	  
	  $("#investor-tools h6 a").click(function(clickevent){
		clickevent.preventDefault();
		$(clickevent.currentTarget).parent().prev().click();
		
	  });
}



// run custom flash replacement on h1.page-title
function stylize_page_title(){
  
  if( $('h1.page-title').hasClass('no-flash')) return;// opt out based on class presence
  
	var currentTitle = encodeURIComponent( $('h1.page-title .page-title-inner').text() );
	
	$.flash.encodeParams = false;
  $('h1.page-title').wrapInner('<span id="page-title-flashed" />').flash(
  {
    swf: '/theme/swf/apex_custom.swf',
    width: 460,
    height: 60,
		encodeParams: false,
    wmode: 'transparent',
    flashvars: {
      t: currentTitle
    }
  });

}


function setup_printable_links() {
	var popup_profiles = {
		windowPrint:
		{
			height:600, // sets the height in pixels of the window.
			width:600, // sets the width in pixels of the window.
			toolbar:0, // determines whether a toolbar (includes the forward and back buttons) is displayed {1 (YES) or 0 (NO)}.
			scrollbars:0, // determines whether scrollbars appear on the window {1 (YES) or 0 (NO)}.
			status:0, // whether a status line appears at the bottom of the window {1 (YES) or 0 (NO)}.
			resizable:1, // whether the window can be resized {1 (YES) or 0 (NO)}. Can also be overloaded using resizable.
			//left:0, // left position when the window appears.
			//top:0, // top position when the window appears.
			center:1, // should we center the window? {1 (YES) or 0 (NO)}. overrides top and left
			createnew:1, // should we create a new window for each occurance {1 (YES) or 0 (NO)}.
			location:0, // determines whether the address bar is displayed {1 (YES) or 0 (NO)}.
			menubar:0, // determines whether the menu bar is displayed {1 (YES) or 0 (NO)}.
			onUnload:null // function to call when the window is closed
		},
		windowEmail:
		{
			height:650, // sets the height in pixels of the window.
			width:425, // sets the width in pixels of the window.
			toolbar:0, // determines whether a toolbar (includes the forward and back buttons) is displayed {1 (YES) or 0 (NO)}.
			scrollbars:0, // determines whether scrollbars appear on the window {1 (YES) or 0 (NO)}.
			status:0, // whether a status line appears at the bottom of the window {1 (YES) or 0 (NO)}.
			resizable:1, // whether the window can be resized {1 (YES) or 0 (NO)}. Can also be overloaded using resizable.
			//left:0, // left position when the window appears.
			//top:0, // top position when the window appears.
			center:1, // should we center the window? {1 (YES) or 0 (NO)}. overrides top and left
			createnew:1, // should we create a new window for each occurance {1 (YES) or 0 (NO)}.
			location:0, // determines whether the address bar is displayed {1 (YES) or 0 (NO)}.
			menubar:0, // determines whether the menu bar is displayed {1 (YES) or 0 (NO)}.
			onUnload:null // function to call when the window is closed
		}
	}
	$(".popupwindow").popupwindow(popup_profiles);
	
}

// finds active link in tree and adjusts accordionly 
function highlight_tree() {
  $("#side-nav a").each(
    function()
    {
      if (location.pathname.indexOf(this.href) > -1)
      {
        $(this).addClass("side-tree-selected");
      }
    }
  );
}



function filter_link_types(){
	$("a[href*=.pdf]").addClass('pdf-link').click(function(){
		window.open(this.href);
		return false;
	});	
}

function setup_rotating_home_banners() {
	var isFront = $('body').hasClass('front');
	if(!isFront) return;
	var $bannerWrapper = $('#homebanners');
	$bannerWrapper.wrapInner('<div id="homebanner-inner" />');
	var $bannerWrapperInner = $("#homebanner-inner");
	var nextBgImage = get_next_bg();
	$bannerWrapperInner.css('background-image', nextBgImage);
	$bannerWrapper.css('background-image', nextBgImage);
	// set height
	var newHeight = $('.content_half_right').height();
	$bannerWrapper.height(newHeight);
	//$bannerWrapperInner.height(newHeight);// always set the inner taller than the outer
	
}

function get_next_bg(){
	var lastCookieID = parseInt($.cookie('lastCookieID'));
	if(isNaN(lastCookieID) || (lastCookieID+1) >= rotatingImages.length ) lastCookieID = -1
	var nextCookieID = lastCookieID +1;
	var saveCookie = nextCookieID;
	$.cookie('lastCookieID', saveCookie);// save rotation
	return 'url('+rotatingImages[nextCookieID]+')';
}


function setup_lightbox(){
	if(typeof $().fancybox == 'function') { 
		$("#main a.lightbox").fancybox({
			'width'				: 435,
			'padding'			: 0,
			'titleShow'			: false,
			'height'			: '75%',
			'autoDimensions'	: true,
			'autoScale'     	: false,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'type'				: 'iframe'
		});
	}
}


