/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
 
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

  
  
  
/*
 * jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
 *
 * Uses the built in easing capabilities added In jQuery 1.1
 * to offer multiple easing options
 *
 * TERMS OF USE - jQuery Easing
 * 
 * Open source under the BSD License. 
 * 
 * Copyright © 2008 George McGinley Smith
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of 
 * conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list 
 * of conditions and the following disclaimer in the documentation and/or other materials 
 * provided with the distribution.
 * 
 * Neither the name of the author nor the names of contributors may be used to endorse 
 * or promote products derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 *  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE. 
 *
*/

// t: current time, b: begInnIng value, c: change In value, d: duration
jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,{
  easeOutBounce: function (x, t, b, c, d) {
    if ((t/=d) < (1/2.75)) {
      return c*(7.5625*t*t) + b;
    } else if (t < (2/2.75)) {
      return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
    } else if (t < (2.5/2.75)) {
      return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
    } else {
      return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
    }
  }
});


$(document).ready(function(){

            //Toggle fold out divs
            var sliderHeight = 17;

            $('.puffcontentl').each(function () {
                var c = $(this);
                c.attr("box_h", c.height());
                var im = '#op_'+c.attr('id').split('_')[1];
                he=$.cookie(c.attr('id'));
                if(he!=null){
                    c.css("height", he+'px');
                    if(he>sliderHeight){
                            $(im).attr("src","/layouts/wilaria/arrow-down.png");
                    }else{
                            $(im).attr("src","/layouts/wilaria/arrow-right.png");
                    }
                }
            });

            $(".puffcontentl .puffheader").click(function(){
                var c = $(this).parent();
                var im = '#op_'+c.attr('id').split('_')[1];
                //alert(im);
                var open_height = c.attr("box_h");
                if (c.height()!=sliderHeight) {
                    $.cookie(c.attr('id'), sliderHeight);
                    c.stop().animate({height:sliderHeight+'px'},{queue:false, duration:600, easing: 'easeOutBounce'})
                    $(im).attr("src","/layouts/wilaria/arrow-right.png");
                }else{
                    $.cookie(c.attr('id'), open_height);
                    c.stop().animate({height:open_height+'px'},{queue:false, duration:600, easing: 'easeOutBounce'})
                    $(im).attr("src","/layouts/wilaria/arrow-down.png");
                        
                }
            });
            /* Set active link */
            highlight_active_sidebar_links();
            /* Footer stuff */
            setFooter();
            $('a.email').nospam({ replaceText: true });
      /* Fix footer if a radio button is clicked, which might load html that increase page height */
      $('input[type="radio"]').click(function() {
        setTimeout(setFooter,2000);
      });
    });
                          
function highlight_active_sidebar_links() {
  $("#ul_0 a").each(function(){
    var current_href = 'http://'+ window.location.host + $(this).attr("href");
    if (document.location == current_href) {
      $(this).parent().addClass("current");
    }
  });
  $("#menu a").each(function(){
    var current_href = 'http://'+ window.location.host + $(this).attr("href");
    if (document.location == current_href) {
      $(this).parent().addClass("menu_active");
    }
  });
}

size = new Array();
                 
function setFooter() {
  if (document.getElementById) {
    var windowHeight=$(window).height();
    if (windowHeight>0) {
      var contentHeight=obj_height('maincontainer')+obj_height('contentbtm');
      var footerElement=document.getElementById('footer');
      size['footer'] = footerElement.offsetHeight;
    size['ornaments'] = $('.ornament').height();
    if (windowHeight-(contentHeight+size['footer'])>=0) {
        footerElement.style.position='absolute';
        footerElement.style.bottom='0px';
    $('.ornament').css('position','fixed');
    $('.ornament').css('top','');
    $('.ornament').css('bottom',size['footer']+'px');
    size['mode'] = 1;
      }
    else if(windowHeight < contentHeight) {
        $('.ornament').css('position','fixed');
    $('.ornament').css('top','');
    $('.ornament').css('bottom','0');
    footerElement.style.position='static';
    size['mode'] = 3;
    size['delta'] = $(document).height() - $(window).height();
    }
      else {
    ontop = ($(document).height() - size['footer'] - size['ornaments']) + 'px';
    $('.ornament').css('position','absolute');
    $('.ornament').css('bottom','');
    $('.ornament').css('top',ontop);
    footerElement.style.position='static';
    size['mode'] = 2;
      }
    }
  }
}
    
function scroll_ornaments() {
  // onscroll events might slow things done, as seen in Facebook feeds, don't run the stuff unless necessary
  if(size['mode'] == 3) {
    if(size['delta'] > $(document).scrollTop() + size['footer']) {
      $('.ornament').css('position','fixed');
      $('.ornament').css('top','');
      $('.ornament').css('bottom','0');
    }
    else {
      $('.ornament').css('position','absolute');
      $('.ornament').css('top',($(document).height() - size['footer'] - size['ornaments']) + 'px');
      $('.ornament').css('bottom','');
    }
  }
}
      
function obj_height(id) {
  obj = document.getElementById(id);
  return obj.offsetHeight;
}

window.onload = function() {
  // because $(document).ready(function(){ doesn't work in Google Chrome
  setFooter();
}
window.onscroll = function() {
  scroll_ornaments();
}
window.onresize = function() {
  setFooter();
}

function openWindow(url, wname, width, height) {
window.open(url, wname, "height=" + height + ",width=" + width + "location = 0, status = 1, resizable = 0, scrollbars=1, toolbar = 0");
return true;
}

jQuery.fn.nospam = function(settings) {
  settings = jQuery.extend({
    replaceText: false,   // optional, accepts true or false
    filterLevel: 'normal'   // optional, accepts 'low' or 'normal'
  }, settings);
  
  return this.each(function(){
    e = null;
    if(settings.filterLevel == 'low') { // Can be a switch() if more levels added
      if($(this).is('a[rel]')) {
        e = $(this).attr('rel').replace('//', '@').replace(/\//g, '.');
      } else {
        e = $(this).text().replace('//', '@').replace(/\//g, '.');
      }
    } else { // 'normal'
      if($(this).is('a[rel]')) {
        e = $(this).attr('rel').split('').join('').replace('//', '@').replace(/\//g, '.');
      } else {
        e = $(this).text().split('').join('').replace('//', '@').replace(/\//g, '.');
      }
    }
    if(e) {
      if($(this).is('a[rel]')) {
        $(this).attr('href', 'mailto:' + e);
        if(settings.replaceText) {
          $(this).text(e);
        }
      } else {
        $(this).text(e);
      }
    }
  });
};

