$(document).ready(function() 
{
    $("#tabs").tabs();
    $(".selector").tabs( {selected: 0} );
    $("img.gallery").live('click',
    		function()
    		{
    			var src= $(this).attr('src');
    			var popurl = src.replace("thumbs/", "");
    	    	PopUpImage(popurl,$(this));
    		});
    // Preload animation for browser cache
	var ajaximg = new Image();
	ajaximg.src = 'images/ajax-loader.gif';
});

function LoadProgramsAccordion()
{
    $(function() {
		$("#programsaccordion").accordion({
                        autoHeight: false,
			navigation: true,
                        collapsible: true
		});
	});

    $(function() {
		$("#programsaccordioninner").accordion({
                        autoHeight: false,
			navigation: true,
                        collapsible: true
		});
	});

    $(function() {
		$("#policiesaccordioninner").accordion({
                        autoHeight: false,
			navigation: true,
                        collapsible: true
		});
	});

    $(function() {
		$("#registrationaccordioninner").accordion({
                        autoHeight: false,
			navigation: true,
                        collapsible: true
		});
	});

    $('.accordion .head').click(function() {
                    $(this).next().toggle();
                    return false;
            }).next().hide();
}

function LoadGalleryView()
{
	$('img.gallery').each(function(){
		$(this).hover(function(){
			$(this).attr('style', 'height: 100%; width: 100%;');
	    }, function(){
	    	$(this).removeAttr('style');
	    });
	});
}

function CheckImage(img,html,source)
{
	if ( img.complete )
	{
		$('#galleryProgress').html('');
		var imgwidth = img.width+35;
		var imgheight = img.height+65;
	    $('<div id="viewImage" title="View"></div>').html(html).dialog(
	            {
	                bgiframe: true,
	                autoOpen: true,
	                modal: true,
	                width: imgwidth,
	                height: imgheight,
	                position: 'center',
	                closeOnEscape: true
	            });
	}
	else
	{
		$('#galleryProgress').html('<img src="images/ajax-loader.gif" /><br /><br />');
		setTimeout(function(){CheckImage(img,html);},50);
	}
}

function PopUpImage(url,source)
{
	var html = '<img src="'+url+'" />';
	var img = new Image();
	img.src = url;
	if ( ! img.complete )
	{
		setTimeout(function(){CheckImage(img,html,source);},10);
	}
}

function getHTTPObject()
{
    if (window.ActiveXObject)
    {
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest)
    {
        return new XMLHttpRequest();
    }
    else
    {
        alert("Your browser does not support AJAX.");
        return null;
    }
}

function getInput(obj)
{
    var params = "";
    for ( var i=0; i < obj.childNodes.length; i++ )
    {
        if ( obj.childNodes[i].tagName == "INPUT" || obj.childNodes[i].tagName == "TEXTAREA")
        {
            params+=obj.childNodes[i].name+"="+obj.childNodes[i].value.replace(/&/g,"!amp;")+"&";
        }
        else if ( obj.childNodes[i].childNodes != null )
        {
            params+=getInput(obj.childNodes[i]);
        }
    }
    return params;
}


function SubmitForm(formName,url)
{
    $('#contentsection').append('<br />Please wait.<br /><img src="images/ajax-loader.gif" /><br /><br />');
    var obj = document.getElementById(formName);
    var params = getInput(obj);
    var httpObject = getHTTPObject();
    httpObject.open("POST", url, true );
    httpObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    httpObject.setRequestHeader("Content-length", params.length);
    httpObject.setRequestHeader("Connection", "close");
    httpObject.send(params);
    httpObject.onreadystatechange = function() {
        if (httpObject.readyState == 4)
        {
            $('#contentsection').html(httpObject.responseText);
        }
    }
}

