var $j = jQuery.noConflict();

function errorSubmit(){
    if (document.getElementById('file'))
        document.getElementById('file').style.visibility = 'visible';
     $j('div#errorDiv').css('display','none');
    document.getElementById('errorForm').submit();
    return false;
}

function getScrollY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return scrOfY;
}

function getHeight() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}

function ShowDialog(x) {
    var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
    if (IE6) {
        selectLists = document.getElementsByTagName('select');
        for (var counter=0; counter<selectLists.length; counter++) {
            if (selectLists[counter].id != 'newYear' && selectLists[counter].id != 'newTerm') {
                selectLists[counter].style.visibility = 'hidden';
            }
        }
    }
    pos = getScrollY();
    vpHeight = getHeight();
    dialogHeight = x.offsetHeight;
    dialogTop = (vpHeight/2) - (dialogHeight/2) + pos;
    x.style.top = dialogTop+"px";
    x.style.display = "block";
	
    dialogHeight = x.offsetHeight;
    dialogTop = (vpHeight/2) - (dialogHeight/2) + pos;
    x.style.top = dialogTop+"px";

}

function termFound(strToSearch, term){
    return (strToSearch.indexOf(term) !== -1);
}

function hasReaderActiveX(){
    var axObj = null;
    var version = '';
    for (x=2; x<10; x++) {
        try {
            oAcro=eval("new ActiveXObject('PDF.PdfCtrl."+x+"');");
            if (oAcro) {
                version=x+'.0';
                return version;
            }
        } catch(e) {}
    }
    
    try {
        oAcro7=new ActiveXObject('AcroPDF.PDF.1');
        if (oAcro7) {
            version='7.0';
            return version;
        }
    } catch(e) {}
    
    try {
        oAcro4=new ActiveXObject('PDF.PdfCtrl.1');
        if (oAcro4) {
            version='4.0';
            return version;
        }
    } catch(e) {}
    try {
        oAcro7=new ActiveXObject('AcroPDF.PDF');
        if (oAcro7) {
            version='7.0';
            return version;
        }
    } catch(e) {}
    
    try {
        oAcro4=new ActiveXObject('PDF.PdfCtrl');
        if (oAcro4) {
            version='4.0';
            return version;
        }
    } catch(e) {}

    return version;
			//~ var axObj = null;
			//~ if (window.ActiveXObject) {
				//~ axObj = new ActiveXObject("AcroPDF.PDF");
				//~ if(!axObj){
					//~ axObj = new ActiveXObject("PDF.PdfCtrl");
				//~ }
				//~ if (axObj !== null) { 
					//~ return "2.0";
				//~ }
			//~ }
			//~ return "";
}

function hasReader(){
    var n = navigator.plugins;
    
    for(var i=0; i < n.length; i++){
        var name = n[i].name;
        if(termFound(name, "Adobe Reader") || termFound(name, "Adobe PDF") || termFound(name, "Acrobat")){
            return n[i].description;
        }
    }
    return '';
}


function hasGeneric(){				//Detects PDF plugin, regardless of manufacturer
    var plugin = navigator.mimeTypes["application/pdf"];
    if (plugin && plugin.enabledPlugin) {
        return plugin.description;
    } else {
        return '';
    }
}


