// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function openDialog()
{
  var w = jQuery(window).width() - 350;
  jQuery.blockUI({ message: jQuery('#loginForm'),
                   css: { padding:        0, 
                          margin:         0, 
                          width:          '350px',  
                          top:            '40%',  
                          left:           w/2,  
                          textAlign:      'left',  
                          color:          '#000',  
                          border:         'none', 
                          backgroundColor:'white' 
                          } 
                 }); 
}

function closeLoginDialog()
{
  jQuery.unblockUI(); 
}


function afterLogin(lat, lng, zoom)
{
  if (jQuery("#map").length >0 )
  {
    clearMap();
    setCenterMap(lat, lng, zoom);
  }
}


function checkAddressPresence(model)
{
  if ($(model+"_address").value != 'default location')
  {
    var addressValue = $(model+"_address").value;
    $('lookupAddress').replace('<span id="lookupAddress">'+addressValue+'</span>');
  }
  
}

function checkLatPresence(model)
{
  if ($(model+"_lat").value != '0')
  {
    return $(model+"_lat").value;
  }
  else
  {
    return 0;
  }
}

function checkLngPresence(model)
{
  if ($(model+"_lng").value != '0')
  {
    return $(model+"_lng").value;
  }
  else
  {
    return 0;
  }
}

function generateSearchLiveValidation()
{
  // search events
	var eventsAddress = new LiveValidation('eventsAddress', {
		onlyOnSubmit: true,
		insertAfterWhatNode: 'eventsAddressLabel'
  });
	eventsAddress.add( Validate.Presence );
	
	// search artists
	var artistsAddress = new LiveValidation('artistsAddress', {
		onlyOnSubmit: true,
		insertAfterWhatNode: 'artistsAddressLabel'
  });
	artistsAddress.add( Validate.Presence );
	
	// search live clubs
	var eventsAddress = new LiveValidation('liveClubsAddress', {
		onlyOnSubmit: true,
		insertAfterWhatNode: 'liveClubsAddressLabel'
  });
	eventsAddress.add( Validate.Presence );
}


function generateSignupLiveValidation()
{
	// login
	var user_login = new LiveValidation('user_login', {
		onlyOnSubmit: true,
		insertAfterWhatNode: 'signupLoginLabel'
  });
	user_login.add( Validate.Presence );
	user_login.add( Validate.Length, { minimum: 4, maximum: 40 } );
	
	// email
	var user_email = new LiveValidation('user_email', {
		onlyOnSubmit: true,
		insertAfterWhatNode: 'signupEmailLabel'
  });
	user_email.add( Validate.Presence );
	user_email.add( Validate.Email);
	user_email.add( Validate.Length, { minimum: 5, maximum: 150 } );
	
	// password
	var user_password = new LiveValidation('user_password', {
		onlyOnSubmit: true,
		insertAfterWhatNode: 'signupPwdLabel'
  });
	user_password.add( Validate.Presence );
	user_password.add( Validate.Length, { minimum: 4, maximum: 40 } );
	
	// password_confirmation
	var user_password_confirmation = new LiveValidation('user_password_confirmation', {
		onlyOnSubmit: true,
		insertAfterWhatNode: 'signupPwdConfLabel'
  });
	user_password_confirmation.add(Validate.Confirmation, { match: 'user_password' } );
	
}


function generateEditUserLiveValidation()
{	
	// email
	var user_email = new LiveValidation('user_email', {
		onlyOnSubmit: true,
		insertAfterWhatNode: 'editUserEmailLabel'
  });
	user_email.add( Validate.Presence );
	user_email.add( Validate.Email);
	user_email.add( Validate.Length, { minimum: 5, maximum: 150 } );
		
}

