$(document).ready(function() {
    Cufon.replace('.HelveticaNeueLTStd-Lt');
    Cufon.replace('.HelveticaNeueLTStd-Md');

    $(".tabs").tabs();

    $("div#mostVisited").bind("mouseover", function() {
        $(this).addClass("mostVisitedCloudHover");
    }).bind("mouseout", function() {
        $(this).removeClass("mostVisitedCloudHover");
    });

    $("div#breadCrumbs").bind("mouseover", function() {
        $(this).addClass("breadCrumbsHover");
    }).bind("mouseout", function() {
        $(this).removeClass("breadCrumbsHover");
    });

    $("ul.mostVisitedCloud").each(function(index) {
        var maxm = 0;
        var items = [];

        $("li.mostVisitedCloudItem a", $(this)).each(function(index) {
            maxm += parseInt($(this).attr("rel"));
            items.push(this);
        });
        for (var i = 0; i < items.length; i++) {
            var m = parseInt($(items[i]).attr("rel"));
            var s = (150 * (1.0 + (1.5 * m - maxm / 2) / maxm)) / 100;
            $(items[i]).css("font-size", s + "em");
        };
    });
	$("form").bind("submit", function() {
		$(".inputText input[type=text]", $(this)).focus();
	});
    $(".inputText input[type=text]").bind("focus", focusContent).bind("blur", blurContent).blur();
    function focusContent() {
        var _me = $(this);
        if (_me.attr("value") == _me.attr("title")) {
            _me.attr("value", "");
            _me.removeClass("watermark");
        }
    };
    function blurContent() {
        var _me = $(this);
        if (_me.attr("value").replace(/\s/g, "") == "") {
            _me.attr("value", _me.attr("title"));
            _me.addClass("watermark");
        }
        else {
			_me.removeClass("watermark");
        };
    };
    $("div#topBanner").cycle({
		startingSlide: 0,
		timeout: 10000,
		pause: 1,
		random: 0
	});

    $("div#topBanner div.topBannerImagePage").cycle({
		startingSlide: 0,
		timeout: 10000,
		pause: 1,
		random: 0,
		slideExpr: "img"
	});


	
	
	$("table.kultunautAdvancedSearchFilterCategories").checkboxGroup();
	$("table.newsArchiveSearchFilterCategories").checkboxGroup();
	$("select.select").selectbox({
		inputClass: 'selectbox',
        containerClass: 'selectbox-wrapper',
        hoverClass: 'current',
        currentClass: 'selected',
        debug: false
    });
    
	$("div.pageIndex").accordion({
		active: ".pageIndexSelected",
		autoHeight: false,
		collapsible: true
	});
	$("ul.sitemap").treeview({
		collapsed: true,
		animated: "medium",
		prerendered: false
	});
	/*
	$('#search').autocomplete({ 
		serviceUrl:'http://autocomplete.searchimprove.com/autocomplete/autocomplete.aspx?callback=?',
		autoSubmit: true,
		minChars: 2,
		maxHeight: 120,
		deferRequestBy: 200,
		width: 258,
		highlight: true,
		params: {account_id:273531,package_id:883451229}
	  });
	*/
	
	/* Foreningsdatabase */
   $("select.foreningstypeSelecter").change(function()
   {
     var str ="";
     $("select.foreningstypeSelecter option:selected").each(function()
     {
		 str = $(this).val();
		 if(str =="alle")
		 {
		 str ="";
	 	 location = '?foreningstype=' + str;
	 	 }
	 	 else
	 	 {
	 	  location = '?foreningstype=' + str;
	 	 }
     });
   }); 
   
   
   	/* Foreningsdatabase */
   $("#createCsvLink").click(function()
   {
   
   $(this).val("Opdater CSV link");
   
     var str ="";
     var Forening="";
     var Institution ="";
     var Forsamlingshus ="";
     var Spotlight ="";
     var Medlem_FR ="";
     var Medlem_KS ="";
     var Folkeoplysning ="";
     var overgang_over_4aar ="";
     var Flag1 ="";
     var Flag2 ="";
     var linkString ="";
     
     $("select.foreningstypeSelecterCsv option:selected").each(function()
     {
		 str = $(this).val();
     });
     

		if($("input[name='Forening']").is(':checked')){
		Forening = $("input[name='Forening']").val();
		}
		else
		{
		Forening = "no";
		}

		if($("input[name='Institution']").is(':checked')){
			Institution = $("input[name='Institution']").val();
		}
		
		if($("input[name='Forsamlingshus']").is(':checked')){
			Forsamlingshus = $("input[name='Forsamlingshus']").val();
		}
		
		if($("input[name='Spotlight']").is(':checked')){
			Spotlight = $("input[name='Spotlight']").val();
		}
		
		if($("input[name='Medlem_KS']").is(':checked')){
			Medlem_KS = $("input[name='Forening']").val();
		}
		
		if($("input[name='Medlem_FR']").is(':checked')){
			Medlem_FR = $("input[name='Medlem_FR']").val();
		}
		
		
		if($("input[name='Folkeoplysning']").is(':checked')){
			Folkeoplysning = $("input[name='Folkeoplysning']").val();
		}
	
	
		if($("input[name='overgang_over_4år']").is(':checked')){
			overgang_over_4aar = $("input[name='overgang_over_4år']").val();
		}
		
		if($("input[name='Flag1']").is(':checked')){
			Flag1 = $("input[name='Flag1']").val();
		}
		
		if($("input[name='Flag2']").is(':checked')){
			Flag2 = $("input[name='Flag2']").val();
		}     


	linkString = "?foreningstype=" + str +"&amp;Forening="+ Forening + "&amp;Institution="+ Institution +"&amp;Forsamlingshus="+ Forsamlingshus +"&amp;Spotlight="+ Spotlight +"&amp;Medlem_KS=" + Medlem_KS+"&amp;Medlem_FR="+ Medlem_FR +"&amp;Folkeoplysning="+ Folkeoplysning +"&amp;overgang_over_4aar=" + overgang_over_4aar +"&amp;Flag1=" + Flag1 +"&amp;Flag2=" + Flag2;
		
		$(".foreningstypeSelecterCsvLink").hide();
		$(".foreningstypeSelecterCsvLink").fadeIn();
		$(".foreningstypeSelecterCsvLink").html("<br /><a href=\"http://www.aabenraa.dk/foreningsdatabase/csv+-+udtr%c3%a6k/foreninger"+ linkString +"\" target=\"_blank\">Download CSV fil</a><br /><br />");
   }); 



 $("input[name='SelectAllFields']").click(function() {

	if ($(this).attr('checked')) {
			   $(".checkFields input[type='checkbox']").attr('checked', 'checked');
			   $(".selectAllFieldsText").text("Afmarker alle");  
		}
		else
		{
		  $(".checkFields input[type='checkbox']").attr('checked', '');
		  $(".selectAllFieldsText").text("Marker alle");  
		}		
  });


/*Spotbox slide start*/

$("div.spotBoxSlide").hover(
  function () {
  $(".spotBoxSlideImage",this).animate({"top": "-=28px"}, "slow");
   $("h1.spotBoxHeader",this).animate({"top": "-=28px"}, "slow");
   $(".spotBoxSlideHolder",this).animate({"top": "-=91px"}, "slow");
  },
  function () {
	$(".spotBoxSlideImage",this).animate({"top": "+=28px"}, "slow");    
    $("h1.spotBoxHeader",this).animate({"top": "+=28px"}, "slow");
     $(".spotBoxSlideHolder",this).animate({"top": "+=91px"}, "slow");
  });

/*Spotbox slide end*/


/* Institution chooser start */
$(".chooserMenu").click(function(){

var marginCheck = $(".institutionChooser").css('margin-top');
if(marginCheck == "-120px")
{
	$(".institutionChooser").animate({"marginTop": "0px"}, {duration: 800, easing: "easeOutQuart"});
}
else
{
	 $(".institutionChooser").animate({"marginTop": "-120px"}, {duration: 800, easing: "easeOutQuart"});
}

});

 $("select.typeSelecter").change(function()
   {
     var str ="";
     $("select.typeSelecter option:selected").each(function()
     {
		 str = $(this).val();
		 if(str !="")
		 {
			 $("select.institutionSelecter").removeAttr("disabled");
				
				/*Reset*/
				 $("select.institutionSelecter option").hide();
				
				/*Selecter*/	 
				 $("select.institutionSelecter option").each(function()
					 {
						 var className = $(this).attr("class");
						 if(className ==str){ 
							 $(this).css("display", "block");
						 } 
					 });
	 	 }
	 	 else
	 	 {
			 $("select.institutionSelecter").attr("disabled","disabled");
	 	 }
     });
   }); 
   
       $("select.institutionSelecter").change(function()
   {
	   $("select.institutionSelecter option:selected").each(function()
		 {
			 url = $(this).attr("value");
			location = url; 
		 });
   });


/* Institution chooser end */

});

