var count, lock, selItem, currentHover;

//< -------------------------------------------------------------------- >
//  switchVid
//< -------------------------------------------------------------------- >
var switchVid = function( obj ) {
  if( obj.id != selItem ) {
    $(selItem).style.backgroundColor = '#333333';
    $(selItem).style.cursor = 'default';
    selItem = obj.id;
  }

  $('player-container').innerHTML = "<div id=\"playerbox\"></div><div id=\"player-info\"><div id=\"video-title\"></div><div id=\"video-description\"></div><div id=\"video-tags\"></div></div>";

    //need to make this work
  $('playerbox').innerHTML = "<br \/><h3 style=\"padding-left: 15px\">Loading video...</h3><br />"
    + "<div style=\"padding: 0 15px\">"
    + "If you don\'t have the latest Flash Player installed, please visit <a href=\"http://www.adobe.com/products/flashplayer/\" target=\"_blank\">http://www.adobe.com/products/flashplayer/</a>."
    + "</div>";

  var flashvars = {
    file: obj.file,
    image: obj.image,
    'viral.link': obj.url,
    'viral.functions': 'embed,link'
  };

  var params = {
    allowfullscreen: "true",
    allowscriptaccess: "always"
  };

  var attributes = {
    id: "flashplayer",
    name: "flashplayer"
  };

    var tags = obj.tags;
    tags = tags.split(",");
    var html = "<ul class=\"video-tags\"><li>Tags: </li>";
    for(var i=0; i< tags.length; i++) {
        if( tags[i] != "") {
            var url = obj.search;
            if( i < tags.length - 1 ) {
                html = html + "<li><a href=\""+ url + "?vtags=" + tags[i] + "\">" + tags[i] + "</a>, </li>";
            } else {
                html = html + "<li><a href=\""+ url + "?vtags=" + tags[i] + "\">" + tags[i] + "</a></li>";
            }                
        }
    }
    html = html + "</ul>";

  window.addEvent('domready', function(){
    $('video-title').innerHTML = obj.title;
    $('video-description').innerHTML = obj.description;
    if( obj.tags !=  "") {
        $('video-tags').innerHTML = html;        
    }
  });

  var player;

  swfobject.embedSWF("/wp-content/themes/nmc/video-gallery/player.swf", "playerbox", "360", "290", "10","/wp-content/themes/nmc/video-gallery/expressInstall.swf", flashvars, params, attributes);

};


//< -------------------------------------------------------------------- >
//  switchPic
//< -------------------------------------------------------------------- >
var switchPic = function( obj ) {
  if( obj.id != selItem ){
    $(selItem).style.backgroundColor = '#333333';
    $(selItem).style.cursor = 'default';
    selItem = obj.id;
  }

  // enable when starting to collect stats
  //$('video-views-rating').innerHTML = obj.numViews + " views  |  Rated on Average:" + obj.rating;

  $('player-container').innerHTML = "<div id=\"playerbox\"></div><div id=\"player-info\"><div id=\"video-title\"></div><div id=\"video-description\"></div><div id=\"video-tags\"></div></div>";

  $('playerbox').innerHTML = "<a target=\"_blank\" href=\"" + obj.link + "\"><img class=\"bigthumb\" src=\"" + obj.image + "\"><img id=\"launchbutton\" src=\"/wp-content/themes/nmc/video-gallery/images/launch-button.gif\"></a>";


    var tags = obj.tags;
    tags = tags.split(",");
    var html = "<ul class=\"video-tags\"><li>Tags: </li>";
    for(var i=0; i< tags.length; i++) {
        if( tags[i] != "") {
            var url = obj.search;
            if( i < tags.length - 1 ) {
                html = html + "<li><a href=\""+ url + "?vtags=" + tags[i] + "\">" + tags[i] + "</a>, </li>";
            } else {
                html = html + "<li><a href=\""+ url + "?vtags=" + tags[i] + "\">" + tags[i] + "</a></li>";
            }                
        }
    }
    html = html + "</ul>";


  window.addEvent('domready', function(){
                    $('video-title').innerHTML = obj.title;
                    $('video-description').innerHTML = obj.description;
                      if( obj.tags != "" ) {
                          $('video-tags').innerHTML = html;
                      }
                  });
};

// function to get the number of items in a content box (used for example in the Move* funtions below)
var getItemCount = function( category ) {
  var elements = $(category).getElementsByTagName('div');
  var arr = [];

  for( var n = 0; n < elements.length; n++)
  {
	if( elements[n].className == 'itembox' )
	{
	  arr[arr.length] = elements[n];
	}
  };
  return arr.length;
};

