/* ------------ Chilli Voting --------------*/

/* Die einzelnen Zustände werden vorgeladen */
onechilli_notvoted = new Image(77, 110);
onechilli_notvoted.src = '/img/vote/hot-n.gif';

onechilli_hover = new Image(77, 110);
onechilli_hover.src = '/img/vote/hot-a.gif';

onechilli_voted = new Image(77, 110);
onechilli_voted.src = '/img/vote/hot-s.gif';

onechilli_notvotedable = new Image(77, 110);
onechilli_notvotedable.src = '/img/vote/hot-p.gif';

twochillis_notvoted = new Image(77, 110);
twochillis_notvoted.src = '/img/vote/hotter-n.gif';

twochillis_hover = new Image(77, 110);
twochillis_hover.src = '/img/vote/hotter-a.gif';

twochillis_voted = new Image(77, 110);
twochillis_voted.src = '/img/vote/hotter-s.gif';

twochillis_notvotedable = new Image(77, 110);
twochillis_notvotedable.src = '/img/vote/hotter-p.gif';

threechillis_notvoted = new Image(77, 110);
threechillis_notvoted.src = '/img/vote/hottest-n.gif';

threechillis_hover = new Image(77, 110);
threechillis_hover.src = '/img/vote/hottest-a.gif';

threechillis_voted = new Image(77, 110);
threechillis_voted.src = '/img/vote/hottest-s.gif';

threechillis_notvotedable = new Image(77, 110);
threechillis_notvotedable.src = '/img/vote/hottest-p.gif';

message_cancel_passiv = new Image(273, 24);
message_cancel_passiv.src = '/img/vote/cancel-a.gif';

message_cancel_activ = new Image(273, 24);
message_cancel_activ.src = '/img/vote/cancel-n.gif';

message_change_activ = new Image(273, 24);
message_change_activ.src = '/img/vote/change-n.gif';

message_change_passiv = new Image(273, 24);
message_change_passiv.src = '/img/vote/change-p.gif';

message_save_activ = new Image(273, 29);
message_save_activ.src = '/img/vote/save-n.gif';

message_save_passiv = new Image(273, 29);
message_save_passiv.src = '/img/vote/save-p.gif';

chillis_passiv = new Image(273, 81);
chillis_passiv.src = '/img/vote/no-more-votes.gif';

chillis_message_passiv = new Image(273, 81);
chillis_message_passiv.src = '/img/vote/info.gif';

votebutton_notvoted = new Image(33, 94);
votebutton_notvoted.src = '/img/vote.gif';

votebutton_voted_one = new Image(33, 94);
votebutton_voted_one.src = '/img/vote1.gif';

votebutton_voted_two = new Image(33, 94);
votebutton_voted_two.src = '/img/vote2.gif';

votebutton_voted_three = new Image(33, 94);
votebutton_voted_three.src = '/img/vote3.gif';

background = new Image(33, 94);
background.src = '/img/vote/bg.gif';

cur_id = 0;

/*Beim start werden die Cookiewerte der Votings eingespeichert oder auf 0 gesetzt */

onepoint = 0;
twopoints = 0;
threepoints = 0;


$(document).ready(function(){
  $('#javascript-required').css("display","none");
  
  if(holeCookie(" onepoint")){
    onepoint = cookieWert;
   $('#votebuttonnr_'+onepoint).attr("src", votebutton_voted_one.src);
  }
  else {
  onepoint = 0;
 }
  
  if(holeCookie(" twopoints")){
    twopoints = cookieWert;
   $('#votebuttonnr_'+twopoints).attr("src", votebutton_voted_two.src);
  }
  else {twopoints = 0; }
  
  if(holeCookie(" threepoints")){
    threepoints = cookieWert;
   $('#votebuttonnr_'+threepoints).attr("src", votebutton_voted_three.src);
  }
  else {threepoints = 0; }
  


})


