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

$(document).ready(function() {
	
	populate_rails_flash();

	// Change party invitation subject based on from name
	var invitation = 'Invitación para la fiesta de ';
	$("#party_invitation_subject").attr("value", invitation + $("#party_invitation_from").attr("value"));
	
	$("#party_invitation_from").keyup(function() {
		$("#party_invitation_subject").attr("value", invitation + this.value);
	 }); 

	// Login box reset
	$("#header_login_username").click(function() {
		if (this.value == 'Nombre de Usuario') {
    		$("#header_login_username").attr("value", "");
		}
     }); 
	$("#header_login_username").blur(function() {
		if (this.value == '') {
    		$("#header_login_username").attr("value", "Nombre de Usuario");
		}
     }); 
	$("#header_login_password").click(function() {
		if (this.value == '********') {
    		$("#header_login_password").attr("value", "");
		}
     }); 
	$("#header_login_password").blur(function() {
		if (this.value == '') {
    		$("#header_login_password").attr("value", "********");
		}
     }); 

  // Party Invitation's checkboxes
  $('div#invitations input[@type=checkbox]').click(function() {
    $.get('/dashboard/party_invitations/toggle/' + this.value);
  });

  // Party's vote
  $('a#vote').click(function() {
    $(this).hide('slow');
    $('#votes').load('/parties/' + this.name + '/vote');
    return false;
  });

	var votes = $.cookie('votes');
	if (votes != null) {
		var vote_array = votes.split('-');
		var party_id = $('a#vote').attr("name");
		var array_pos = $.inArray(party_id, vote_array);
		if ( array_pos > "-1") {
			$("#vote_link").html('Gracias por votar por esta página');
			$("#party_vote_description").hide();
		}
	}

  // Submit button
  $('.sub_image').click(function() {
    $('.sub_image').hide('slow');
  });

  // Candy submit button
  $('#new_party_comment a.candy_button').click(function() {
    $(this).parent().submit();
    return false;
  });

	// Candy submit button
  $('#recommend_to_a_friend a.candy_button').click(function() {
    $(this).parent().submit();
    return false;
  });

  var bookmark = $('#bookmark_this_page');
  if (bookmark) {
    var ver = navigator.appName;
    var num = parseInt(navigator.appVersion);
    if ((ver == "Microsoft Internet Explorer")&&(num >= 4)) {
      bookmark.click(function() { window.external.AddFavorite(location.href); return false; });
    }else{
      bookmark.click(function() { alert('Presiona Control+D para agregar esta página a tus Favoritos.'); return false; });
    }
  }

  // Handling of cached parts
  $.get("/session/info/logged_in", function(responseTxt) {
    if (responseTxt == "1") {
      $("body").addClass("logged_in");
    } else {
      $("body").addClass("logged_out").addClass("baloons");
    }
  });
  
  $.get("/session/info/survey_completed", function(responseTxt) {
    if (window.use_javascript_css_toggling) {
      if (responseTxt == "1") {
        $("body").addClass("signed_up");
      } else {
        $("body").addClass("not_signed_up");
     /* * this breaks index_reg_closed *  */
     /* $("#content").addClass("narrow"); */
     /* $("#sidebar").addClass("wide");   */
      }
    }
  });

  $('#nav a').click(function() { window.open(this.href); return false; });

  $('.photo_detail .print a').click(function() { window.open(this.href); return false; });

  if (window.GBrowserIsCompatible && GBrowserIsCompatible()) {
    window.map = new GMap2(document.getElementById("map_div"));
    window.map.setCenter(new GLatLng(37.09024,-95.712891), 3);
    map.addControl(new GLargeMapControl());

    window.clusterer = new Clusterer(window.map);
  }

  if (window.map_source) {
    for (var i = 0; i < map_source.length; i++) {
      var item = map_source[i];

      var html = '<div style="clear:both;font-size:1.2em">' +
        '<img src="' + item.avatar + '" style="float:left;margin:0 5px 0 0" />' +
        '<p style="color:#2B7EC8;font-size:1.4em">' + item.title + '</p>' +
        '<p>' + item.location + '</p>' +
        item.link +  
        '</div>';

      addMarker(item.latitude, item.longitude, html);
    }
  }

});

function addMarker(latitude, longitude, description) { 
  var marker = new GMarker(new GLatLng(latitude, longitude)); 

  GEvent.addListener(marker, 'click', function() { 
      marker.openInfoWindowHtml(description); 
  }); 

  window.map.addOverlay(marker); 
}