function generateEditArtistLiveValidation(validateLogin)
{			
  if (validateLogin) {
  	// name
  	var artist_name = new LiveValidation('artist_name', {
  		onlyOnSubmit: true,
  		insertAfterWhatNode: 'editArtistNameLabel'
    });
  	artist_name.add( Validate.Presence );
  	artist_name.add( Validate.Length, { maximum: 150 } );
  }

	// email
	var artist_email = new LiveValidation('artist_email', {
		onlyOnSubmit: true,
		insertAfterWhatNode: 'editArtistEmailLabel'
  });
	artist_email.add( Validate.Presence );
	artist_email.add( Validate.Email);
	artist_email.add(Validate.Length, {minimum: 5,maximum: 150});
	
	// telephone
	var artist_telephone = new LiveValidation('artist_telephone', {
		onlyOnSubmit: true,
		insertAfterWhatNode: 'editArtistTelephoneLabel'
  });
	artist_telephone.add( Validate.Presence );
	artist_telephone.add( Validate.Length, { maximum: 40 } );
	
	// description
	var artist_description = new LiveValidation('artist_description', {
		onlyOnSubmit: true,
		insertAfterWhatNode: 'editArtistDescriptionLabel'
  });
	artist_description.add( Validate.Presence );
	artist_description.add(Validate.Length, {
        maximum: 2000
    });
    
  // min_cachet
	var min_cachet = new LiveValidation('artist_min_cachet', {
		onlyOnSubmit: true,
		insertAfterWhatNode: 'editArtistMinCachetLabel'
  });
	min_cachet.add( Validate.Numericality );
	
}

function generateEditLiveClubLiveValidation(validateLogin)
{	
    
  if (validateLogin) {
  	// name
  	var live_club_name = new LiveValidation('live_club_name', {
  		onlyOnSubmit: true,
  		insertAfterWhatNode: 'editLiveClubNameLabel'
    });
  	live_club_name.add( Validate.Presence );
  	live_club_name.add( Validate.Length, { maximum: 150 } );
	}

	// email
	var live_club_email = new LiveValidation('live_club_email', {
		onlyOnSubmit: true,
		insertAfterWhatNode: 'editLiveClubEmailLabel'
  });
	live_club_email.add( Validate.Presence );
	live_club_email.add( Validate.Email);
	live_club_email.add(Validate.Length, {minimum: 5,maximum: 150});
	
	// telephone
	var live_club_telephone = new LiveValidation('live_club_telephone', {
		onlyOnSubmit: true,
		insertAfterWhatNode: 'editLiveClubTelephoneLabel'
  });
	live_club_telephone.add( Validate.Presence );
	live_club_telephone.add( Validate.Length, { maximum: 40 } );
	
	// description
	var live_club_description = new LiveValidation('live_club_description', {
		onlyOnSubmit: true,
		insertAfterWhatNode: 'editLiveClubDescriptionLabel'
  });
	live_club_description.add( Validate.Presence );
	live_club_description.add(Validate.Length, {
        maximum: 2000
    });
}

function generateEditEventLiveValidation(validateDate)
{	
		
	// name
	if (validateDate)
	{
  	var event_name = new LiveValidation('event_name', {
  		onlyOnSubmit: true,
  		insertAfterWhatNode: 'editEventNameLabel'
    });
  	event_name.add( Validate.Presence );
  	event_name.add( Validate.Length, { maximum: 150 } );
	}

	// description
	var event_description = new LiveValidation('event_description', {
		onlyOnSubmit: true,
		insertAfterWhatNode: 'editEventDescriptionLabel'
  });
	event_description.add( Validate.Presence );
	event_description.add(Validate.Length, {
        maximum: 1000
  });
  
  // event_date
  if (validateDate)
  {
  	var event_event_date = new LiveValidation('event_event_date', {
  		onlyOnSubmit: true,
  		insertAfterWhatNode: 'editEventDateLabel'
    });
  	event_event_date.add( Validate.Presence );
	}
	
	// from_time
	var event_from_time = new LiveValidation('event_from_time', {
		onlyOnSubmit: true,
		insertAfterWhatNode: 'editEventFromLabel'
  });
	event_from_time.add( Validate.Presence );
	event_from_time.add( Validate.Length, { maximum: 40 } );
	
	// to_time
	var event_to_time = new LiveValidation('event_to_time', {
		onlyOnSubmit: true,
		insertAfterWhatNode: 'editEventToLabel'
  });
	event_to_time.add( Validate.Presence );
	event_to_time.add( Validate.Length, { maximum: 40 } );
	
	
}

