//cookie
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options = $.extend({}, options); // clone object since it's unexpected behavior if the expired property were changed
            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
        }
        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.fn.center = function (absolute) {
	return this.each(function () {
		var t = jQuery(this);

		t.css({
			position:	absolute ? 'absolute' : 'fixed', 
			left:		'50%', 
			top:		'50%'
		}).css({
			marginLeft:	'-' + (t.outerWidth() / 2) + 'px', 
			marginTop:	'-' + (t.outerHeight() / 2) + 'px'
		});

		if (absolute) {
			t.css({
				marginTop:	parseInt(t.css('marginTop'), 10) + jQuery(window).scrollTop(), 
				marginLeft:	parseInt(t.css('marginLeft'), 10) + jQuery(window).scrollLeft()
			});
		}
	});
};

jQuery(document).ready(function($){

  $('.article_carusel').cycle({ 
    fx:     'fade', 
    speed:  'slow', 
    timeout: '5000', 
    next:   '#next2', 
    prev:   '#prev2' ,
    pause:   1 
  });

  var cookie = $.cookie('avd');
  var $body = $("body");
  var choooseDepartment = function() {
    $.get(ips.system.urlroot + "?template=splash",function(data){
      $("<div class='overlay'></div>").prependTo($body).css("opacity","0.5");
      $(data).prependTo($body);
      $("object").hide();
      $("#logo img").attr("src",ips.system.urlroot + "filestore/dev/GFX/logo-plain.png");
      $(".splash a").live("click",function(e){
        $.cookie('avd', $(this).attr("href"), { expires: 1000, path: '/'});
      });
    });
  }
  function endsWith(str, suffix) {
      return str.indexOf(suffix, str.length - suffix.length) !== -1;
  }
  if (cookie !== null) {
    if (endsWith(window.location.toString(),'cageball.no/')) {
       if (window.location.toString().indexOf("redir=true") == -1 ) {
         window.location = cookie;
       }
    } else {
      $("<li id='chooseDepartment' title='Gjeldende:"+cookie+"'>Velg avdeling</li>").appendTo("#navMain").click(function(){
         choooseDepartment();
      });
    }
  } else {
      choooseDepartment();
  }

  /*
$.get(ips.data.urlroot + "?template=splash",function(data){
    $(data).prependTo("body");
    $(".splash a").click(function(e){  
      $.cookie('avd', $(this).attr("href"), { expires: 100, path: '/'});
    });
  });
*/
});
