// JavaScript Document

$(document).ready(function(){						   
	// Common functions
	clearClick();
	setBrowserClass();
	cssTweaks();
	
	if(!hasFlash){
		$('.no_flash').show();
		$('.has_flash').remove();
		$('body').addClass("noflash");
	}else{
		$('.no_flash').remove();
		$('body').addClass("hasflash");
	}
	
	$('ul#nav>li').each(function() {
		var id = $(this).attr('id');
		$('#'+id+'> a').attr('href', '#');
		$(this).addClass('topNav');
	});
	
	//Dropdown Navs
	$('.topNav').click(function() {
		var browser = getBrowser();
		var id = $(this).attr('id');
		var height = $('#nav #'+ id + '> ul').height();
		var offset = height - 12;
		
		if(browser['name'] == "ie") {
			if($('#nav li ul').is(':visible')) {
				if($('#nav #'+ id + '> ul').is(':visible')) {
					$('#nav #'+ id + '> ul').hide();
					$('#content_top').animate({marginTop: 122}, 1, 'linear');	
				}
				else {
					var diff = 122 + (offset);
					$('#nav li ul').hide()
						$('#content_top').animate({marginTop: diff}, 1, 'linear', function() {$('#nav #'+ id + '> ul').show();});
				}
			}
			else {
				$('#content_top').animate({marginTop: '+='+offset}, 1, 'linear', function() {$('#nav #'+ id + '> ul').show();});	
			}			
		}
		else {
			if($('#nav li ul').is(':visible')) {
				if($('#nav #'+ id + '> ul').is(':visible')) {
					$('#nav #'+ id + '> ul').hide();
					$('#content_top').animate({marginTop: 122}, 240, 'linear');	
				}
				else {
					var diff = 122 + (offset);
					$('#nav li ul').fadeOut('fast', function() {
						$('#content_top').animate({marginTop: diff}, 1, 'linear', function() {$('#nav #'+ id + '> ul').fadeIn('fast');});
					});
				}
			}
			else {
				$('#content_top').animate({marginTop: '+='+offset}, 240, 'linear', function() {$('#nav #'+ id + '> ul').fadeIn('fast');});	
			}
		}
	});
	
	//Help with Cufon'ing top navigation	
	//$('#nav li').wrap('<span class="topnav" />');
	//$('#nav li ul li').remove();		
	
	// PNG support for IE6
	if(isIE(6)){
		$(document).pngFix();
	}
	
	var browser = getBrowser();
	if(browser['name'] != "ie") {
			$('.submit').each(function(){
    		$(this).hide().after('<span class="submit">').next('span.submit').text($(this).val()).click(function(){
    	   	 	$(this).prev('input.submit').click();
	 	  	 });
			});
			Cufon.replace('.submit', { fontFamily: 'GM' });

	}
});

function cssTweaks(){
	$('blockquote').each(function(i,elem){ $(elem).find('p:last').css('margin',0); });
	$('ol li').wrapInner('<span></span>');
}

/*-----------------------------------------------------*/
/*	Gallery Template
/*-----------------------------------------------------*/

function initLightBox(){
	$(function(){
		$("a[rel='lightbox']").lightBox({
			imageLoading: '/images/lightbox-ico-loading.gif',
			imageBtnClose: '/images/lightbox-btn-close.gif',
			imageBtnPrev: '/images/lightbox-btn-prev.gif',
			imageBtnNext: '/images/lightbox-btn-next.gif',
			fixedNavigation: true
		});
	});
}

function gallery_prevPage(){
	var previous = $('.photo_list').attr('current_page');
	current = parseInt(previous) - 1;
	if(current == 1) $('.paging .prev a').hide();
	$('.paging .next a').show();
	$('.photo_list').attr('current_page', current);
	$('.paging .info').fadeOut('fast',function(){
		$('#current_page').text(current);
		$('.paging .info').fadeIn();
	});
	$('#gallery_group'+previous).fadeOut('fast',function(){ $('#gallery_group'+current).fadeIn(); });
}

function gallery_nextPage(){
	var previous = $('.photo_list').attr('current_page');
	current = parseInt(previous) + 1;
	var maxgroups = $('.gallery_group').length;
	if(current == maxgroups) $('.paging .next a').hide();
	$('.paging .prev a').show();
	$('.photo_list').attr('current_page', current);
	$('.paging .info').fadeOut('fast',function(){
		$('#current_page').text(current);
		$('.paging .info').fadeIn();
	});
	$('#gallery_group'+previous).fadeOut('fast',function(){ $('#gallery_group'+current).fadeIn(); });
}
