$(document).ready(function() {
    //Shadowbox.init();
    $("a.link1").bind("click", function() {getFirstPage(this.id)});
    $("a.link2").bind("click", function() {getNextPage(this.id)});
    $("a.bmark").bind("click", function() {setBookmark(this.id)});
    $("a.email").bind("click", function() {getEmailForm()});
    $("a.fsize").bind("click", function() {styleCycle()});
    $("a.logout").bind("click", function() {chkLogout(this.id)});
    $('a.styleswitch').bind('click', function(){switchStylestyle(this.getAttribute('rel'));return false;});
    var c = readCookie('RamosusStyle');
    if (c) switchStylestyle(c);
    if (c && $('link[@rel*=style][title="' + c + '"]').size()>0) 
        {switchStylestyle( c );}
        else {switchStylestyle('ramosus100');}
    
    var options = {dataType: 'json', beforeSubmit: beforeAjax,success: afterAjax};
    $('#ewlform,#quizform,#textform,#emailform,#glossform').submit(function() 
        {$(this).ajaxSubmit(options); return false;}); 
    getTWidth('#sText','#textform');
   // $('textarea.expanding').autogrow();
    Shadowbox.init({skipSetup:true});
});

thumbWidth = 80;		scrollDivWidth = 520;
var cel = null;
function toggleGlossary(el){
    if (cel) {
            $('#email').hide();
    $('#themeaning').html("").removeClass("box").slideUp('slow', function() {$(this).height('');});
    $('#thereply').html("").slideUp('slow').removeClass("box");
            $("#glossform")[0].reset();
    $('#' + cel).slideUp('slow');
    }
	if (el != cel) {
		cel = el;
        $('#themeaning').addClass("box");
		$('#' + cel).slideDown('slow');
	} else {
		cel = null;
	}
	return false;
}
function emailGlossaryRequest(el,si){ 
    $.post("scripts/ajax_ramosus.asp?sid=glsemail&w="+el+"&s="+si, "", function(text){
    $("#themeaning").fadeOut('slow').html("");
    $('#glossform').find("input[@type='text']").val('');
    $("#glossary").fadeOut('slow'); 
    $("#indicate").hide();
    $("#themeaning").html(text).fadeIn("slow").animate({backgroundColor:'yellow'},2000);
    return false; });
    }
        
function emailRamosus(el){ 
    $("div#themeaning").html("");
    $.post("scripts/ajax_ramosus.asp?sid=showEmail&pblID="+el, "", function(text){
    $("div#indicate2").hide();
    $("div#thereply").html(text).fadeIn("slow");
    return false; });
    }        
        
