/**
 * Add default "temporary" text to inputs
 * Courtesy of http://dabrook.org/blog/articles/input-field-hints-plugin-for-jquery/
 */
jQuery.fn.inputFieldText = function(string, hintClass) {
  this.each(function() {
      $(this).addClass(hintClass).filter(function(){
      	return ($(this).val() == '' || $(this).val() == string);
      }).val(string);
      $(this).focus(function(){
        if ($(this).val() == string){
          $(this).removeClass(hintClass).val('');
        }
      });
      $(this).blur(function(){
        if ($(this).val() == ''){
          $(this).addClass(hintClass).val(string);
        }
      });
  });
};


/**
 * Hookup location selector in header
 */
$(function(){
	$('#locationSelector').hover(function(){
		this.className = 'open';
	}, function(){
		this.className = 'closed';
	}).attr('className', 'closed');
});


/**
 * Hookup Search Bar
 */
$(function(){
	$('#SearchKeyword').inputFieldText('KEYWORD', 'hint');
	var $form = $('#searchBar FORM');
	if ($form.hasClass('ajax') && $('#artists-index, #events-index').length > 0) {
		// use ajax
		var timeout;

		$form.change(function(){
			clearTimeout(timeout);
			timeout = setTimeout(submitAjaxForm, 300);
		}).keyup(function(evt){
			$($form).trigger('change');
		}).submit(function(){
			return false;
		});

		// we need this for IE
		var activeSelectItem;
		$form.find('SELECT').click(function(){
			if (activeSelectItem != $(this).val()) {
				$(this).trigger('change');
				activeSelectItem = $(this).val();
			}
		});

		function submitAjaxForm() {

			var data = $form.serializeArray();

			$.getJSON($form.attr('action'), data, function(json) {
				var type = json.type;
				if (type == 'artist') {
					$('#artistGrid').children().hide();
				} else {
					$('#eventsGrid').children().hide();
				}
				$.each(json.ids, function(){
					$('#'+type+'_'+this).show();
				});
				var summary = '';
				if (data[0].value != 'KEYWORD' && data[0].value != '') {
					summary += 'Matching keyword "'+data[0].value+'". ';
				}
				if (json.ids.length == 0) {
					summary += 'No results found.';
				}
				$('#search-summary').text(summary);
				if (summary.length) {
					$('#search-summary').show();
				} else {
					$('#search-summary').hide();
				}
			});

			// data[1] = genre, data[2] = location
			var type = $('#artists-index').length == 0 ? 'Events' : 'Artists';
			var title = (data[1].value ? data[1].value+' ' : '') + type + (data[2].value ? ' in '+data[2].value : '');
			$('#search-title').text(title).show();
		}

	} else {
		// don't use ajax
		$('SELECT', $form).change(function() {
			$form.submit();
		});
	}

});


/**
 * Hookup Artist Grid Rollovers
 */
$(function(){
	$('.artistGrid A').hover(function(){
		$('IMG', this).fadeOut('fast');
	}, function(){
		$('IMG', this).fadeIn('fast');
	}).each(function(){
		var $div = $('.thumb', this);
		$div.css('backgroundImage', 'url('+$div.attr('data')+')');
	});
});

/**
 * Hookup YouTube videos to fancybox popup
 */
$(function(){
	$('BODY').append('<div style="display:none" id="youtube"></div>');
	$('a[href*="youtube.com/"]').click(function(evt) {
		var matches = $(this).attr('href').match(/v=([^&]+)/);
		if (matches) {
			var id = matches[1];
			$('<a href="#youtube" />')
				.fancybox({
					callbackOnShow:function(){
						$('#fancy_div').html('<object width="480" height="397"><param name="movie" value="http://www.youtube-nocookie.com/v/'+id+'&hl=en&fs=1&rel=0&autoplay=1&ap=%2526fmt%3D18"></param><param name="allowFullScreen" value="true"><param name="bgcolor" value="#000000"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube-nocookie.com/v/'+id+'&hl=en&fs=1&rel=0&autoplay=1&ap=%2526fmt%3D18" type="application/x-shockwave-flash" allowscriptaccess="always" bgcolor="#000000" allowfullscreen="true" width="480" height="397"></embed></object>');
					},
					frameWidth:480,
					frameHeight:397,
					overlayShow: true,
					overlayOpacity: 0.8,
					hideOnContentClick:false
				}).trigger('click');
			return false;
		}
	});
});


/**
 * Hookup image enlarge
 */
$(function(){
	$("a[rel^='fancybox']").fancybox({
		overlayShow: true,
		overlayOpacity: 0.8
	});
});


	/**
	* Hookup Hotnews bar that shows at the top of the page. Relies on jquery.cookies.js
	*/
$(function() {
	var hotnews = '<div id="hotnews" style="padding-top: 10px; height: 40px; color: #FFF; text-align: center;"><h2 style="font-size: 16px; line-height: 16px;"><a href="http://www.qmf.org.au" target="_blank">Welcome to the archived 2009 Queensland Music Festival Website<br />Click here to access our current site</a></h2></div>';
	$(hotnews).prependTo('BODY');


});

// we have this out of the function below so we can overwrite in the page if we need to
var currentPage;

/* Menu Rollover Code */
$(function(){

	/* Mouse Interaction Code */
	var active = $('#nav LI.active');
	active.addClass('over');
	$('#nav LI').hover(
		function(){
			active.removeClass('over');
			$(this).addClass('over');
		},
		function(){
			$(this).removeClass('over');
			active.addClass('over');
		}
	);

});


/* open external + pdf links in a new window */
$(function(){
	$('A, AREA').filter(function(){
		return (!this.target && (this.href.indexOf(window.location.hostname) == -1 || this.href.match(/\.pdf$/i)));
	}).attr('target', 'blank');
});

/* prevent flicker on menu images */
try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}


$(function(){
	$('BODY').removeClass('nojs').addClass('js');
});