var containerHeight;
var bIsClosed = true;

function init(){
}

jQuery(function(){
  (function($){
    "use strict";

    var $pollResult = $("#poll-result");
    var $table = $pollResult.find("table").hide();
    var maxValue = 0;

    $table.find("tbody tr .value").each(function(){
      maxValue += parseInt($(this).html());
    });

    $table.find("tbody tr").each(function(){
      var $row = $(this);
      var value = parseInt($row.find(".value").html());
      var width = ((value / maxValue) * 100) + "%";

      var $answer = $("<div class='answer' />");
      $answer.append($("<div class='label'>").html($row.find(".label").html()));
      $answer.append(
        $("<div class='bar' />").append($("<div class='value' />").width(width))
        .append($("<div class='label' />").html(value))
      );

      $pollResult.append($answer);
    });

  })(jQuery);

  (function($){
    "use strict";
    $("#poll-form input:disabled").parent().addClass("disabled");
  })(jQuery);

  (function($){
    $("a[rel=client]").colorbox({
      opacity : 0.4,
      width : 768,
      height : 498,
      maxWidth : 768,
      maxHeight : 768,
      title : "",
      rel : "nofollow",
      iframe : true,
      fastIframe : false,
      loop : false
    });

    $("#siteClients a[rel=client]").colorbox({
    	opacity : 0.4,
    	width : 768,
    	height : 498,
    	maxWidth : 768,
    	maxHeight : 768,
    	title : "",
    	rel : "client",
    	iframe : true,
    	fastIframe : false,
    	loop : false
    });
    
  })(jQuery);
});


function playHeaderVideo(url) {
  scroll(0, 0);
  headerplayer.sendEvent("LOAD", url);
  headerplayer.sendEvent("PLAY");
}

var oMenuOpen = new Object();
oMenuOpen['hl'] = false;
oMenuOpen['cw'] = false;

var g=navigator.userAgent.toLowerCase();

// AS: avoid blocking formfields by scaling the flash movie
function enlargeFlash(id, project){
  var label = project.client + " - " + project.name;
  _gat._getTrackerByName()._trackEvent("Header highlight", "Open", label);

  jQuery("#" + id).css({
    height: "375px"
  });
}

function shrinkFlash(id, project) {
  var label = project.client + " - " + project.name;
  _gat._getTrackerByName()._trackEvent("Header highlight", "Close", label);

  jQuery("#" + id).css({
    height: "90px"
  });
}


function switchFlash(id, body){
  var project = parseFlashProject(body);

  if (bIsClosed) {
    enlargeFlash(id, project);

  } else {
    shrinkFlash(id, project);
  }

  bIsClosed = !bIsClosed;
}

function parseFlashProject(body) {
  var html =  jQuery("<div>" + body + "</div>");
  var project = {
    "name" : jQuery("h1:contains('Project') ~ p:first", html).html(),
    "client" :  jQuery("h1:contains('Opdrachtgever') ~ p:first", html).html()
  };

  return project;
}


arrow_off = new Image();
//arrow_off.src='#request.approoturl#/gfx/arrow_clients.gif';

arrow_on = new Image();
//arrow_on.src='#request.approoturl#/gfx/arrow_999.gif';

function setArrowOn(number) {
document.images['arrow' + number].src = arrow_on.src;
}
  function setArrowOff(number) {
document.images['arrow' + number].src = arrow_off.src;
}

var prop = "'screenX=0,left=0,screenY=0,top=0,width=600,height=350,menubar=no,resizable=no,titlebar=no,alwaysRaised=yes,status=no,scrollbars=yes,fullscreen=0'"

function openWin(url,props)
{
  _prop = (props ? props : prop)
  var pop = window.open(url,"popup",_prop);
  pop.focus();
}

function showMovie(swfMovie,moviePath){
  thisMovie(swfMovie).showMovie(moviePath);
}

function showImage(movie, imgPath) {
  jQuery("#headerimage").attr("src", imgPath);
}

function thisMovie(movieName) {
    ieo = document.getElementById(movieName+'o');

    if (ieo==null) {
        return document.getElementById(movieName);
    }
    else {
        return document.getElementById(movieName+'o');
    }

}

// Functions to show and hide the popover
function show_popover(title, url, width, height)
{
  var page_size   = get_page_size();
  var page_scroll = get_page_scroll();
  var popover = jQuery("#popover");

  var popoverWidth = popover.css("width");
  var popoverHeigth = popover.css("height");

  if (width) {
    popoverWidth = width + "px";
  }

  if (height) {
    popoverHeigth = height + "px";
  }

  jQuery('#headerimage').hide();
  jQuery('#headerside').hide();
  jQuery('select').hide();
  jQuery('#fade').css('height', page_size[1] + page_scroll[1] + 'px');
  jQuery('#fade').css('width', '100%');

  jQuery('#fade').show();

  popover.css({
    top: 10 + page_scroll[1],
    width: popoverWidth,
    height: popoverHeigth
  });

  jQuery('#popover h1').html(title);
  jQuery('#popover iframe').attr('src', url);
  popover.show();
}

