/*
	* This program is part of "kewego_webtv_functional" code sample. This is a free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
	* 
	* Created by Kewego - 2009  - See more details on <http://developers.kewego.com/>
	* 
	* Version 1.0.1
/*


/*********************************************
 ********* Your configuration *****************
 **********************************************/

//var player_key = '9c37f60da51b'; 
var player_key = '5acda0db14ad';
// Standard player only (also called Epix)
//var epix_skin_key = '71703ed5cea1';  
var epix_skin_key = '1e9fae90e600';
var language_code = 'fr';

// only for custo Fast Player + module channel 
var player_bgcolor = '000000';
var picto_color = 'FFFFFF'; 
var border_color = 'FFFFFF';
var btn_color = '000000';
var cbar_color = 'FF0000';

var player_width = '300';
var player_height = '182';


var channel_menu_width = '296';
var channel_menu_height = '90';

var player_type = 'epix'; // epix or fp

/*********************************************
 *******  End of your configuration ************
 **********************************************/


/*******************/
/* **DO NOT EDIT** */
/*******************/


/*
	Called on each video
 */
function setTitle(title)
{
	document.getElementById('kewegowebtv_title').innerHTML = title;
}

/*
	Called on each video
 */
function setDescription(description)
{
	document.getElementById('kewegowebtv_description').innerHTML = description;
}

/*
	Called on manual video change
 */
function setPlayer(sig, sig_next)
{
	player(sig, sig_next);
}

/*
	Generic function to display a flash using <object>
 */
function flash(container_id, swf_url, width, height, flash_vars, bgcolor, wmode)
{
	var embed_code = '<object id="flash_' + container_id + '" type="application/x-shockwave-flash" data="' + swf_url + '" width="' + width + '" height="' + height + '" bgcolor="' + bgcolor + '" >' +
	'<param name="flashVars" value="' + flash_vars + '&width=' + width + '&height=' + height + '&autostart=false" />' +
	'<param name="movie" value="' + swf_url + '" />' +
	'<param name="allowFullScreen" value="true" />' +
	'<param name="allowscriptaccess" value="always" />' +
	'<param name="wmode" value="' + wmode + '" />' +
	'</object>';

	/*
		container_id receive the embed code
	 */

	document.getElementById(container_id).innerHTML = embed_code;
}

/*
	Developers doc : http://developers.kewego.com/kb/?View=entry&EntryID=120
 */
function player(sig, sig_next)
{	
	if (player_type == 'fp')
	{
		/* player url */
		var player_url = 'http://sa.kewego.com/swf/fp.swf';
		var flash_vars = 'sig=' + sig + '&sig_next=' + sig_next + '&playerkey=' + player_key + '&language_code=' + language_code + '&redirect=false&suffix=';

		/*
		Decorations
		 */
		flash_vars += '&picto=' + picto_color + '&border=' + border_color + '&btn=' + btn_color + '&cbar=' + cbar_color;
	}
	else
	{
		/* player url */
		var player_url = 'http://sa.kewego.com/swf/p3/epix.swf';		
		var flash_vars = 'sig=' + sig + '&sig_next=' + sig_next + '&playerKey=' + player_key + '&skinKey=' + epix_skin_key + '&language_code=' + language_code + '&redirect=false&suffix=';
	}

	 /*
		Display or replace existing player
	 */

	flash('kewegowebtv_player' /* container_id */, player_url /* swf_url*/, player_width /* width */, player_height /* height */, flash_vars, player_bgcolor, 'window');
}

function channel_menu(csig, sig)
{		
	/* Channel menu url */
	var channel_menu_url = 'http://sa.kewego.com/swf/channelmenu.swf';

	/*
		Display or replace existing channel menu
	 */

	var flash_vars = 'csig=' + csig + '&sig=' + sig + '&playerkey=' + player_key + '&language_code=' + language_code + '&suffix=&senduserprofileinfo=false';

	/*
		Decorations
	 */
	flash_vars += '&picto=' + picto_color + '&border=' + border_color + '&btn=' + btn_color + '&cbar=Null';


	flash('kewegowebtv_channel_menu' /* container_id */, channel_menu_url /* swf_url*/, channel_menu_width /* width */, channel_menu_height /* height */, flash_vars, '', 'transparent');
}

