/* Globale vars */
var arVersion		= null;
var version			= null;

var isBelowIE7		= false;

// ------------------------------------

function changeHeight(id,h,dummy){ // Til at skalere .swf overskrifter
	$('#'+id).css('height',h+'px');
}

function setSelvbetjening(show){
	var sh = (show) ? 0 : 1;
	animatedcollapse.addDiv('selvbetjening-submenu','fade=0,speed=180,hide=1,persist=1');
	animatedcollapse.ontoggle=function($, divobj, state){ };
	animatedcollapse.init();	
}

// ------------------------------------

/* Topbar dropdown */
var timeout         = 500;
var closetimer		= 0;
var ddmenuitem      = 0;
var ddmenuitemdiv   = 0;
var imgtag			= null;
var origimg			= null;
var swapimg			= null;

function jsddm_open()
{	
	jsddm_canceltimer();
	jsddm_close();
		
	ddmenuitem = $(this).find('ul').eq(0).css('display', 'block');
	ddmenuitemdiv = $(this).find('div').eq(0).css('display', 'block');
	
	if (!isBelowIE7){
		imgtag = $(this).find('img').eq(0);
		origimg = imgtag.attr('origimg');
		swapimg = imgtag.attr('swapimg');	
	
		imgtag.attr("src", swapimg);	
	}
}

function jsddm_close()
{	if(ddmenuitem){
		
		ddmenuitem.css('display', 'none');
		ddmenuitemdiv.css('display', 'none');
		if (!isBelowIE7){
			imgtag.attr("src", origimg);	
		}
	}
}

function jsddm_timer()
{	closetimer = window.setTimeout(jsddm_close, timeout);}

function jsddm_canceltimer()
{	if(closetimer)
	{	window.clearTimeout(closetimer);
		closetimer = null;}}

$(document).ready(function()
{	
	arVersion = navigator.appVersion.split("MSIE")
	version = parseFloat(arVersion[1])
	
	if (version < 7.0 ){
		isBelowIE7 = true;		
		fixPng(); 
	}
	$('#jsddm > li').bind('mouseover', jsddm_open);
	$('#jsddm > li').bind('mouseout',  jsddm_timer);
	$('#jsddm > li > ul').css('display', 'none'); // Hide because of IE 6 bug
	$('#jsddm > li > div').css('display', 'none'); // Hide because of IE 6 bug

	/* auto image enlarge links/dialog */
	$('body').append('<div class="jqmWindow" id="jqm1"><table class="table-center" border="0"><tr><td class="jqmClose"><img id="image-auto-large" src="/images/s.gif" alt=""/><div id="image-auto-large-close-txt">Klik for at lukke</div></td></tr></table></div>');
	$(window).resize(imageAutoLargeScale);	
	$('.image-auto-click-enlarge').each(function() {
		$(this).removeAttr('width');$(this).removeAttr('height');
		$(this).after('<a class="link-auto-click-enlarge" href="javascript:void(0)" onclick="$(\'#image-auto-large\').attr(\'src\',\''+$(this).attr('src')+'\');$(\'#jqm1\').jqmShow();imageAutoLargeScale();">Klik for stor version</a>');
		$(this).wrap('<a class="link-auto-click-enlarge" href="javascript:void(0)" onclick="$(\'#image-auto-large\').attr(\'src\',\''+$(this).attr('src')+'\');$(\'#jqm1\').jqmShow();imageAutoLargeScale();"/>');
	});
	$('#jqm1').jqm();

	//overlay 2
	$('body').append('<div class="jqm-dialog" id="jqm2"><div id="jqm2content"></div></div>');
	$('#jqm2').jqm();
	
});

document.onclick = jsddm_close;

function swapImage(id){
	
	if (!isBelowIE7){
		
		var img		 	= $('#'+id).find('img').eq(0);
		var origimg 	= img.attr('origimg');
		var swapimg 	= img.attr('swapimg');
	
		var newimg = (img.attr('src') === origimg) ? swapimg : origimg;
		
		img.attr("src", newimg);
		
	}
	
}