function GetPdfVer(){
    var type = null;
    var ff_ver = hasReader();
    if (ff_ver != '') {
        return "Adobe" + " FF " +  ff_ver;
    }
    var ie_ver = hasReaderActiveX()
    if(ie_ver != ''){
        return "Adobe" + " IE " +  ie_ver;
    }
    var gen = hasGeneric();
    if(gen != '') {
        return "Generic " + gen;
    }
    return 'No PDF viewer';
}

 $j(document).ready(function() {
    //~ alert(GetPdfVer());
    $j('#errorClose').click(function(){
        if (document.getElementById('file'))
            document.getElementById('file').style.visibility = 'visible';
        $j('div#errorDiv').css('display','none');
        var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
        if (IE6) {
            selectLists = document.getElementsByTagName('select');
            for (var counter=0; counter<selectLists.length; counter++) {
                selectLists[counter].style.visibility = 'visible';
            }
        }
        return false;
    });
    
    $j('#errorOk').click(errorSubmit);
     
    $j('#reportError').click(function(){
        //~ if (document.getElementById('file'))
            //~ document.getElementById('file').style.visibility = 'hidden';
        document.getElementById("page_cont").value = document.documentElement.innerHTML;
        document.getElementById("flsver").value = GetSwfVer();
        document.getElementById("pdfver").value = GetPdfVer();
        showPopup(440, 280, 'feedbackPopup');
        document.getElementById('comment_rep').focus();
        return false;
    });
    
});

/** New Code **/
(function($){
    $(function(){
        
        var _opt = $("#search-option");
        var _dd = $("#search-dd");
        var _a = $("#search-dd a");
        var _body = $("body");
        var _searchBox = $("#search-box");
        var _searchBtn = $("#search-btn");
        var _cannedBtn = $("#canned-hint-btn");
        var _login = $("#loginLink");
        var _searchBoxSpan = $("#search-box-span");
        var _cannedHint = "";
        var _cannedDoc = "<select id='cans'><option>See free sample queries</option><option>Math - Calculus Intro</option><option>English - Intro</option><option>Chemistry - Intro</option><option>History - American History Intro</option><option>Physics - Intro</option></select>";
        var _cannedTb = "<select id='cans'><option>Algebra</option><option>Calculus</option><option>Physics</option><option>0495011665</option><option>Mechanics</option></select>";
        var _inputBox = '<input name="search_term" id="search-box" type="text" value="Enter search term here"/>';
        var cannedHandler = function(){
            //var sel = $("#cans option:selected");
            //window.location = $("#search-form").attr("action") + "?basic&search_term=" + sel.val();
            document.getElementById("search-form").submit();
            return false;
        }
        _opt.mouseover(function(){
            _dd.slideDown("slow");
        });
        _dd.click(function(){
            _dd.slideUp("slow");
            _searchBox.focus();
        });
        _body.click(function(){
            _dd.slideUp("slow");
        });
        _a.click(function(){
            var _txt = $(this).text();
            var _action = $(this).attr("action");
            _opt.text("Search " + _txt);
            $("#search-form").attr("action", _action);
            
            if(__basic){
                if(_action == "/docSearch.php"){
                    _searchBoxSpan.html(_cannedDoc + "&nbsp;&nbsp;" + _cannedHint + "&nbsp;&nbsp;");
                    _searchBtn.click(cannedHandler);
                    //$("#canned-hint-btn").click(function(){window.location="registration.php"});
                }else{
                    _searchBoxSpan.html(_inputBox);
                    _searchBtn.unbind("click");
                }
            }
            if( $("#search-box") != null ){
                $("#search-box").focus(function(){
                    onFocusText(document.getElementById("search-box"), "Enter search term here");
                })
                $("#search-box").blur(function(){
                    onBlurText(document.getElementById("search-box"), "Enter search term here");
                })
                $("#search-box").focus();
            }
            _dd.slideUp("slow");
            return false;   
        });
        if(__basic){
            _searchBtn.click(cannedHandler);
        }
        
        $("#search-box").focus(function(){
            onFocusText(document.getElementById("search-box"), "Enter search term here");
        })
        $("#search-box").blur(function(){
            onBlurText(document.getElementById("search-box"), "Enter search term here");
        })
            
            $("#logoutLink").click(function(){
                var _win = window.open("logoutframeset.php","logout","height=300,width=300");
                (_win) ? _win.focus() : "";
            })
            
        _login.click(function(){
            var _win = window.open(_login.attr("href"),"login","height=450,width=600");
            (_win) ? _win.focus() : "";
            return false;
        });
            
    });
}(jQuery))