function generateEditEventThreadLiveValidation()
{	
		
	// name
	var event_thread_subject = new LiveValidation('event_thread_subject', {
		onlyOnSubmit: true,
		insertAfterWhatNode: 'editThreadSubjectLabel'
  });
	event_thread_subject.add( Validate.Presence );
	event_thread_subject.add( Validate.Length, { minimum: 5, maximum: 255 } );

	// description
	var event_thread_body = new LiveValidation('event_thread_body', {
		onlyOnSubmit: true,
		insertAfterWhatNode: 'editThreadBodyLabel'
  });
	event_thread_body.add( Validate.Presence );
	event_thread_body.add(Validate.Length, {
	      minimum: 5,
        maximum: 1000
  });	
}

function generateEditUserEventThreadLiveValidation()
{	
		
	// name
	var user_event_thread_subject = new LiveValidation('user_event_thread_subject', {
		onlyOnSubmit: true,
		insertAfterWhatNode: 'editThreadSubjectLabel'
  });
	user_event_thread_subject.add( Validate.Presence );
	user_event_thread_subject.add( Validate.Length, { minimum: 5, maximum: 255 } );

	// description
	var user_event_thread_body = new LiveValidation('user_event_thread_body', {
		onlyOnSubmit: true,
		insertAfterWhatNode: 'editThreadBodyLabel'
  });
	user_event_thread_body.add( Validate.Presence );
	user_event_thread_body.add(Validate.Length, {
	      minimum: 5,
        maximum: 1000
  });	
}


function generateEditUserEventLiveValidation(validateDate)
{	
		
	// name
	if (validateDate)
	{
  	var user_event_name = new LiveValidation('user_event_name', {
  		onlyOnSubmit: true,
  		insertAfterWhatNode: 'editEventNameLabel'
    });
  	user_event_name.add( Validate.Presence );
  	user_event_name.add( Validate.Length, { maximum: 150 } );
	}

	// description
	var user_event_description = new LiveValidation('user_event_description', {
		onlyOnSubmit: true,
		insertAfterWhatNode: 'editEventDescriptionLabel'
  });
	user_event_description.add( Validate.Presence );
	user_event_description.add(Validate.Length, {
        maximum: 2000
  });
  
  // artist
	var user_event_artist = new LiveValidation('user_event_artist', {
		onlyOnSubmit: true,
		insertAfterWhatNode: 'editEventArtistLabel'
  });
	user_event_artist.add( Validate.Presence );
	user_event_artist.add(Validate.Length, {
        maximum: 150
  });
  
  // live_club
	var user_event_live_club = new LiveValidation('user_event_live_club', {
		onlyOnSubmit: true,
		insertAfterWhatNode: 'editEventLiveClubLabel'
  });
	user_event_live_club.add( Validate.Presence );
	user_event_live_club.add(Validate.Length, {
        maximum: 150
  });
  
  // event_date
  if (validateDate)
  {
  	var user_event_event_date = new LiveValidation('user_event_event_date', {
  		onlyOnSubmit: true,
  		insertAfterWhatNode: 'editEventDateLabel'
    });
  	user_event_event_date.add( Validate.Presence );
	}
	
	// from_time
	var user_event_from_time = new LiveValidation('user_event_from_time', {
		onlyOnSubmit: true,
		insertAfterWhatNode: 'editEventFromLabel'
  });
	user_event_from_time.add( Validate.Presence );
	user_event_from_time.add( Validate.Length, { maximum: 40 } );
	
	// to_time
	var user_event_to_time = new LiveValidation('user_event_to_time', {
		onlyOnSubmit: true,
		insertAfterWhatNode: 'editEventToLabel'
  });
	user_event_to_time.add( Validate.Presence );
	user_event_to_time.add( Validate.Length, { maximum: 40 } );
	
	
}