/* Die Funktion bekommt den Cookiename über übergibt den Wert in cookieWert liefert true zurück wenn es den cookie gibt */
function holeCookie(cookiename){
  alleCookies=document.cookie;
  cookieArr=alleCookies.split(";");
  for(var i=0;i<cookieArr.length;i++){
    if(cookieArr[i].split("=")[0] == cookiename){
 	  	cookieWert=cookieArr[i].split("=");
    	cookieWert=unescape(cookieWert[1]);
  	  return true;
  	}
  }
  return false;
}

/* 
*  name: showvoting(vereinsid, el)
*  parameters: vereinsid: Der Verein für den man gevotet hat
*  el: Der Button der die Funktion aufruft (den den wird das voting div drangehängt)
*  Beschreibung: Die Funktion zeigt das voting div an und formatiert den Inhalt
*/

function showvoting(vereinsid, el){  

  // Unsichtbares Div für den blureffekt

  // Es wird angezeigt und der aktuelle Verein wird ausgewählt
  el.parent().append($('#voting'));  
  $('#voting').fadeIn('fast'); 
  cur_id = vereinsid;

  //$('#voting').parent().append($('#blureffekt'));  
  $("#blureffekt").css("display","block");
  $('#blureffekt').click(function () {hidevoting();})


  // Alle divs werden anklickbar
  $('#chillis1').parent().parent().attr('class', 'chilli'); 
  $('#chillis2').parent().parent().attr('class', 'chilli');    
  $('#chillis3').parent().parent().attr('class', 'chilli');      

  //  Wenn ein Punkt bereits gesetzt ist von einem anderen Verein wird er grau und nicht selektierbar
  if(onepoint!=0 && onepoint != vereinsid){
    $('#chillis1').parent().parent().html('<a href="javascript:;" onclick="vote(1); return false;"><img id="chillis1" src=../img/vote/hot-s.gif" alt="1 Chilli" title="1 Chilli"></a>');
    $('#chillis1').parent().parent().attr('class', 'chilli unselectable'); 
    $('#chillis1').attr("src", onechilli_notvotedable.src);   
  }

  if(twopoints!=0 && twopoints != vereinsid){
    $('#chillis2').parent().parent().html('<a href="javascript:;" onclick="vote(2); return false;"><img id="chillis2" src="../img/vote/hotter-s.gif" alt="2 Chilli" title="2 Chilli"></a>');
    $('#chillis2').parent().parent().attr('class', 'chilli unselectable'); 
    $('#chillis2').attr("src", twochillis_notvotedable.src);
  }

  if(threepoints!=0 && threepoints != vereinsid){
    $('#chillis3').parent().parent().html('<a href="javascript:;" onclick="vote(3); return false;"><img id="chillis3" src="../img/vote/hottest-s.gif" alt="3 Chilli" title="3 Chilli"></a>');
    $('#chillis3').parent().parent().attr('class', 'chilli unselectable'); 
    $('#chillis3').attr("src", threechillis_notvotedable.src);
  }

 // Wenn für den Verein bereits abgestimmt wurde wird er nicht selektierbar
  if(onepoint==vereinsid) {
    $('#chillis1').parent().parent().attr('class', 'chilli unselectable'); 
    $('#chillis1').attr("src", onechilli_voted.src);
  }

  if(twopoints==vereinsid) {
    $('#chillis2').parent().parent().attr('class', 'chilli unselectable'); 
    $('#chillis2').attr("src", twochillis_voted.src);
  }
  
  if(threepoints==vereinsid) {
    $('#chillis3').parent().parent().attr('class', 'chilli unselectable'); 
    $('#chillis3').attr("src", threechillis_voted.src);
  }
  
  //Wenn man bereits die 3 Stimmen gevotet hat und man zurücksetzen kann(in dem Verein einer seiner Votings ist)
  if(onepoint !=0 && twopoints !=0 && threepoints !=0 && (onepoint == cur_id || twopoints == cur_id || threepoints == cur_id) ){$('#voting').html('<img src="../img/vote/no-more-votes.gif" alt=""  title="" width="273" height="81" /><div id="submitmessage"><form action="/electors/add/" method="post"><input type="hidden" name="onepoint" value="'+onepoint+'" /><input type="hidden" name="twopoints" value="'+twopoints+'" /><input type="hidden" name="threepoints" value="'+threepoints+'" /><input type="image" src="../img/vote/save-n.gif" alt="Bewertung beenden und speichern"  title="Bewertung beenden und speichern" width="273" height="29" /></form></div><div id="message"><a href="javascript:;" onclick="change_vote(); return false;"><img id="changevote" src="../img/vote/change-n.gif" alt="Stimme noch mal ändern" title="Stimme noch mal ändern" width="273" height="23"></a></div>');
  }
  
  //Wenn man bereits die 3 Stimmen gevotet hat und nicht zurücksetzen kann / Das fenser geschlossen wird 
  if(onepoint !=0 && twopoints !=0 && threepoints !=0 && !(onepoint == cur_id || twopoints == cur_id || threepoints == cur_id) ){$('#voting').html('<img src="../img/vote/info.gif" alt=""  title="" width="273" height="81" /><div id="submitmessage"><form action="/electors/add/" method="post"><input type="hidden" name="onepoint" value="'+onepoint+'" /><input type="hidden" name="twopoints" value="'+twopoints+'" /><input type="hidden" name="threepoints" value="'+threepoints+'" /><input type="image" src="../img/vote/save-n.gif" alt="Bewertung beenden und speichern"  title="Bewertung beenden und speichern" width="273" height="29" /></form></div><div id="message"><a href="javascript:;" onclick="hidevoting(); return false;"><img id="changevote" src="../img/vote/change-n.gif" alt="Stimme noch mal ändern" title="Stimme noch mal ändern" width="273" height="23"></a></div>');
  }
  
  // Der Hovereffekt - Gilt nur für divs deren Elterndiv nicht unselected ist
  var unselected = "";

  // Hovereffekt - 1 Chilli
  $("#chillis1").hover(    
    function () { 
      unselected = $("#chillis1").parent().parent().attr("class");   
      if(unselected=="chilli"){
        $('#chillis1').attr("src", onechilli_hover.src);
      }
    }, 
    function () {
      unselected = $("#chillis1").parent().parent().attr("class");      
      if(unselected=="chilli"){
        $('#chillis1').attr("src", onechilli_notvoted.src);
      }
    }   
  )  

  // Hovereffekt - 2 Chillis
  $("#chillis2").hover(    
    function () { 
      unselected = $("#chillis2").parent().parent().attr("class");   
      if(unselected=="chilli"){
        $('#chillis2').attr("src", twochillis_hover.src);
      }
    }, 
    function () {
      unselected = $("#chillis2").parent().parent().attr("class");      
      if(unselected=="chilli"){
        $('#chillis2').attr("src", twochillis_notvoted.src);
      }
    }   
  ) 

  // Hovereffekt - 3 Chillis
  $("#chillis3").hover(    
    function () { 
    //alert("sd");
      unselected = $("#chillis3").parent().parent().attr("class");   
      if(unselected=="chilli"){
        $('#chillis3').attr("src", threechillis_hover.src);
      }
    }, 
    function () {
      unselected = $("#chillis3").parent().parent().attr("class");      
      if(unselected=="chilli"){
        $('#chillis3').attr("src", threechillis_notvoted.src);
      }
    }   
  )
  
  $("#clearvotefield").hover(    
    function () {
      $('#clearvotefield').attr("src", message_cancel_passiv.src);
    }, 
    function () {
      $('#clearvotefield').attr("src", message_cancel_activ.src);
    }   
  )



}      
 
