jQuery(function(){
	// color banner flag
	jQuery('#platform-canvas').append('<div class="flag">&nbsp;</div>');
	
	// breadcrumb dropdown arrow
	jQuery('.filed-under > li > ul').append('<li class="dropdown-triangle">&nbsp;</li>');
	
	// sidebar tool arrows
	jQuery('.secondary-top .tools .share, #secondary .tools .download, #secondary .tools .share, #secondary .tools .notebook, #secondary .tools .export, .tools-box .tools .share').append('<span class="tool-arrow">&nbsp;</span>');
	
	// setup menus
	showHide('#secondary .tools .download-list', '#secondary .tools .download');
	showHide('#secondary .tools .share-list', '#secondary .tools .share');
	showHide('#secondary .tools .notebook-list', '#secondary .tools .notebook');
	showHide('#secondary .tools .export-list', '#secondary .tools .export');
	showHide('.secondary-top .tools .share-list', '.secondary-top .tools .share');
	showHide('.tools-box .tools .share-list', '.tools-box .tools .share');
    
	// select menu
	jQuery('.select-menu-list').hide();
	
	jQuery('.select-menu').each(function() {
		var buttonWidth = jQuery(this).children('.select-menu-button').innerWidth() - 24;
		jQuery(this).children('.select-menu-list').css("width", buttonWidth);
	})
		
	jQuery('#details .select-menu > .select-menu-button, .watch-conference .select-menu > .select-menu-button, .select-menu > .select-menu-button').toggle(
		function(){
			jQuery(this).parent().children('.select-menu-list').slideDown('fast');
			jQuery(this).parent().addClass('active');
		},
		function(){
			jQuery(this).parent().children('.select-menu-list').slideUp('fast');
			jQuery(this).parent().removeClass('active');
		}
	);
	
	jQuery('body').click(function(){
		if (jQuery('.select-menu').hasClass('active')) {
			jQuery('.select-menu.active > .select-menu-button').click();
		};
	});
});

// show/hide menu
function showHide(what, trig, hideTxt, showTxt) {
    jQuery(what).hide();
    jQuery(trig).click(function() {
        if (jQuery(this).hasClass('hide')) {
            jQuery(what).slideUp('fast');
            jQuery(this).removeClass('hide').addClass('show');
            if (showTxt) jQuery(trig).html(showTxt + '<span>&nbsp;</span>');     
        } else {
            jQuery(what).slideDown('fast');
            jQuery(trig).removeClass('show').addClass('hide');
            if (hideTxt) jQuery(trig).html(hideTxt + '<span>&nbsp;</span>');
        }
        return false;
    });
}

function closeMenu(what, trig){
	jQuery(trig).click(function(){
		jQuery(what).click();
	});
}

// slide/fade/show menus
function clickMenu(menu, trigger, transition, hideTxt, showTxt) { 
	// hide menu
	jQuery(menu).hide();
	// setup transitions
	var t = (transition == 'slide' ? ['slideDown', 'slideUp'] : (transition == 'fade' ? ['fadeIn', 'fadeOut'] : ['show', 'hide']));
	// setup click toggle
	jQuery(trigger).toggle(
		function() {
			jQuery(this).addClass('active');
			var currentMenu = jQuery(this).next(menu);
			jQuery(currentMenu)[t[0]]();
			if (hideTxt) jQuery(this).html(hideTxt + '<span>&nbsp;</span>');
		},
		function() {
			jQuery(this).removeClass('active');
			var currentMenu = jQuery(this).next(menu);
			jQuery(currentMenu)[t[1]]();
			if (showTxt) jQuery(this).html(showTxt + '<span>&nbsp;</span>');
		}
	);
}

//cache images
menuHoverArrow = new Image()
menuHoverArrow.src = "/resources/images/common/arrow-4x7-a4a39e.png"

//Popup Window Functions
function newWindow(theURL) {
	var portionFloat = .6;
	var features='menubar=yes,scrollbars=yes,buttonbar=yes,status=yes,toolbar=yes,resizable=yes,location=yes,';

	winName = 'contentWindow';
	windowCoord = setWindowValues();

	var windowWidth = windowCoord.shift();
	var windowHeight = windowCoord.shift();
	var screenWidth = windowCoord.shift();
	var screenHeight = windowCoord.shift();
	var calcTop = windowCoord.shift();
	var calcLeft = windowCoord.shift();

	newWidth = parseInt(windowWidth * portionFloat);
	newHeight = parseInt(windowHeight * portionFloat);

	newXOffset = parseInt((windowWidth - newWidth) * .5) + calcLeft;
	newYOffset = parseInt((windowHeight - newHeight) * .5) + calcTop;

	if (navigator.appName == 'Microsoft Internet Explorer'){
	    offsetString = ',top=' + newYOffset + ', left=' + newXOffset;
	    }
	    else {
	    offsetString = ',screenX=' + newYOffset + ', screenY=' + newXOffset;
	    }

	features = features + 'width=' + newWidth + ',height=' + newHeight + offsetString;

	windowName = self.open(theURL,winName,features);
	if (windowName.focus) {windowName.focus()}
}

function setWindowValues(){
 
    windowWidth = getWindowWidth(self); // or you could just apply some value here
    windowHeight = getWindowHeight(self); // or you could just apply some value here
    screenWidth = screen.availWidth ? screen.availWidth : screen.width;
    screenHeight = screen.availHeight ? screen.availHeight : screen.height;
    
    calcTop = window.screenY ? window.screenY : self.screenTop;
    calcLeft = window.screenX ? window.screenX : self.screenLeft;
    
    return (windowCoord = new Array(windowWidth,windowHeight,screenWidth,screenHeight,calcTop,calcLeft));
}

function getWindowWidth (windowObject) {
    var windowWidth = 0

    if (windowObject.innerWidth)
    {
        windowWidth = windowObject.innerWidth;
    }
    else if (windowObject.document.documentElement && windowObject.document.documentElement.clientWidth)
    {
        windowWidth = windowObject.document.documentElement.clientWidth;
    }
    else if (windowObject.document.body)
    {
        windowWidth = windowObject.document.body.clientWidth;
    }
    if (windowWidth < 500){
        windowWidth = 500;
    }
    return windowWidth;
}

function getWindowHeight (windowObject) {
    
    if (windowObject.innerHeight)
    {
        windowHeight = windowObject.innerHeight;
    }
    else if (windowObject.document.documentElement && windowObject.document.documentElement.clientHeight)
    {
        windowHeight = windowObject.document.documentElement.clientHeight;
    }
    else if (windowObject.document.body)
    {
        windowHeight = windowObject.document.body.clientHeight;
    }
    if (windowHeight < 325){
        windowHeight = 325;
    }
    return windowHeight;
}
