Array.prototype.sum = function(){
	for(var i=0,sum=0;i<this.length;sum+=this[i++]);
	return sum;
}
Array.prototype.max = function(){
	return Math.max.apply({},this)
}
Array.prototype.min = function(){
	return Math.min.apply({},this)
}



/**
 * Legt ein Default-Value fuer eine Textbox fest.
 * Wird auf die Textbox geklickt, die den Default-Value enthaelt, wird dieser aus der 
 * Textbox geloescht. Wird die Textbox ohne Eingabe verlassen, wird der Inhalt wieder
 * auf den Default-Value zurueckgesetzt.
 *
 * @param value string Default-Wert der Textbox
 * @param options Object
 *                  - class : CSS-Klasse der Textbox die den Default-Value enthaelt
 *
 * @version 1.0
 */
$.fn.setDefaultValue = function(options)
{
    var options = $.extend(
    						{ 
								css : 'txt_default'
							}, options);
    
    return this.each(function()
    {
        var $this = $(this);
        
        var value = $this.attr('value');
        
        $(this).attr('autocomplete','off');
        
        if($this.val().length <= 0 || $this.val() == value)
        {
            $this.addClass(options.css);
            $this.val(value);
        }
        
        $this.click(clear);
        
        $this.blur(fill);
        
        $this.focus(clear).blur();
        
        function clear()
        {
            if($this.hasClass(options.css) || $this.val() == value)
            {
                $this.val('');
                $this.removeClass(options.css);
            }
        }
        
        function fill()
        {
            if($this.val().length <= 0)
            {
                $this.addClass(options.css);
                $this.val(value);
            }
        }
        $(window).unload(clear); // Firefox-Autocomplete
    });  
};
/* End of input plugin */



/* Slider function */
$.fn.slider = function(options)
{    

    var options = $.extend(
    { 
        buttons : ".left-side .buttons button",
        duration : 600,
        easing: "easeOutSine"
        
    }, options);
    						
    						
    return this.each(function()
    {
        var $this = $(this);
        
        var contentsize = $this.find(".content > div").size();
            
        var contentwidth = $this.find(".content").width();
            
        var contentwidth = contentsize * contentwidth;
            
        var sliderwidth = $this.width();
            
        $this.find(".content").width(contentwidth);
            
        $this.scrollLeft("0").attr("slide", "1");
        
        function sliderer(direction) {
            
            if (direction == "right") {
                   
               var slide = $this.attr("slide");
                   
                 if (slide < contentsize) {
                           
                   $this.attr("slide", parseFloat(slide) + 1);
                           
                   $this.animate({scrollLeft: "+=" + sliderwidth},{queue:false, duration:options.duration, easing:options.easing});
                           
                 }
                       
                 else { 
                         
                   $this.attr("slide", "1");
                       
                   $this.animate({scrollLeft: "0"},{queue:false, duration:options.duration, easing:options.easing}); 
                       
                  }
                
              }
                
              else {
                    
                   $this.attr("slide", parseFloat(slide) - 1);
                    
                   $this.animate({scrollLeft: "-=" + sliderwidth},{queue:false, duration:options.duration, easing:options.easing});
                
               }
        
        }
        
        //set_slideshow = setInterval(sliderer("right"), 4000);
        
        $(options.buttons).click(function() {
        
            var slide = $this.attr("slide");
                
            var direction = $(this).attr("class");
            
            sliderer(direction)
               
          });
        
        
    });  
};

/* End of slider function */