/* Blendet das Voting-div aus und setzt den aktuellen Verein auf 0 */
function hidevoting(){ 
  $("#blureffekt").css("display","none");
  $('#voting').fadeOut('fast'); 
  this.cur_id = 0;
} 


/*
* name: voting(voting)
* Parameter: voting: Die Chilli die gevotet werden soll (1/2/3) 
* Beschreibung: Es wird für einen Verein gevotet und der voting div entsprechend formatiert
*/

function vote(voting){  
  // Wenn man für einen Verein bereits gevotet hat wird er direkt geschlossen
  if((voting == 1 && onepoint == cur_id ) || (voting == 2 && twopoints == cur_id ) ||(voting == 3 && threepoints == cur_id )){
    hidevoting();
  }
  // Nur wenn die Stimmen noch vorhanden sind/die Stimme vergeben werden kann:
  if((voting == 1 && onepoint == 0) || (voting == 2 && twopoints == 0) || (voting == 3 && threepoints == 0)){
    // Die Votes vom aktuellen Verein werden entzogen
    if(onepoint==cur_id){
      $('#chillis1').attr("src", onechilli_notvoted.src); 
      $('#chillis1').parent().parent().attr('class', 'chilli');       
      onepoint = 0;
      document.cookie = "onepoint=0; path=/";
    }
    
    if(twopoints==cur_id){
      $('#chillis2').attr("src", twochillis_notvoted.src); 
      $('#chillis2').parent().parent().attr('class', 'chilli'); 
      twopoints = 0; 
      document.cookie = "twopoints=0; path=/";
    }
    
    if(threepoints==cur_id){
      $('#chillis3').attr("src", threechillis_notvoted.src); 
      $('#chillis3').parent().parent().attr('class', 'chilli'); 
      threepoints = 0;
      document.cookie = "threepoints=0; path=/";
    }

    // Für den aktuellen Verein wird gevotet
    if(voting==1 && onepoint==0){
      onepoint = cur_id;     
      document.cookie = "onepoint="+cur_id + "; path=/";
      $('#chillis1').parent().parent().attr('class', 'chilli unselectable'); 
      $('#chillis'+voting).attr("src", onechilli_voted.src);
      $('#votebuttonnr_'+cur_id).attr("src", votebutton_voted_one .src);
    }
  
    if(voting==2 && twopoints==0){
      twopoints = cur_id;
      document.cookie = "twopoints="+cur_id + "; path=/";
      $('#chillis2').parent().parent().attr('class', 'chilli unselectable'); 
      $('#chillis'+voting).attr("src", twochillis_voted.src);
      $('#votebuttonnr_'+cur_id).attr("src", votebutton_voted_two .src);
    }
    if(voting==3 && threepoints==0){
      threepoints = cur_id;
      document.cookie = "threepoints="+cur_id + "; path=/";
      $('#chillis3').parent().parent().attr('class', 'chilli unselectable');        
      $('#chillis'+voting).attr("src", threechillis_voted.src);
      $('#votebuttonnr_'+cur_id).attr("src", votebutton_voted_three .src);
    } 
    
    // Wenn nicht für alle gevotet wurde wird der div anschließend geschlossen
    if(!(onepoint !=0 && twopoints !=0 && threepoints !=0)){
      hidevoting();
    } 
  }
  
  // Wenn alle Punkte besetzt sind wird der div anders formatiert
  if(onepoint !=0 && twopoints !=0 && threepoints !=0){
    $('#voting').html('<img src="../img/vote/no-more-votes.gif" alt=""  title="" width="273" height="81" /><div id="submitmessage"><form action="/electors/add/" method="post"><input type="hidden" name="onepoint" value="'+onepoint+'" /><input type="hidden" name="twopoints" value="'+twopoints+'" /><input type="hidden" name="threepoints" value="'+threepoints+'" /><input type="image" src="../img/vote/save-n.gif" alt="Bewertung beenden und speichern"  title="Bewertung beenden und speichern" width="273" height="29" /></form></div><div id="message"><a href="javascript:;" onclick="change_vote(); return false;"><img id="changevote" src="../img/vote/change-n.gif" alt="Stimme noch mal ändern" title="Stimme noch mal ändern" width="273" height="23"></a></div>');
  }

}   

