var showAdvertisement = false;
var videos_shown = 0;
var start_time = new Date();
var playerWASP = new Object();

function addStat ( node_id ) {
  var node = $('#link_node_'+ node_id);
  var action = '';
  if ( node.hasClass ( 'subsection' ) )
    action = 'ajax_stat_subsection_visit';
  else ( node.hasClass ( 'section' ) )
    action = 'ajax_stat_section_visit';
  if ( action.length > 0 ) {
    var banner_id = parseInt($('#img_banner').next ().html ());
    var campaign_id = parseInt($('#img_banner').next ().next ().html ());
    var postData = {};
    postData['node_id'] = node_id;
    postData['channel_id'] = channel_id;
    postData['platform_id'] = '3';
    if ( banner_id != -1 && campaign_id != -1 ) {
      postData['banner_id'] = banner_id;
      postData['campaign_id'] = campaign_id;
    }
    $.post('?action=' + action, postData);
  }
}

function rand ( min, max ) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

function showBanner ( node_id ) {
  if ( typeof banners == 'undefined' )
    return;

  if ( banners[node_id] && banners[node_id].length > 0 ) {
    //get a random banner:
    var banner = banners[node_id][rand(0, banners[node_id].length - 1)];
    $('#img_banner').attr ( 'src', banner['@attributes'].location );
    $('#img_banner').attr ( 'title', banner['@attributes'].title );
    $('#img_banner').attr ( 'alt', banner['@attributes'].title );
    //$('#img_banner').attr ( 'width', banner['@attributes'].width );
    //$('#img_banner').attr ( 'height', banner['@attributes'].height );
    $('#img_banner').next ().html ( banner['@attributes'].identifier);
    $('#img_banner').next ().next ().html ( banner['@attributes'].campaign_identifier);
    $('#img_banner').show ();
  }
  else {
    $('#img_banner').hide ();
    $('#img_banner').next ().html (-1);
    $('#img_banner').next ().next ().html (-1);
  }
}

function showBumper ( node_id ) {
  if ( typeof bumpers == 'undefined' )
    return;

  if ( bumpers[node_id] && bumpers[node_id].length > 0 ) {
    $('#actionfield').remove();
		var bumper = bumpers[node_id][0];
		$('#img_bumper').attr ( 'src', bumper['@attributes'].location );
    //$('#img_bumper').attr ( 'title', "bumper" );
    $('#img_bumper').attr ( 'alt', "bumper");
    //$('#img_bumper').attr ( 'width', bumper['@attributes'].width );
    //$('#img_bumper').attr ( 'height', bumper['@attributes'].height );
    $('#img_bumper').next ().html ( bumper['@attributes'].identifier);
    $('#img_bumper').show ();
  }
  else {
    $('#img_bumper').hide ();
    $('#img_bumper').next ().html (-1);
  }
}

function hideContent()
{
  $('#contenttext').hide();
}


function onNodeClick ( element_id ) {
  var node_id = parseInt(element_id.substr ( 10 )); // strip link_node_
	showBanner ( node_id );
	showBumper ( node_id );
  addStat ( node_id );
//  if ( mainPit ) mainPit.count ();
//	var new_url = 'index.php?action=public_view_node&node_id=' + node_id;
  setCurrentNodeId ( node_id );
  hideContent();
}

function onSubnodeClick ( element_id ) {
  var node_id = parseInt(element_id.substr ( 10 )); // strip link_node_
  if ( node_id === current_sub_node_id )
    return; // do nothing
 // nodePit.count ( false );
  setCurrentSubnodeId ( node_id );
  // change location
  var new_url = 'index.php?action=public_view_node&node_id=' + node_id;
  if ( current_node_id != 0 )
    new_url += '&pnid=' + current_node_id;
 // if ( mainPit && mainPit.interaction_count !== 0 )
 //   new_url += '&mic=' + mainPit.interaction_count;
 // if ( nodePit && nodePit.interaction_count !== 0 )
 //   new_url += '&nic=' + nodePit.interaction_count;
  window.location.href = new_url;
}

/*
function PopupInteractionType () {
  this.node_id = 0;
  this.popup = null;
  this.interaction_count = 0;
  this.max_interaction_count = 0;

  this.count = function ( show ) {
    if ( typeof(show) == 'undefined' ) show = true;
    if ( this.popup == null || this.max_interaction_count === 0 )
      return;
    if ( ++this.interaction_count >= this.max_interaction_count && show )
      this.show ();
  }

  this.check = function () {
    if ( this.popup == null || this.max_interaction_count === 0 )
      return;
    if ( this.interaction_count >= this.max_interaction_count )
      this.show ();
  }

  this.reset = function ( node_id ) {
    this.node_id = node_id;
    this.popup = null;
    this.interaction_count = 0;
    this.max_interaction_count = 0;

    var node_popups = popups[node_id];
    if ( node_popups && node_popups.length > 0 ) {
      this.popup = node_popups[rand(0, node_popups.length-1)];
      this.max_interaction_count = rand(parseInt(this.popup['@attributes'].interactions_min), parseInt(this.popup['@attributes'].interactions_max));
    }
  }

  this.show = function () {
    $('#popup_link').attr('href', this.popup['@attributes'].location );
    $('#popup_link').trigger('click');
    $.post ( '?action=ajax_stat_popup_view', {node_id:this.node_id, popup_id:this.popup['@attributes'].identifier, campaign_id:this.popup['@attributes'].campaign_identifier, platform_id:'3'} );
    setTimeout("LightBox_finish ();", this.popup['@attributes'].display_timeout * 1000 );
    this.reset ( this.node_id );
  }
}

var mainPit = null;
var nodePit = null;
var subnodePit = null;
*/
function setCurrentNodeId ( node_id ) {
  current_node_id = node_id;
  //if ( nodePit ) nodePit.reset ( current_node_id );
}

