$(document).ready(function(){
	//preload images
	var slides = base_url + 'img/slides/'
	var borders = base_url + 'img/borders/'
	var images = [
		borders + 'topleft.png',
		borders + 'top.png',
		borders + 'topright.png',
		borders + 'left.png',
		borders + 'right.png',
		borders + 'bottomleft.png',
		borders + 'bottom.png',
		borders + 'bottomright.png',	
	
		slides + 'slideshow1.png', 
		slides + 'slideshow2.png',
		
		slides + 'omoss1.png',		
		slides + 'omoss2.png',
		slides + 'omoss3.png',
				
		base_url + 'img/gb_right.png',
		base_url + 'img/bg9.png',
		base_url + 'img/top4.png'		
		];

	$(images).each(function(key, value) {
		var img = new Image();
		$(img).attr('src', value).error(function (){
			alert('Error preloading image ' + value);
		})
	});
	
	slideShow(5000);
	
	$.ajaxSetup({ cache:false	});	
	$('.r_corner').corner("tl br 30px");
	$('.thumbs li').corner("tl br 20px");	
			
	$("a[rel='ajax_box']").colorbox({width:"75%", height:"75%"});
	$("a[class='html_box']").colorbox();
	$("#show_gb_form").colorbox({inline:true, href:"#gb_form_div"});
	
	//do fun stuff on hover
	$('#menu li a').hover(function(){
		$(this).corner("tl br 15px");
		$(this).stop().animate({ backgroundColor: "#f9e3e7"}, 1000);  
      },function() {  
      $(this).stop().animate({ backgroundColor: "#ffffff" }, 1500);  
	});
	
	hover_etc();				
	
	//get subcategory 
	$(".cat").click(function(){
		var udiv = '#' + $('.subpage').attr('id');								
		$.post($(this).attr('href'), function(data) {  			   			 			
  			$(udiv).html(data);  			
  			if(data.search("ajax_box") != "-1"){
				//alert('ajax_box');
  				$("a[rel='ajax_box']").colorbox({width:"75%", height:"75%"});  				  				
  			}
			hover_etc();				
						
		});
		return false;
	});
	
	//on submitting a form...
	$("form").submit(function(){
		var form_data = $(this).serialize();
		var url = $(this).attr('action');
		var form_id = '#' + $(this).attr('id'); 		
		$.ajax({
   		type: "POST",
   		url: url,
   		data: form_data,
   		success: function(msg){   			
   			if(form_id == "#gb_form"){						   								
					$('#gb_posts').html(msg);	
					$.colorbox.close()								  				
  				}
  				else{
     				$('#message_status').html(msg);
  					$('#message_status').fadeIn('fast');
  				}
  				$(form_id)[0].reset();  				  				  				
   		}
 		});			   			 			  			  			  			
		return false;		
	});
			
});

function hover_etc(){
		$('.thumbs li').corner("tl br 20px");
		
		$('.thumbs li').mouseover(function(){
			$(this).css({opacity: 0.5});		  
		});
		$('.thumbs li').mouseout(function(){
			$(this).css({opacity: 1.0});		  
		});
		$('.tiplink').corner("tl br 20px");	
		
		$('.r_corner').corner("tl br 30px");		
}

function slideShow(speed) {
	
	//append a LI item to the UL list for displaying caption
	$('ul.slideshow').append('<li id="slideshow-caption" class="caption"><div class="slideshow-caption-container"><h3 id="testH3"></h3><p></p></div></li>');

	//Set the opacity of all images to 0
	$('ul.slideshow li').css({opacity: 0.0});
	
	//Get the first image and display it (set it to full opacity)
	$('ul.slideshow li:first').css({opacity: 1.0});
	
	//Get the caption of the first image from REL attribute and display it
	$('#slideshow-caption h3').html($('ul.slideshow li:first').find('img').attr('title'));
		
	//Display the caption
	$('#slideshow-caption').css({opacity: 0.5, bottom:0});
	
	//Call the gallery function to run the slideshow	
	var timer = setInterval('gallery()',speed);
}

function gallery() {

	//if no IMGs have the show class, grab the first image
	var current = ($('ul.slideshow li.show')?  $('ul.slideshow li.show') : $('#ul.slideshow li:first'));

	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().attr('id') == 'slideshow-caption')? $('ul.slideshow li:first') :current.next()) : $('ul.slideshow li:first'));
		
	//Get next image caption
	var title = next.find('img').attr('title');		

	//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}, 1500);
		
	//Hide the caption first, and then set and display the caption
	$('#slideshow-caption').animate({bottom:-70}, 300, function () {	
		//Display the content
		
		//arghh, fix broken browser
		if($.browser.msie){
			document.getElementById('slideshow-caption').innerHTML = '<h3 style="margin-top:5px;">&nbsp&nbsp' + title + '</h3>';	
		}
		else{
			$('#slideshow-caption h3').html(title);					
		}										
		$('#slideshow-caption').animate({bottom:0}, 500);				
	});		

	//Hide the current image
	current.animate({opacity: 0.0}, 1000).removeClass('show');
	
}