function insertValueInField(field, value)
{
	document.getElementById(field).value = value;
}


var map;
var markerHash={};
var currentFocus=false;
var markersArray = [];


function clearMap()
{
	map.clearOverlays();
}

function clearMarkerHash()
{
  markerHash={};
}

function buildMap()
{
	map = new GMap2(document.getElementById("map"));
	markerHash={};
	markersArray = [];
	currentFocus=false;
	map.addControl(new GMapTypeControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 7)));
	//map.addControl(new DragZoomControl());
	//map.enableScrollWheelZoom();
}

function setCenterMap(centerLat, centerLng, zoom)
{
	map.setCenter(new GLatLng(centerLat, centerLng), zoom);
}

function addMarker(lat, lng)
{
	var marker = new GMarker(new GLatLng(lat,lng));
	map.addOverlay(marker);
}

function addUserMarker(lat, lng)
{
	var userMarker = new GMarker(new GLatLng(lat, lng));
	map.addOverlay(userMarker);
}


function addArtistMarker(artist)
{
	var artistMarker = new GMarker(new GLatLng(artist.lat,artist.lng));
	map.addOverlay(artistMarker);
}

function addLiveClubMarker(live_club)
{
	var liveClubMarker = new GMarker(new GLatLng(live_club.lat,live_club.lng));
	map.addOverlay(liveClubMarker);
}

function addEventMarker(evento)
{
	var eventoMarker = new GMarker(new GLatLng(evento.lat,evento.lng));
	map.addOverlay(eventoMarker);
}

function addUserEventMarker(evento)
{
	var eventoMarker = new GMarker(new GLatLng(evento.lat,evento.lng));
	map.addOverlay(eventoMarker);
}


function focusPoint(id, lat, lng) {
  if (currentFocus) {
    jQuery("#sidebar-item-"+currentFocus).removeClass('current');               
  }
  jQuery("#sidebar-item-"+id).addClass('current');
  map.setCenter(new GLatLng(lat, lng), 17);
  markerHash[id].mymarker.openInfoWindowHtml(markerHash[id].description, {maxWidth:350}); 
  currentFocus=id;
}


function addFullArtistMarker(artist, view_path, final_view_path)
{
	var artistMarker = new GMarker(new GLatLng(artist.lat,artist.lng));
	GEvent.addListener(artistMarker, 'click',
	  function() {
	    focusPoint(artist.id, artist.lat,artist.lng);
	  }
	);
	//map.addOverlay(artistMarker);
	markersArray.push(artistMarker);
	var artistDescription = "<div align=\"center\" class=\"pTitle\">" + view_path + "</div><span class=\"pDescription\">" + artist.description.substring(0, 200).stripScripts() + "</span><br/>" + final_view_path;
	markerHash[artist.id]={mymarker:artistMarker, description:artistDescription, visible:true};
}

function addFullLiveClubMarker(live_club, view_path, final_view_path)
{
	var liveClubMarker = new GMarker(new GLatLng(live_club.lat,live_club.lng));
	GEvent.addListener(liveClubMarker, 'click',
	  function() {
	    focusPoint(live_club.id, live_club.lat,live_club.lng);
	  }
	);
	//map.addOverlay(liveClubMarker);
	markersArray.push(liveClubMarker);
	var liveClubDescription =  "<div align=\"center\" class=\"pTitle\">" + view_path + "</div><span class=\"pDescription\">" + live_club.description.substring(0, 200).stripScripts() + "</span><br/>" + final_view_path;
	markerHash[live_club.id]={mymarker:liveClubMarker, description:liveClubDescription, visible:true};
}

