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

function cartAdd(redirect, sql_id) 
{
  prodid="prod_"+sql_id;
  countObj=document.getElementById(prodid);
  count=countObj.value;
  var url="/CartAddXML?sql_id=" + sql_id + "&count=" + count + "&redirect=" + redirect;
  xmlHttp=false;
  xmlHttp=GetXmlHttpObject(updateCart);
  xmlHttp.open("GET", url , true);
  xmlHttp.send(null);
  return 0;
}

function updateCart() 
{
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  { 
    var xmldoc = xmlHttp.responseXML;
    root=xmldoc.getElementsByTagName('root')[0];
    html_content=root.getElementsByTagName('contents')[0].firstChild.nodeValue;
    cartObj=document.getElementById('shopping_cart');
    cartObj.innerHTML=html_content;
    cartObj.style.display='block';
  }
}

function toggleDisplay(idName, textObj, showText, hideText)
{
  if (document.layers) obj = document.layers[idName];
  else if (document.getElementById) obj = document.getElementById(idName);
  else if (document.all) obj = document.all[idName];

  if (navigator.userAgent.indexOf("MSIE")>=0)
  {
    cl=obj.getAttribute('className');
    if (cl=='hide')
    {
      obj.setAttribute('className','');
      textObj.innerHTML=hideText;
    }
    else
    {
      obj.setAttribute('className','hide');
      textObj.innerHTML=showText;
    }
  }
  else 
  {
    cl=obj.getAttribute('class');
    if (cl=='hide')
    {
      obj.setAttribute('class','');
      textObj.innerHTML=hideText;
    }
    else 
    {
      obj.setAttribute('class','hide');
      textObj.innerHTML=showText;
    }
  }
}

