/*
# Carousel 1.5 [jQuery plugin for 1.3+]
#
# Copyright (C) 2010 - Snapper Net Solutions | www.snapper.no
#
# Usage:
# $('#yourcarousel').carousel()
#
#   > where #yourcarousel points to your ul containing one li per item to be displayed
#
# 1.2 adds fade as a transition mode
# 1.3 is rewritten to be more robust when used with other plugins
# 1.4 adds optional < and > (prev, next)
# 1.5 adds new mode, circle, which just circles

# Modes:
# restart > 1, 2, 3, 4 < 1, 2, 3, 4 (snaps rapidly back to first when finished)
# circle > 1, 2, 3, 4, 1, 2, 3, 4
*/

(function($)
{
  $.fn.carousel = function(options)
  {
    var self = this;

    self.current = 0;
    self.previous = 0;
    self.items = [];
    self.itemw = 0;
    self.fullw = 0;
    self.timer = null;
    self.locked = false;
    self.animating = false;

    var obj = $(this);
    var defaults =
    {
      auto: 0,
      pos: 0,
      mode: 'restart',
      transmode: 'slide',
      transition: 1.0,
      tickbox: false,
      buttons: ''
    };

    // Get parameters
    self.options = $.extend({}, defaults, options);
    self.options.auto = self.options.auto * 1000;
    self.options.transition = self.options.transition * 1000;

    //
    // Public methods
    //

    this.prev = function()
    {
      if (self.locked == true || self.animating == true)
        return false;

      // Remember previous object id
      self.previous = self.current;
      self.current--;
      self._animate(self.current);
    };

    this.next = function()
    {
      if (self.locked == true || self.animating == true)
        return false;

      // Remember previous object id
      self.previous = self.current;
      self.current++;
      self._animate(self.current);
    };

    this.goto = function(n, noanim)
    {
      if (self.locked == true || self.animating == true)
        return false;

      // Remember previous object id
      self.previous = self.current;
      self.current = n;
      self._animate(self.current, noanim);
    };

    this.stop = function()
    {
      clearInterval(self.timer);
      self.timer = null;
    };

    this.clicked = function(el)
    {
      self.stop();
      if ($(el).hasClass('prev'))
        self.prev();
      else
        self.next();
    };

    this.update_tickbox = function()
    {
    	var ticknum = self.current;
    	if (self.options.mode == 'circle' && self.current >= self.shownum)
    		ticknum = 0;

      var tb = $('.tickbox', self.master);
      var ti = $('li', tb);
      ti.removeClass('active');
      $(ti[ticknum]).addClass('active');
    };

    //
    // Private methods
    //

    var _finished = function()
    {
      self.animating = false;
      if (self.options.tickbox)
        self.update_tickbox();

      if (self.options.mode == 'circle')
      {
      	if (self.current >= self.shownum)
	      {
	      	self.current = 0;
	      	self.goto(0, true);
	      }
	      else if (self.current < 0)
	      {
	      	self.current = self.shownum-1;
	      	self.goto(self.shownum-1, true);
	      }
	    }
    };

    // Transition mode slide (default)
    var _slide = function(i, noanim)
    {
      _track_control(i);

     	var cp = (self.options.mode == 'circle') ? self.current+1 : self.current;
     	var pos = cp * self.itemw * -1;

      if (noanim == true)
      {
        var x = 0;
        self.css({left: pos});
        _finished();
      }
      else
      {
        self.animating = true;
        self.animate({left: pos, easing: 'swing'}, self.options.transition, function(){_finished()});
      }
    };

    var _fade = function(i, noanim)
    {
      _track_control(i);

      if (noanim == true)
        var x = 0;
      else
      {
        self.animating = true;
        var pel = self.items[self.previous];
        var nel = self.items[self.current];
        $(pel).fadeOut(self.options.transition, function(){ $(nel).fadeIn(self.options.transition, function(){_finished()}) });
      }
    };

    var _track_control = function(i)
    {
      if (self.options.mode == 'restart')
      {
        if (self.current >= self.items.length) self.current = 0;
        else if (self.current < 0) self.current = self.items.length - 1;
      }
      else if (self.options.mode == 'circle')
      {
        if (self.current >= self.items.length) self.current = self.items.length - 1;
        else if (self.current < -1) self.current = -1;
      }
      else
      {
        if (self.current >= self.items.length) self.current = self.items.length - 1;
        else if (self.current < 0) self.current = 0;
      }
    };

    // Set up
    return this.each( function()
    {
      self.master = $(this).parent();
      self.items = $('li', this);
      self.itemw = $(self.items[0]).width();
      self.fullw = self.items.length * self.itemw;
      self.shownum = self.items.length;
      var li_css = {width: self.itemw};

			// Adjust to circle mode
      if (self.options.mode == 'circle')
      {
        var pref = $(self.items[0]).clone();
        var suff = $(self.items[self.items.length-1]).clone();
        pref.appendTo(this);
        suff.prependTo(this);
        self.items.push(pref[0]);
        self.items.push(suff[0]);
        self.fullw += (self.itemw*2);
      }

			// Set transition mode
      switch (self.options.transmode)
      {
        case "fade":
          self.fullw = self.itemw;
          li_css.position = 'absolute';
          li_css.display = 'none';
          self._animate = _fade;
          break;

        default:
          self._animate = _slide;
          if (self.options.mode == 'circle')
            self.css({left: self.itemw*-1});

          break;
      }

      $.each(self.items, function(){$(this).css(li_css)});
      $(this).addClass('lime_carousel').css('width', self.fullw);

      if (self.options.auto)
        self.timer = setInterval(function(){self.next()}, self.options.auto);

      if (self.options.transmode == 'fade')
        self.goto(0);

      // Do we need to add any buttons?
      if (self.options.buttons)
      {
        self.master.append('<div class="' + self.options.buttons + ' prev"></div><div class="' + self.options.buttons + ' next"></div>');
        $('.button', self.master).click(function(){self.clicked(this)});
      }

      // Do we need to add status pointers
      if (self.options.buttons)
      {
      	if (!self.shownum) return;

        var html = '<li class="active" value="0">&nbsp;</li>';
        var il = self.shownum;
        for (var i=1; i < il; i++)
        {
          html += '<li value="' + i + '">&nbsp;</li>';
        }
        self.master.append('<ul class="tickbox">' + html + '</ul>');
        var tb = $('.tickbox', self.master);
        $('li', tb).click(function(){self.stop(); self.goto($(this).attr('value'))});
      }
    });
  };
})(jQuery);