/*
* name: clearvote()
* Beschreinung: Setzt einen Vote zurück wenn noch nicht alle ausgewählt wurden oder schließt das Fenster 
*               wenn für den Verein nicht gevotet wurde
*/
function clearvote(){ 
  $('#votebuttonnr_'+cur_id).attr("src", votebutton_notvoted.src);
  //if(onepoint != cur_id && twopoints != cur_id && threepoints != cur_id){
  //  hidevoting();
  //}

  if(onepoint==cur_id){ 
    $('#chillis1').attr("src", onechilli_notvoted.src); 
    onepoint = 0;
    document.cookie = "onepoint=0; path=/";
    $('#chillis1').parent().parent().attr('class', 'chilli'); 
    $('#submitmessage').text("");
  }
  
  if(twopoints==cur_id){
    $('#chillis2').attr("src", twochillis_notvoted.src); 
    $('#chillis2').parent().parent().attr('class', 'chilli');
    twopoints = 0;
    document.cookie = "twopoints=0; path=/";
    $('#submitmessage').text("");
    }
    
  if(threepoints==cur_id){
    $('#chillis3').attr("src", threechillis_notvoted.src); 
    $('#chillis3').parent().parent().attr('class', 'chilli');     
    threepoints = 0;
    document.cookie = "threepoints=0; path=/";
    $('#submitmessage').text("");
    } 
  hidevoting();
}

