function modal(element) {
    $('body').addClass('modal');
    $('html').css('overflow', 'hidden');
    $('<div id="extra_wrap">').prependTo('body');
    $('</div>').appendTo('body');
    $('#small_tabs .flash').css('display', 'none');
    $('#overlay').removeClass('inviz');
    $('#overlay').fadeIn('fast');
    $(element).css('display', 'none');
    $(element).removeClass('inviz');
    $(element).fadeIn('slow');
	
    $('.xout').click(function() {
        $('body').removeClass('modal');
        $('html').css('overflow', '');
        $('#overlay').fadeOut('fast');
        $(element).fadeOut('fast');
        $('#small_tabs .flash').removeClass('inviz');
        $('#small_tabs .flash').css('display', 'block');
        $('#small_tabs .spacer').addClass('inviz');
        return false;
    });
    $('#overlay').click(function() {
        $('body').removeClass('modal');
        $('html').css('overflow', '');
        $('#overlay').fadeOut('fast');
        $(element).fadeOut('fast');
        $('#small_tabs .flash').removeClass('inviz');
        $('#small_tabs .flash').css('display', 'block');
        $('#small_tabs .spacer').addClass('inviz');
        return false;
    });
}

function scrollPatient(button, element) {
    if (button == 'next') {
        window[element+'Current']++;
        if (window[element+'Current'] >= window[element]) {
            window[element+'Current'] = 0;
        }
    }
    if (button == 'prev') {
        window[element+'Current']--;
        if (window[element+'Current'] <= window[element]) {
            window[element+'Current'] = (window[element]-1);
        }
    }

    $('#'+element+' .patient').addClass('inviz');
    $('#'+element+' #'+element+window[element+'Current']).removeClass('inviz');
    $('.sub_tabs li').addClass('hide_div');
    $('#'+element+' .sub_tabs li:nth-child(1)').removeClass('hide_div');
}

$(document).ready(function() {
    $('a[href=#meet-members]').click(function() {
        modal('#meet-members');
        return false;
    });
    $('#open').click(function() {
        modal('#player');
        $('#small_tabs .flash').addClass('inviz');
        $('#small_tabs .spacer').removeClass('inviz');
        return false;
    });
    $('#tabs a').click(function() {
        $('#tabs a').removeClass('this');
        $(this).addClass('this');
        $('#player .inner').addClass('inviz');
        $('#player #'+this.name).removeClass('inviz');
        return false;
    });
    $('.sub_tabs a').click(function() {
        $('.sub_tabs li').addClass('hide_div');
        $(this).parents('li').removeClass('hide_div');
        return false;
    });
    $('#small_tabs ul a').click(function() {
        $('#small_tabs .inner').addClass('inviz');
        $('#small_tabs a').attr('id', '');
        $('#small_tabs .flash a').attr('title', 'Play').find('img').attr('src', '/images/play.gif');
        $('#small_tabs #'+this.className).removeClass('inviz');
        $(this).attr('id', 'on');
        stop();//stops any mp3 that is currently playing
        return false;
    });
    $('#small_tabs .previous-button').click(function() {
        var thisTab = $(this).parent().parent(), active = thisTab.find('.samples .active');
        if(active.prev().length == 1) {
            active.removeClass('active').prev().addClass('active');
        }
        else {
            var slides = thisTab.find('.samples > div');
            active.removeClass('active');
            slides.eq(slides.length - 1).addClass('active');
        }
        return false;
    });
    $('#small_tabs .next-button').click(function() {
        var thisTab = $(this).parent().parent(), active = thisTab.find('.samples .active');
        if(active.next().length == 1) {
            active.removeClass('active').next().addClass('active');
        }
        else {
            active.removeClass('active');
            thisTab.find('.samples > div').eq(0).addClass('active');
        }
        return false;
    });

    var loadedTrack = '';
    $('#small_tabs .flash a').click(function(){
        if(this.title=='Play'){
            if(loadedTrack != this.rel) {
                _getFlashObject().SetVariable('method:setUrl',this.rel);
                loadedTrack = this.rel;
            }
            this.getElementsByTagName('img')[0].src='/images/pause-icon.gif';
            this.title='Pause';
            play();
        }
        else{
            pause();
            this.getElementsByTagName('img')[0].src='/images/play.gif';
            this.title='Play';
        }
        return false;
    });
    /*$('#abductor p.buttons a').click(function() {
		scrollPatient(this.className, "abductor");
		return false;
	});
	$('#adductor p.buttons a').click(function() {
		scrollPatient(this.className, "abductor");
		return false;
	});
	$('#other p.buttons a').click(function() {
		scrollPatient(this.className, "other");
		return false;
	});*/
    $('#abductor .next').click(function() {
        scrollPatient(this.className, "abductor");
        return false;
    })
    $('#adductor .next').click(function() {
        scrollPatient(this.className, "adductor");
        return false;
    })
    $('#other .next').click(function() {
        scrollPatient(this.className, "other");
        return false;
    })
    var firstFired = true;
    $('a.expand_news').click(function() {
        var thisId = this.id;
        if(!firstFired) {
            $('.article').fadeOut(700).parent().find('.expand_news').show();
        }
        $(this).hide();//hide the current "more" link
        var parentOfArticle = $(this).parents(".news_section");
        if(!firstFired) {
            setTimeout(function() {
                $('.news_section').removeClass("showarticle");
                parentOfArticle.addClass("showarticle");
            }, 700);
        } else {
            $('.news_section').removeClass("showarticle");
            parentOfArticle.addClass("showarticle");
        }
        if(!firstFired) {
            setTimeout(function() {
                $('.article').fadeIn(700);
                setTimeout(function() {
                    //alert(thisId);
                    window.location.hash="top_"+thisId;
                }, 700);
            }, 700);
        } else {
            $('.article').css("display", "none");
            $('.article').fadeIn(700);
            setTimeout(function() {
                window.location.hash="top_"+thisId;
            }, 700);
            firstFired = false;
        }
        return false;
    });
});

