
$(document).ready(function() {

	
	// ajax feedback logic for page /petition/kommentare
	var selected = "";
	var id = "";
	
	// initialize button titles
	$("img.rating_up").attr("title","Gefällt mir");
	$("img.rating_down").attr("title","Gefällt mir nicht");
		
	$("img.rating_up").hover(
	  function(){
		  selected = $(this).attr("selected");
		  $(this).attr("src","/images/up.png");
	  },
	  function(){
		  if(selected == "true")
			  $(this).attr("src","/images/up.png");
		  else
			  $(this).attr("src","/images/_up.png");
	  }
	);

	$("img.rating_up").click(
	  function(ev){
		  if(selected == "true")
		  {
			  //switch off this button
			  selected = "false";
			  $(this).attr("selected",selected);
			  $(this).attr("src","/images/_up.png");
		  }
		  else
		  {
			  //switch off other button
			  var other = $(this).parent().parent().children().children('.rating_down');
			  other.attr("src","/images/_down.png");
			  other.attr("selected","false");
			  
			  //switch on this button
			  selected = "true";
			  $(this).attr("selected",selected);
			  $(this).attr("src","/images/up.png");

			  //send feedback to the server
			  id = $(this).attr("id");			  
			  $.get("/ajax/feedback", { id: id, action: "thumbs_up" } );
		  }	

		  //prevent the default action of the event, this will stop the href in the anchor being followed
		  ev.preventDefault();		  
	  }
	);	

	$("img.rating_down").hover(
	  function(){
		  selected = $(this).attr("selected");
		  $(this).attr("src","/images/down.png");
	  },
	  function(){
		  if(selected == "true")
			  $(this).attr("src","/images/down.png");
		  else
			  $(this).attr("src","/images/_down.png");
	  }
	);

	$("img.rating_down").click(
	  function(ev){
		  if(selected == "true")
		  {
			//switch off this button
			  selected = "false";
			  $(this).attr("selected",selected);
			  $(this).attr("src","/images/_down.png");
		  }
		  else
		  {
			  //switch off other selected button
			  var other = $(this).parent().parent().children().children('.rating_up');
			  other.attr("src","/images/_up.png");
			  other.attr("selected","false");
			  
			  //switch on this button
			  selected = "true";
			  $(this).attr("selected",selected);
			  $(this).attr("src","/images/down.png");
			  
			  //send feedback to the server
			  id = $(this).attr("id");
			  $.get("/ajax/feedback", { id: id, action: "thumbs_down" } );
		  }			  

		  //prevent the default action of the event, this will stop the href in the anchor being followed
		  ev.preventDefault();		  
	  }
	);
	
	// ajax form plugin configurations
	// http://jquery.malsup.com/form/
	
	// ajax form binding for action in /petition/argumentation
    var options = {success:showAjaxNewArgumentResponse};	  // post-submit callback 
    $('#ajax_new_argument').ajaxForm(options);

    
}); // end document ready


/* ajax form callback functions
 * 
 */
function showAjaxNewArgumentResponse(responseText, statusText, xhr, $form) { 
	   
	var parent = $($form).parent();
	var parent_id = $(parent).attr("id");

	if(parent_id == "pro_button_form")
	{
		$("#pro_button_form").attr("style", "display:none;");
		$("#pro_button > a").text("Neues Pro-Argument");
		$("#pro_button").attr("style", "display:inline;");
		$("#pro_argument").append(responseText);
	}
	else if(parent_id == "contra_button_form")
	{
		$("#contra_button_form").attr("style", "display:none;");
		$("#contra_button > a").text("Neues Contra-Argument");
		$("#contra_button").attr("style", "display:inline;");
		$("#contra_argument").append(responseText);
	}
	
	
} 

/* show form and hide button 
 * 
 */
function showForm(id) {
	
	// hide form button
	$("#" + id).attr("style", "display:none;");
	
	// show corresponding form
	var form_id = id + "_form";
	$("#" + form_id).attr("style", "display:block;");
}
	


/*
 * open new window, centered
 */
function wopen(url, name, w, h)
{
  // Fudge factors for window decoration space.
  // In my tests these work well on all platforms & browsers.
  w += 32;
  h += 96;
  wleft = (screen.width - w) / 2;
  wtop = (screen.height - h) / 2;
  // IE5 and other old browsers might allow a window that is
  // partially offscreen or wider than the screen. Fix that.
  // (Newer browsers fix this for us, but let's be thorough.)
  if (wleft < 0) {
    w = screen.width;
    wleft = 0;
  }
  if (wtop < 0) {
    h = screen.height;
    wtop = 0;
  }
  var win = window.open(url,
    name,
    'width=' + w + ', height=' + h + ', ' +
    'left=' + wleft + ', top=' + wtop + ', ' +
    'location=no, menubar=no, ' +
    'status=no, toolbar=no, scrollbars=yes, resizable=yes');
  // Just in case width and height are ignored
  win.resizeTo(w, h);
  // Just in case left and top are ignored
  win.moveTo(wleft, wtop);
  win.focus();
}


// google analytics
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-17873760-1']);
_gaq.push(['_trackPageview']);

(function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

