// Bryan Migliorisi

// Fixed dimScreen function - sets the width to 100% instead of calculating it  
jQuery.extend({dimScreen:function(a,b,c){var d;if(jQuery('#__dimScreen').size()>0){return}jQuery('embed,object').each(function(){jQuery(this).css('visibility','hidden')});if(navigator.appVersion.indexOf('MSIE 6.0')!=-1){jQuery('select').each(function(){jQuery(this).css('visibility','hidden')})}if(typeof a=='function'){c=a;a=null}if(typeof b=='function'){c=b;b=null}if(a<1){d=b;b=a;a=d}if(b>=1){d=a;a=b;b=d}a=(a>0)?a:500;b=(b>0)?b:0.5;return jQuery('<div></div>').attr({id:'__dimScreen',fade_opacity:b,speed:a}).css({background:'#000',height:((jQuery(document).height()>=jQuery(window).innerHeight())?jQuery(document).height():jQuery(window).innerHeight())+'px',left:'0px',opacity:0,position:'absolute',top:'0px',width:'100%',zIndex:999}).appendTo(document.body).fadeTo(a,b,c)},dimScreenStop:function(a){var x=jQuery('#__dimScreen');var b=x.attr('fade_opacity');var c=x.attr('speed');x.fadeOut(c,function(){x.remove();jQuery('embed,object').each(function(){jQuery(this).css('visibility','visible')});if(navigator.appVersion.indexOf('MSIE 6.0')!=-1){jQuery('select').each(function(){jQuery(this).css('visibility','visible')})}if(typeof a=='function'){a()}})}});


$j(document).ready(function(){
    $j('<li id="su_jobsTab"><a id="su_jobsBoardsTab_nav" href="http://www.SpeechiesUnited.org/Jobs.aspx"><span>Job Board</span></a></li>').insertAfter('li#ka_messageBoardsTab');
    updateOnline();
    loadMembersOnline();
    loadNewsFeeds();
    if (Ka.Info.PAGE == "pages/manageProfileQuestions.jsp") showQuestions();
    if (Ka.Info.PAGE == "pages/kickPlaceServerSide.jsp")    showAnswers();
    
    $j("<li><a id='su_sendFeedback' href='#' title='Send Us Feedback!'>Send Feedback</a></li>").appendTo("ul#ka_headerSubNav_list")

    $j("a#su_sendFeedback").click(function(){
        showFeedback();
        return false;
    });
    // Load Extra Scripts after page load
    loadFacebox();
});

function loadFacebox()
{
    if ($j('#su_Facebox').length > 0) return;
    $j('<link id="su_Facebox" rel="stylesheet" type="text/css" href="http://www.speechiesunited.org/scripts/facebox/facebox.css" />').appendTo("head");
    $j('<script type="text/javascript" src="http://www.speechiesunited.org/scripts/facebox/facebox.js"></script>').appendTo("head");
    while ($j('#su_Facebox').length <= 0) { }
    return;
}

function faceboxDestroyed()
{

}


function showFeedback()
{
    loadFacebox();
    $j.facebox(function() {
        $j.facebox('<div id="suFeedback"><h2>Speechies United Feedback</h2><p>Tell us what you think about Speechies United.  We would love to hear all of your comments, questions, complaints, and suggestions.</p><label for="txtEmailAddress">Your E-Mail Address:</label><input id="txtEmailAddress" name="txtEmailAddress" type="text" /><label for="txtFeedBack">Your Feedback:</label><textarea id="txtFeedBack" name="txtFeedBack" cols="20" rows="2"></textarea><br /><br /><input id="cmdSubmitFeedback" type="button" value="Send Feedback!" /></div>');
        $j("#cmdSubmitFeedback").click(function(){
            $j.facebox('<div id="suFeedback"><h3>Please wait...</h3><p>We are processing your feedback.  It\'ll take just a moment.</p></div>');
    	    params = $j(this).siblings("input,textarea").serialize();
            $j.getScript("http://www.speechiesunited.org/modules/feedback.ashx?"+params+"&memberName="+Ka.Info.USERNAME+"&page="+Ka.Info.PAGE, function(){  
                setTimeout(function() {
                    $j.facebox('<div id="suFeedback"><h3>Thanks for your feedback!</h3><p>We appreciate it, and will certainly take it into consideration.</p></div>');
                }, 500);            
            });   
        });
    });
    return; 
}



function getViewPort()
{
    // Returns the viewport dimensions as a js object
    
    var viewport = { height: 600, width: 800 };
    
    if (typeof window.innerWidth != 'undefined')
    {
        viewport.width = window.innerWidth;
        viewport.height = window.innerHeight;
    }
    else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
    { 
        viewport.width = document.documentElement.clientWidth;
        viewport.height = document.documentElement.clientHeight;
    }
    else
    {
        viewport.width = document.getElementsByTagName('body')[0].clientWidth;
        viewport.height = document.getElementsByTagName('body')[0].clientHeight;
    }
    return viewport;
}