function GetXmlHttpObject(handler)
{ 
  var objXmlHttp = false;

  if (window.XMLHttpRequest)
  { 
    objXmlHttp=new XMLHttpRequest();
    objXmlHttp.onload=handler;
    objXmlHttp.onerror=handler;
  }
  if (window.ActiveXObject)
  {
    try 
    {
      objXmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      try 
      {
         objXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(e)
      {}
    }
    if (objXmlHttp)
    {
      objXmlHttp.onreadystatechange=handler;
    }
  }

  if (!objXmlHttp)
  {
    alert('Giving up - Cannot create an XMLHTTP instance');
    return false;
  }
  if (objXmlHttp.overrideMimeType)
  {
    objXmlHttp.overrideMimeType('text/xml');
  }
  return objXmlHttp;
}

function highlight_item(num, speed)
{
  speed = speed || 'fast';
  pos = $('#menu_item_' + num).offset();
  width = $('#menu_item_' + num).width() + 20;
  dest = pos.left - $('.menu_bg').offset().left;
  $('#highlight').animate({'left': dest + 'px', 'width': width + 'px' }, speed);
}

function setup_gallery()
{
  $(".gallery_archive ul.thumb li .image_frame").hover(function() {
	$(this).css({'z-index' : '10'});
	$(this).addClass("hover").stop()
		.animate({
			marginTop: '0px',
			marginLeft: '0px',
			top: '-20px',
			left: '-20px',
			width: '200px',
			height: '280px',
			padding: '5px'
		}, 200);

        $(this).find("img").addClass("hover").stop()
		.animate({
			marginTop: '0px',
			marginLeft: '0px',
			top: '-20px',
			left: '-20px',
			width: '200px',
			height: '140px'
		}, 200);

	} , function() {
	$(this).css({'z-index' : '0'});
	$(this).removeClass("hover").stop()
		.animate({
			marginTop: '0',
			marginLeft: '0',
			top: '0',
			left: '0',
			width: '170px',
			height: '180px',
			padding: '0px'
		}, 400);
	$(this).find("img").removeClass("hover").stop()
		.animate({
			marginTop: '0',
			marginLeft: '0',
			top: '0',
			left: '0',
			width: '170px',
			height: '120px'
		}, 400);
  });

  //$('.gallery_archive .navigation a').bind('click', update_gallery);
  $('.gallery_archive .image_frame a').lightBox({ fixedNavigation:true, maxWidth:800, maxHeight:600 });
}

function update_gallery()
{
  var items = $('.gallery_options input[type=checkbox]:checked');
  if (items.length == 0) items = $('.gallery_options input[type=checkbox]');
  var selected = [];
  for (var i = 0; i < items.length; i++) selected.push(items[i].value);
  topics = selected.join(',');
  var page = 0;

  if (this.nodeName == 'A')
  {
    var att = $(this).attr('href');
    if (att)
    {
      att = att.substring(1);
      page = att - 1;
    }
  }

  $('.gallery_archive').html('<div id="spinner_container"><img src="/bilder/ajax-loader.gif"/></div>');

  $.get('/mycoteam/renderImageGalleryArchive?page=' + page + '&topics=' + topics + '&search=' + $('#gallery_search_text').val(),
    function(data)
    {
      $('.gallery_archive').html(data);
      setup_gallery();
    }
  );
}

function showPage(n)
{
  var items = $('.gallery_options input[type=checkbox]:checked');
  if (items.length == 0) items = $('.gallery_options input[type=checkbox]');
  var selected = [];
  for (var i = 0; i < items.length; i++) selected.push(items[i].value);
  topics = selected.join(',');
  var page = n-1;
  if (page < 0) page = 0;

  $('.gallery_archive').html('<div id="spinner_container"><img src="/bilder/ajax-loader.gif"/></div>');

  $.get('/mycoteam/renderImageGalleryArchive?page=' + page + '&topics=' + topics + '&search=' + $('#gallery_search_text').val(),
    function(data)
    {
      $('.gallery_archive').html(data);
      setup_gallery();
    }
  );
}

function clear_gallery_search()
{
  $('.gallery_options input[type=checkbox]:checked').each(function(index) { $(this).attr('checked', false); });
  $('#gallery_search_text').val('');
  update_gallery();
}

var closeMenu = false;
function do_products()
{
  var self = this;
  function megaHoverOver()
  {
    $(this).find(".sub").stop().fadeTo('fast', 1).show();

    (function($)
    {
      jQuery.fn.calcSubWidth = function()
      {
        rowWidth = 0;
        $(this).find("ul").each(function()
        {
          rowWidth += $(this).width();
        });
      };
    })(jQuery);

    if ($(this).find(".row").length > 0)
    {
      var biggestRow = 0;
      $(this).find(".row").each(function()
      {
        $(this).calcSubWidth();
        if(rowWidth > biggestRow) biggestRow = rowWidth;
      });
      $(this).find(".sub").css({'width' :biggestRow});
      $(this).find(".row:last").css({'margin':'0'});
    }
    else
    {
      $(this).calcSubWidth();
      $(this).find(".sub").css({'width' : rowWidth});
    }
  }

  function megaHoverOut()
  {
    $(this).find(".sub").stop().fadeTo('fast', 0, function() { $(this).hide(); });
  }

  function empty()
  {
  }

  var config = {
     sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
     interval: 100, // number = milliseconds for onMouseOver polling interval
     over: empty, // function = onMouseOver callback (REQUIRED)
     timeout: 500, // number = milliseconds delay before onMouseOut
     out: megaHoverOut // function = onMouseOut callback (REQUIRED)
  };

  $("#products li .sub").css({'opacity':'0'});
  $("#products").hoverIntent(config);
  $("#products").click(function(){megaHoverIn(this)});
}

function megaHoverIn(el)
{
  if (closeMenu) return false;

  $(el).find(".sub").stop().fadeTo('fast', 1).show();

  (function($)
  {
    jQuery.fn.calcSubWidth = function()
    {
      rowWidth = 0;
      $(el).find("ul").each(function()
      {
        rowWidth += $(el).width();
      });
    };
  })(jQuery);

  if ($(el).find(".row").length > 0)
  {
    var biggestRow = 0;
    $(el).find(".row").each(function()
    {
      $(el).calcSubWidth();
      if(rowWidth > biggestRow) biggestRow = rowWidth;
    });
    $(el).find(".sub").css({'width' :biggestRow});
    $(el).find(".row:last").css({'margin':'0'});
  }
  else
  {
    $(el).calcSubWidth();
    $(el).find(".sub").css({'width' : rowWidth});
  }

  return false;
}

function megaHoverOut(el)
{
  closeMenu = true;
  el = $(el).parent();
  el.stop().fadeTo('fast', 0, function() { $(el).hide(); closeMenu=false;});
}


function open_item(el)
{
  $(el).html('skjul &#9650;');
  el = $(el).parent()
  $('.text', el).toggleClass('show', 1000);
}

function close_item(el)
{
  $(el).html('les mer &#9660;');
  el = $(el).parent()
  $('.text', el).toggleClass('show', 1000);
}

$(document).ready(function()
{
  $('.slide_link', '.lexicon').toggle(function(){open_item(this)}, function(){close_item(this)});

  try
  {
    $('#tabs').tabs();
  }
  catch(e)
  {}
  highlight_item(sel_menu_item || 0, 1);

  var gEl = $('#image_gallery');
  if (gEl.length) gallery.init(page.url + '/gallery_images.xml');

  $('input[id^="tag_"]').bind('click', update_gallery);
  $('#gallery_search_submit').bind('click', update_gallery);
  $('#gallery_search_text').keyup(function(e) { if (e.keyCode == 13) update_gallery(); });
  $(".closer").click(function(){megaHoverOut(this)});

  update_gallery();
  do_products();
});

