﻿var se_dl2_Banner_CUSTOMER_ID = "DAMedia-MLUK-8KSH68";
var se_dl2_Banner_SITE_ID = "SITE_MLUK-8NJQWW"; 

$(function(){
	var $modComments = $('#modComments, #modDebatt'),
		$pollFormView = $('.pollform_view_results'),
		$pollform = $pollFormView.closest('form'),
		$info = $('div.info');
		


	//hide comments initially
	if ( $modComments.find('input:first').val() === '' ) {
		$modComments.hide();
	} else {
		$('a.comment-toggle').addClass('up');
	}
	
	//webbfraga - visa resultat:
	if ( $pollFormView.length ){
		$pollFormView.fancybox({
			'hideOnContentClick': true,
			'width':370,
			'autoDimensions': false
		});
	}
	
	//kommentarer - toggla
	$('.comment-toggle').click(function(e){
		var $this = $(this);
		e.preventDefault();
		$modComments.toggle('fast');
		if ( $this.hasClass('up') ){
			$this.removeClass('up');
		} else {
			$this.addClass('up');
		}
	});

	//commentcount pluralandelse och 0 kommentarer
	$('.comment').each(function(){
		var $this = $(this),
			$a = $this.find('a'),
			html = $a.html();
		if ( html === '1 kommentarer' ){
			$a.html('1 kommentar');
		} else if ( html === '0 kommentarer' ){
			$this.remove();
		} 
	});
	
	
	//submitting poll with ajax:
	$pollform.bind( 'submit', function (e){
		var days_into_future = 30,
			pollkey = 'p3poll_' + $('#pollkey').val(),
			cookieexpires = new Date().getTime() + days_into_future * 24 * 60 * 60 * 1000,
			strexpires = new Date(cookieexpires).toGMTString(),
			path = 'path=/;';

		e.preventDefault();

		
		$.ajax({
			url: $pollform.attr('action'),
			data: $pollform.serialize() + '&ajax=true',
			type: 'POST',
			success: function( xml ){
				if ( $(xml).find('success').text() === 'true' ){
					document.cookie = pollkey + '=done;' + path + 'expires=' + strexpires;
					$pollFormView.trigger('click');
				} else {
					$pollFormView.trigger('click');
				}
			}, 
			error: function ( mess ){
				$pollFormView.trigger('click');
			}
		});
	
	});
	
	try {
		if (typeof(jQuery) != 'undefined') {
			if(typeof(updatePageCounter1798) == 'function') { 
				updatePageCounter1798(se_dl2_Banner_CUSTOMER_ID); 
			}
			// -- Insert these lines if the 140x350 Banner should be displayed on the site (START)  -->
			var se_dl2_Banner_BANNER_TYPE = "banner_140x350";
			if(typeof(getBannerInfo1798) == 'function') { 
				getBannerInfo1798(se_dl2_Banner_CUSTOMER_ID, se_dl2_Banner_BANNER_TYPE);
			}
			// -- 140x350 Banner (END)

			// -- Insert these lines if the 728x90 Banner should be displayed on the site (START)  -->
			var se_dl2_Banner_BANNER_TYPE = "banner_728x90";
				if(typeof(getBannerInfo1798) == 'function') { 
					getBannerInfo1798(se_dl2_Banner_CUSTOMER_ID, se_dl2_Banner_BANNER_TYPE);
				}
			// -- 728x90 Banner (END)
		}// if
	} catch( err ){}
	
	
	if ($('.info-window').length) {
		if ($('.info-window .related h4').html() === null && 
			$('.info-window .related ul li').html() === null && 
			$('.info-window .related p').html() === null) {
				$('.info-window .related').remove()
		}
	}

	//removing empty div.info:
	if ( $.trim($info.html()) === '' ){
		$info.remove();
	}
	
	//bildspel:
	$('div.p3-image-gallery a').fancybox({
		overlayColor: "#000",
		overlayOpacity: "0.8",
		titlePosition: 'over',
		cyclic: true,
		titleFormat: function(title) {
			if ( title === '' ){
				return '';
			}
		    return '<span id="fancy-bildtext-platta">' + title + '</span>';
		}
	});
	
	// Räknar antal tecken i Debattinlägg
	$('#dcomment, #comment').bind('keyup', function() {
		var $this = $(this),
			maxlength = $this.attr('id') === 'dcomment' ? 2000 : 500;
		var nrOfChars = $this.val().length;

		if ( nrOfChars > maxlength ) {
			$this.val($this.val().substring(0, maxlength)).focus();
			if ($this.get(0).setSelectionRange) {
			  $this.get(0).setSelectionRange(maxlength, maxlength);
			} else if ($this.get(0).createTextRange) {
			  var range = $this.get(0).createTextRange();
			  range.collapse(true);
			  range.moveEnd('character', maxlength);
			  range.moveStart('character', maxlength);
			  range.select();
			} 
			alert('Du får använda max ' + maxlength + ' tecken')
		}
	});
	
	
	
});
