var checkframe;
var frameloc;

$(document).ready(function()
{

    if($.browser.safari){
        $(".mainDiv").wrap('<div class="saf">');
        $(".mainDiv").append('</div>')
    }
    if($.browser.mozilla){
        $(".mainDiv").wrap('<div class="firefox">');
        $(".mainDiv").append('</div>')
    }
    if($.browser.msie){
        $(".mainDiv").wrap('<div class="ie">');
        $(".mainDiv").append('</div>')
    }
    //examine the iframe
    checkframe = setInterval(function() {

    watchLocation();
    }, 500
	);

    $(window).bind("resize", resizeWindow);
    
    
    
    $( ".index a" ).each(
        function(){
            var newalt = $(this).attr("href");
            $( this ).attr({alt: newalt });
            $(this).removeAttr("href");
            //alert($(this).attr("alt"));
        }
    );



    $(".index a").click(function(){
     $(".index a").removeClass("aSel");
        $(this).addClass("aSel");
     
        Navigate($(this).attr("alt"));
    });
    
    resizeWindow();
    NavigateOnQueryString();
    
    $(".printDiv").click(function(){
     if($.browser.msie){
            frames[0].focus();
            frames[0].print(); 
        }else{
            var el= frames[0];
	        el.focus();
	        el.print();
        }
    });

	frames[0].focus();
});

function GetFrameLoc()
{
	return frames[0].location.href;
}

function resizeWindow()
{
	$(".contentFrame").height($(window).height()-25);
	if($.browser.msie){
		$(".contentFrame").width($(window).width()-224);
	}else{
		$(".contentFrame").width($(window).width()-240);
	}
	$(".index").height($(window).height());
	
	$(".addr").width($(window).width()-274);
}

//Navigates to the appropriate querystring set doc
function NavigateOnQueryString()
{
    var u = querySt("u");
    
    if(u != "" && u != undefined){
    u = "Pages/"+u
    u= u.replace("--","#");
        Navigate(u);
        $("[alt="+u+"]").addClass("aSel");
        

    }
}

//QueryString Parser
function querySt(ji)
{
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i=0;i<gy.length;i++) {
        ft = gy[i].split("=");
        if (ft[0] == ji) {
            return ft[1];
        }
    }
}

function Navigate(navPath)
{
	var el= frames[0];

	el.location= navPath;
	el.focus();
}

function TreatedFrameString()
{
    var framePath = GetFrameLoc().replace('Pages/','');
    var loc = window.location.toString();
    var ind = framePath.lastIndexOf('/');
    framePath = framePath.substring((ind+1),framePath.length);
	var ind= loc.lastIndexOf('/');
    loc = loc.substr(0,ind)+"?u="+framePath;
    //search our index and hilight it.
    loc =loc.replace('#','--');
    return loc;
}

function watchLocation()
{
    if(frameloc != GetFrameLoc())
	{
        frameloc = GetFrameLoc();
        //alert("frame has changed to:"+ frameloc);
        $("#addr").attr("value", TreatedFrameString());
        UpdateNavHilite();
    }
}

function UpdateNavHilite()
{
    var path = GetFrameLoc();
    path = path.substring((path.lastIndexOf('/')+1),path.length);
    path = 'Pages/'+path;
    $(".index a").removeClass("aSel");
    if($(".index a[alt="+path+"]").offset() == null){
        path = path.substring(0,(path.lastIndexOf('#')));
    }
    $(".index a[alt="+path+"]").addClass("aSel");
        var divOffset = $('.index').offset().top;
        var pOffset;
    if($(".index a[alt="+path+"]").offset() != null){
        pOffset = $(".index a[alt="+path+"]").offset().top;
    }else{ 
        //alert(' couldnt find ' +path);
        pOffset = 0
     }
    var pScroll = pOffset - divOffset;
    pScroll = pScroll - ($(window).height() * .5);
    $('.index').animate({scrollTop: '+=' + pScroll + 'px'}, 500, 'easeOutCirc');
}

function KWIsOnline()
{
	return 1;
}