// Addthis config
var addthis_config = { ui_click: true, services_compact: 'facebook,linkedin,live,google,favorites,twitter,email,print', services_expanded: 'facebook,linkedin,live,google,favorites,twitter,email,print', ui_use_addressbook: true, ui_cobrand: "AP Pension", data_track_linkback: true, data_use_cookies: true, data_use_flash: true };

function imageAutoLargeScale(){
  var maxWidth = $(window).width()-20; // Max width for the image
  var maxHeight = $(window).height()-30;    // Max height for the image

  $('#image-auto-large').each(function() {
	  var ratio = 0;  // Used for aspect ratio
	  $(this).css("width", ""); // Set new width
	  $(this).css("height", "");  // Scale height based on ratio
	  var width = $(this).width();    // Current image width
	  var height = $(this).height();  // Current image height
  
	  // Check if the current width is larger than the max
	  if(width > maxWidth){
	      ratio = maxWidth / width;   // get ratio for scaling image
	      $(this).css("width", maxWidth); // Set new width
	      $(this).css("height", Math.round(height * ratio));  // Scale height based on ratio
	      height = height * ratio;    // Reset height to match scaled image
	      width = width * ratio;    // Reset width to match scaled image
	  }
  
	  // Check if current height is larger than max
	  if(height > maxHeight){
	      ratio = maxHeight / height; // get ratio for scaling image
	      $(this).css("height", maxHeight);   // Set new height
	      $(this).css("width", Math.round(width * ratio));    // Scale width based on ratio
	      width = width * ratio;    // Reset width to match scaled image
	  }
      });
}

/* overlay 2 load */
function loadJqm2(url){
	$('#jqm2content').load(url,function(){$('#jqm2').jqmShow();});
}

/* Fold-out texts */
$(function() {
	$(".dd-head").click(function () {
		var ddhead = $(this);
		var ddheaddown = ddhead.hasClass("dd-head-down");
		if(!ddheaddown)
			ddhead.toggleClass("dd-head-down");
		$("#"+ddhead.attr("id").replace(/head/,"text")).slideToggle(400,function(){if(ddheaddown) ddhead.toggleClass("dd-head-down");});
	});
});

/* julekalender 2010 */
$(function(){
	if(typeof $.cookie != 'undefined'){
		if($.cookie('julekalender')){
			
			var url = window.location.href;
		
			if(typeof window.location.pathname != 'undefined' && window.location.pathname != '/' && window.location.pathname != ''){
				url+=(url.charAt(url.length-1)!='/'?'/':'')+'&template_id=1399';		
			} else {
				url+=(url.indexOf('?')!=-1?'&':'?')+'template_id=1399';		
			}
			//console.log(url);
			
			//cache buster
			url+='&_nocc=' + Math.random()*1000000;

			$.getJSON(url, null, function(data) {				
	  			if(typeof data == 'object'){
					if(data.is_active_on_doc == 1){
						//background
						$('body').addClass('jul10bg');
						
						/* for www.appension.dk */
						$('#main-content-area-right-column').css({overflow:'visible'});
						$('#main-content-area-right-column').prepend($('<div id="jul10wrap"><div id="jul10swf"></div></div>'));
						/* for update.appension.dk */											
						$('#content-block-right-column').css({overflow:'visible'});
						$('#content-block-right-column').prepend($('<div id="jul10wrap"><div id="jul10swf"></div></div>'));
						var flashvars = {
							date: data.date,
							user_id: $.cookie('user_id'),		
							has_answered: data.has_answered		
						};
						var flashparams = { menu: "false", wmode: "transparent", scale: "noScale", allowScriptAccess: "always" };
						swfobject.embedSWF("/swf/Julekalender.swf?cachebuster="+Math.random()*1000000000000, "jul10swf", "280", "320", "9.0.0", null, flashvars, flashparams);
					}
				}	
			});		
		}
	}
});