$(document).ready(function() {

    
    $("table").attr({cellpadding: "0", cellspacing: "0"});
    
    $("input[type~='text']").setDefaultValue();
   
    //change the shape of special buttons
    $("a.button.style, a.article-type.style").each(function() {
   
       $(this).wrapInner('<div class="middle" />');
       $(this).prepend('<div class="left" />');
       $(this).append('<div class="right" />');
       
       $(this).removeClass("style");
   
   });
   
   
   /* Start tab function */
   
  // $(".nav").nav-tabs(".nav-tabs > div");
  
  $(".nav-tabs > div").each(function() {
  
  	$(this).find(".row:last").addClass("last");
  
  });
  
   	 $(".container .nav li").live("mouseover", function() {
	       
		     var container = $(".container");
		     
		      container.find(".nav-tab").appendTo(".container .nav-tabs");
		      
		      container.find(".nav-tab .row").removeAttr("style");
		      container.find(".nav-tab").removeClass("nav-tab");
		       
		       $(".selectback").remove();
		       
		       $(".nav .current").removeClass("current");
		       $(this).addClass("current");
		   
		       var width = $(this).outerWidth(true);
		       var height = $(this).outerHeight(true);
		       
		       var offset = $(this).offset();
		       var position = $(this).position();
		       
		       var classit = $(this).attr("class").split(" ");
		       
		       //$(".selectback").css({"width": width + 2, "height": height + 3, "left": position.left - 1, "top": -4, display: "block" });
		       //$(".selectback div").css({"width": width + 2, "height": height + 3})
		       
		       var tabclass = ".nav-tabs ." + classit[0];
		       
		       $(tabclass).addClass("nav-tab").css({"top": (position.top + height + 80), "display": "block", "left": position.left}).appendTo(".container");
		       
		       container.find(".nav-tab").append("<div class='selectback'><div></div></div>");
		       $(".selectback").css({"width": width + 2, "height": height + 3 });
		       $(".selectback div").css({"width": width + 2, "height": height + 3});
		       
		       var rowswidth = [];
		       var rowsheight = [];
		       
		      //console.log("1: " + rowswidth)
		       
		       $(".nav-tab .row").each(function() {
		       
		       	var thiswidth =	$(this).outerWidth();
		       	var thisheight = $(this).height();
		       	
		       	//console.log("2: " + thiswidth)
		       	
		       	rowswidth.push(thiswidth);
		       	rowsheight.push(thisheight);
		       
		       });
		       
		      //console.log("3: " + rowswidth)
		       
		       var rowswidth = rowswidth.sum();
		       var rowsheight = rowsheight.max();
		       
		      //console.log(rowswidth);
		       
		       //console.log((rows + 50))
		       
		       $(".nav-tab").width(rowswidth + 50);
		       $(".nav-tab .row").height(rowsheight);
		       
		       var posright = ( $(".nav-tab").offset().left );
		       
		       
		       
		      // $(".container .nav li").die("mouseover");
	       
	       
	       
	   });
   
   
   $("body").click(function() {
   		
   		$(".container .nav-tab").appendTo(".container .nav-tabs");
   			
   			$(".nav").find(".current").removeClass("current");
   			
   			$(".selectback").removeAttr("style");
   			
   			$(".selectback").remove();
   			
   			//mouseover();
   		
   });
   
   
   $(".nav-tab, .nav-tabs").live("mouseleave", function() {
   
  	    	$(".container .nav-tab").appendTo(".container .nav-tabs");
   	    	
   	    	$(".nav").find(".current").removeClass("current");
   	    	
   	    	$(".selectback").removeAttr("style");
   	    	
   	    	$(".selectback").remove();
   	    	
   	    	//mouseover();
   	
   
   
   });
   
   $(".nav-tab *, .nav-tabs *, .container .nav *").click(function(event){
   
   		event.stopPropagation();
   
   });
   
   
   $(".nav-tab, .nav-tabs .row *").live("mouseleave", function(event) {
   		
   		event.stopPropagation();
   
   });
   
   var allElements = $('.right-side .deildirnar dl'),
       WRAP_BY = 3;
   for (var i = 0; i < allElements.length; i += WRAP_BY) {
       //first loop, elements 0 : 20, next loop elements 20:40 and so on
       allElements.slice(i, i + WRAP_BY).wrapAll('<div class="wrap" />');
   }
   
   /* Eftirfarandi kóði er til að rappa saman */
   var allElements = $('.articles .article'),
          WRAP_BY = 3;
      for (var i = 0; i < allElements.length; i += WRAP_BY) {
          //first loop, elements 0 : 20, next loop elements 20:40 and so on
          allElements.slice(i, i + WRAP_BY).wrapAll('<div class="wrap" style="clear:both" />');
      }
      
   
    
/* End of Tab functon */

    $(".slider .content").bxSlider();
    
    $(".milli .container .right-side .form .tab").click(function() {
    
        var rel = $(this).attr("rel");
        
        $(".milli .container .right-side .form .tab.active, .milli .container .right-side .form form.active").removeClass("active", 1000);
        
        var id = ".milli .container .right-side .form form#" + rel;
        
        $(id).addClass("active", 1000);  $(this).addClass("active", 1000);
    
    });
    
    
    
    
    
    /* script for styling radio buttons */
    
    //$('input[type="checkbox"]').button();
    
    /*
        $("input[type=radio]").each(function() {
            
            var check = $(this).is(':checked');
            
            var id  = ( Math.floor ( Math.random ( ) * 1000000 + 1 ) );
            
            if (check == true) {
            
                $(this).before("<span class='radio checked' id='"+ id +"'></span>");
            }
            
            else {
            
                $(this).before("<span class='radio' id='"+ id +"'></span>");
            }
            
            $(this).attr("id", id).css("display", "none");
            
        });
        
        $("span.radio").click(function() {
                  
            $(this).parents(".amount").find(".value input[type=radio]").attr('checked', false);
            
            $(this).parents(".amount").find(".value .radio").removeClass("checked");
            
            $(this).addClass("checked");
                 
            $(this).parents(".value").find("input[type=radio]").attr('checked', true);
          
        }); 
        
        
        
    //create span for checkboxes and then a loop to click on and off the checkboxes under them
        $("checkbox, input[type=checkbox]").each(function() {
            
            var check = $(this).prop('checked');
            
            var id = ( Math.floor ( Math.random ( ) * 1000000 + 1 ) );
            
            
            if (check == true) {
            
                $(this).before("<span class='checkbox checked' id='"+ id +"'></span>");
            }
            
            else {
            
                $(this).before("<span class='checkbox' id='"+ id +"'></span>");
            }
            
            $(this).attr("id", id).addClass(id).css("display", "none");
            
        });
        
        $("span.checkbox").click(function() {
        
            var id = $(this).attr("id");
            
            input = "input[id='"+ id +"']";
        
            
             if ($(this).hasClass("checked") != true) {
             
                 $(this).addClass("checked");
                 $(input).attr('checked', true);
             
             }
             
             else {
             
                 $(this).removeClass("checked");
                 $(input).attr('checked', false);
    
             }    
        });    
    
       */ 
       
       
     
     
     /* Using plugin for dialogs */
     
     $(".milli div.container div.right-side div.form form#mannvinur.active a.darkbutton").click(function() {
         
       $.post('../redcross/dialog/millifaera.php', function(data) {
               
               $.blockUI({ message:data});
               
               $(".close").click(function(){
                   
                   $.unblockUI()
               
               
               });
        });
      });   
      
      
      /* Code for the gallery */
      
      $(".gallery").each(function() {
          
          
          
            //var $.this = $(this);
              
              /*
              
              $.this.find(".main-image").css("opacity", "0");
              
              $.this.find(".main-image:first-child").css("opacity", "1").addClass("current");
              
              var mainsize = $.this.find(".main-image").size();
              
              $.this.find(".main-image").each(function(n) {
              
                 $(this).attr("id", n + 12); 
              
              });
              
              
              //add images to the navigation from the gallery
              
              
              $.this.find(".main-image").each(function() {
              
                  var src = $(this).find("img").attr("src");
                  var id = $(this).attr("id");
                  
                  $.this.find(".gallery-nav .images").append("<img src='"+ src +"' rel='"+ id +"'");
              
              });
              
              
              
              
              $.this.find(".gallery-nav .images img:first-child").addClass("current");
              
             
              
              //group the image navigation icons into groups 
              var allElements = $.this.find(".gallery-nav .images img"),
                  WRAP_BY = 5;
              for (var i = 0; i < allElements.length; i += WRAP_BY) {
                  //first loop, elements 0 : 20, next loop elements 20:40 and so on
                  allElements.slice(i, i + WRAP_BY).wrapAll('<div class="group" />');
              }
              
               */
              
              //set the width of the images navigation
              $(this).find(".gallery-nav .images").each(function(n){
              
                 // var howmany = $(this).find(".group").size();
                  
                 // var howbig = $(this).find(".group").outerWidth(true);
                 
                 var howmany = $(this).find("a").size();
                  
                 var howbig = $(this).find("a").outerWidth(true);
                  
                  var width = howmany * howbig;
                  
                  $(this).width(width);
              
              });
              
              $(this).find(".gallery-nav .images img").each(function(n){
                  
              
              });
              
             
              
              
              /*
              $.this.find(".gallery-nav .images img").click(function(){
                  
                  var imageid = $(this).attr("rel");
                  
                  $.this.find(".main-image.current").animate({
                      opacity: 0
                    }, 500, function() {
                    
                      var imgthis = $(this);  
                      
                      var classid = ".main-image#" + imageid;
                      
                     //console.log(classid)
                      
                      $.this.find(classid).animate({
                          opacity: 1,
                        }, 500, function() {
                        
                          imgthis.removeClass("current");
                          $(this).addClass("current");
                      
                        });                      
                    });
                  
                  
              
              
              
              });
              
              */
          
      
      });
      
        
      
 

});


$(window).load(function(){

   $("body").animate({
       opacity: 1,
     }, 500, function() {
       // Animation complete.
     });
   

});


