// HIL Theme v9.3
// ------------------------------------------------------------------

pageProtocol = window.location.protocol;
pageHost = window.location.host;
domain = pageProtocol + '//' + pageHost;
URL = window.location.href;
URI = URL.replace(domain,'');
hashBang = URI.substr(0,4);
//alert(domain + URI + '\n' + domain + '/#!/' + URI + '\nhashBang=' + hashBang);

if ( hashBang != '/#!/' ) {
	//alert('Change URL');
	URL = domain + '/#!' + URI;
	window.location.replace(URL);
}
//_________ /replace unhashed URL _________\\

$(document).ready(function() {
	$('#search_form').submit(function() {
		window.location.href = '/#!/as_search?q=' + $('#search_input').val();
		$('#search_input').val('SEARCH');
		//alert('Handler for .submit() called.');
		return false;
	});
	$('#search_form input.search-box').focus(function () {
		$(this).css('color','#000');
	});
	
	$('.op-channel-name').hover(function() {
		if($('a.artist-omni-trigger').width() > 200) {
			$('.channel-name-wrapper').animate({
				right: 100,
				duration: 100
			});
		}
		}, function(){
			$('.channel-name-wrapper').animate({
				right: 0,
				duration: 100
			});
	});

	// END SCROLLERS
	
	//$('ul.sub-nav a').attr('href', function(i, a){ 
	//	alert(a);
	//	var cleanA = a.replace('/#!','');
	//	alert(cleanA);
	//	return domain+'/#!'+ cleanA.replace(domain,'');
	
	//}); //makes sub-nav hasher-ized
	
	//_________ /hasher url maker _________\
	// $('#container').delegate('.cal-cover,.logged-out-thumb-cover,.loggedout-cover-image', 'click', function(){
	// 	if($(this).next().hasClass('popup')){
	// 		$(this).next().slideUp(function(){
	// 			$(this).remove();
	// 		});
	// 	} else {
	// 		var popup = $('<div>').addClass('popup').hide().html("You need a Hear It Local to use this feature. <a href='/#!/as_account-manager'>Click here to create one now.</a>");
	// 		$(this).after(popup.slideDown(300, function(){
	// 			popup.show(100);
	// 		}));
	// 	}
	// });


	$('a.as_status-close').click(function(){
		$(this).parents('#statusMessage').hide(400);
		return false;
	});
//_________ /status msg closer _________\\
	//$("#main_nav li").hoverIntent(config);
//________ end sub nav hover _________\\
	$('.trans').css('opacity', 0.9);
	$('.trans-95').css('opacity', 0.95);
	$('.trans-60').css('opacity', 0.60);
//_________ /opacity _________\\
	var currentLink = window.location.hash.replace('#!/as_','');//get the page handle name after as_
	var navItem = $('#main_nav > li > a');
	navItem.each(function(){ // get last class name from each top level nav item
		var navClass = $(this).attr('class').split(' ').slice(-1); //last class
		if(navClass == currentLink){ //check to see if nav item class is current page class
			$('#main_nav a').removeClass('on').addClass('off');
			$('#main_nav a.'+navClass).removeClass('off').addClass('on');
		}
	});
//_________ nav state _________\\
	$('#main_nav a').click(function(){
		$('#main_nav a').addClass('off');
		$('#main_nav a').removeClass('on');
		$(this).removeClass('off');
		$(this).addClass('on');
		$('#masthead > ul.sub-nav').remove();
		var activeSub = $('#main_nav > li > a.on').next('ul.sub-nav'); //get the sub-nav of the currently active nav
		activeSub.clone().appendTo('#masthead'); //append that sub-nav to the end of #masthead
	});
//_________ /nav on class _________\\
	var activeSub = $('#main_nav > li > a.on').next('ul.sub-nav'); //get the sub-nav of the currently active nav
	activeSub.clone().appendTo('#masthead'); //append that sub-nav to the end of #masthead
//_________ sub nav mover _________\\
	$('#omniplayer').hover(function(){
		$(this).find('.fade').fadeTo("fast", 1.0);
	},
	function(){
		$(this).find('.fade').fadeTo("fast", 0.0);
	});
//_________ /omni hover _________\\

});
//_________ /doc ready _________\\
var config = {
	sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)
	interval: 30, // number = milliseconds for onMouseOver polling interval
	over: hoverOn, // function = onMouseOver callback (REQUIRED)
	timeout: 250, // number = milliseconds delay before onMouseOut
	out: hoverOff // function = onMouseOut callback (REQUIRED)
};
function hoverOn(){
	$(this).find('ul').slideDown(300);
}
function hoverOff(){
	$(this).find('ul').slideUp(300);
}


function openWindow(url) {
	// Change "_blank" to something like "newWindow" to load all links in the same new window
	var newWindow = window.open(url, '_blank');
	newWindow.focus();
	return false;
}