function hide_popover() {
  jQuery('#headerimage').show();
  jQuery('#headerside').show();
  jQuery('select').show();
  jQuery('#fade').hide();
  jQuery('#popover').hide();
  jQuery('#popover iframe').attr('src', 'about:blank');
}

/**
 * A function from quirksmode to get the pagesize
 **/
function get_page_size()
{

  var x_scroll, y_scroll;

  x_scroll = jQuery(document).width();
  y_scroll = jQuery(document).height();

  var window_width, window_height;

  window_width = jQuery(window).width();
  window_height = jQuery(window).height();

  // for small pages with total height less then height of the viewport
  if(y_scroll < window_height)
  {

    page_height = window_height;

  }
  else
  {

    page_height = y_scroll;

  }

  // for small pages with total width less then width of the viewport
  if(x_scroll < window_width)
  {

    page_width = x_scroll;

  }
  else
  {

    page_width = window_width;

  }

  array_page_size = new Array(page_width,
                              page_height,
                              window_width,
                              window_height);

  return array_page_size;

}

/**
 * A function from quirksmode which determines how far the page has scrolled
 **/
function get_page_scroll(){

  var x_scroll, y_scroll;

  if (self.pageYOffset) {

    y_scroll = self.pageYOffset;
    x_scroll = self.pageXOffset;

  }
  else
    if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
      y_scroll = document.documentElement.scrollTop;
      x_scroll = document.documentElement.scrollLeft;

    }
    else
      if (document.body) {// all other Explorers
        y_scroll = document.body.scrollTop;
        x_scroll = document.body.scrollLeft;

      }

  array_page_scroll = new Array(x_scroll, y_scroll);

  return array_page_scroll;

}


(function($) {
  function init_slideshow() {

    var $slideshow = $('#slideshow');
    var $all_items = $('li', $slideshow);

    // Hide all descriptions and show the one of the current open h3
    $('p', $slideshow).hide();
    $('img', $slideshow).hide();

    $('img', '.open', $slideshow).show();
    $('p', '.open', $slideshow).show();

    function interval_slideshow_action() {
      // find the current item, jquery has no easy way to do this. Then we trigger the click
      // event for the next item using modulus to wrap around
      for(var i = 0; i < $all_items.length; i++) {
        if ($($all_items[i]).hasClass('open')) {
          $($all_items[(i+1) % $all_items.length]).click();
          break;
        }
      }
    }

    // Add click event to h3s to open/close and show corresponding image
    function default_slideshow_action() {
      var $current_item = $('li.open', $slideshow);
      var $next_item = $(this);

      if($next_item.hasClass('open')) {
        console.log('has_open');
        return;
      }

      $current_item.animate({height: '75px'}, 750);
      $next_item.animate({height: '146px'}, 750);

      $('img', $all_items).hide();
      $('img', $next_item).show();

      $('p', $all_items).hide();
      $('a', $all_items).hide();
      $all_items.removeClass('open');
      $all_items.addClass('closed');

      $next_item.removeClass('closed');
      $next_item.addClass('open');

      $('p', $next_item).show();
      $('a', $next_item).show();

      clearTimeout(timer);
      timer = setTimeout(interval_slideshow_action, 6000);

    }

    $('li', $slideshow).click(default_slideshow_action);
    var timer = setTimeout(interval_slideshow_action, 6000);
  }

  $('document').ready(function() {
   $('#filterpicker').change(function() {
    $('#actueel_nl').load(BASEPATH+'/homepagefilter/mediahighlight.cfm'+'/'+MARKPATH+'/?containerid='+MEDIAHIGHLIGHTCONTAINERID+'&filter='+escape(this.value));
    $('#column1articlespositionering').load(BASEPATH+'/homepagefilter/column1articlespositionering.cfm'+'/'+MARKPATH+'/?containerid='+COLUMN1CONTAINERID+'&filter='+escape(this.value));
    $('#thirdContainer').load(BASEPATH+'/homepagefilter/column3articles.cfm'+'/'+MARKPATH+'/?containerid='+COLUMN3CONTAINERID+'&filter='+escape(this.value));
  });

  if(!ADMINMODE) init_slideshow();
});

}(jQuery));

