//init
window.addEvent('domready', function()
{
	// FlashDetect is from flash_detect.js
	var path		= location.pathname.split('/', 2);
	var params	= new Array();
	
	// setable variables
	params['version']	= 8;
	params['return']	= '/flash/player.flv.error.'+path[1]+'.php';
	
	//don't change
	player_init (params, FlashDetect);
	
	$$('.item_short').addEvent('click', function(e){
    // this.getElement('.a_play').fireEvent('click', this.getElement('.a_play'));  
    this.getElement('.a_play').click();  
  });
  
});

//send event
function sendEvent (type, params)
{
	$("player_flv").sendEvent(type, params);
};

//on update callback ftom player
function getUpdate(type, params)
{
	if (type == "item")
	{
		currentItem = params; //setTimeout("getItemData(currentItem)",100);
	}
	var container_divs	= $$('#eshop_items .item_short');
	var i = 0;	
	container_divs.each(function(el)
	{
		if (i == currentItem)
		{
			el.addClass('focused');
			link = el.getElement("div.link-detail a");
			$('current-item').innerHTML = link.getProperty('title');
		}
		else {
			el.removeClass('focused');
		}
		i++;
	});
};

//javascript player init
function player_init (params, detection)
{
	if (params['version'] > detection.major)
	{
		var container = new Element('div', {'id': 'player-flv-error'});
		container.injectBefore('player-flv');
		
		var url = params['return'];
		new Ajax(url,
		{
			method: 'get',
			update: $('player-flv-error'),
			onComplete: function()
			{
				$('player-flv').remove();
			}
		}).request();
	}
	else
	{
		player_controls ();
	}
}

//controls
function player_controls ()
{
	var container_divs	= $$('#eshop_items .item_short');
	var container_links	= $$('#eshop_items .a_play');
	var i = 0;
	var titles = new Array();
	
	container_links.each(function(el)
	{
		titles[i]	= el.getProperty('title');
		links[el.getProperty('href')]	= i;
		
		el.addEvent('click', function(e) {
		  new Event(e).stop();
		  el.click();
		});
		
		el.click = function()
		{
      // new Event(e).stop();

      new Fx.Scroll(window, {
             wait: false,
             duration: 1000,
             transition: Fx.Transitions.Quad.easeInOut
      }).toElement('player-flv');
      
			position = links[this.href];
      // alert(position);
			sendEvent('playitem', position);
		};
		i++;
	});
	
	var i = 0;
	container_divs.each(function(el)
	{
		el.setProperty('id', i);
		el.setProperty('title', titles[i]);

		el.addEvent('mouseenter', function()
		{
			el.addClass('hover');
		});
		el.addEvent('mouseleave', function()
		{
			el.removeClass('hover');
		});
    // el.addEvent('click', function(e)
    // {
    //       // new Event(e).stop();
    //  // alert(el.id);
    //  
    // 
    //  
    //  sendEvent('playitem', el.getProperty('id'));
    // });
		i++;
	});
}
