$(document).ready(function(){
    // hover function for inputs and images

        $('.hover').css("cursor", "pointer").hover(function(){
    		$(this).attr('src', $(this).attr('src').replace(/\.(\w{3})/g, "Hover.$1"));
    	},function() {
    		$(this).attr('src', $(this).attr('src').replace(/Hover\.(\w{3})/g, ".$1"));
    	});
// top nav 
        $('#headerInner table').each(function() {
            if ($(this).children('tbody').children('tr').children('td').length > 1) {
                $(this).children('tbody').children('tr').children('td:first').children('ul').addClass('dots');
    	    }
        });

	    
	    $('#headerInner div').addClass('jsNav').hide();
        $('#headerInner ul li').hover(function() {
           $(this).children('div').show();
        }, function () {   
            $(this).children('div').hide();
        });

//side nav
        $('#nav li.sub ul').hide();
        
        $('#nav .expander').toggle(function() {
            $(this).attr('src', '/assets/images/core/minusHover.gif').next().slideDown('fast');
        }, function () {
            $(this).attr('src', '/assets/images/core/plusHover.gif').next().slideUp('fast');
        });

// news archive


    if($('#nNav').length > 0) {
        
        function onOrOff(theObj) {
            it = new Object();
            if($(theObj).next().hasClass('open')) {
                it.span =  "<span>- </span>";
                it.isOpen = "1";
            } else {
                it.span = "<span>+ </span>";
                it.isOpen = "0";
            }
             return it;
        }
        
        $('#nNav ul li.on').children('ul').addClass('open');
        
        $('#nNav ul').not(':first').not('.open').hide();
        
        $('#nNav a').not('#nNav ul ul ul a').each(function() {
            $(this).prepend(onOrOff($(this)).span).attr('childOpen', onOrOff($(this)).isOpen).click(function() {
                if($(this).attr('childOpen') == "0") {
                    $(this).attr('childOpen', '1').children('span').html("- ").end().next().slideDown('fast');
                } else {
                    $(this).attr('childOpen', '0').children('span').html("+ ").end().next().slideUp('fast');
                }
                
                return false;
            });
        })
    }
// diary     
   if($('#diary').length > 0) {
       introStatement = "<div><h3>Welcome to the Diary</h3><p>Please select a day on the left</p>";
       eventDays = new Array();
       
       $('#diary td div.event').addClass('active').hover(function() { // gives them a pointer...
           $(this).addClass('hover'); // adds the hover state css
       }, function() {
           $(this).removeClass('hover'); // and removes it...
       }).each(function(i) {
           eventDays[i] = $(this).html(); // makes an array out of the event days contents
           $(this).attr('day', $(this).html()); // assigns each day its content as the 'day' attribute
       }).click(function() {
          theDay = $(this).attr('day'); // gets its 'day' attribute
          $('#diary div.event').removeClass('selected'); //removes 'selected' from all divs
          $(this).addClass('selected'); // puts it back on this just clicked one
          $('#diaryInfo div').hide(); // hides all info divs
          $('#diaryInfo div[day='+theDay+']').show(); // grabs the div with the same day attribute and shows it 
       }); 
       
       if(eventDays.length == 0) {
           introStatement = '<div><h3>Welcome to the Diary</h3><p>There are no events currently written in for this month. <a href="/contact">Contact us</a> if you\'d like to change that!</p>';
           
       } else {
           introStatement = "<div><h3>Welcome to the Diary</h3><p>Please select a day on the left</p>";
       }
       
       $('#diaryInfo').prepend(introStatement).children('div').not(':first').each(function(i) {
          $(this).attr('day', eventDays[i]); //assigns the 'day' attribute in order to the info divs          
       }).hide(); //hides all
   }


// sortTable
    if($('#sortTable').length > 0 ) {
        function odd_rows() {
            $('#sortTable tr').removeClass('dark').filter(':even').addClass('dark');
        }
        
        odd_rows();
        $('#sortTable tbody tr').css('cursor', 'pointer').hover(function() {
            $(this).addClass('hover');
        }, function() { 
            $(this).removeClass('hover');
        }).click(function() {
            theUrl = $(this).children(':last').children().children('a').attr('href');
            window.location = theUrl;
        });
        
        $("#sortTable").tablesorter({
            sortList: [[0,0]],
            headers: { 
                        // assign the secound column (we start counting zero) 
                        2: { 
                            // disable it by setting the property sorter to false 
                            sorter: false 
                        }
                    } 
        }).bind("sortEnd", function() {
            odd_rows();
        });
        
        $('#sortTable th').not(':last').css('cursor', 'pointer').hover(function() {
            $(this).addClass('hover');
        }, function () {
            $(this).removeClass('hover');
        });
    }

//lightbox calls

    if($('.lightBoxTable').length > 0 ) {
        $('.lightBoxTable a ').lightBox();
    }

	//--------------------------------------
	//  FlASH METHODS
	//--------------------------------------
	
	$.flashExpressInstaller = '/assets/swf/expressInstall.swf';
	
	$('#flashPhoto').each(function() { $(this).html(getFlashPhoto($('#flashPhoto'))) });
	
	function getFlashPhoto($html) {
	    return $.flash({
	         swf: '/assets/swf/gallery.swf',
	         width: 600,
	         height: 404,
	         params: {
	         	 bgcolor: "#ffffff",
	             scale: "noscale",
	             menu: "false",
	             flashvars: {   
	                 galleryListSrc: $html.attr('galleryListSrc'),
	                 gallerySrc: $html.attr('gallerySrc')
	             }
	         }   
	    });
	}

	$('#flashVideo').each(function() { $(this).html(getFlashVideo($('#flashVideo'))) });
	
	function getFlashVideo($html) {
	    return $.flash({
	         swf: '/assets/swf/video.swf',
	         width: 600,
	         height: 404,
	         params: {
	         	 bgcolor: "#ffffff",
	             scale: "noscale",
	             menu: "false",
	             flashvars: {   
	                 videoXMLSrc: $html.attr('videoXMLSrc')
	             }
	         }   
	    });
	}
	

});