// JavaScript Document
$(document).ready(function () {
    $("ul.sf-menu").superfish({ autoArrows: false, dropShadows: false });
    adjustHomeBuckets();
    checkShowHeights();

    var to;
    var itsOn = 0;
    var mnID;

    $("#mainNav ul li ul li").mouseover(function () {
        clearTimeout(to);
        itsOn = 1;
    });

    $('.inputBox168').focus(function () {
        $(this).css('color', '#000');
    });

    $('#byAge').hide();

    $("#mainNav ul li").hover(function () {
        var thisID = $(this).children().attr("id");
        $("#" + thisID + " img").attr("src", $("#" + thisID + " img").attr("src").split(".").join("_ovr."));
    }, function () {
        var thisID = $(this).children().attr("id");
        $("#" + thisID + " img").attr("src", $("#" + thisID + " img").attr("src").split("_ovr.").join("."));
    });

    $(".hoverClass").hover(function () {
        $(this).attr("src", $(this).attr("src").split(".").join("-ovr."));
    }, function () {
        $(this).attr("src", $(this).attr("src").split("-ovr.").join("."));
    });


    //TEXT RESIZER ROLLOVER
    $("#textResizeContainer ul li img").hover(function () {
        if ($(this).attr('src').indexOf('_ovr') > 0) {
            $(this).attr("src", $(this).attr("src").split("_ovr.").join("."));
        } else {
            $(this).attr("src", $(this).attr("src").split(".").join("_ovr."));
        }
    }, function () {
        if ($(this).attr('class') != 'txtActive') {
            if ($(this).attr('src').indexOf('_ovr') > 0) {
                $(this).attr("src", $(this).attr("src").split("_ovr.").join("."));
            } else {

                $(this).attr("src", $(this).attr("src").split(".").join("_ovr."));
            }
        }
    });

    //TEXT RESIZER ACTIVE STATE
    $('#textResizeContainer ul li a').click(function () {
        $('#textResizeContainer ul li img').each(function () {
            $(this).attr("src", $(this).attr("src").split("_ovr.").join("."));
            $(this).attr('class', '');
        });
        var thisID = $(this).attr("id");
        $("#" + thisID + " img").attr("src", $("#" + thisID + " img").attr("src").split(".").join("_ovr."));
        $("#" + thisID + " img").attr('class', 'txtActive');
    });

    //Conditions Detail page - Show More/Less buttons
    $('.showMore,.showLess').click(function () {
        if ($(this).attr('class') == 'showMore') {
            var cHeight = 0;
            var pmHeight = 0;
            var thisID = $(this).attr('rel');
            $('#' + thisID).children().each(function () {
                cHeight = cHeight + $(this).height();
                pmHeight = pmHeight + parseInt($(this).css('padding-top')) + parseInt($(this).css('padding-bottom'));
                pmHeight = pmHeight + parseInt($(this).css('margin-top')) + parseInt($(this).css('margin-bottom'));
            });

            $('#' + thisID).animate({ height: parseInt(cHeight) + parseInt(pmHeight) + 'px' });
            $(this).fadeOut(function () {
                $(this).html("<a href='javascript:void(0)' title='Collapse section'><img src='/images/grnLgArrow_up.png' width='12' height='9' />&nbsp;Show Less</a>").fadeIn();
            });
            $(this).attr('class', 'showLess');
        } else {
            var thisID = $(this).attr('rel');
            $('#' + thisID).animate({ height: '3.9em' });
            $
            $(this).fadeOut(function () {
                $(this).html("<a href='javascript:void(0)' title='Collapse section'><img src='/images/grnLgArrow_dn.png' width='12' height='9' />&nbsp;Show More</a>").fadeIn();
            });
            $(this).attr('class', 'showMore');
        }
        return false;
    });


    checkCondValue();


    $('#condAlpha').hover(function () {
        if ($('#hidListType').val() != 'listAlpha') {
            $('.condRndLeft').css('background-image', 'url(/images/condTopLeft_ovr.png)');
            $('.condTextAlpha').css('background', '#c0ddea');
        }
    }, function () {
        if ($('#hidListType').val() != 'listAlpha') {
            $('.condRndLeft').css('background-image', 'url(/images/condTopLeft.png)');
            $('.condTextAlpha').css('background', '#FFF');
            checkCondValue();
        }
    });


    $('#condAge').hover(function () {
        if ($('#hidListType').val() != 'listAge') {
            $('#condAge').css('background', '#c0ddea');
        }
    }, function () {
        if ($('#hidListType').val() != 'listAge') {
            $('#condAge').css('background', '#FFF');
            checkCondValue();
        }
    });

    $('#condAlpha').click(function () {
        $('#hidListType').val('listAlpha');
        if ($('#byAlpha').css('display') != 'block') {
            $('#byAge').fadeOut(function () { $('#byAlpha').fadeIn() });
        }
        checkCondValue();
    });
    $('#condAge').click(function () {
        $('#hidListType').val('listAge');
        if ($('#byAge').css('display') != 'block') {
            $('#byAlpha').fadeOut(function () { $('#byAge').fadeIn() });
        }
        checkCondValue();
    });

    $('.question').click(function () {
        var myID = $(this).attr('id');
        if (!$(this).hasClass('expanded')) {
            contractFAQs();
            expandFAQ(myID);
        } else {
            contractFAQs();

        }
    });

    $('.viewTrans a').click(function () {
        var myID = $(this).attr('class');
        if ($(this).text() == "View Transcript") {
            $(this).text('Hide Transcript');
            $('#videoTrans').slideToggle();
            //showTS(myID);
        } else {
            $(this).text('View Transcript');
            $('#videoTrans').slideToggle();
            //hideTS(myID);
        }
    });
});