$('a.share.facebook').live('click', function(e) {
	e.preventDefault();
	var title = $(this).siblings('span.name-share').html();
	var url = $(this).siblings('span.url-share').html();
	var id = $(this).attr('href');
	var link = "http://www.facebook.com/sharer.php?t=" + title + "&u=" + url;
	openWindow(link);
});

$('a.share.twitter').live('click', function(e) {
	e.preventDefault();
	var title = $(this).siblings('span.name-share').html();
	var url = $(this).siblings('span.url-share').html();
	var id = $(this).attr('href');
	var perfDate = $(this).siblings('span.date-share').html();
	var perfLocation = $(this).siblings('span.location-share').html();
	var perfStr = '';
	perfStr += ( !(perfLocation=='' || perfLocation==null || typeof perfLocation == 'undefined') ) ? ' at '+perfLocation : ''; 
	perfStr += ( !(perfDate=='' || perfDate==null || typeof perfDate == 'undefined') ) ? ' on '+perfDate : '';
	var message = escape('Check out ' + title + perfStr + ': ') + url;
	var link = 'http://twitter.com/intent/tweet?text=' + message;
	openWindow(link);
});

$('a.share.email').live('click', function(e) {
	e.preventDefault();
	var title = $(this).siblings('span.name-share').html();
	var url = $(this).siblings('span.url-share').html();
	var id = $(this).attr('href');
	var message = escape("Check out " + title + " at ") + url;
	var link = 'mailto:?subject='+title+'&body='+message;
	window.open(link);
});

$('a.share.permalink').live('click', function(e) {
	e.preventDefault();
	var title = $(this).siblings('span.name-share').html();
	var url = $(this).siblings('span.url-share').html();
	var id = $(this).attr('href');
	message = escape("Find out more about " + title + " at " + url);
});

//_________ /out of site interaction _________\\
$('a.modal-trigger').live('click',function(){
	$('div.modal').modal({
		opacity:80,
		overlayCss: {backgroundColor:'#000'},
		overlayClose:true,
		closeClass: 'modal-close',
		minHeight:350,
		minWidth: 500,
		onOpen: function (dialog) {
			dialog.overlay.fadeIn('fast', function () {
				dialog.data.hide();
				dialog.container.fadeIn('fast', function () {
					dialog.data.slideDown('fast');
				});
			});
		},
		onClose: function (dialog) {
			dialog.data.fadeOut('fast', function () {
				dialog.container.hide('fast', function () {
					dialog.overlay.hide('fast', function () {
						$.modal.close();
					});
				});
			});
		}
	});
	return false;
});
	
	

//_________ /modal _________\\
var geocoder =  geocoder || new google.maps.Geocoder();
$('a.location-link').live('click',function(){
	var venueID =  $(this).attr('class').match(/venue-([^\s]+)/)[1];
	$('div.modal').append($('<div>').attr('id','google_map').css('height','350px').text('Loading map...'));
	$.ajax({
		url : 'http://hearitlocal.com/api/venues&id=' + venueID,
		dataType : 'jsonp',
		success : function(response){
			var result = response.rows[0];
			//console.log("lat=",result.lat,": lon=",result.lon,": city=",result.city,": address1=",result.address1,": zip=",result.zipCode);
			function build_map(myLatlng){
				var myOptions = {
				  zoom: 14,
				  center: myLatlng,
				  mapTypeId: google.maps.MapTypeId.ROADMAP
				};
				var map = new google.maps.Map(document.getElementById("google_map"), myOptions);
				var marker = new google.maps.Marker({
		            map: map,
		            position: myLatlng
		        });
			}
			if(result.lat && result.lon){
				build_map(new google.maps.LatLng(parseFloat(result.lat), parseFloat(result.lon)));
			} else {
				var address = result.address1 + ',' + result.city + ',' + result.state + ',' + result.zipCode;
				var geocoder= new google.maps.Geocoder();
				geocoder.geocode( { 'address': address}, function(results, status) {
				      if (status == google.maps.GeocoderStatus.OK) {
				        build_map(results[0].geometry.location);
				      } else {	
						var link = 'http://maps.google.com/maps?q=' + encodeURIComponent(address);
 						$('#google_map').text("I'm sorry, we couldn't find "+result.name+" on <a href='"+link+"' target='_blank'>google maps</a>");
				      }
				  });
			}
		}
	});
});
//_________ /Google map _________\\
function destroyCyclePlugin() {
	$('#shows').cycle('destroy');
	$('#artists').cycle('destroy');
	$('#superfans').cycle('destroy');
	// $('#shows').unbind('.cycle');
	// $('#artists').unbind('.cycle');
	// $('#superfans').unbind('.cycle');
	//console.log('cycle destroyed');
	return false;
}
//_________ /Remove previous instances of the cycle plugin _________\\