function showQuestions()
{
    $tb = $j('#cq_4');
    $p = $j("<div id='su_profileHereFor'></div>")
	    .append("<span class='su_hereFor'><input type='checkbox' id='chk-herefor-1' name='chk-herefor-1' /><label for='chk-herefor-1'> Networking </label></span>")
	    .append("<span class='su_hereFor'><input type='checkbox' id='chk-herefor-2' name='chk-herefor-2' /><label for='chk-herefor-2'> Career Opportunities</label></span>")
	    .append("<span class='su_hereFor'><input type='checkbox' id='chk-herefor-3' name='chk-herefor-3' /><label for='chk-herefor-3'> Job Inquiries</label></span>")
	    .append("<span class='su_hereFor'><input type='checkbox' id='chk-herefor-4' name='chk-herefor-4' /><label for='chk-herefor-4'> Trading Therapy Techniques</label></span>")
	    .append("<span class='su_hereFor'><input type='checkbox' id='chk-herefor-5' name='chk-herefor-5' /><label for='chk-herefor-5'> Staying up to date with information in the field</label></span>")
	    .appendTo($tb.parent());
    $p.find("input").click(function() 
    { 
        var output = "";
        $p.find("input").each(function(i,e){ if(e.checked) output=output+$j(this).next("label").html()+", "; });   
        $tb.val(output).text(output); 
    });
    var text = $j('#cq_4').val().split(",");
    for(i=0;i<text.length;i=i+1) $j('#su_profileHereFor input').each(function(j,e){if ($j(e).next("label").html().trim(" ")==text[i].trim(" ")) this.checked=true; });
}

function showAnswers()
{
    $profile = $j("#ka_profileAboutToggleShow");
    $profile.find("a.ka_profileMediaToggleJS").hide();
    $profileShort= $j("#ka_profileAboutShort");
    $j("<div id='su_aboutMeMore'></div>").html($j("#ka_profileAboutLong").find("p")).insertAfter($profileShort);
}

function updateOnline()
{
    $j.getScript("http://www.speechiesunited.org/modules/onlinenow.ashx?action=set&id="+Ka.Info.USERID+"&n="+Ka.Info.USERNAME+"&page="+Ka.Info.PAGETYPE);
}

function loadMembersOnline()
{
    $j("<div id='su_OnlineNow'>Finding Members Online...</div>").insertAfter("#ka_myHomeURL")
    $j.getScript("http://www.speechiesunited.org/modules/onlinenow.ashx?action=get", function(){  
        setTimeout(function() {
            $j("#su_OnlineNow").html("<a href='' title='Members Online Now'>There are currently <em>"+(suOnlineNow.count)+"</em> Members Online</a>");
            $j("#su_OnlineNow a").click(function() {showMembersOnline();return false;})
        }, 500);            
    });      
}
function showMembersOnline()
{
    content = "";
    for(i=0;i<suOnlineNow.count;i+=1)
    {
        content += "<li><a href='http://community.speechiesunited.org/"+suOnlineNow.items[i].memberName+"' title='"+suOnlineNow.items[i].memberName+"'>"+suOnlineNow.items[i].memberName+"</a></li>"
    }
    content = "<div id='fb_membersOnline'><h3>Who's Online?</h3><ul>"+content+"</ul></div>";
    $j.facebox(content);
}

var hoverNewsFeed = false;
var rotateNewsFeedTimeout;
function rotateNewsFeeds()
{
    rotateNewsFeedTimeout=setTimeout("rotateNewsFeeds();", 8000);
    if (hoverNewsFeed == false )
    {
        $j('#su_newsFeeds ul li.active').removeClass('active').slideUp(500).next('li').slideDown(400).addClass('active');
        if ($j('#su_newsFeeds ul li.active').length <1) {$j('#su_newsFeeds ul li:first').slideDown(400).addClass('active'); }
    }
}

function loadNewsFeeds()
{
    $mhd = $j('#ka_myhomeDiscussions');
    if ($mhd.length > 0 )
    {
        $mhd.before("<div id='su_newsFeeds'><h3 class='ka_nullTitle ka_contentTitle'>Recent News</h3><span class='loading'>Loading news feeds...</span></div>");
        output = ""; 
        $j.getScript("http://www.speechiesunited.org/modules/newsfeed.ashx", function(){  
            setTimeout(function() {
                for (i=0;i<=suNewsFeed.items.length-2;i=i+1) 
                {
                    output += "<li style='display:none'>";
                    output += "<span class='desc'>"+suNewsFeed.items[i].description+"</span>";
                    output += "</li>";
                }
                
                $j.getScript("http://www.speechiesunited.org/ka-scripts/thickbox.js")
                $j('#su_newsFeeds h3').after("<ul>"+output+"</ul>")
                $j('#su_newsFeeds a').each(function(i,e){
                        $j(e).addClass("thickbox");
                        e.href=e.href+"?KeepThis=true&TB_iframe=true&height="+(getViewPort().height-100)+"&width="+(getViewPort().width-100)
                });
                    
                $j('#su_newsFeeds ul li:first').slideDown(500).addClass('active');
                $j('#su_newsFeeds span.loading').hide();}, 2000);
        });  
        $j('#su_newsFeeds').hover(
            function() {hoverNewsFeed = true;},
            function() {hoverNewsFeed = false; }
        );
        rotateNewsFeedTimeout=setTimeout("rotateNewsFeeds();", 8000);
    }
}
