var newsrcbopen = false;
//var debugBox = null; // DEBUG

function initMore() {
	textMore = '(more)';
	textLess = '(less)';

	jQuery('div.more').css('display','none');
	jQuery('div.more').before(' <a href="#" class="more">'+textMore+'</a>');
	
	jQuery('a.more').click(function(){
		var jThis = jQuery(this);
		
		if (jThis.text() == textMore) {
			// expand
			jThis.next('div.more').slideDown();
			jThis.text(textLess);
		} else {
			// collapse
			jThis.next('div.more').slideUp();
			jThis.text(textMore);
		}
		
		return false;
	});
}

function debugOut(msg, elem) {
	var jElem = jQuery(elem);
	var time = '';
	var path = '#'+elem.id+'.'+elem.className;
	debugBox.prepend('<span style="display: block; width: 350px; float:left;">'+time+' ['+path+']</span>'+msg+'<br />');
}

function initFancyBox() {
    jQuery('a.iframe').fancybox({
        'width'			: '7',
        'height'		: '4',
        'autoScale'		: false,
        'modal'			: false,
        'transitionIn'	: 'elastic',
        'transitionOut'	: 'elastic',
        'type'          : 'iframe'
    });
}

function initSubpages() {
	/*
	// DEBUG
	jQuery('body').append('<div style="position: absolute; top: 10px; right: 10px; width: 500px; height: 600px; background-color: black; color: white; overflow: auto; z-index: 1000; text-align: left;" id="debugbox">DEBUG BOX</div>');
	debugBox = jQuery('#debugbox');
	
	
	*/
	initMore();
    /*jQuery(".subnavigation2").hide();
    //jQuery(".subnavigation2 .subnavigation2 a").bind("mouseover",showSubnavigation);*/
    /*jQuery(".subnavigation").bind("mouseover",showSubnavigation);
    jQuery(".subnavigation").bind("mouseout",hideSubnavigation);
    jQuery(".subnavigation2").bind("mouseover",showSubnavigation);
    jQuery(".subnavigation2").bind("mouseout",hideSubnavigation);*/
    
    // 01.2011 new menu
    jQuery('ul.sf-menu').supersubs({
        minWidth:    12, 
        maxWidth:    27,
        extraWidth:  1
    }).superfish({
        delay: 300,
        animation: {opacity:'show'},
        autoArrows: false,
        dropShadows: false
    });
    
    // tt_news right content browser
    //initTTNewsRightContentBrowser();
    
    // tt_news "more" link below Xth entry
    initTTNewsListMore(3);
    initTTNewsListRightColumnMore(3);
    
    if (jQuery('div.slidenavigation').length > 0) {
        initGallerific();
    }
    initFancyBox();
}

function initTTNewsListMore(visibleCount) {
    
    if (jQuery("#innermaincontent div").hasClass('presscontainer') == true){
	var textMore = 'show more press';
	var textLess = 'show only latest press';
    }else {
	var textMore = 'show more events';
	var textLess = 'show only latest events';
    }
    
    
    var container = jQuery("#innermaincontent .news-list-container");
    var count = jQuery('.news-list-item', container).length;
    
    if (jQuery(container).hasClass('press')) {
        
    }
    
	
    if (count > visibleCount) {
			jQuery('.news-list-item:gt('+(visibleCount-1)+')', container).wrapAll('<div class="news-list-more-container"></div>');
			jQuery('.news-list-more-container', container).hide(0);
			jQuery('.news-list-more-container', container).before('<div class="news-list-more-link"><a href="#">'+textMore+'</a></div>');
			
			jQuery('.news-list-more-link a', container).click(function(){
				
				var jThis = jQuery(this);
				var actionContainer = jThis.parent().next('.news-list-more-container');
				
				if (jThis.text() == textMore) {
					actionContainer.fadeIn();
					jQuery(this).text(textLess);
				} else {
					actionContainer.fadeOut();
					jQuery(this).text(textMore);
				}
				return false;
			});
		}
}