/*
	Start
 */
function start(csig)
{
	/*
		Remove existing player
	 */
 document.getElementById('kewegowebtv_player').innerHTML = '';
 channel_menu(csig, '');

}

/*
	Extract CSIG from a "hash"
	<a href="#iLyRo..."/> be return "iLyRo..."
*/
function extract_csig(hash)
{
	/*
	    Do extract it
	 */
	var csig = hash.substr(hash.indexOf('#') + 1, hash.length);
	
	return csig;
}

/*
	OnClick attached callback for Web TV launch links
 */
function on_link_click()
{
	/*
	    CSIG is into the "href" attribute
	    Do extract it
	 */
    var csig = extract_csig(this.href);

    /*
        Start
     */
    start(csig);


    return true;
}

function on_auto_start(link)
{
	/*
	    CSIG is into the "href" attribute
	    Do extract it
	 */
    var csig = extract_csig(link.href);

    /*
        Start
     */
    start(csig);
}

function on_url_auto_start(hash)
{
	/*
	    CSIG is into location.hash
	    Do extract it
	 */
	var csig = extract_csig(hash);

	/*
        Start
     */
    start(csig);
}

/*
	Links that have "kewegowebtv_link" class become an Web Tv launch links
 */
function links_binding()
{
	var all_links = document.getElementsByTagName('a');

	/*
	    Foreach all links on the page
	 */
	for (var i = 0; i < all_links.length; i++)
	{
    	var link_class = all_links[i].className;

		if (link_class && link_class.indexOf('kewegowebtv_link') != -1 && link_class.indexOf('binded') == -1)
		{
		    /*
		        That link have the "kewegowebtv_link" class
		        Become an Web TV launch link
		     */

			/*
			    Attach onclick callback
			 */
            all_links[i].onclick = on_link_click;

			/*
			    Flag the link that be already binded
			 */
			all_links[i].className = all_links[i].className + ' binded';

		}
	}
}

/*
	First link that have "kewegowebtv_auto_start" class become the auto started Web Tv channel
 */
function auto_start()
{
	var all_links = document.getElementsByTagName('a');

	/*
	    Foreach all links on the page
	 */
	for (var i = 0; i < all_links.length; i++)
	{
    	var link_class = all_links[i].className;

		if (link_class && link_class.indexOf('kewegowebtv_auto_start') != -1)
		{
		    /*
		        This link is the assigned be auto started
		     */

			/*
				Exec auto start on this link
			*/
			on_auto_start(all_links[i]);
		     
		     
			break;
		}
	}
}

/*
	Read location.hash and launch the Web Tv on this CSIG
 */
function url_auto_start()
{
    on_url_auto_start(location.hash);
}

/*
	On document ready
 */
function on_ready()
{
	if (location.hash.length > 0)
	{
	    /*
	        A CSIG is possibly present into the URL, we start on this CSIG
	     */
	    url_auto_start();
	}
	else
	{
	    /*
			Auto start by the "kewegowebtv_auto_start" class
	     */
		auto_start();
	}
	
	
	/*
		Bind all Web Tv link
	 */
	links_binding();
}





$(document).ready(function() {
  if($('#kewegowebtv_player').length == 0){
    return;
  }
  if($('#firstluxe-webtv-page').length > 0){
    player_width = '400';
    player_height = '230';
    channel_menu_width = '396';
    channel_menu_height = '100';
  }else{
    player_width = '300';
    player_height = '182';
    channel_menu_width = '296';
    channel_menu_height = '90';
  }
  on_ready();
  start('iLyROoafYMMx');
});