/*Setzt einen Vote zurück wenn bereits alle ausgewählt wurden (div-Struktur wird wieder hergestellt) */

function change_vote(){
  if(onepoint==cur_id){
    $('#voting').html('<div class="chilli"><a href="javascript:;" onclick="vote(1); return false;"><img id="chillis1" src="../img/vote/hot-s.gif" alt="1 Chilli"  title="1 Chilli" /></a></div><div class="chilli unselectable"><a href="javascript:;" onclick="vote(2); return false;"><img id="chillis2" src="../img/vote/hotter-p.gif" alt="2 Chillis" title="2 Chillis" /></a></div><div class="chilli unselectable"><a href="javascript:;" onclick="vote(3); return false;"><img id="chillis3" src="../img/vote/hottest-p.gif" alt="3 Chillis" title="3 Chillis" /></a></div><div id="submitmessage"></div><div id="message"><a href="javascript:;" onclick="clearvote(); return false;"><img id="clearvotefield" src="../img/vote/cancel-n.gif" alt="...doch keine Stimme vergeben." title="...doch keine Stimme vergeben." /></a></div>');
  }

  if(twopoints==cur_id){
    $('#voting').html('<div class="chilli unselectable"><a href="javascript:;" onclick="vote(1); return false;"><img id="chillis1" src="../img/vote/hot-p.gif" alt="1 Chilli"  title="1 Chilli" /></a></div><div class="chilli"><a href="javascript:;" onclick="vote(2); return false;"><img id="chillis2" src="../img/vote/hotter-s.gif" alt="2 Chillis" title="2 Chillis" /></a></div><div class="chilli unselectable"><a href="javascript:;" onclick="vote(3) return false;"><img id="chillis3" src="../img/vote/hottest-p.gif" alt="3 Chillis" title="3 Chillis" /></a></div><div id="submitmessage"></div><div id="message"><a href="javascript:;" onclick="clearvote(); return false;"><img id="clearvotefield" src="../img/vote/cancel-n.gif" alt="...doch keine Stimme vergeben." title="...doch keine Stimme vergeben." /></a></div>');
  }

  if(threepoints==cur_id){
    $('#voting').html('<div class="chilli unselectable"><a href="javascript:;" onclick="vote(1); return false;"><img id="chillis1" src="../img/vote/hot-p.gif" alt="1 Chilli"  title="1 Chilli" /></a></div><div class="chilli unselectable"><a href="javascript:;" onclick="vote(2); return false;"><img id="chillis2" src="../img/vote/hotter-p.gif" alt="2 Chillis" title="2 Chillis" /></a></div><div class="chilli"><a href="javascript:;" onclick="vote(3); return false;"><img id="chillis3" src="../img/vote/hottest-s.gif" alt="3 Chillis" title="3 Chillis" /></a></div><div id="submitmessage"></div><div id="message"><a href="javascript:;" onclick="clearvote(); return false;"><img id="clearvotefield"" src="../img/vote/cancel-n.gif" alt="...doch keine Stimme vergeben." title="...doch keine Stimme vergeben." /></a></div>'); 
  }   
}