function addFullLiveClubEventMarker(live_club, artist, evento, view_path, event_date, tags, final_view_path)
{
	var liveClubEventMarker = new GMarker(new GLatLng(live_club.lat,live_club.lng));
	GEvent.addListener(liveClubEventMarker, 'click',
	  function() {
	    focusPoint(evento.id, live_club.lat,live_club.lng);
	  }
	);
	//map.addOverlay(liveClubEventMarker);
	markersArray.push(liveClubEventMarker);
	var liveClubEventDescription = "<div align=\"center\" class=\"pTitle\">" + view_path + "</div>";
	liveClubEventDescription = liveClubEventDescription + " <span class=\"pArtist\">" + artist.name + " </span><span class=\"pDate\"> show " + "";
	liveClubEventDescription = liveClubEventDescription + " on " + event_date;
	liveClubEventDescription = liveClubEventDescription + " at </span><span class=\"pLiveClub\">" + live_club.name+ "</span><br/>";
	liveClubEventDescription = liveClubEventDescription + "<span class=\"pDate\">" + tags + "</span> <br/>";
	liveClubEventDescription = liveClubEventDescription + " <br/><span class=\"pDescription\">" + evento.description.substring(0, 500).stripScripts() + "...</span><br/>" + final_view_path;
	markerHash[evento.id]={mymarker:liveClubEventMarker, description:liveClubEventDescription, visible:true};
}


function addFullLiveClubOnlyEventMarker(live_club, evento, view_path, event_date, tags, final_view_path)
{
	var liveClubEventMarker = new GMarker(new GLatLng(live_club.lat,live_club.lng));
	GEvent.addListener(liveClubEventMarker, 'click',
	  function() {
	    focusPoint(evento.id, live_club.lat,live_club.lng);
	  }
	);
	//map.addOverlay(liveClubEventMarker);
	markersArray.push(liveClubEventMarker);
	var liveClubEventDescription = "<div align=\"center\" class=\"pTitle\">" + view_path + "</div>";
	liveClubEventDescription = liveClubEventDescription + "<span class=\"pDate\"> show " + "";
	liveClubEventDescription = liveClubEventDescription + " on " + event_date;
	liveClubEventDescription = liveClubEventDescription + " at </span><span class=\"pLiveClub\">" + live_club.name+ "</span><br/>";
	liveClubEventDescription = liveClubEventDescription + "<span class=\"pDate\">" + tags + "</span> <br/>";
	liveClubEventDescription = liveClubEventDescription + " <br/><span class=\"pDescription\">" + evento.description.substring(0, 500).stripScripts() + "...</span><br/>" + final_view_path;
	markerHash[evento.id]={mymarker:liveClubEventMarker, description:liveClubEventDescription, visible:true};
}


function addFullArtistOnlyEventMarker(artist, evento, view_path, event_date, tags, final_view_path)
{
	var liveClubEventMarker = new GMarker(new GLatLng(evento.lat,evento.lng));
	GEvent.addListener(liveClubEventMarker, 'click',
	  function() {
	    focusPoint(evento.id, evento.lat,evento.lng);
	  }
	);
	//map.addOverlay(liveClubEventMarker);
	markersArray.push(liveClubEventMarker);
	var liveClubEventDescription = "<div align=\"center\" class=\"pTitle\">" + view_path + "</div>";
	liveClubEventDescription = liveClubEventDescription + " <span class=\"pArtist\">" + artist.name + " </span><span class=\"pDate\"> show " + "";
	liveClubEventDescription = liveClubEventDescription + " on " + event_date;
	liveClubEventDescription = liveClubEventDescription + "</span><br/><span class=\"pDate\">" + tags + "</span> <br/>";
	liveClubEventDescription = liveClubEventDescription + " <br/><span class=\"pDescription\">" + evento.description.substring(0, 500).stripScripts() + "...</span><br/>" + final_view_path;
	markerHash[evento.id]={mymarker:liveClubEventMarker, description:liveClubEventDescription, visible:true};
}


