/**
 * 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 pageViews = parseInt($.cookie('pageViews'), 10);
	if (isNaN(pageViews)) pageViews = 0;
	$.cookie('pageViews', String(++pageViews), {expires:1, path:'/'});
	var clickedClose = $.cookie('clickedClose');
		
	if (!clickedClose && pageViews > 3) {
		var hotnews = '<div id="hotnews"> \
				<table border="0" align="center"> \
					<tr> \
						<td class="signup"> \
							<a href="/newsletter/signup">Sign Up to QMF News</a> \
						</td> \
						<td class="program"> \
							<a href="/files/documents/QMF_Brochure_2009.pdf">Download PDF Program (15MB)</a> \
						</td> \
						<td class="printed"> \
							<a href="/content/about-us/your-feedback-win-an-ipod">Your Feedback - Win an iPod!</a> \
						</td> \
						<td class="close"> \
							<a href="#close"><img src="/img/hotnews_close.gif"></a> \
						</td> \
					</tr> \
				</table> \
			</div>';
		$(hotnews).prependTo('BODY').hide().animate({opacity: 1.0}, 1000).slideDown('slow');
		$('#hotnews').find('.close A').click(function(){
			$.cookie('clickedClose', '1', {expires:1, path:'/'});
			$('#hotnews').slideUp('slow');
			return false;
		});
	}
		
	//
						
});*/

// 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');
});

$(function(){
	$('#highlights a').click(function(){
		$('#highlights .normal').hide();
		$('#highlights .over').show();
		$('#upcomingevents .normal').show();
		$('#upcomingevents .over').hide();
		$('#videopanel').show();
		$('#eventspanel').hide();
	});
	
	$('#upcomingevents a').click(function(){
		$('#upcomingevents .normal').hide();
		$('#upcomingevents .over').show();
		$('#highlights .over').hide();
		$('#highlights .normal').show();
		$('#videopanel').hide();
		$('#eventspanel').show();
	});
});