function fbs_click() 
{
  u=location.href;
  t=document.title;
  window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
  return false;
}

function digg_click()
{
  u=location.href;
  t=document.title;
  window.open('http://digg.com/submit?phase=2&url='+encodeURIComponent(u)+'&title='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
  return false;
}

function twitter_click()
{
  u=location.href;
  t=document.title;
  window.open('http://twitter.com/home/?status='+encodeURIComponent(t)+'%20'+encodeURIComponent(u),'sharer','toolbar=0,status=0,width=626,height=436');
  return false;
}

function reddit_click()
{
  u=location.href;
  t=document.title;
  window.open('http://reddit.com/submit?url='+encodeURIComponent(u)+'&title='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
  return false;
}

function mixx_click()
{
  u=location.href;
  t=document.title;
  window.open('http://www.mixx.com/submit?page_url='+encodeURIComponent(u)+'&title='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
  return false;
}




var faq = {
  toggle : function(el)
  {
    var item = $(el).closest('li');
    item.toggleClass('active');
    if (item.hasClass('active'))
    {
      $('.text', item).slideDown('fast');
    }
    else
    {
      $('.text', item).slideUp('fast');
    }
  }
}

$(document).ready(function()
{
  $('h2', '.faq').click(function(){faq.toggle(this)})
});


function fbs_click() {
  u=location.href;
  t=document.title;
  window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
  return false;
}


function renderFlashObject(url, width, height, id)
{
  return '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + width + '" height="' + height + '" id="' + id + '" align="left">' +
         '  <param name="allowScriptAccess" value="sameDomain" />' +
         '  <param name="movie" value="' + url + '" />' +
         '  <param name="quality" value="high" />' +
         '  <param name="bgcolor" value="#ffffff" />' +
         '  <param name="wmode" value="transparent" />' +
         '  <embed wmode="transparent" src="' + url + '" quality="high" bgcolor="#ffffff" width="' + width + '" height="' + height + '" name="' + id + '" align="left" allowscriptaccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />' +
         '</object>';
}

/* *** */

var imageCache = [];

function PreloadImages()
{
  for (var i=0; i < imgs.length; i++)
  {
    var image = new Image();
    image.src = imgs[i];
    imageCache[imageCache.length] = image;
  }
}

function highlight(el)
{
  if (IE6)
  {
    var cn = el.getAttribute('cn');
    el.className = el.className + ' ' + cn;
  }
}

function nolight(el)
{
  if (IE6)
  {
    var cn = el.getAttribute('cn');
    el.className = el.className.replace(' ' + cn, '');
  }
}

function validateEmail(email) {
  var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
  if(reg.test(email) == false)
    return false;
  return true;
}

function validateForm(el)
{
  var email = $('input[name=user]', el).val();
  if (email == '' || email == 'E-post')
  {
    alert("Du m\xe5 fylle inn en gyldig e-postadresse");
    return false;
  }
  else if (!validateEmail(email))
  {
    alert("Du m\xe5 fylle inn en gyldig e-postadresse");
    return false;
  }

  var chex = $('input:checked', el);
  if (chex.length < 1)
  {
    alert("Du m\xe5 angi kategori");
    return false;
  }  

  return true;
}

$(document).ready(function()
{
  //$('.logo').html(renderFlashObject('/images/layout/toppbanner.swf', 990, 120, 'toppbanner'));
  $(".page .text table[align=right]").addClass('marginleft');
  $('.carousel').carousel({mode:'circle', auto: 7, transition: .7, buttons:'button sprites', tickbox:true});

  var boxes = $(".boxes .box .body");
  var maxHeight = 0;
  for (var i=0; i<boxes.length; i++) {
    var thisHeight=boxes.eq(i).height();
    if (maxHeight<thisHeight) maxHeight=thisHeight;
  }
  boxes.height(maxHeight);

});

