$(document).ready(function(){

    //    $("button,input:submit,input:button,.btn").button();
    var bIsIECheckDone = false;
    var bIsIE = false;
    function isIE()
    {
        if (!bIsIECheckDone){
            if(navigator.userAgent.match(/MSIE \d\.\d+/))
                bIsIE = true;
            else 
                bIsIE = false;
            bIsIECheckDone = true;
        }
        return bIsIE;
    }
	
    // setup the drop downs
    $('#navbar ul > li').hoverIntent(
        // mouseover so show the drop down
        function()
        {
            // make the nav item have the over state
            $('a span', this).addClass('hovering');
            $('> .sub-menu', this).toggle();
        },
        // mouseout so hide the drop down
        function() 
        {
            // remove the over state on the nav item
            $('a span', this).removeClass('hovering');
            $('> .sub-menu', this).toggle();
        }
        );
	
    // IE fun
    if(isIE())
    {
        $('#navbar li li').hover(
            function()
            {
                $(this).addClass('hover_ie');
            },
            function()
            {
                $(this).removeClass('hover_ie');
            }
            );
    }
	

	
    //from http://richa.avasthi.name/blogs/tepumpkin/2008/01/11/ie7-lessons-learned/
    function zIndexWorkaround()
    {
        // If the browser is IE,
        if(isIE())
        {
            /*
	        ** For each div with class menu (i.e.,
	        ** the thing we want to be on top),
	        */
            $(".sub-menu").parents().each(function() {
                var p = $(this);
                var pos = p.css("position");
	
                // If it's positioned,
                if(pos == "relative" ||
                    pos == "absolute" ||
                    pos == "fixed")
                    {
                    /*
	                ** Add the "on-top" class name when the
	                ** mouse is hovering over it, and remove
	                ** it when the mouse leaves.
	                */
                    p.hover(function() {
                        $(this).addClass("on-top");
                    },
                    function() {
                        $(this).removeClass("on-top");
                    });
                }
            });
        }
    }
	
    zIndexWorkaround();

    //Execute the slideShow
    slideShow();

    var oTabs = $("#tabs");
    if(oTabs.length>0){
        oTabs.tabs({
            cache: true,
            ajaxOptions: {
                cache: false
            }
        }).addClass('ui-tabs-vertical ui-helper-clearfix').removeClass('ui-corner-all');
        $("#tabs li").removeClass('ui-corner-top');
        $(".ui-tabs-panel").removeClass('ui-corner-bottom');
    }
    $('a.fancybox').fancybox({
        'hideOnContentClick'	:	true
    });
	
})

function slideShow() {
    //Set the opacity of all images to 0
    var oGallery = $('#gallery');
    if (oGallery.length > 0){
        $('#gallery li').css({
            opacity: 0.0
        });

        //Get the first image and display it (set it to full opacity)
        $('#gallery li:first').css({
            opacity: 1.0
        });

        //Set the caption background to semi-transparent
        $('#gallery .caption').css({
            opacity: 0.7
        });

        //Resize the width of the caption according to the image width
        $('#gallery .caption').css({
            width: $('#gallery li').find('img').css('width')
        });

        //Get the caption of the first image from REL attribute and display it
        $('#imageCaption').html($('#gallery li:first').find('img').attr('alt'));

        //Call the gallery function to run the slideshow, 6000 = change to next image after 6 seconds
        setInterval('gallery()',4000);
    }
}
var caption = "";
var current = null;
var next = null;

function gallery() {
    //if no IMGs have the show class, grab the first image
    current = ($('#gallery li.show')?  $('#gallery li.show') : $('#gallery li:first'));

    //Get next image, if it reached the end of the slideshow, rotate it back to the first image
    next = ((current.next().length) ? ((current.next().hasClass('caption'))? $('#gallery li:first') :current.next()) : $('#gallery li:first'));

    //Get next image caption
    caption = next.find('img').attr('alt');

    //Set the fade in effect for the next image, show class has higher z-index
    next.css({
        opacity: 0.0
    })
    .addClass('show')
    .animate({
        opacity: 1.0
    }, 1000);

    //Hide the current image
    current.animate({
        opacity: 0.0
    }, 1000)
    .removeClass('show');

    //Display the content
    $('#imageCaption').html(caption);
}


function popUP(mypage, myname, w, h, scroll, titlebar)
{

	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) {
		win.window.focus();
	}
}