var myListener = new Object();

/**
        	 * Initialize
        	 */
myListener.onInit = function()
{
    /*_addEventListener(document.getElementById("playerslider"), "mousedown", _sliderDown, false);
    _addEventListener(document, "mousemove", _sliderMove, false);
    _addEventListener(document, "mouseup", _sliderUp, false);*/
};
/**
        	 * Update
        	 */
myListener.onUpdate = function()
{
    /*var isPlaying = this.isPlaying;
    var url = this.url;
    var volume = this.volume;
    var position = this.position;
    var duration = this.duration;

    var id3_artist = this.id3_artist;
    var id3_album = this.id3_album;
    var id3_songname = this.id3_songname;
    var id3_genre = this.id3_genre;
    var id3_year = this.id3_year;
    var id3_track = this.id3_track;
    var id3_comment = this.id3_comment;

    document.getElementById("info_playing").innerHTML = isPlaying;
    document.getElementById("info_url").innerHTML = url;
    document.getElementById("info_volume").innerHTML = volume;
    document.getElementById("info_position").innerHTML = position;
    document.getElementById("info_duration").innerHTML = duration;
    document.getElementById("info_bytes").innerHTML = this.bytesLoaded + "/" + this.bytesTotal + " (" + this.bytesPercent + "%)";

    document.getElementById("info_id3_artist").innerHTML = id3_artist;
    document.getElementById("info_id3_album").innerHTML = id3_album;
    document.getElementById("info_id3_songname").innerHTML = id3_songname;
    document.getElementById("info_id3_genre").innerHTML = id3_genre;
    document.getElementById("info_id3_year").innerHTML = id3_year;
    document.getElementById("info_id3_track").innerHTML = id3_track;
    document.getElementById("info_id3_comment").innerHTML = id3_comment;

    isPlaying = (isPlaying == "true");
    document.getElementById("playerplay").style.display = (isPlaying)?"none":"block";
    document.getElementById("playerpause").style.display = (isPlaying)?"block":"none";

    var timelineWidth = 160;
    var sliderWidth = 40;
    var sliderPositionMin = 40;
    var sliderPositionMax = sliderPositionMin + timelineWidth - sliderWidth;
    var sliderPosition = sliderPositionMin + Math.round((timelineWidth - sliderWidth)* position / duration);

    if (sliderPosition < sliderPositionMin) {
        sliderPosition = sliderPositionMin;
    }
    if (sliderPosition > sliderPositionMax) {
        sliderPosition = sliderPositionMax;
    }

    document.getElementById("playerslider").style.left = sliderPosition+"px";*/
};

/**
 * private functions
 */
//var sliderPressed = false;
function _getFlashObject()
{
    return document.getElementById("myFlash");
}
/*function _cumulativeOffset (pElement)
{
    var valueT = 0, valueL = 0;
    do {
        valueT += pElement.offsetTop  || 0;
        valueL += pElement.offsetLeft || 0;
        pElement = pElement.offsetParent;
    } while (pElement);
    return [valueL, valueT];
}
function _xmouse(pEvent)
{
    return pEvent.pageX || (pEvent.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft));
}
function _ymouse(pEvent)
{
    return pEvent.pageY || (pEvent.clientY + (document.documentElement.scrollTop || document.body.scrollTop));
}
function _findPosX(pElement)
{
    if (!pElement) return 0;
    var pos = _cumulativeOffset(pElement);
    return pos[0];
}
function _findPosY(pElement)
{
    if (!pElement) return 0;
    var pos = _cumulativeOffset(pElement);
    return pos[1];
}
function _addEventListener(pElement, pName, pListener, pUseCapture)
{
    if (pElement.addEventListener) {
        pElement.addEventListener(pName, pListener, pUseCapture);
    } else if (pElement.attachEvent) {
        pElement.attachEvent("on"+pName, pListener);
    }
}
function _sliderDown(pEvent)
{
    sliderPressed = true;
}
function _sliderMove(pEvent)
{
    if (sliderPressed) {
        var timelineWidth = 160;
        var sliderWidth = 40;
        var sliderPositionMin = 40;
        var sliderPositionMax = sliderPositionMin + timelineWidth - sliderWidth;
        var startX = _findPosX(document.getElementById("timeline"));
        var x = _xmouse(pEvent) - sliderWidth / 2;

        if (x < startX) {
            var position = 0;
        } else if (x > startX + timelineWidth) {
            var position = myListener.duration;
        } else {
            var position = Math.round(myListener.duration * (x - startX - sliderWidth) / (startX + timelineWidth - sliderWidth - startX));
        }
        _getFlashObject().SetVariable("method:setPosition", position);
    }
}
function _sliderUp(pEvent)
{
    sliderPressed = false;
}*/

/**
 * public functions
 */
function play() {
    /*if (myListener.url == "undefined") {
        _getFlashObject().SetVariable("method:setUrl", "test.mp3");
    }*/
    //_getFlashObject().SetVariable("method:setUrl", "http://scfire-nyk-aa01.stream.aol.com:80/stream/1074");
    _getFlashObject().SetVariable("method:play", "");
}
function pause() {
    _getFlashObject().SetVariable("method:pause", "");
}
function stop() {
    _getFlashObject().SetVariable("method:stop", "");
}

$(document).ready(function(){
    $('.next-member a').click(function(){
        $(this).parent().parent().hide();
        $($(this).attr('href')).show().find('.diagnosis').click();
        return false;
    });
});

