
var textresize_diff = 0;
var textresize_step = 2;
var textresize_max = 6;
var textresize_min = -2;
var textresize_tags = "#main H1,#main H2,#main H3,#main H4,#main H5,#main P,#main A,#main LI"



$(document).ready(function(){



	var poll_options = {
		target : '#kdm-poll',
		beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponse  // post-submit callback 
	};

	// bind to the form's submit event 
	$('FORM.kdm-poll').ajaxForm(poll_options);
	$('INPUT.kdm-poll').click(function(){
		$(this.form).ajaxSubmit(poll_options);
	});
	
	$('TEXTAREA.maxlength').each(function(){
		var obj = {
			defaultStr : $(this).val(),
			maxLength : parseInt($(this).attr("title")),
			textArea : this,
			noteArea : $('SPAN#' + $(this).attr("id") + '_ind')
		};
		var updateNote = function(elm){
			str = $(elm['textArea']).val().substr(0,200);
			$(elm['textArea']).val(str);
			charleft = elm['maxLength'] - str.length;
			$(elm['noteArea']).html(charleft + ' tecken kvar');
		}
		updateNote(obj);
		$(obj['textArea']).bind('keyup',function(){
			updateNote(obj);
		});
	});


	$("A.expandable")
		.click(function(){
		var subid = $(this).attr("value");
		//alert(subid);
		if($(this).hasClass("open")){
			$(this).removeClass("open");
			$("#" + subid).hide();
		}else{
			$(this).addClass("open");
			$("#" + subid).show();
		}
	});
	
	$("INPUT.search").focus(function(){
		if(this.value=="Sök..."){
			this.value = "";
		}else{
			this.select();
		}
	});
	$("A.search").click(function(){
		$("#site-search").submit();
		return false;
	});
	
	if($.cookie('textresize_diff')!=null){
		textresize_diff = parseInt($.cookie('textresize_diff'));
	}
	$("A.text_plus").click(function(){
		resizeText(textresize_diff+textresize_step);
		return false;
	});
	$("A.text_minus").click(function(){
		resizeText(textresize_diff-textresize_step);
		return false;
	});
	initText(textresize_diff);
});

function resizeText(value){
	textresize_diff = value<textresize_min?textresize_min:value>textresize_max?textresize_max:value;
	$.cookie('textresize_diff',textresize_diff, {path:'/'});
	$(textresize_tags).each(function(i){
		var size = parseInt($(this).attr("originalSize"));
		size += textresize_diff;
		$(this).css("fontSize",size);
	});
}
function initText(value){
	$(textresize_tags).each(function(i){
		$(this).attr("originalSize",parseInt($(this).css("fontSize")));
	});
	if(value!=0){
		resizeText(value);
	}
}


// pre-submit callback 
function showRequest(formData, jqForm, options) { 
    // formData is an array; here we use $.param to convert it to a string to display it 
    // but the form plugin does this for you automatically when it submits the data 
    var queryString = $.param(formData); 
 
    // jqForm is a jQuery object encapsulating the form element.  To access the 
    // DOM element for the form do this: 
    // var formElement = jqForm[0]; 
 
    //alert('About to submit: \n\n' + queryString); 
 
    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue 
    return true; 
} 
 
// post-submit callback 
function showResponse(responseText, statusText)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
 
    /*
    alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
        '\n\nThe output div should have already been updated with the responseText.'); 
        */
} 

