
function endsWith(sInput, sPatten) {
    return (sInput.match(sPatten + "$") == sPatten);
}

function fish_dropMenu()
{
	$("#nav ul ").css({display: "none"});
	$("#nav li").hover(
			function()
			{
				$(this).find('ul:first').css({visibility: "visible",display: "none"}).slideDown();
			},
			function()
			{
				$(this).find('ul:first').css({visibility: "hidden"});
			}
	);
}
 
$(document).ready(function(){					
	fish_dropMenu();
	
	$('#banner ul').cycle({ 
		fx:     'fade', 
		speed:  2000, 
		timeout: 10000
	}); 
	
});

function toggleDiv(divId)
{
	// use Jquery
	$("#" + divId).toggle();
}

function showDiv(divId, type)
{
	document.getElementById(divId).style.display = type;
}

function hideDiv(divId)
{
	document.getElementById(divId).style.display = "none";
}

function loadContent(sURL, sDivId) {
    var bool_Return = false;
		
	//clearTimeout(objTimer);
	
	    
    add2History(sURL, sDivId);

	bool_Return = loadHTML(sURL, sDivId);
	//objTimer 	= setTimeout('doUpdate()', 50000);
	
	return bool_Return;
}

function loadHTML(sURL, sDivId){
    var objNow = new Date();
	var sURL_To_Load = sURL;
	
	/*if (sURL_To_Load.indexOf("ajax-content") == -1) {
        sURL_To_Load = ROOT + 'ajax-content/' + ((sURL_To_Load.indexOf(SITE_URL) > -1) ? sURL_To_Load.replace(SITE_URL + ROOT, '') : sURL_To_Load.replace(ROOT, ''));
    }*/
	
    sURL_To_Load = sURL_To_Load.replace("/site/", "/ajax-content/");
    
    sURL_To_Load += (!endsWith(sURL_To_Load, "/")) ? "/" : "";
    sURL_To_Load += "default.asp?ajaxload=true&requestTime=" + objNow.toString();

	$.ajax({
        url: sURL_To_Load,
        success: function(xhmlContent, textStatus, XMLHttpRequest) { handleResponse(sDivId, xhmlContent, textStatus, XMLHttpRequest); }
    });

	return false;
}

function postContent(objForm, sDivId) {
    var sURL_To_Load = objForm.action;
    var sData        = $(objForm).serialize();
    
    sURL_To_Load = sURL_To_Load.replace("/site/", "/ajax-content/");
    
    $.ajax({
        type: 'POST',
        url: sURL_To_Load,
        data: sData,
        success: function(xhmlContent, textStatus, XMLHttpRequest) { handleResponse(sDivId, xhmlContent, textStatus, XMLHttpRequest); }
    });
    
	return false;
}


function handleResponse(sDivId, xhmlContent, textStatus, XMLHttpRequest) { 

    $("#" + sDivId).html($(xhmlContent).children());
   
    Cufon.replace('.Cooper-font', {fontFamily: 'Cooper', hover: true });
    
    $("#overlay-content-blackout").fadeIn('slow');
    $("#overlay-holder").fadeIn('slow');
   
    $("#" + sDivId).find("a").each(function () {
        var sHref = $(this).attr("href");
        
        if ((sHref.indexOf("http:") == -1)) {
            if (!$(this).hasClass("noOverlay")) {
                $(this).click(function() {return loadContent(this.href, 'overlay-content');});
            } 
        }           
    });
    
    $('#overlay-holder .scroll-pane').jScrollPane({
        animateTo: true,
        scrollbarWidth: 10
    });
    
    tooltip.init();

    try {
        //JT_init();
        
        popupLoaded();

    } catch (e2) {
        //alert("error");
    }
}

function closeOverlay(sDivId){
    $("#overlay-content-blackout").hide();	//.fadeOut('fast');
    $("#overlay-holder").hide();	//.fadeOut('fast');
    $("#" + sDivId).html('<div style="text-align: center;padding-top: 150px; color: #000000;"><img src="' + ROOT + '/images/icons/wait_loader.gif" /><br />Loading...</div>');
    
    return false;
}


var aPageHistory    = new Array();
var sLastHash       = "";

function add2History(sURL, sDivId) {
    aPageHistory.push({url:sURL, div:sDivId});
    
    location.hash = sLastHash = "#" + aPageHistory.length;
}

function checkHistoryHash() {
    var iIndex  = -1;
    var sURL    = "";
    var sDivId  = "";
    
    if (location.hash != sLastHash) {
        sLastHash = location.hash;
        
        try{
            if (location.hash != "") {              
                iIndex  = parseInt(location.hash.replace('#', ''));

                sURL    = aPageHistory[iIndex-1].url;
                sDivId  = aPageHistory[iIndex-1].div;
                
                loadHTML(sURL, sDivId);
            } else {

                closeOverlay(aPageHistory[0].div);
            }
        } catch(e) {
            //donothing
        }
    }
}

