/* Png fix */
$(document).ready(function() {
	$("#leftMenu").pngFix();
	$(".hotspotRight").pngFix();
});
/* Search */
function searchSubmit(query, sortby,displayas,searchpage,category){
	var tmp = document.getElementById(query).value;
      var q = escape(tmp).replace("%27","%22");
	var submit_this = searchpage+'?opendocument&charset=ISO-8859-1&Query='+q+'&SearchMax=0&SortResultBy=';
	var tq = q; 
	if(isNaN(parseFloat(tq))){
		submit_this += sortby+'&SortResultByType='+displayas;
	}else{
		submit_this += 'Title&SortResultByType=Ascending';
	}
	top.location.href = submit_this;
	return false;
}
/* Union finder */
function unionFinder(vUrl) {
	if (vUrl != 'EMPTY') {
		vUrlVal = vUrl.split('##');
		$.cookie('buplmunicipality', '##'+vUrlVal[1], { expires: 3650, path: '/' });	
		document.location = vUrlVal[0];
	}
}
/* Union goto */
function unionGoto(vUrl){
	vUrlVal = vUrl.split('##');
	$.cookie('buplmunicipality', '##'+vUrlVal[1], { expires: 3650, path: '/' }); 
	return vUrlVal[0];
}
/* Fancy box - send to friend + login */
$(document).ready(function() {
	$("a.sendtofriend").fancybox({
		'hideOnContentClick': false,
		'frameWidth': 400,
		'frameHeight': 400
	 });
	$("a.sendtofriend").show();
	$("a#logonLink").fancybox({
		'hideOnContentClick': false,
		'frameWidth': 600,
		'frameHeight': 600
	 });
	$("a#logonLink").show();
});
/* Fancy box - union map */
function loadMap(linkname) {
	$(linkname).unbind('click');
	swfobject.embedSWF("/bupl/content.nsf/webressources/map.swf/$file/map.swf?openelement", "flashmap", "734", "612", "9.0.0", "/bupl/content.nsf/webressources/expressInstall.swf/$file/expressInstall.swf?openelement");
	$(linkname).fancybox({
		'frameWidth': 734,
		'frameHeight': 612
	});
	$(linkname).trigger('click');
}
$(document).ready(function() {
	$("a#flashmapbutton").bind('click', function() {loadMap("a#flashmapbutton")});
	$("a#flashmapbutton").show();
	$("a#flashmapbutton2").bind('click', function() {loadMap("a#flashmapbutton")});
	$("a#flashmapbutton2").show();
});
/* Fancy box - map municipality to union and set the cookie */
function flashMapSetCookie(cityAlias) {
	var map				= new Array();
/*Hovedstaden*/
map[0]					= 'Albertslund Kommune#albertslund#STORK\u00D8BENHAVN';
map[1]					= 'Ballerup Kommune#ballerup#STORK\u00D8BENHAVN';
map[2]					= 'Br\u00F8ndby Kommune#brondby#STORK\u00D8BENHAVN';
map[3]					= 'Drag\u00F8r Kommune#dragor#HOVEDSTADEN';
map[4]					= 'Frederiksberg Kommune#frederiksberg#HOVEDSTADEN';
map[5]					= 'Gentofte Kommune#gentofte#NORDSJ\u00C6LLAND';
map[6]					= 'Gladsaxe Kommune#gladsaxe#STORK\u00D8BENHAVN';
map[7]					= 'Glostrup Kommune#glostrup#STORK\u00D8BENHAVN';
map[8]					= 'Herlev Kommune#herlev#STORK\u00D8BENHAVN';
map[9]					= 'Hvidovre Kommune#hvidovre#STORK\u00D8BENHAVN';
map[10]					= 'H\u00F8je-Taastrup Kommune#hoje-taastrup#STORK\u00D8BENHAVN';
map[11]					= 'Ish\u00F8j Kommune#ishoj#STORK\u00D8BENHAVN';
map[12]					= 'K\u00F8benhavns Kommune#kobenhavn#HOVEDSTADEN';
map[13]					= 'R\u00F8dovre Kommune#rodovre#STORK\u00D8BENHAVN';
map[14]					= 'T\u00E5rnby Kommune#tarnby#HOVEDSTADEN';
map[15]					= 'Vallensb\u00E6k Kommune#Vallensbak#STORK\u00D8BENHAVN';
/*hovedstaden nordsjælland*/
map[16]					= 'Aller\u00F8d Kommune#allerod#NORDSJ\u00C6LLAND';
map[17]					= 'Egedal Kommune#egedal#NORDSJ\u00C6LLAND';
map[18]					= 'Fredensborg Kommune#fredensborg#NORDSJ\u00C6LLAND';
map[19]					= 'Frederikssund Kommune#frederikssund#NORDSJ\u00C6LLAND';
map[20]					= 'Fures\u00F8 Kommune#fureso#STORK\u00D8BENHAVN';
map[21]					= 'Gribskov Kommune#gribskov#NORDSJ\u00C6LLAND';
map[22]					= 'Halsn\u00E6s Kommune#halsnas#NORDSJ\u00C6LLAND';
map[23]					= 'Helsing\u00F8r Kommune#helsingor#NORDSJ\u00C6LLAND';
map[24]					= 'Hiller\u00F8d Kommune#hillerod#NORDSJ\u00C6LLAND';
map[25]					= 'H\u00F8rsholm Kommune#horsholm#NORDSJ\u00C6LLAND';
map[26]					= 'Lyngby-Taarb\u00E6k Kommune#lyngby-taarbak#NORDSJ\u00C6LLAND';
map[27]					= 'Rudersdal Kommune#rudersdal#NORDSJ\u00C6LLAND';
/*sydsjælland og falster ok*/
map[28]					= 'Faxe Kommune#faxe#SYD\u00D8ST';
map[29]					= 'Greve Kommune#greve#MIDTSJ\u00C6LLAND';
map[30]					= 'Holb\u00E6k Kommune#holbek#MIDTSJ\u00C6LLAND';
map[31]					= 'Kalundborg Kommune#kalundborg#MIDTSJ\u00C6LLAND';
map[32]					= 'K\u00F8ge Kommune#koge#SYD\u00D8ST';
map[33]					= 'Lejre Kommune#lejre#MIDTSJ\u00C6LLAND';
map[34]					= 'N\u00E6stved Kommune#nestved#SYD\u00D8ST';
map[35]					= 'Odsherred Kommune#odsherred#MIDTSJ\u00C6LLAND';
map[36]					= 'Ringsted Kommune#ringsted#SYD\u00D8ST';
map[37]					= 'Roskilde Kommune#roskilde#MIDTSJ\u00C6LLAND';
map[38]					= 'Slagelse Kommune#slagelse#MIDTSJ\u00C6LLAND';
map[39]					= 'Solr\u00F8d Kommune#solrod#MIDTSJ\u00C6LLAND';
map[40]					= 'Sor\u00F8 Kommune#soro#MIDTSJ\u00C6LLAND';
map[41]					= 'Stevns Kommune#stevns#SYD\u00D8ST';
map[42]					= 'Vordingborg Kommune#vordingborg#SYD\u00D8ST';
/*falster ok*/ 
map[43]					= 'Guldborgsund Kommune#guldborgsund#SYD\u00D8ST';
map[44]					= 'Lolland Kommune#lolland#SYD\u00D8ST';
/*syddanmark ok*/ 
map[45]					= 'Billund Kommune#billund#SYDJYLLAND';
map[46]					= 'Esbjerg Kommune#esbjerg#SYDJYLLAND';
map[47]					= 'Fan\u00F8 Kommune#fano#SYDJYLLAND';
map[48]					= 'Fredericia Kommune#fredericia#SYDJYLLAND';
map[49]					= 'Haderslev Kommune#haderslev#SYDJYLLAND';
map[50]					= 'Kolding Kommune#kolding#SYDJYLLAND';
map[51]					= 'S\u00F8nderborg Kommune#sonderborg#SYDJYLLAND';
map[52]					= 'T\u00F8nder Kommune#tonder#SYDJYLLAND';
map[53]					= 'Varde Kommune#varde#SYDJYLLAND';
map[54]					= 'Vejen Kommune#vejen#SYDJYLLAND';
map[55]					= 'Vejle Kommune#vejle#SYDJYLLAND';
map[56]					= 'Aabenraa Kommune#aabenraa#SYDJYLLAND';
/*fyn ok*/ 
map[57]					= 'Assens Kommune#assens#FYN';
map[58]					= 'Faaborg-Midtfyn Kommune#faaborg#FYN';
map[59]					= 'Kerteminde Kommune#kerteminde#FYN';
map[60]					= 'Langeland Kommune#langeland#FYN';
map[61]					= 'Middelfart Kommune#middelfart#FYN'
map[62]					= 'Nordfyns Kommune#nordfyns#FYN';
map[63]					= 'Nyborg Kommune#nyborg#FYN';
map[64]					= 'Odense Kommune#odense#FYN';
map[65]					= 'Svendborg Kommune#svendborg#FYN';
map[66]					= '\u00C6r\u00F8 Kommune#aro#FYN';       
/*nordjylland ok*/ 
map[67]					= 'Br\u00F8nderslev Kommune#bronderslev#NORDJYLLAND';
map[68]					= 'Frederikshavn Kommune#frederikshavn#NORDJYLLAND';
map[69]					= 'Hj\u00F8rring Kommune#hjorring#NORDJYLLAND';
map[70]					= 'Jammerbugt Kommune#jammerbugt#NORDJYLLAND';
map[71]					= 'Mariagerfjord Kommune#mariagerfjord#NORDJYLLAND';
map[72]					= 'L\u00E6s\u00F8 Kommune#laso#NORDJYLLAND';
map[73]					= 'Mors\u00F8 Kommune#morso#NORDJYLLAND';
map[74]					= 'Rebild Kommune#rebild#NORDJYLLAND';
map[75]					= 'Thisted Kommune#thisted#NORDJYLLAND';
map[76]					= 'Vesthimmerlands Kommune#vesthimmerland#NORDJYLLAND';
map[77]					= 'Aalborg Kommune#aalborg#NORDJYLLAND';
/*midtjylland ok*/ 
map[78]					= 'Herning Kommune#herning#MIDTVESTJYLLAND';
map[79]					= 'Holstebro Kommune#holstebro#MIDTVESTJYLLAND';
map[80]					= 'Ikast-Brande Kommune#ikast-brande#MIDTVESTJYLLAND';
map[81]					= 'Lemvig Kommune#lemvig#MIDTVESTJYLLAND';
map[82]					= 'Ringk\u00F8bing-Skjern Kommune#ringkobing-skjern#MIDTVESTJYLLAND';
map[83]					= 'Silkeborg Kommune#silkeborg#\u00D8STJYLLAND';
map[84]					= 'Skive Kommune#skive#MIDTVESTJYLLAND';
map[85]					= 'Struer Kommune#struer#MIDTVESTJYLLAND';
map[86]					= 'Viborg Kommune#viborg#MIDTVESTJYLLAND';            
map[87]					= 'Favrskov Kommune#favrskov#\u00D8STJYLLAND';
map[88]					= 'Hedensted Kommune#hedensted#\u00D8STJYLLAND';
map[89]					= 'Horsens Kommune#horsens#\u00D8STJYLLAND';
map[90]					= 'Norddjurs Kommune#norddjurs#\u00D8STJYLLAND';
map[91]					= 'Odder Kommune#odder#\u00D8STJYLLAND';
map[92]					= 'Randers Kommune#randers#\u00D8STJYLLAND';
map[93]					= 'Sams\u00F8 Kommune#samso#\u00D8STJYLLAND';
map[94]					= 'Skanderborg Kommune#skanderborg#\u00D8STJYLLAND';
map[95]					= 'Syddjurs Kommune#syddjurs#\u00D8STJYLLAND';
map[96]					= '\u00C5rhus Kommune#arhus#\u00C5RHUS';
/*bornholm ok*/
map[97]					= 'Bornholm Kommune#bornholm#BORNHOLM';
	for(i=0;i < map.length; i++){
		mapVal = map[i].split('#');
		if(mapVal[1]==cityAlias){
			mapRes = map[i];
			try {
				$.cookie('buplmunicipality', null, { domain: 'www.bupl.dk', path: '/' });
			} catch (e) {}
			$.cookie('buplmunicipality', mapRes, { expires: 3650, domain: '.bupl.dk', path: '/' });	
			return false;
			}
		}	
}
/* Union map - result handling */
function flashMapSelectCity(cityAlias, cityName) {
	$.fn.fancybox.close();
	flashMapSetCookie(cityAlias);
	// Selecting municipality on frontpage sends user to union frontpage
	// Variables placed on frontpage to handle this (subform: 'webLT_Content_nawLTFrontpage')
	if(isFrontpage) {
		unionAlias = $.cookie('buplmunicipality');
		unionAlias = unionAlias.split('#')
		unionAlias = unionAlias[2]
		for(i=0;i < unionUrls.length; i++){
			uuVal = unionUrls[i].split('#');
			if(uuVal[0]==unionAlias){
				window.location = uuVal[1];
			}
		}
	} else {
		window.location.reload();
	}
}
function answerQuickPoll(vform, setCookie) {
	vspot = vform.closest(".quickpoll");
	vspot.find(".qploading").show();
	vspot.find(".qpanswers").css('visibility', 'hidden');
	unid = vform.find("input[name='val1']").val();
	formaction = vform.attr('action');
	formValues = fixAjaxPost(vform.serialize());
	$.get(formaction, formValues, function(msg){
		vresp = msg.split('\n')[0];
		
		varr = trimArray(vresp.split(';'));
		for(x in varr) {
			iname = varr[x].split('=')[0];
			ivalue = varr[x].split('=')[1];
			if(iname == 'total') {
				$('.qpanswers').after('<p class="qptotal">'+ivalue+' har deltaget i denne afstemning</p>');
			} else {
				vform.find("#"+iname).replaceWith(ivalue+'%');
			}
		}
		vform.find(".QuickPollAnswer").replaceWith('0%');
		vspot.find(".qploading").hide();
		vspot.find(".qpanswers").css('visibility', 'visible');
	});
	// set cookie
	if (setCookie) {
		$.cookie('QuickPoll' + unid, 'YES', { expires: 3650, domain: '.bupl.dk', path: '/' });
	}
}
function trimArray(arr2){
	var arr1=new Array();
	for(j=0;j<arr2.length;j++)
		if(arr2[j]!="")
	arr1[arr1.length]=arr2[j];
	arr2.length=arr1.length;
	for(j=0;j<arr1.length;j++)
		arr2[j]=arr1[j];
	return arr2;
} 