function addFullUserEventMarker(evento, view_path, event_date, tags, final_view_path)
{
	var liveClubEventMarker = new GMarker(new GLatLng(evento.lat,evento.lng));
	GEvent.addListener(liveClubEventMarker, 'click',
	  function() {
	    focusPoint(evento.id +"-user", evento.lat,evento.lng);
	  }
	);
	//map.addOverlay(liveClubEventMarker);
	markersArray.push(liveClubEventMarker);
	var liveClubEventDescription = "<div align=\"center\" class=\"pTitle\">" + view_path + "</div>";
	liveClubEventDescription = liveClubEventDescription + " <span class=\"pArtist\">" + evento.artist + " </span><span class=\"pDate\"> show " + "";
	liveClubEventDescription = liveClubEventDescription + " on " + event_date;
	liveClubEventDescription = liveClubEventDescription + " at </span><span class=\"pLiveClub\">" + evento.live_club+ "</span><br/>";
	liveClubEventDescription = liveClubEventDescription + "<span class=\"pDate\">" + tags + "</span> <br/>";
	liveClubEventDescription = liveClubEventDescription + " <br/><span class=\"pDescription\">" + evento.description.substring(0, 500).stripScripts() + "...</span><br/>" + final_view_path;
	markerHash[evento.id+"-user"]={mymarker:liveClubEventMarker, description:liveClubEventDescription, visible:true};
}


function removeNewThreadFormDisplayLink()
{
  jQuery('#newThreadForm').remove();
}

function collapseThreads(id)
{
  if (jQuery(".threadForm").length > 0) {
    jQuery(".threadForm").remove();
  }
  
  if (jQuery(".eventHide").length > 0) {
    jQuery(".eventHide").hide();
  }
  
  
  if (jQuery(".replayThreadLink").length > 0) {
    jQuery(".replayThreadLink").show();
  }
  
  if (jQuery("#newThreadLink").length > 0) {
    jQuery("#newThreadLink").show();
  }
 
  if (jQuery("#threadPlaceholder-"+id).length > 0) {
    jQuery("#threadPlaceholder-"+id).show();
  }
  
}

function performResize()
{
      var h = jQuery('#body').innerHeight(); // height of the container
      if ((h - 80) < 450)
      {
        h = 450;
        var styleStrTable = "height:" +h+ "px;";
        jQuery("#mainContent").attr({
            style: styleStrTable
        });
      }
}

function spin_div(div_id) {
  container = $(div_id);
  positioning = 'top: '+container.offsetTop+'px; width: '+container.offsetWidth+'px; height: '+container.offsetHeight+'px; ';
  container.innerHTML += '<div class="spin_div" id="spinner" style="position: absolute; ' + positioning + '"></div>';
}


function openIntroDialog(id)
{
  var w = jQuery(window).width() - 900;
  jQuery.blockUI({ message: jQuery('#'+id),
                   css: { padding:        0, 
                          margin:         0, 
                          width:          '900px',  
                          top:            '10%',  
                          left:           w/2,  
                          textAlign:      'left',  
                          color:          '#000',  
                          border:         '2px solid #919191', 
                          backgroundColor:'white' 
                          } 
                 }); 
}


function addClassNames(){ 
  jQuery(":text").addClass("textbox");
  jQuery(":password").addClass("textbox");
}

function resetSearchFields()
{
    jQuery("#eventsAddress").val("");
    jQuery("#evento_when_today").attr("checked", "checked"); 
    jQuery("#evento_range_500").attr("checked", "checked"); 
    jQuery("#event_date").val("");
    jQuery("#artistsAddress").val("");
    jQuery("#artist_range_10").attr("checked", "checked"); 
    jQuery("#liveClubsAddress").val("");
    jQuery("#live_club_range_10").attr("checked", "checked"); 
}