var onMouseOver = function( object ) {
  if( object.id != selItem ) {
	object.style.backgroundColor = '#FDCB33';
	object.style.cursor = 'pointer';
  }
};

var onMouseOut = function( object ) {
  if( selItem != object.id ) {
	object.style.backgroundColor = '#333333';
	object.style.cursor = 'default';
  }
};

var MoveLeftByOne = function( category ) {
  var numItems = getItemCount( category );
  if(lock == 0)
  {
	if (count <= 0)
	{
	  //alert('cannot go any further left!');
	}
	else {
	  var effect = new Fx.Tween( category, {
			                       property: 'marginLeft',
			                       duration: 250,
			                       transition: Fx.Transitions.Sine.easeInOut,
			                       onStart: function() {
			                         lock=1;
			                       },
			                       onComplete: function() {
			                         lock=0;
			                       }
		                         });

	  var left = document.getElementById( category ).style.marginLeft;

	  if (left == "")
		left = 0;

	  // start the transition
	  effect.start(parseInt(left)+132);

	  // finally decrement our counter
	  count = count-1;

	}
  }
};

var MoveAllTheWayLeft = function( category ) {
  var numItems = getItemCount( category );
  if(lock == 0)
  {
	if( count <= 0 )
	{
	  //alert('you\'re already all the way left!');
	}
	else
	{
	  var effect = new Fx.Tween( category, {
			                       property: 'marginLeft',
			                       duration: 250,
			                       transition: Fx.Transitions.Sine.easeInOut,
			                       onStart: function() {
				                     lock=1;
			                       },
			                       onComplete: function() {
				                     lock=0;
			                       }
			                     });

	  effect.start(0);

	  count = 0;
	}
  }
};

var MoveRightByOne = function( category ) {
  var numItems = getItemCount( category );
  if(lock == 0)
  {
	if( count >= numItems-3 )
	{
	  //alert('cannot go any further right!');
	}
	else
	{
	  var effect = new Fx.Tween( category, {
			                       property: 'marginLeft',
			                       duration: 250,
			                       transition:  Fx.Transitions.Sine.easeInOut,
			                       onStart: function() {
				                     lock=1;
			                       },
			                       onComplete: function() {
				                     lock=0;
			                       }
			                     });

	  var left = document.getElementById( category ).style.marginLeft;

	  if (left == "")
		left = 0;

	  // start the transition
	  effect.start(parseInt(left)-132);

	  // finally increment our counter
	  count = count + 1;
	}
  }
};

var MoveAllTheWayRight = function( category ) {
  var numItems = getItemCount( category );
  if(lock == 0)
  {
	if( count >= numItems-3 )
	{
	  //alert('you\'re already all the way to the right!');
	}
	else
	{
	  var effect = new Fx.Tween( category, {
			                       property: 'marginLeft',
			                       duration: 250,
			                       transition: Fx.Transitions.Sine.easeInOut,
			                       onStart: function() {
				                     lock=1;
			                       },
			                       onComplete: function() {
				                     lock=0;
			                       }
			                     });

	  effect.start(parseInt(numItems-3)*-132);

	  count = numItems-3;
	}
  }
};

var MoveToIndex = function( index, category ) {
    var numItems = index;
  if(lock == 0)
  {
	if( count >= numItems-3 )
	{
	  //alert('you\'re already all the way to the right!');
	}
	else
	{
	  var effect = new Fx.Tween( category, {
			                       property: 'marginLeft',
			                       duration: 250,
			                       transition: Fx.Transitions.Sine.easeInOut,
			                       onStart: function() {
				                     lock=1;
			                       },
			                       onComplete: function() {
				                     lock=0;
			                       }
			                     });

	  effect.start(parseInt(numItems-3)*-132);

	  count = numItems-3;
	}
  }
};

window.addEvent('domready',function() {
	                // initialise the counter
	                count = 0;
	                // initialise the global lock
	                lock = 0;

                    var cat = Number(currentHover.charAt(5));
                    var vid = Number(currentHover.charAt(7));
                    
	                // var myAccordion = new Accordion($('accordion'), 
                    //                                 'div.accordion-toggle', 
                    //                                 'div.accordion-content', {
		            //                                     opacity: false,
		            //                                     fixedHeight: 150
	                //                                 });
                    // myAccordion.display(cat);

                    if(vid >2) {
                        for(i=0; i < (vid-2); i++) {
                            MoveToIndex(vid+1,currentCat);
                        }
                    }

	                onMouseOver($(currentHover));
	                selItem = currentHover;
                });