function showTS(myID){
    $('#'+myID).fadeIn();
}

function hideTS(myID) {
    $('#' + myID).fadeOut();
}

function adjustFontSize(size, div){
	var lh;
	var curSize;
	if(size == 'normal'){
	    //$(this).css('font-size','100%');
        $(div).children().each(function(){
	        $(this).removeAttr('style');
        });
    } else if (size == 'large') {
		//$(div + '> div').children().each(function(){  This was the old way.  It stopped working.
		$(div).children().each(function(){
			$(this).removeAttr('style');
			curSize = $(this).css('font-size');
			lh = $(this).css('line-height');
			$(this).css('line-height',parseInt(lh) + 2 + 'px');
			$(this).css('font-size', parseInt(curSize) + 2 + 'px');
		});
	}else if(size == 'largest'){
		$(div).children().each(function(){//Gets every single child node regardless if it's a child or grandchild
			$(this).removeAttr('style'); //remove any existing styles so we can get our original font-size
			curSize = $(this).css('font-size'); //get the current size
			lh = $(this).css('line-height');
			$(this).css('line-height',parseInt(lh) + 5 + 'px');
			$(this).css('font-size', parseInt(curSize) + 4 + 'px'); //add 4 pixels to the current size because this is the largest
		});
		//$(div).css('line-height',parseInt(lineHeight) + .2 + 'em');
	}
}

function centerSubNav(subNavID){
//	var subMenuWidth = 0;	
//	$('.' + subNavID + ' li').each(function(){
//		var leftPad = parseInt($(this).css('padding-left')); 
//		var rightPad = parseInt($(this).css('padding-right')); 
//		var liWidth = parseInt($(this).width());
//		subMenuWidth = subMenuWidth + leftPad + rightPad + liWidth;
//	});
//	
	//$('.'+subNavID).width(subMenuWidth);
}

function adjustHomeBuckets(){
	var maxHeight;
	var heightArray = new Array();
	$('.featContentBox').each(function(){
		heightArray.push($(this).height());
	});
	
	//var firmsLeftHeight = $('#firmsIntLeft-Mid').height();
	//var firmsMiddleHeight = $('#firmsIntMiddle-Mid').height();
	//var firmsRightHeight = $('.featBlueRight').height();
	//var heightArray = [parseInt(firmsLeftHeight),parseInt(firmsMiddleHeight),parseInt(firmsRightHeight)];
	
	heightArray.sort(function(a,b){return b - a});
	
	maxHeight = heightArray[0];
	
	$('.featContentBox').css('height',maxHeight);
}

