var expert_tabs = [];

function tab_is_loaded(id)
{
	for (var i=0; i<expert_tabs.length; i++) {
		if (expert_tabs[i]==id)
			return true;
	}
	expert_tabs.push(id);
	return false;
}

function get_expert_tab(id)
{
	var tab = $('#experts-block ul.tabs li').eq(id);
	var cntr = $('#experts-block .tab_content').eq(id);
	tab.addClass('active');
	var context = $('> a',tab).attr('href');

	if ( !tab_is_loaded(id) ) {
		$.get('/includes/modules/experts.tab.inc.php', {'context': context}, function(data){ // todo: call just once per sub-nav item
			$('.expertsummary',cntr).html(data.intro);
			if (data.image.length)
				$('.expert > img',cntr).attr('src',('/scripts/sliderimages/68/84'+data.image));
			$('.expert > .expert-name',cntr).html(data.byline);
			$('.expert-question',cntr).html(data.abstract);
		},'json');
	}
	$('#experts-block .tab_content').hide();
	cntr.show();
}

$(document).ready(function() {

	if ( $('#experts-block').length )
		get_expert_tab(0); // load first tab
		
		// todo: disable reload of siteblock after each

	//On Mouseover Event experts-block
	$('#experts-block ul.tabs li').mouseover(function() {
		$('#experts-block ul.tabs li').removeClass('active'); //Remove any 'active' class
		//$(this).addClass('active'); //Add 'active' class to selected tab
		//$('#experts-block .tab_content').fadeOut(); //Hide all tab content
		var id = $(this).index(); //Find the href attribute value to identify the active tab + content
		//$('#tab'+activeTab+'e').show(); //Fade in the active ID content
		get_expert_tab(id);
		return false;
	});

	//Tabs - When page loads...
	$('.block ul.tabs li').eq(0).addClass('active').show(); //Activate first tab

	//On Click Event popular-block
	$('#popular-block ul.tabs li').click(function() {
		$('#popular-block ul.tabs li').removeClass('active'); //Remove any 'active' class
		$(this).addClass('active'); //Add 'active' class to selected tab
		$('#popular-block .tab_content').hide(); //Hide all tab content

		var activeTab = $(this).find('a').attr('href'); //Find the href attribute value to identify the active tab + content
		$(activeTab).show(); //Fade in the active ID content
		return false;
	});
	//On Click Event wire-block
	$('#wire-block ul.tabs li').click(function() {
		$('#wire-block ul.tabs li').removeClass('active'); //Remove any 'active' class
		$(this).addClass('active'); //Add 'active' class to selected tab
		$('#wire-block .tab_content').hide(); //Hide all tab content

		var activeTab = $(this).find('a').attr('href'); //Find the href attribute value to identify the active tab + content
		$(activeTab).show(); //Fade in the active ID content
		return false;
	});
	//On Click Event resources-block
	$('#resources-block ul.tabs li').click(function() {
		$('#resources-block ul.tabs li').removeClass('active'); //Remove any 'active' class
		$(this).addClass('active'); //Add 'active' class to selected tab
		$('#resources-block .tab_content').hide(); //Hide all tab content

		var activeTab = $(this).find('a').attr('href'); //Find the href attribute value to identify the active tab + content
		$(activeTab).show(); //Fade in the active ID content
		return false;
	});
	//On Click Event magazines-block
	$('#magazines-block ul.tabs li').click(function() {
		$('#magazines-block ul.tabs li').removeClass('active'); //Remove any 'active' class
		$(this).addClass('active'); //Add 'active' class to selected tab
		$('#magazines-block .tab_content').hide(); //Hide all tab content

		var activeTab = $(this).find('a').attr('href'); //Find the href attribute value to identify the active tab + content
		$(activeTab).show(); //Fade in the active ID content
		return false;
	});

	//Override Cycle ActivePagerLink function
	$.fn.cycle.updateActivePagerLink = function(pager, currSlideIndex) { 
		$('#slide-block .pager').find('div.row').removeClass('active').filter('div.row:eq('+currSlideIndex+')').addClass('active'); 
	}; 
	
	//Cycle Slider
	$('#slide-block .slides').cycle({
		fx: 'scrollVert',
		activePagerClass: 'active',
		speed: 500,
		timeout: 5000,
		delay: -1000,
		rev: 1,
		nowrap:  1, 
		pager: '#numericPager',
		cleartypeNoBg: true, 
		timeout: 0 
	});
	
	$('#slide-block .pager .row').mouseenter(function(){
		var thisIndex = $(this).index();
		$('#numericPager a').eq(thisIndex).trigger('click');
	});
	
	//Gallery Thumbs slider 
	$('#gallery-block .slides').cycle({ 
		fx:     'scrollHorz', 
		prev:   '#galprev', 
		next:   '#galnext', 
		after:   onAfter,
		nowrap:  1, 
		timeout: 0 
	});

	//Rounded Corners
	$('#podcasts-block h2,#wpapers-block h2,#checklist-block, #news-block h2').corner('round 10px top');
	$('#podcasts-block .content,#wpapers-block .content,#checklist-block .content,#jobs-block .block-footer,#news-block .content').corner('round 10px bottom keep');	
	$('#valuator-block,#incident-block,#currentissue-block,#exclusive-block, #comment-block .content, #topical-listing1 .content, #topical-listing2 .content').corner('round 10px');
	$('#jobs-block .content').corner('round 10px tr cc:#fbfbfb keep');
	
	//change borders on end tabs
	$('#popular-block ul.tabs li:first a, #wire-block ul.tabs li:first a, #resources-block ul.tabs li:first a, #magazines-block ul.tabs li:first a').css('border-left','0px');
	$('#experts-block ul.tabs li:first a').css({'border-left':'0px'}); //,'width':'77px'});
	
	$('#experts-block ul.tabs li:last a').css('border-right','0px');
	$('#popular-block ul.tabs li:last a').css({'border-right':'0px','width':'146px'});
	$('#wire-block ul.tabs li:last a').css({'border-right':'0px','width':'131px'});
	$('#resources-block ul.tabs li:last a').css({'border-right':'0px','width':'158px'});
	$('#magazines-block ul.tabs li:last a').css({'border-right':'0px','width':'157px'});

	// Initialize Font Size
	if ($.cookie("investor_cookie")!=='' && $.cookie("investor_cookie")!== 0 && $.cookie("investor_cookie")!== null) {
		cookie_value = $.cookie("investor_cookie");
		cookie_value = Math.round(cookie_value*10)/10;
		$('#content-inner .fontresize').css('font-size', cookie_value);
	}

	// font Resizing
	// Reset Font Size
	$("#gadgetbox .resetfont").click(function(){
    	newFontSize = 11.0;
    	$('#content-inner .fontresize').css('font-size', newFontSize);
    	$.cookie("investor_cookie", "11.0", { path: '/' });
	});
	// Increase Font Size
	$("#gadgetbox .increase").click(function(){
    	var currentFontSize = $('#content-inner .fontresize').css('font-size');
    	var currentFontSizeNum = parseFloat(currentFontSize, 10);
    	var newFontSize = currentFontSizeNum*1.1;
    	$('#content-inner .fontresize').css('font-size', newFontSize);
    	$.cookie("investor_cookie", newFontSize, { path: '/' });
    	return false;	
    });
	// Decrease Font Size
	$("#gadgetbox .decrease").click(function(){
    	var currentFontSize = $('#content-inner .fontresize').css('font-size');
    	var currentFontSizeNum = parseFloat(currentFontSize, 10);
    	var newFontSize = currentFontSizeNum*0.9;
    	$('#content-inner .fontresize').css('font-size', newFontSize);
    	$.cookie("investor_cookie", newFontSize, { path: '/' });
    	return false;
	});
  	// Show Font Options
  	$('#fonttools').show();

}); // end doc ready

function textClear(ctrl){ if (ctrl.value == ctrl.defaultValue) {ctrl.value = '';}}
function textRestore(ctrl){ if (ctrl.value === '') {ctrl.value = ctrl.defaultValue;}}
function onAfter(curr, next, opts) {
    var index = opts.currSlide;
    $('#galprev')[index === 0 ? 'removeClass' : 'addClass']('active');
    $('#galnext')[index == opts.slideCount - 1 ? 'removeClass' : 'addClass']('active');
}	
	
/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie !== '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

