$(document).ready(function(){
    /* ---- speakerscroller js ---- */
    $(".speakernavi a").click(function() {
        // dont return speakernavi href
        return false;
    });
    // add container for the hover name
    $(".speaker").append("<div class='namebox'></div>");
    // scrolling scrolling
    $("#speakerscroll").scrollable({         
       size: 7,
       nextPage: ".nextsp",
       prevPage: ".prevsp",
       items: "#scrollArea",
       navi: ".speakernavi",
       activeClass: "activesp"
   });
   // show name of the speaker on mouseover
   $(".speaker img").hover(function() {
        // get the name
        var spname = $(this).parent("a").next("div").find("h2").html();
		// insert line break between the first and last name
		spname_arr = spname.split(" ");
		var lastname = "<br />" + spname_arr.pop();
		var lastname_arr = lastname.split("-");
		lastname = lastname_arr.join("-<br />");
		var firstname = spname_arr.join(" ");
		spname = firstname + lastname;
        // show the name in the correct position
        $(this).parent("a").next().next(".namebox").html(spname).show();
   },function() {
        // hide the name on mouseout
        $(this).parent("a").next().next(".namebox").html("").hide();
   });

	//show random tab in the scroller
	var tab_width = 504; //@How wide the tab-area is in pixels
	var speaker_width = 72; //@How wide is the speaker image
	var speakers_shown = 7; //@How many speakers are shown at one time
	var number_of_speakers = $("div.speaker").length; //@How many speakers there are	
	var number_of_tabs = Math.ceil(number_of_speakers / speakers_shown); //@Number of tabs
	var random_id = Math.ceil(Math.random() * number_of_tabs); //@Random id that sets the shown tab
	var offset = random_id - 1; //@The offset of the speaker area
	var leftVal = -1 * tab_width * offset; //@The value of the margin-left -css
	var empty_speakers = speakers_shown - (number_of_speakers%speakers_shown);

	//if we are in the last tab, remove the possible empty speakers	
	if(random_id == number_of_tabs)
		leftVal += empty_speakers * speaker_width;

	//activate the correct tab link
	$("div.speakernavi a").removeClass("activesp");
	$("div.speakernavi a:contains('"+random_id+"')").addClass("activesp");

	//activate or deactivate the navigation
	if(random_id != 1)
		$("a.prevsp").removeClass("disabled");
	
	if(random_id == 4)
		$("a.nextsp").addClass("disabled");

	rnd_page = random_id - 1;// * speakers_shown;*/

	//move the scrollArea to the right position and show the scroller
	$("div#scrollArea").animate({ 
        left: leftVal
    },function(){

		$("div#loader").hide();

	});
	//$("div#scrollArea").css("left",leftVal);

   /* ---- end speakerscroller js ---- */

	/*APPS FOR DEMOCRACY*/
	
	//Add anchor link before the poll
	$("div.choices").before("<a name='poll'></a>");

	//Add anchor links to options
	$("div.choices label.option").each(function(e){ //div.choices div.form-radios 

		var label_cont_arr = this.innerHTML.split("> ");
		var label_cont = label_cont_arr.pop();
		var anchor_target_arr = label_cont.split(" - ");
		var anchor_target = anchor_target_arr[0];
		anchor_target = anchor_target.replace(/ /g,"_");
		anchor_target = anchor_target.toLowerCase();
		var anchor_link = " <a href='#"+anchor_target+"'>read more</a>";

		//this.innerHTML = label_cont_arr.join("") + anchor_link;
		$(this).after(anchor_link);


	});
});
