﻿$(document).ready(function() {
	$('hr').each(function(){
		var obj = $(this);
		var cssStyle = obj.attr('style');cssStyle=(cssStyle?' style="'+cssStyle+'"':'');
		var cssClass = obj.attr('class');cssClass=' class="hr '+(cssClass?cssClass:'')+'"';
		obj.wrap('<div '+cssClass+cssStyle+' />');
		obj.removeAttr('class');
		obj.removeAttr('style');
	});
	
	$("input.round[type=\'submit\'],input.round[type=\'button\'],input.round[type=\'reset\']").each(function() {
		var obj = $(this);
		var css = 'margin:' + obj.css('marginTop') + ' ' + obj.css('marginRight') + ' ' + obj.css('marginBottom') + ' ' + obj.css('marginLeft') + ';';
		if (obj.css('float') != 'none1') { css = css + 'float:' + obj.css('float') + ';'; }
		$(this).wrap('<span class="round" style="' + css + ';" />');
		$(this).parent('span.round').addClass('round_button');
	});
	$("input.round[type=\'submit\']").each(function() {
		$(this).parent('span.round').addClass('round_button_mvr');
	});


	if (jQuery().corner && $.fn.corner != 'undefined') {
		$('a.button').corner({
			tl: { radius: 5 },
			tr: { radius: 5 },
			bl: { radius: 5 },
			br: { radius: 5 },
			antiAlias: true
		});

		$('span.round').corner({
			tl: { radius: 5 },
			tr: { radius: 5 },
			bl: { radius: 5 },
			br: { radius: 5 },
			antiAlias: true
		});
	}

	/*ensure that #body_m is the same height as the <body> tag */
	var docHeight = $(document).height();
	var viewHeight = $(window).height();

	//alert(docHeight);
	//alert(viewHeight);

	if(docHeight-15 > viewHeight){
		docHeight+=15;
	}else{
		docHeight = docHeight;
	}
	$('#body_m').height(docHeight);

//	$('#body_m').height(docHeight);
	//alert($('#body_m').height());

	/*update equal height columns*/
	setTimeout("equalHeightStart()",100);
});

function equalHeightStart(){
	var i=1;
	var l=10;
	for(i;i<=l;i++){
		equalHeight($('.ehg_'+i));
	}
//	group.each(function(){
//		equalHeight($(".equalHeight:not([rel])"));
//		equalHeight($(".equalHeight([rel='"+$(this).attr('rel')+"'])"));
//	});
}
function equalHeight(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
	//[rel='gallerybox']
}

function cancelEvent(e){
	if(!e) var e = window.event;
	try{
		e.returnValue = false;
		e.cancelBubble = true;
	}catch(err){}
	try{
		//e.cancelBubble is supported by IE - this will kill the bubbling process.
		if(e.returnValue){
			e.returnValue = false;
		}
		if(e.cancelBubble){
			e.cancelBubble = true;
		}
	}catch(err){}
	try{
		//e.stopPropagation works only in Firefox.
		if (e.stopPropagation) {
			e.stopPropagation();
			e.preventDefault();
		}
	}catch(err){}
}

//Extending jQuery with a better trigger function
(function($){
    //Attach to jQuery
    $.fn.extend({ 
		triggerLink: function(e, url) {
			if(e.target.tagName.toLowerCase() != 'a'){
				cancelEvent(e);
				if(e.ctrlKey){
					window.open(url,'a'+Math.random());
				}else if(e.shiftKey){
					window.open(url,'_blank','menubar=1,toolbar=1,status=1');
				}else{
					window.location = url;
				}
			}
        }
    });
     
//pass jQuery to the function, 
//So that we will able to use any valid Javascript variable name 
//to replace "$" SIGN. But, we'll stick to $ (I like dollar sign: ) )
})(jQuery);

function hasFocus(jqueryObject){
	return jqueryObject.hasClass('has-focus');
}

function fillDropDown(id, JSONdata, postUpdateFn) {
	if (id != null) {
		var jddl = $('#' + id);
		var ddl = jddl[0];
		var url = jddl.attr('data-url');
		var ie7 = (jQuery.browser.msie && parseInt(jQuery.browser.version) == 7) ? true : false;
		var currentVal = jddl.val();
		jddl.attr('data-sv', currentVal);

		//		var sel = eval(new String(jTargetDdl.attr('data-sv')));
		//		if (typeof (sel) == "undefined" || sel == '' || sel == '0') {sel = jTargetDdl.val();}
		//		if (sel != '') { jsonData += ',"selected":"' + sel + '"'; }

		jddl.addClass('updateing');
		ddl.options.length = 0;
		ddl.options.add(new Option('Opdatere...', currentVal));
		ddl.selectedIndex = 0;
		$.ajax({
			type: 'GET'
			, url: url + (ie7 == true ? '&ie7=true' : '')
			, data: jQuery.parseJSON(JSONdata)
			, dataType: 'html'
			, async: true
			, success: function (res) {
				eval(res);
				if (typeof (JSONres) != "undefined") {
					if (JSONres != null && typeof (JSONres.element) != 'undefined') {

						var jObj = $('#' + JSONres.element);
						var ddl = jObj[0];
						var dataDefault = jObj.attr('data-default');

						ddl.options.length = 0;
						if (typeof (JSONres.items) != 'undefined' && JSONres.items.length > 0) {
							var l = JSONres.items.length;
							var selectedIndex = 0;
							for (var i = 0; i < l; i++) {
								var opt;
								if (JSONres.items[i].text == '-' && JSONres.items[i].value == '-') {
									opt = new Option('------', '-');
									opt.disabled = true;
								} else {
									opt = new Option(JSONres.items[i].text, JSONres.items[i].value);
									if (eval(JSONres.items[i].selected)) {
										selectedIndex = i;
									}
								}
								ddl.options.add(opt);
							}
							ddl.selectedIndex = selectedIndex;
							ddl.disabled = (l == 1 && eval(JSONres.items[0].disabled));
							jObj.attr('data-sv', jObj.val());
							//alert('her');
							if (hasFocus(jObj)) { ddl.focus(); }
							//alert('her2');
						} else {
							ddl.disabled = true;
							ddl.options.add(new Option('Ingen data...', ""));
						}
						if (ddl.disabled) { jObj.addClass('disabled'); }
						else { jObj.removeClass('disabled'); }

						if (eval(postUpdateFn) == null) {
							$(ddl).change();
						} else {
							postUpdateFn(ddl);
						}
						jObj.removeClass('updateing');
					}
				}
			}
		});
	}
}