function checkCondValue(){
	if($('#hidListType').val() == 'listAlpha'){
		$('.condRndLeft').css('background','url(/images/condTopLeft_ovr.png)');
		$('.condTextAlpha').css('background','#c0ddea');
		$('.condTextAlpha img').attr('src','/images/brnArrow_dwn.png');
		$('#condAge').css('background', '#FFF');
		$('#condAge img').attr('src','/images/brnArrow_rt.png');
	} else if($('#hidListType').val() == 'listAge'){
		$('#condAge').css('background', '#c0ddea');
		$('#condAge img').attr('src','/images/brnArrow_dwn.png');
		$('.condRndLeft').css('background','url(/images/condTopLeft.png)');
		$('.condTextAlpha').css('background','#FFF');
		$('.condTextAlpha img').attr('src','/images/brnArrow_rt.png');
	}	
}

function checkShowHeights(){
	$('.accContent').each(function(){
		if($(this).text().length <= 300){
			var thisID = $(this).attr('id');
			$(this).siblings().each(function(){
				if($(this).attr('rel') == thisID){
					$(this).hide();
				}
			});
		}
	});
}

function expandFAQ(myID) {
    $('.' + myID).slideDown();
    $('#' + myID).addClass('expanded');
}

function contractFAQs() {
    $('.qa a').each(function (i, val) {
        $(this).removeClass('expanded');
    });
    $('.hidden').each(function (i, val) {
        $(this).slideUp();
    });
}

function Left(str, n) {
    if (n <= 0)
        return "";
    else if (n > String(str).length)
        return str;
    else
        return String(str).substring(0, n);
}
function Right(str, n) {
    if (n <= 0)
        return "";
    else if (n > String(str).length)
        return str;
    else {
        var iLen = String(str).length;
        return String(str).substring(iLen, iLen - n);
    }
}

//fibro log flash interaction
function thisMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
	    return window[movieName];
	} else {
	    return document[movieName];
	}
}

function requestLogin() {
	try {
//let the flash do the redirect
	thisMovie("fibroLog").gotoLogin();
} catch (err) {
// no flash on this page - redirect now
	window.location = '/registration.aspx'
    }
}

function requestRegister() {
	try {
	    //let the flash do the redirect
	thisMovie("fibroLog").gotoRegister();
} catch (err) {
	// no flash on this page - redirect now
	window.location = '/registration.aspx'
    }
}

function loadFlowPlayer(_v400, _v700, _v1000) {
    var clipWidth = $('#video').css('width');
    var clipHeight = $('#video').css('height');

    //FMS or Influxis filename must match the querystring groupID or gid
    flowplayer('video', "/swf/flowplayer.commercial-3.2.7.swf", {
        key: '#@0e64664641fce2c4b95',
        clip: {

            urlResolvers: 'bwcheck',
            provider: 'rtmp',
            autoPlay: true,
            scaling: 'fit',

            // available bitrates and the corresponding files. We specify also the video width
            // here, so that the player does not use a too large file. It switches to a 
            // file/stream with larger dimensions when going fullscreen if the available bandwidth permits.
            bitrates: [
                {
                    url: _v700, width: clipWidth, height: clipHeight, bitrate: 700,
                    // this is the default bitrate, the playback kicks off with this and after that
                    // Quality Of Service monitoring adjusts to the most appropriate bitrate
                    isDefault: true
                },
                { url: _v400, width: clipWidth, bitrate: 400 },
                { url: _v700, width: clipWidth, bitrate: 700 },
                { url: _v1000, width: clipWidth, bitrate: 1000 }
            ]
        },
        plugins: {

            //bandwidth check plugin
            bwcheck: {
                url: '/swf/flowplayer.bwcheck-3.2.5.swf',

                serverType: 'fms',

                // we use dynamic switching, the appropriate bitrate is switched on the fly
                dynamic: true,

                netConnectionUrl: 'rtmp://cf4d20pk2rt.rtmphost.com/acpa',
                bitrates: [400, 700, 1000],
                defaultbitrate: 700,

                // show the selected file in the content box. This is not used in real installations.
                onStreamSwitchBegin: function (newItem, currentItem) {
                    //$('.viewTrans').html(newItem.streamName);
                },
                onStreamSwitch: function (newItem) {
                    //$('.viewTrans').html('switched to: '+newItem.streamName);
                }
            },

            // RTMP streaming plugin
            rtmp: {
                url: '/swf/flowplayer.rtmp-3.2.3.swf',
                netConnectionUrl: 'rtmp://cf4d20pk2rt.rtmphost.com/acpa'
            }
        }
    });
}