function toggleContent(el){
		if (cel) {
			$('#' + cel).slideUp('slow');
		}
	if (el != cel) {
		cel = el;
		$('#' + cel).slideDown('slow');
	} else {
		cel = null;
	}
	return false;
}
function getTWidth(rj,pj){
    var ez=$(rj).length;
    if(ez==1){
        var ej = $(pj).parent().attr("id");
        if(ej=='fullscreen' || ej=='email'){
            var wi = ($(window).width() - 85);
       // $.log(ej);
        } else {
        var wi = 0.5*($(window).width() - 45);
        }
       // $.log(wi);
        $(rj).css({width:wi+'px'});
        $(rj)[0].focus();
    }
};
function switchStylestyle(styleName){
    if ($('link[@rel*=style][title="' + styleName + '"]').size()==0) return;
    $('link[@rel*=style][title]').each(function(i)
    {
            this.disabled = true;
            if (this.getAttribute('title') == styleName) this.disabled = false;
    });
    createCookie('RamosusStyle', styleName, 365);
}
function styleCycle(){
        $("#ssheet").slideToggle(500);
        $("#ssheet").removeClass("fsize").addClass("fsizeh"); 
        $("a.fsizeh").unbind();
        $("a.fsizeh").bind("click", function() {hideStyle()});
};
function hideStyle(){  
        $("#ssheet").slideToggle(500);
        $("#ssheet").removeClass("fsizeh").addClass("fsize"); 
        $("a.fsize").unbind();
        $("a.fsize").bind("click", function() {styleCycle()});
};
function chkLogout(ej) {
        $("#chkLO").slideToggle(500);
        $("#chkLO").removeClass("logout").addClass("logouth"); 
        $("a.logouth").unbind();
        $("a.logouth").bind("click", function() {hideChk()});
        $("a.logout2").bind("click", function() {setLogout(this.id)});
        $("a.logout3").bind("click", function() {Logout()});
};
function Logout(){   
    $.getJSON("scripts/ajax_ramosus.asp?sID=logout2&q="  + new Date().getTime(), function(json){    
    if (json.fields) {
        for (var i = 0; i < json.fields.length; i++) {
            var field = json.fields[i]; 
            $("#wrapper").html(field.msg).fadeIn(1000).animate({opacity: 1.0}, 1000).fadeOut(3000, function(){self.close()}); 
         }}
        });   
};
function hideChk(){  
        $("#chkLO").slideToggle(500);
        $("#chkLO").removeClass("logouth").addClass("logout"); 
        $("a.logout").unbind();
        $("a.logout").bind("click", function() {chkLogout()});
};
function setLogout(ej)  {    
    $.getJSON("scripts/ajax_ramosus.asp?sID=logout&brm="+ej +"&q="  + new Date().getTime(), function(json){    
    if (json.fields) {
        for (var i = 0; i < json.fields.length; i++) {
            var field = json.fields[i]; 
                switch(field.yesno) {
                case "Y":
                    $("#wrapper").html(field.msg).fadeIn(1000).animate({opacity: 1.0}, 1000).fadeOut(3000, function(){self.close()}); 
                    break;
                case "N":
                    $("#wrapper").html(field.msg).fadeIn(700); 
                    break;
         }}}
        });   
};
function setBookmark(ej) {    
    $.getJSON("scripts/ajax_ramosus.asp?sID=bMark&brm="+ej +"&q="  + new Date().getTime(), function(json){    
    if (json.fields) {
        for (var i = 0; i < json.fields.length; i++) {
            var field = json.fields[i]; 
                switch(field.yesno) {
                case "Y":
                    $("#bruce").attr("src","images/site/r_check.gif"); 
                    break;
                case "N":
                    $("#bruce").animate({backgroundColor:'#99CC33'},2000);
                    break;
         }}}
        });   
};
function getEmailForm(){  
    $.get("scripts/ajax_ramosus.asp?sID=email&q="  + new Date().getTime(), function(responseText){
        $("#email").html(responseText).slideToggle(500);
        $("#theemail").removeClass("email").addClass("emailh"); //.css("height","300");
        $("a.emailh").unbind();
        $("a.emailh").bind("click", function() {hideEmailForm()});
        var options = {dataType: 'json', beforeSubmit: beforeAjax,success: afterAjax};  
        $('#emailform').submit(function() {$(this).ajaxSubmit(options); return false; }); 
        getTWidth('#message','#emailform')
        });   
};
function hideEmailForm(){  
        $("#email").slideToggle(500);
        $("#theemail").removeClass("emailh").addClass("email"); 
        $("a.email").unbind();
        $("a.email").bind("click", function() {getEmailForm()});
};

