	function isset(variable)  {
		try {
			if(typeof(eval(variable))!='undefined')
			if(eval(variable)!=null) return true;
		} catch(e) {}
		return false;
	}
	
	function trace(msg) {
		try {window.console.log(msg)} catch (err) {}
	}

$(document).ready(function(){

	// initiate dynamItScript for dialogs
	dynamItScript({
		xout: {
			image: 'img/admin/xout.gif',
			width: 17,
			height: 18,
			right: 10,
			top: 4
		},
		dialogTop: '<div class="formcontainer"><div class="formouter"><div class="formcap"></div> <div class="formbody">',
		dialogBottom: '</div><div class="formcup"></div></div></div>',
		dialogWidth: 486
	});

	// Fix IE 6
	if( IE6 ) {
		// cached background images
		document.execCommand("BackgroundImageCache",false,true);

		// PNGs
		$("img[src$='.png']").each(function() {
			var src = '/' + $(this).attr('src');
			$(this).attr("src", "http://www.dynamit.us/img/spacer.gif").attr("style", "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "');");
		});
	}
	
	
	/* Sticky Footer Fix for Extremely Tall Screens */
	$("#content").attr("origheight", $("#content").height())
	fixSticky();
	$(window).resize(function(){
		fixSticky();
	});

	/* MAIN NAV */
	$("ul#nav li").hover(
	  function () {
	    $(this).addClass('hover');
	  }, 
	  function () {
	    $(this).removeClass('hover');
	  }
	);
	
	
	$('ul.tab>li').live('click', function() {
		var index = $(this).parent().children('li').index(this);
		$content = $(this).parent().next('ul.tab-content');
		$content.children('li').hide();
		$content.children('li').eq(index).show();
		$(this).parent().children('li').removeClass('on').children('a').removeClass('on');
		$(this).addClass('on').children('a').addClass('on');
		return false;
	});
	
	$('ul.voice-tab>li').live('click', function() {
		var index = $(this).parent().children('li').index(this);
		$content = $(this).parent().next('ul.voice-tab-content');
		$content.children('li').hide();
		$content.children('li').eq(index).show();
		$(this).parent().children('li').removeClass('on').children('a').removeClass('on');
		$(this).addClass('on').children('a').addClass('on');
		return false;
	});

	
	//
	// attach the date picker
	//
	$('.dateselect').datepicker();
	$('.daterangeselect').datepicker({rangeSelect: true});
	$('.dateselectfuture').datepicker({minDate: 0});
	
	
	// cal description bubble on hover
	$('div.calendars > ul > li > a').hover(
		function() {
			$(this).eq(0).parent('li').children('span.caldesc').show();
		},
		function() {
			$(this).eq(0).parent('li').children('span.caldesc').hide();
		}
	);
	
	
	// homepage carousel
	$('#home-carousel').each(function() {

		$(this).data('current',1);
		$(this).data('total',$(this).find('#home-carousel-img ul li').length);
		
		$(this).find('#home-carousel-img ul li').css({top:'-266px',zIndex:1});
		$(this).find('#home-carousel-img ul li:first').css({top:0,zIndex:10});
		
		$(this).find('#carousel-next').click(function() {
			
			var parent=$(this).parents('#home-carousel');
			var current=$(parent).data('current');
			var total=$(parent).data('total');
			var slides=$(parent).find('#home-carousel-img ul li');
			
			$(slides).css({top:'-266px',zIndex:1});
			$(slides[current-1]).css({top:0,zIndex:10});
			
			$(slides[current-1]).stop().animate({top:$(slides[0]).height()+'px'},1000,'easeOutExpo');
			
			current--;
			if(current<=0) current=total;
			
			$(slides[current-1]).stop().css({top:-$(slides[0]).height()+'px',zIndex:20}).animate({top:0},500,'easeOutExpo');
			
			$(parent).data('current',current);
			
			return false;
		
		});
		
		$(this).find('a#carousel-prev').click(function() {
		
			var parent=$(this).parents('#home-carousel');
			var current=$(parent).data('current');
			var total=$(parent).data('total');
			var slides=$(parent).find('#home-carousel-img ul li');
			
			$(slides).css({top:'-266px',zIndex:1});
			$(slides[current-1]).css({top:0,zIndex:10});
			
			$(slides[current-1]).stop().animate({top:-$(slides[0]).height()+'px'},1000,'easeOutExpo');
			
			current++;
			if(current>total) current=1;
			
			$(slides[current-1]).stop().css({top:$(slides[0]).height()+'px',zIndex:20}).animate({top:0},500,'easeOutExpo');
			
			$(parent).data('current',current);
			
			return false;
		
		});
	
	});
	
	
	// toggle search box
	$('#searchtoggle').mouseover(function() {
		// clearTimeout( $(this).data('timeout') );
		$('#searchcontainer').animate({ top: '0' }, 700);
		
	});/*, function() {
		var t = setTimeout(function() {
			$('#searchcontainer').animate({ top: '-43px' }, 1000);
		}, 3000);
		$(this).data('timeout', t);
	});*/
	
	
	/**
	 * SEARCH AUTOCOMPLETE
	 *
	 * Autocomplete search results.
	 */
	$('#search').autocomplete({
		minLength: 2,
		delay: 400,
		search: function(event,ui) {
				
			//
			// Set Variables
			var search = $('#resultscontainer');
   			var autocomplete = $('#resultscontainer');
   			var keywords = $(this).val();
   				
   			//
   			// Setup Autocomplete List
   			$(autocomplete).show();
   			$('body').bind('click',close_search_autocomplete);
	   			
	   		//
	   		// Stop Previous Ajax Requests
	   		if($(search).data('ajax')!=undefined) $(search).data('ajax').abort();
	   			
	   		//
	   		// Track Search
			//ga_track_event('Search','Live',keywords);
				
	   		//
	   		// Retrieve List
		   	$(search).data('ajax',$.ajax({
				type: "POST",
				url: "/search/",
				data: 'q='+keywords,
				dataType: "html",
				success: function(html){
					$(autocomplete).html(html);
					$(search).removeData('ajax');
				},
				error: function(msg1,msg2,msg3) {
					$(search).removeData('ajax');
				}
		   	}));
			
			return false;
				
		}
	}).keyup(function() {
		var search = $('#resultscontainer');
		if($(this).val().length<3&&$(search).data('ajax')!=undefined) {
			$(search).data('ajax').abort();
		}
	});
	
	/**
	* CLOSE SEARCH AUTOCOMPLETE
	*
	* Close search autocomplete dropdown.
	*/
	function close_search_autocomplete() {
		$('#resultscontainer').hide();
	}
	
	$('#resultscontainer a.searchsubmit').live('click', function(e) {
		e.preventDefault();
		$('#searchcontainer').submit();
	});
		
});

function fixSticky() {
	var windowh = $(window).height();
	var mainh = $("#content").height();
	var origh = $("#content").attr("origheight");
	if(mainh < (windowh - 482)) {
		$("#content").css('height', (windowh - 482));
	} else if(mainh > origh) {
		$("#content").css('height', (windowh - 482));
	}
}
	
function show_overlay() {
	var hide_select = $('<iframe id="popup_overlay_hide_select" class="popup_overlay_hide_select"></iframe>');
	var overlay = $('<div id="popup_overlay" class="popup_overlay"></div>');
	$("body").prepend(hide_select).append(overlay);
}

function hide_overlay() {
	$("iframe.popup_overlay_hide_select").remove();
	$("div.popup_overlay").remove();
}

function open_popup(popup_id) {
	show_overlay();
	var popup = $(popup_id);
	var width = ($(window).width() - parseFloat($(popup).css("width")))/2 + $(window).scrollLeft();
	var height = ($(window).height() - parseFloat($(popup).css("height")))/2 + $(window).scrollTop();
	if(width < 0) { width = 0; }
	if(height < 0) { height = 0; }
	$(popup).css("top",height+"px").css("left",width+"px").css("display","block");
	return false;
}

function close_popup(popup_id) {
	$(popup_id).css("display","none");
	hide_overlay();
	return false;
}