; (function($) {
	$.fn.checkboxGroup = function(options) {
        var o = { s: this.selector, c: this.context };
        var opts = $.extend({}, $.fn.checkboxGroup.defaults, options || {});
        return this.each(function() {
            initialize(opts, $(this));
        });
        o = null;
        opts = null;
    };
    function initialize(opts, $c) {
		$(opts.checkbox_all, $c).bind("click", function() {
			if ($(this).attr("checked") == true) {
				$(opts.checkbox, $c).attr("checked", true);
			}
			else {
				$(opts.checkbox, $c).attr("checked", false);
			};
		});
		$(opts.checkbox, $c).bind("click", function() {
			if ($(opts.checkbox, $c).size() == $(opts.checkbox + ":checked", $c).size()) {
				$(opts.checkbox_all, $c).attr("checked", true);
			}
			else {
				$(opts.checkbox_all, $c).attr("checked", false);
			};
		});
		if ($(opts.checkbox, $c).size() == $(opts.checkbox + ":checked", $c).size()) {
			$(opts.checkbox_all, $c).attr("checked", true);
		};
    };
    $.fn.checkboxGroup.defaults = {
        checkbox_all:		"input.checkbox_all[type=checkbox]",
        checkbox:				"input.checkbox[type=checkbox]"
    };
    
    
})(jQuery);