/* site specific jquery */
$(function()
{
	/* add the carousel controls container */
	var controls = $('<ul></ul>')
		.addClass('controls')
		.insertAfter($('#carousel ul.images'));
	
	/* set up the jQuery cycle plugin */
	$('#carousel ul.images')
		.cycle({
			fx: 'fade',
			pause: 0,
			speed: 600,
			fx: 'fade',
			timeout: 5000,
			pager: '#carousel ul.controls',
			pagerAnchorBuilder: function(idx, slide)
			{
				return '<li><a href="#">'+ (idx + 1) +'</a></li>'; 
			},
			activePagerClass: 'active'
		});
		
	/* apply jquery.uniform to all form fields */
	$("select").uniform();
	
	/* prevents ie's unique rendering mood swings with lists & display:block! */
	if(jQuery.browser.msie)
	{
		$('#left .sub-nav *').css({zoom: 1});
	}
	
	var _this;
	var calendarText;
	var s;
	
	/* add a span to each anchor */
	$('.calendar table').find('td a').each(function()
	{
		_this = $(this);
		
		calendarText = _this.text();
		
		_this
			.text('');
		
		s = $('<span />')
			.appendTo(_this);
			
		s.text(calendarText);
	});
	
	/* custom dropdown modifications */
	var selector = $('div.selector');
	
	var o = selector
		.find('select')
		.css({
			 width: '100%',
			 /* required to allow even the text to be clicked */
			 position: 'relative'
		});
	
	/* set the selector height and give it relative position */
	selector
		.height(o.height() + 'px')
		.css({position: 'relative'});
	
	/* position the span */
	selector
		.find('span')
		.css({
			position: 'absolute',
			top: 0,
			left: 0,
			height: selector.height() + 'px',
			lineHeight: selector.height() + 'px',
			paddingLeft: '8px'
		});	
		
	/* reversed z-index throughout the document - fixes some of the usual stacking issues in <=ie7 */
      var run_z_index = true;
      $("div[id=dm_tool_bar]").each(function() {
       run_z_index = false;
      });

      
      if(run_z_index) {
        var zIndexNumber = 9000;
        $('div').each(function() {
          $(this).css('zIndex', zIndexNumber);
          zIndexNumber -= 10;
        });
      }
    });

/* enable sIFR */
var font = { src: '/sifr.swf' };
sIFR.activate(font);
sIFR.replace(font,
{
	selector: 'h1.sIFR',
	css: ['.sIFR-root { text-transform: uppercase; color: #555555; leading: -7 }', 
		  '.sIFR em { background-color: transparent; color: #007fc5; font-style: normal; }'],
	repaintOnResize: false,
	wmode: 'transparent'
});

function add_AddThis()
{
	// ie6 see's a syntax error in the external script, so we'll exclude it from ie6
	if(!($.browser.msie && parseInt(jQuery.browser.version) <= 6))
	{
		$.getScript('http://s7.addthis.com/js/250/addthis_widget.js#username=xa-4be82fce56f90321&domready=1');		   
	}
}

$(function()
{
	setTimeout("add_AddThis()", 50);
});

$(function() 
{
	/* iterate through each select and do some magic to allow the options to overflow (particularly IE related) */
	$('div.selector').each(function() 
	{
		var _this = $(this);
		
		$('span', this)
			.css({
				/* div.selector width - padding - dropdown arrow width */
				width: _this.width() - parseInt($(this).css('padding-left')) - 35 + 'px',
				overflow: 'hidden'
			});
		
		
		/* force the select options to 'overflow' in IE :( */
		if($.browser.msie)
		{
			/* grab the select element that belongs to div.selector */
			var select = $('select', this);
			
			/* create a temp clone and apply css to render off page */
			var temp = select.clone().css({width: 'auto', position: 'absolute', top: '-1000px', left: 0});
			
			/* append to the body */
			$('body').append(temp);
			
			/* get the width of the select element & apply it to the original select element */
			if(temp.width() >= select.width())
			{
				select.width(temp.width());
			}
			
			/* remove the temporary cloned element */
			temp.remove();
		}
	});
});

$(function()
{
	// render the buy now's as images
	if($.browser.msie && parseInt(jQuery.browser.version) <= 6)
	{
		$('#content p.btn-1 a').each(function()
		{
			var image = $(this).css('background-image');
			$(this).css({backgroundImage: 'none'});
			$(this).css({textIndent: 0});
			image = image.replace(/url\(\"/g, '');
			image = image.replace(/\"\)/, '');
			
			$(this).html('');
			$(this).append($('<img src="' + image + '" />'));
		});
	}
});

/* search refine form */
$(document).ready(function()
{
	/* only applies to gecko/webkit/ie8+.... */
	if(!($.browser.msie && parseInt(jQuery.browser.version) < 8))
	{
		var isOpen = true;

		/* requires 'submitted' class to be appended to #search-animate */
		if($('.form #search-animate').hasClass('submitted'))
		{
			isOpen = false;

			/* if isOpen == true, hide the form by default */
			$('.form #search-animate').hide();
			$('.overview .search.refine .form h2 span').addClass('down');
		}

		

		$(function()
		{
			var isSubmitted;

			var ele = $('.form #search-animate');

			$('form', ele).submit(function(e)
			{
				e.preventDefault();

				$(this).slideUp(500, function()
				{
					$('.overview .search.refine .form h2 span').addClass('down');

					$('form', ele).unbind('submit').submit();
				});
			});

			ele.parent()
			.find('h2')
			.css({cursor: 'pointer'})
			.click(function()
			{
				if(!isOpen)
				{
					ele.slideDown(500, function() {
						isOpen = true;
						$('.overview .search.refine .form h2 span').removeClass('down');
					});
				}
				else
				{
					ele.slideUp(500, function() {
						isOpen = false;
						$('.overview .search.refine .form h2 span').addClass('down');
					});
				}
			});
		});
	}

	$('form')
		.submit(function()
		{
			/* if submitted with 'Keywords' still in field, clear it */
				if($('#event_search_form_keywords').val() == 'Keywords')
					$('#event_search_form_keywords').val('');
		});

	/* render some text in empty search fields */
	$('#event_search_form_keywords').each(function()
	{
		var _this = $(this);
		var fText = 'Keywords';
		
		if(_this.val() == '')
			_this.val(fText);

		_this
			.focus(function()
			{
				if(_this.val() == fText)
					_this.val('');
			})
			.blur(function() {
				if(_this.val() == '')
					_this.val(fText);
			});
	});
});