function initTTNewsListRightColumnMore(visibleCount) {
    var container = jQuery(".newsrightcolumnbrowser ul");
    var count = jQuery('li', container).length;
    
		var textMore = 'show more press';
		var textLess = 'show only latest press';
    
    if (count > visibleCount) {
			jQuery('li:gt('+(visibleCount-1)+')', container).hide(0);
			jQuery('li:nth-child('+visibleCount+')', container).after('<li><div class="news-list-more-link"><a href="#">'+textMore+'</a></div></li>');
			
			jQuery('.news-list-more-link a', container).click(function(){
				
				var jThis = jQuery(this);
				var actionItems = jQuery('li:gt('+visibleCount+')', container);
				
				if (jThis.text() == textMore) {
					actionItems.fadeIn();
					jQuery(this).text(textLess);
				} else {
					actionItems.fadeOut();
					jQuery(this).text(textMore);
				}
				return false;
			});
		}
}

function showSubnavigation() {
    //debugOut('showSubnavigation', this); // DEBUG
    if (!jQuery(this).hasClass("subnavigation2")) {
        mySubnavi = jQuery(this).find(".subnavigation2");
        mySubnavi.show();
    }
}

function hideSubnavigation() {
    //debugOut('hideSubnavigation', this); // DEBUG
    if (jQuery(this).hasClass("subnavigation")) {
        mySubnavi = jQuery(this).find(".subnavigation2");
    } else {
        mySubnavi = jQuery(this);
    }
    mySubnavi.hide();
}

function initTTNewsRightContentBrowser() {
	var browser = jQuery('.newsrightcolumnbrowser');
	
	// add selection button
	browser.before('<div class="newsrcbselectother">select other press release</div>');
	
	// hover selection button
	jQuery('.newsrcbselectother').mouseover(function(){
			if (!newsrcbopen) {
				browser.slideDown();
				newsrcbopen = true;
			}
		});
	
	browser.mouseleave(function(){
			if (newsrcbopen) {
				newsrcbopen = false;
				browser.slideUp();
			}
		});
		
	//browser.show();
}

function initGallerific() {
    jQuery('div.slidenavigation').css({'width' : '186px', 'float' : 'left'});
    jQuery('div.content').css('display', 'block');

    // Initially set opacity on thumbs and add
	// additional styling for hover effect on thumbs
	var onMouseOutOpacity = 0.67;
	jQuery('#thumbs ul.thumbs li').opacityrollover({
		mouseOutOpacity:   onMouseOutOpacity,
		mouseOverOpacity:  1.0,
		fadeSpeed:         'fast',
		exemptionSelector: '.selected'
    });
    var gallery = jQuery('#thumbs').galleriffic({
       delay:                     2500,
					numThumbs:                 12,
					preloadAhead:              10,
					enableTopPager:            true,
					enableBottomPager:         true,
					maxPagesToShow:            7,
					imageContainerSel:         '#slideshow',
					controlsContainerSel:      '#controls',
					captionContainerSel:       '#caption',
					loadingContainerSel:       '#loading',
					renderSSControls:          true,
					renderNavControls:         true,
					playLinkText:              'Play Slideshow',
					pauseLinkText:             'Pause Slideshow',
					prevLinkText:              '&lsaquo; Previous Photo',
					nextLinkText:              'Next Photo &rsaquo;',
					nextPageLinkText:          'Next &rsaquo;',
					prevPageLinkText:          '&lsaquo; Prev',
					enableHistory:             false,
					autoStart:                 false,
					syncTransitions:           true,
					defaultTransitionDuration: 900,
					onSlideChange:             function(prevIndex, nextIndex) {
						// 'this' refers to the gallery, which is an extension of $('#thumbs')
						this.find('ul.thumbs').children()
							.eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
							.eq(nextIndex).fadeTo('fast', 1.0);
					},
					onPageTransitionOut:       function(callback) {
						this.fadeTo('fast', 0.0, callback);
					},
					onPageTransitionIn:        function() {
						this.fadeTo('fast', 1.0);
					}
    });
}


jQuery(document).ready(initSubpages);
