$(document).ready(function(){
	hs.graphicsDir = '/images/highslide/';
    hs.outlineType = 'rounded-white';
    hs.outlineWhileAnimating = true;
	
	$(".buttonOrangeRight").bind("click", function(){
		var use_id = "#but"+$(this).attr("id");
		var picture = "#picturebut"+$(this).attr("id");
		$(".boxRight").slideUp("slow");
		$(".hidden").hide();
		$(".visible").show();
		if ( $(use_id).css("display") == "none" ){
			$(use_id).slideDown("slow");
			$(picture).hide();
			$(picture+"Hover").show();
		}else{
			$(use_id).slideUp("slow");
			$(picture+"Hover").hide();
			$(picture).show();
		}
	});
	
	
	$('a.highslide').click(function() {
	   return hs.expand(this, { outlineType: 'rounded-white' });
	});
	
	$("#displayComments").bind("click", function(){
		if ($("#commentsForm").css("display") == "none"){
			$("#commentsForm").slideDown("slow");
		}else{
			$("#commentsForm").slideUp("slow");
		}
	});
	
	$(function() {
		$( "#datepicker" ).datepicker({
			onSelect: function(dateText) { 
				dateText = str_replace(dateText,"/", "-") ;
				window.location = "/news/archive/"+dateText;
			}
		});
	});
	
	$(".button").bind("click", function(){
		var use_id = $(this).attr("id");
		$(".button").show();
		$(".buttonActive").hide();
		$(".hiddenMenu").hide();
		$(this).hide();
		$("#active_"+use_id).show();
		$("#menu_"+use_id).show();
	});

		
	$(".slide_middle").jCarouselLite({
		auto: 8000,
		speed: 1000,
		btnNext: ".nextHor",
		btnPrev: ".prevHor",
		scroll: 1,
		visible: 2
	});
	
	$(".slideNews").bind("click", function(){
		var use_id = $(this).attr("id");
		//alert(use_id);
		$("#leadingImage").fadeOut("slow");
		$("#leadingImage").attr("src", $("#image"+use_id).text());
		$("#leadingImage").fadeIn("slow");
		
		$("#leadingDescription").fadeOut("fast");
		$("#leadingDescription").empty();
		$("#leadingDescription").append($("#description"+use_id).html());
		$("#leadingDescription").fadeIn("slow");
		
		$("#leadingHeading").fadeOut("fast");
		$("#leadingHeading").empty();
		$("#leadingHeading").append($("#heading"+use_id).html());
		$("#leadingHeading").fadeIn("slow");
		
		$("#leadingSubHeading").fadeOut("fast");
		$("#leadingSubHeading").empty();
		$("#leadingSubHeading").append($("#subheading"+use_id).html());
		$("#leadingSubHeading").fadeIn("slow");
		
		$("#leadingHeading").attr("href", $("#link"+use_id).text());
		$("#leadingHeading").fadeIn("slow");
		
		
	});
	
	$("#darkGreyMenu > a").bind("mouseenter", function(){
		var index = $("#darkGreyMenu > a").index(this);
		$(".hiddenMenu").slideUp("slow");
		$("#men"+index).slideDown("slow");
		$("#darkGreyMenu > a").removeClass("darkGreyMenuActive");
		$(this).toggleClass("darkGreyMenuActive");
	});
	
	$(".linkNews").bind("mouseenter", function(){
		var news_id = $(this).attr("id");
		var news_idName = $(this).attr("name");
		$(".n"+news_id+"Holder").hide();
		var hideItem = $("#content"+news_idName).html();
		$("#news"+hideItem).hide();
		
		$("#content"+news_idName).empty();
		$("#content"+news_idName).append($("#n"+news_id).attr("name"));
		//$("#showN"+news_id+" > div:first").show();
		$("#n"+news_id).slideDown("slow");
	});
	
	$("#AddComment").bind("click", function(){
		$("#commentsForm").slideDown("slow");
	});

	showPoll();
	$("#votePoll").bind("click", function(){
		var answer_id = $("input[name=poll_option]:checked").val();
		var question_id = $("input[name=question_id]").val();
		
		showPoll();
		
		if ( answer_id && question_id ){
			$.ajax({
				type: "POST",
				url: "/vote.php",
				data: "answer_id="+answer_id+"&question_id="+question_id+"&poll=1",
				success: function(html){
					if (html == 1){
						getPollResults(question_id);
					}else{
						$("#pollMessage").empty();
						$("#pollMessage").append(html);
						$("#pollMessage").fadeIn("slow");
					}
					showPoll();
				}
			});
		}
	});
	
	
	$(".leftArrow").bind("click", function(){
		var select = "#new"+$(this).attr("id");
		var select2 = "#ew"+$(this).attr("id");
		$(".newsFP").hide();
		$(select).show();
		$(".boxRight").css("font-weight", "normal");
		$(".boxRight").css("background-color", "#f2f2f2");
		$(select2).css("font-weight", "bold");
		$(select2).css("background-color", "#dcdcdc");
		/*sIFR.replace(myriadregular, {
			selector: '.headingBlue',
			wmode: 'transparent',
			css: ['.sIFR-root { font-size: 24px; color: #3b81d6;}'
				,'a { color: #3b81d6; text-decoration: none; }'
				,'a:link { color: #3b81d6; }'
				,'a:hover { color: #3b81d6; }'
				,'a:visited { color: #3b81d6; }'
			]
		});*/
	});

	$(".rightArrow").bind("click", function(){
		var select = "#ne"+$(this).attr("id");
		var select2 = "#e"+$(this).attr("id");
		$(".newsFP").hide();
		$(select).show();
		$(".boxRight").css("font-weight", "normal");
		$(".boxRight").css("background-color", "#f2f2f2");
		$(select2).css("font-weight", "bold");
		$(select2).css("background-color", "#dcdcdc");
		/*sIFR.replace(myriadregular, {
			selector: '.headingBlue',
			wmode: 'transparent',
			css: ['.sIFR-root { font-size: 24px; color: #3b81d6;}'
				,'a { color: #3b81d6; text-decoration: none; }'
				,'a:link { color: #3b81d6; }'
				,'a:hover { color: #3b81d6; }'
				,'a:visited { color: #3b81d6; }'
			]
		});*/
	});
	

	/*
	$(".boxRight").bind("click", function(){
		var select = "#n"+$(this).attr("id");
		$(".newsFP").hide();
		$(select).show();
		$(".boxRight").css("font-weight", "normal");
		$(".boxRight").css("background-color", "#f2f2f2");
		$(this).css("font-weight", "bold");
		$(this).css("background-color", "#dcdcdc");
		sIFR.replace(myriadregular, {
			selector: '.headingBlue',
			wmode: 'transparent',
			css: ['.sIFR-root { font-size: 24px; color: #3b81d6;}'
				,'a { color: #3b81d6; text-decoration: none; }'
				,'a:link { color: #3b81d6; }'
				,'a:hover { color: #3b81d6; }'
				,'a:visited { color: #3b81d6; }'
			]
		});
	});
	*/
	$("#votePoll").bind("click", function(){
		var answer_id = $("input[name=poll_option]:checked").val();
		var question_id = $("input[name=question_id]").val();
		
		showPoll();
		
		if ( answer_id && question_id ){
			$.ajax({
				type: "POST",
				url: "/vote.php",
				data: "answer_id="+answer_id+"&question_id="+question_id+"&poll=1",
				success: function(html){
					if (html == 1){
						getPollResults(question_id);
					}else{
						$("#pollMessage").empty();
						$("#pollMessage").append(html);
						$("#pollMessage").fadeIn("slow");
					}
					showPoll();
				}
			});
		}
	});
	
	$('a.highslide').click(function() {
		return hs.expand(this,	{ outlineType: 'rounded-white' });
	});
	


	
});

function getPollResults(question_id){
	$.ajax({
		type: "POST",
		url: "/voteResults.php",
		data: "question_id="+question_id,
		success: function(html){
			$("#pollHolder").slideUp("slow");
			$("#pollResults").empty();
			$("#pollResults").append(html);
			$("#pollResults").slideDown("slow");
			$(".pollResultBar").each(function(i){
				$(this).animate({width: $(this).attr("name")});
			});
			showPoll();
		}
	});
}

function showPoll(){
	$("#displayPoll").bind("click", function(){
		getPollResults($("input[name=question_id]").val());
		$("#pollHolder").slideUp("slow");
		$("#pollResults").slideDown("slow");
	});
	$("#showPoll").bind("click", function(){
		$("#pollHolder").slideDown("slow");
		$("#pollResults").slideUp("slow");
	});
}
function str_replace(haystack, needle, replacement) {
	var temp = haystack.split(needle);
	return temp.join(replacement);
}