function onAfter() { 
    $('#ssoutput').html(this.title); 
}
function getFirstPage(ej) {    
    $('#s1').cycle('stop');
    $.get("scripts/ajax_ramosus_client_first.asp?brm="+ej +"&q="  + new Date().getTime(), function(responseText){
        $("#wrapper").html(responseText).fadeIn(700);  
        //$('#toolbar a').Tooltip({delay: 800,showURL: false,showBody: " - "});
        var options = {dataType: 'json', beforeSubmit: beforeAjax,success: afterAjax};  
        $('#ewlform,#quizform,#textform,#emailform,#glossform').submit(function() {$(this).ajaxSubmit(options); return false; }); 
        getTWidth('#sText','#textform');
        //$('textarea.expanding').autogrow();
        $('a.styleswitch').bind("click", function(){switchStylestyle(this.getAttribute("rel"));return false;});
        var c = readCookie('RamosusStyle');if (c) switchStylestyle(c);
        $("a.link2").bind("click", function() {getNextPage(this.id)});
        $("a.link1").bind("click", function() {getFirstPage(this.id)});
        $("a.bmark").bind("click", function() {setBookmark(this.id)});
        $("a.email").bind("click", function() {getEmailForm()});
        $("a.emailh").bind("click", function() {hideEmailForm()});
        $("a.fsize").bind("click", function() {styleCycle()});
        $("a.logout").bind("click", function() {chkLogout(this.id)});
		$('a.rtip').cluetip({splitTitle: '|',width:250});
        $('#s1').cycle({fx:'fade', timeout: 4000, after: onAfter,pause: 1,next: '#s1',delay: -3000});
        Shadowbox.setup($('a.sbox'),{overlayOpacity: 0.8});   
        });   
};
function getNextPage(ej) {  
    $('#s1').cycle('stop');
	$("#theImage img").fullsize({destroy: true});

    $.get("scripts/ajax_ramosus_client_second.asp?brm="+ej +"&q="  + new Date().getTime(), function(responseText){
        $("#wrapper").html(responseText);  
        
//        if (($('#theImage').length>0)) {   
//            var img_width = $('#theImage').width();         
//            $.log(img_width);
//            $("#sCaption").css("width",img_width);
//        };
        var options = {dataType: 'json', beforeSubmit: beforeAjax,success: afterAjax};  
        $('#ewlform,#quizform,#textform,#emailform,#glossform').submit(function() {$(this).ajaxSubmit(options); return false; }); 
        getTWidth('#sText','#textform');
        $('a.styleswitch').bind("click", function(){switchStylestyle(this.getAttribute("rel"));return false;});
        var c = readCookie('RamosusStyle');if (c) switchStylestyle(c);
        $("a.link1").bind("click", function() {getFirstPage(this.id)});
        $("a.link2").bind("click", function() {getNextPage(this.id)});
        $("a.bmark").bind("click", function() {setBookmark(this.id)});
        $("a.email").bind("click", function() {getEmailForm()});
        $("a.emailh").bind("click", function() {hideEmailForm()});
        $("a.fsize").bind("click", function() {styleCycle()});
        $("a.logout").bind("click", function() {chkLogout(this.id)});
        //fitCaption();
        //alert($('#theImage').width());
        $('#s1').cycle({fx:'fade', timeout: 4000, after: onAfter,pause: 1,next: '#s1',delay: -2000});
       // $('textarea.expanding').autogrow();
	    //alert("here");
		$('a.rtip').cluetip({splitTitle: '|',width:250});
        $("#theImage img.pad5z").fullsize();  
        //Shadowbox.setup($('a.sbox'),{overlayOpacity: 0.8});   
        });   
};
function beforeAjax(formData, jqForm) { 
    //$("#wrk").show();
    $("#ewlmsg").html('');
};
function afterAjax(json) {
    if (json.fields) {
        for (var i = 0; i < json.fields.length; i++) {
            var field = json.fields[i]; 
            $.scrollTo( 0 );//reset the screen to (0,0)
            switch(field.zone) {
                case "quiz":
                    $("#iQuizCycle").val(field.iQC);
                    switch(field.yesno) {
                    case "Y":
                        //$("#quiz").animate({backgroundColor:'#9C3'},2000).animate({backgroundColor:'white'},2000);$("#quiz").slideUp(400,function() {$("#fquiz").html(field.results).slideDown(400).addClass('gray');$.scrollTo('#toolbar',2000);$("input:radio").attr('disabled','disabled');});
                        $("#quiz").animate({backgroundColor:'#9C3'},2000,function(){$("#quiz").html('').animate({backgroundColor:'white'},2000);$("#fquiz").html(field.results).addClass('gray');$.scrollTo('#toolbar',2000);$("input:radio").attr('disabled','disabled'); $("#quizmsg").html(field.msg).addClass("blue").removeClass("dht");$("a.link2").bind("click", function() {getNextPage(this.id)});});
                       
                        
                        //$.log("here");
                        break;
                    case "F1":
                        //$("#quiz").addClass('gray').animate({backgroundColor:'#FF0000'},2000,function() {$("#quiz").animate({backgroundColor:'white'},1000).html(field.results);$.scrollTo('#toolbar', 1000);$("input:radio").attr('disabled','disabled');});
                        $("#quiz").animate({backgroundColor:'#F00'},2000,function(){$("#quiz").html('').animate({backgroundColor:'white'},2000);$("#fquiz").html(field.results).addClass('gray');$.scrollTo('#toolbar',2000);$("input:radio").attr('disabled','disabled');$("#quizmsg").html(field.msg).addClass("red").removeClass("dht");$("a.link2").bind("click", function() {getNextPage(this.id)});});
                                                
                        
                        break;
                    case "F2":
                        $("#quiz").addClass('gray').animate({backgroundColor:'red'},2000);
                        $("#quizmsg").html(field.msg).removeClass("dht");
                        $("#quizform").slideUp(1000);
                        $("a.link2").bind("click", function() {getNextPage(this.id)});
                        break;
                    case "N":
                        $("#quiz").animate({backgroundColor:'#FF0000'},2000,function() {$("#quizmsg").html(field.msg).addClass("red");$('#quiz').animate({backgroundColor:'#FFF'},1000);
                        $.scrollTo('#toolbar', 1000);});                        
                        $("#iQuizCycle").attr("value",field.cycle);
                        $("a.link2").bind("click", function() {getNextPage(this.id)});
                    }
                    break;
                case "ewl":
                    $("#iQuizCycle").val(field.iQC);
                    switch(field.yesno) {
                    case "Y":
                        //$("#quiz").animate({backgroundColor:'#FF0000'},2000,function() {$("#quizmsg").html(field.msg).addClass("red");$('#quiz').animate({backgroundColor:'#FFF'},2000);
                        $("#ewl").animate({backgroundColor:'#99CC33'},2000).animate({backgroundColor:'#FFF'},1000);
                        $("#ewlform").slideUp(500);
                        $("#ewlmsg").html(field.msg).show();
                        $("a.link2").bind("click", function() {getNextPage(this.id)});
                        break;
                    case "F":
                        $("#ewl").animate({backgroundColor:'#FF0000'},2000).animate({backgroundColor:'#FFF'},1000);
//                        $("#ewl").animate({backgroundColor:'red'},2000);
                        $("#ewlmsg").html(field.msg).show();
                        $("#ewlform").slideUp(500);
                        $("a.link2").bind("click", function() {getNextPage(this.id)});
                        break;
                    case "N":
                        $("#ewl").animate({backgroundColor:'#FF0000'},2000).animate({backgroundColor:'#FFF'},1000);
                        $("#ewlmsg").html(field.msg);
                        $("a.link2").bind("click", function() {getNextPage(this.id)});
                    }
                    break;
                case "gls":
                    switch(field.yesno) {
                    case "Y":
                        //$("#glossform").highlightFade({color:'yellow',speed:4000,iterator:'sinusoidal'});
                        $("#themeaning").html(field.msg).show().animate({backgroundColor:'yellow'},2000);
                        //$("#glossform")[0].reset();
                        break;
                    case "N":
                        //$("#glossform").highlightFade({color:'red',speed:4000,iterator:'sinusoidal'});
                        $("#themeaning").html(field.msg).show().animate({backgroundColor:'red'},2000);
                        //$("#glossform")[0].reset();
                    }
                break;
                case "email": 
                    switch(field.yesno) {
                    case "Y":
                        $("#indicate2").hide();
                        $("#email").animate({backgroundColor:'yellow'},2000).slideToggle(600);
                        $("#emailform")[0].reset();
                        $("#thereply").html(field.message).show();
                        break;
                    case "N":
                        $("#editnode").animate({backgroundColor:'red'},2000);
                        $('#fbackMsg').html(field.message).addClass("error").show();
                    }
                break;
                case "txt":
                    switch(field.yesno) {
                    case "Y":
                        $("#sText").animate({backgroundColor:'yellow'},2000);
                        $("#textform").slideUp(500);
                        $("#textmsg").html(field.msg).addClass("dht");
                        $("a.link2").bind("click", function() {getNextPage(this.id)});
                        break;
                    case "Y2":
                        $("#sText").animate({backgroundColor:'yellow'},2000);
                        $("#textform").slideUp(500);
                        $("#textmsg").html(field.msg).animate({backgroundColor:'#99CC33'},2000).animate({backgroundColor:'#FFF'}, 1000);
                        $("a.link2").bind("click", function() {getNextPage(this.id)});
                        break;
                    case "N":
                        $("#ewl").animate({backgroundColor:'red'},2000);
                        $("#ewlmsg").html(field.msg);
                    }
                    break;
            }
        }
    }
};

//var $jQ= jQuery.noConflict();

jQuery.preloadImages = function()
{
	for(var i = 0; i<arguments.length; i++)
	{
		jQuery("<img>").attr("src", arguments[i]);
	}
};

jQuery.fn.focusFirstInput = function(){

  // If there's a form on the page, then set focus to the first visible input element

  var e = $('form input:visible');

  if(e && e.get(0)){e.get(0).focus();}  

}



jQuery.fn.fadeIn = function(speed, callback) { 
    return this.animate({opacity: 'show'}, speed, function() { 
        if (jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (typeof callback == 'function')  
            callback();  
    }); 
}; 

 

jQuery.fn.fadeOut = function(speed, callback) { 
    return this.animate({opacity: 'hide'}, speed, function() { 
        if (jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (typeof callback == 'function')  
            callback();  
    }); 
}; 

 

jQuery.fn.fadeTo = function(speed,to,callback) { 
    return this.animate({opacity: to}, speed, function() { 
        if (to == 1 && jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (typeof callback == 'function')  
            callback();  
    }); 
}; 