function setCurrentSubnodeId ( node_id ) {
  current_sub_node_id = node_id;
  //if (subnodePit) subnodePit.reset ( current_sub_node_id );
}

/*
Simple JQuery menu.
HTML structure to use:

Notes:

1: each menu MUST have an ID set. It doesn't matter what this ID is as long as it's there.
2: each menu MUST have a class 'menu' set. If the menu doesn't have this, the JS won't make it dynamic

Optional extra classnames:

noaccordion : no accordion functionality
collapsible : menu works like an accordion but can be fully collapsed
expandfirst : first menu item expanded at page load

<ul id="menu1" class="menu [optional class] [optional class]">
<li><a href="#">Sub menu heading</a>
<ul>
<li><a href="http://site.com/">Link</a></li>
<li><a href="http://site.com/">Link</a></li>
<li><a href="http://site.com/">Link</a></li>
...
...
</ul>
<li><a href="#">Sub menu heading</a>
<ul>
<li><a href="http://site.com/">Link</a></li>
<li><a href="http://site.com/">Link</a></li>
<li><a href="http://site.com/">Link</a></li>
...
...
</ul>
...
...
</ul>

Copyright 2008 by Marco van Hylckama Vlieg

web: http://www.i-marco.nl/weblog/
email: marco@i-marco.nl

Free for non-commercial use
*/

function initMenus() {
	$('ul.menu ul').hide();
	$.each($('ul.menu'), function(){
		$('#' + this.id + '.expandfirst ul:first').show();
	});
	$('ul.menu > li a').click(
		function() {
			var checkElement = $(this).next();
			var parent = this.parentNode.parentNode.id;

			if($('#' + parent).hasClass('noaccordion')) {
				$(this).next().slideToggle('normal');
				return false;
			}
			if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
				if($('#' + parent).hasClass('collapsible')) {
					$('#' + parent + ' ul:visible').slideUp('normal');
					$('#' + parent + ' ul:visible').each(function() {
						if($(this).children().length === 0)
							$(this).hide()
					});
				}
				return false;
			}
			if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
				onNodeClick ( this.parentNode.id );

				$('#' + parent + ' ul:visible').slideUp('normal');
				$('#' + parent + ' ul:visible').each(function() {
					if($(this).children().length === 0)
						$(this).hide()
				});
				checkElement.slideDown('normal');
				return false;
			}
		}
	);
}

/*****************************************************/
/* Lightbox hack: this is not nice, but we need it to hide a popup after a timeout */
function LightBox_finish() {
  $('#jquery-lightbox').remove();
  $('#jquery-overlay').fadeOut(function() { $('#jquery-overlay').remove(); });
  // Show some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
  $('embed, object, select').css({ 'visibility' : 'visible' });
}

/*****************************************************/

$(document).ready(
  function()
  {
    if ( navigator.appVersion.indexOf ( 'MSIE' ) <  0 )
      $('#bannerarea').addClass('banner');
    else
      $('#bannerarea').addClass('bannerIE');

    $('.link_node a').click ( function () {
      onSubnodeClick ( this.parentNode.id );
    });

    initMenus();

    $('#popup a').lightBox ( {
        imageLoading:     'cms/images/lightbox-ico-loading.gif',  // (string) Path and the name of the loading icon
			  imageBtnPrev:     'cms/images/lightbox-btn-prev.gif',     // (string) Path and the name of the prev button image
			  imageBtnNext:     'cms/images/lightbox-btn-next.gif',     // (string) Path and the name of the next button image
			  imageBtnClose:    'cms/images/lightbox-btn-close.gif',    // (string) Path and the name of the close btn
			  imageBlank:       'cms/images/lightbox-blank.gif'         // (string) Path and the name of a blank image (one pixel)
      }
    );

    // main menu interaction counter
/*
    mainPit = new PopupInteractionType ();
    if ( default_menu_id != -1 ) {
      mainPit.reset ( default_menu_id );
      mainPit.interaction_count += mic;
    }

    // node interaction counter
    nodePit = new PopupInteractionType ();
    if ( current_node_id != 0 ) {
      nodePit.reset ( current_node_id );
      nodePit.interaction_count += nic;
      nodePit.check ();
    }

    // subnode interaction counter
    subnodePit = new PopupInteractionType ();
    if ( current_sub_node_id != 0 )
      subnodePit.reset ( current_sub_node_id );
*/
  }
);

