// Start of code to store player position.
var plInfo = {'playlist':''};
var curVid = null;

function wimpy_amReady() {
  setTimeout('setUpVideo()',500);
  /* setup for video in home */
  try{ getPlaylist();}catch(e){} 
}

function handlTrackStarted(ob) {
  curVid = ob;
  plInfo.track = ob.tracknumber;

  // Track clips.
  _hbSet("m.f",ob.filename);
  _hbSet("m.s","play");
  _hbSet("m.cp","0");
  _hbSend(); 

}

// Go straight to where we last were if we have information.
function setUpVideo() {	
  if(typeof(playerConfigs) == 'object') {
    plInfo.playlist = playerConfigs.wimpyApp;
    var pl = getPlayerInfo(plInfo.playlist);
    if(pl.track && autoPlay == true) {
      wimpy_gotoTrack(pl.track);
      if(pl.position) {    
        wimpy_setPlayheadSeconds(pl.position);
      }
    }
    // Update player position so it can be stored when page is left.
    if(document.wimpy) {
      setInterval('updatePlayerInfo()',2000);
    }
    $(window).unload(function() {
      setPlayerInfo();
    });
  }
  if(autoPlay == true){
	  setTimeout('wimpy_play()',150);
  }
  if(autoMute == true){
	  setTimeout('wimpy_setMuteState("on")',100);	  
  }
}

function updatePlayerInfo() {
  var player = wimpy_getPlayerState();
  if(player) {
    plInfo.position = player.current;
  }
}

function getPlaylistCookieName(playlist) {
  var name = playlist.replace(/[^a-z0-9]+/g,'_');
  var res = 'tvpl_'+name;
  return res;
}

function getCookie(n) {
 var c=''+document.cookie;
 var a=c.indexOf(n+'=');
 var b=-1;
 var r='';
 if(a != -1) {
  a += n.length+1;
  b=c.indexOf(';',a);
  r=(b != -1)?c.substring(a,b):c.substring(a);
  r=unescape(r);
 }
 return r;
}

function getPlayerInfo(playlist) {
  var ob = {
    'playlist': '',
    'track': 1,
    'position': 0
  };

  var cookieName = getPlaylistCookieName(playlist);
  var val = getCookie(cookieName);
  if(val) {
    var data = [];
    var pairs = val.split(';');
    for(var i = 0;i < pairs.length;i++) {
      var pair = pairs[i].split(':');
      if(pair.length == 2) {
        data[pair[0]] = pair[1];
      }
    }
    if(data['track']) {
      ob.track = parseInt(data['track']);
    }
    if(data['pos']) {
      ob.position = parseFloat(data['pos']); 
    }
  }
  return ob;
}

function setPlayerInfo() {
  if(plInfo.track && plInfo.position && plInfo.playlist) {
    var cookieName = getPlaylistCookieName(plInfo.playlist);
    var val = 'track:'+plInfo.track+';pos:'+plInfo.position;
    document.cookie = cookieName+'='+escape(val)+';path=/;domain=.tiscali.co.uk';
  }
}

// End of code for storing player position.

$(document).ready(function() {

  var delay = 500;
  $(".ipromo").hover(function() {
    $(this).find(".content").stop().animate({top: "10px", opacity: "0.9"},delay);
  },
  function() {
    $(this).find(".content").stop().animate({top: "95px", opacity: "0.8"},delay);
  });

  $(".ipromo-l").hover(function() {
    $(this).find(".content").stop().animate({top: "20px", opacity: "0.9"},delay);
  },
  function() {
    $(this).find(".content").stop().animate({top: "200px", opacity: "0.8"},delay);
  });
  
  $(".ipromo-item").hover(function() {
    $(this).find(".content").stop().animate({top: "20px", opacity: "0.9"},delay);
  },
  function() {
    $(this).find(".content").stop().animate({top: "200px", opacity: "0.8"},delay);
  });  
  
  $(".header-1-promo .ipromo").hover(function() {
    $(this).find(".content").stop().animate({top: "50px", opacity: "0.9"},delay);
  },
  function() {
    $(this).find(".content").stop().animate({top: "105px", opacity: "0.8"},delay);
  });

  $(".header-2-promo .ipromo").hover(function() {
    $(this).find(".content").stop().animate({top: "50px", opacity: "0.9"},delay);
  },
  function() {
    $(this).find(".content").stop().animate({top: "105px", opacity: "0.8"},delay);
  });
  
/* expand box script */
	$('.expand h3').click(function() {
		var checkElement = $(this).next();
		if ((checkElement.is('.content-box')) && (checkElement.is(':visible'))) {return false;}
		if ((checkElement.is('.content-box')) && (!checkElement.is(':visible'))) {
			$('.expand .content-box:visible').hide('normal');
			$('.expand h3').addClass('toexpand');
			$(this).removeClass('toexpand');
			checkElement.show('normal');
			return false;
		}
	});
	$('.expand h3').each(function() {
		if(!$(this).is('.show')){
			$(this).addClass('toexpand');
			var checkElement = $(this).next();
			if (checkElement.is('.content-box')){checkElement.hide();}
		}
	});

	$('#list-programs-more-btn').click(function(){
		$('#list-programs-more').toggle();
		text = ($(this).html() == 'More listing')?'Less listing':'More listing';
		$(this).html(text);		
		refresh(true);
		return false;
	});

// tooltip
setupTooltip();

});

function setupTooltip(){
	try{	
		if(tooltipjs){
			if(!$('#tooltip-js').html()){
				tooltip = $('<div id="tooltip-js"><span></span><div id="tooltip-js-bottom"></div></div>');
				$('body').append(tooltip);
			}
			$('.tooltip-js').hover(
			  function () {
				$('#tooltip-js span').html($(this).attr('title'));	  
				offset = $(this).offset();
				offset.top -= ($('#tooltip-js').height());
				offset.left -= (parseInt($('#tooltip-js').width())-95);
				offset.left = (offset.left<0)?0:offset.left;				
				$('#tooltip-js').css({left:offset.left,top:offset.top});
				$(this).attr('title','');
				$('#tooltip-js').show();
			  }, 
			  function () {
				if($('#tooltip-js span').html()=='') return false;
				$('#tooltip-js').hide();
				$(this).attr('title',$('#tooltip-js span').html());
				$('#tooltip-js span').html();		
			  }
			);
		}
	}catch(e){}
}

function showAlert(text){
	text = '<strong style="font-size:1.3em">'+text+'</strong>';
    show_iframe(text,Array(300,80));
	$('#iframe-overlay').hide();
	$('#iframe-ajax').css('top',parseInt($('#iframe-ajax').css('top'))+100);
	fadePopup = setTimeout('closePopup()',5000);
}

function remindMe(eId){
	url = '/tv/tools/remind-me-dev.html?id='+eId;
	html = '<iframe frameborder="0" name="iframe_data" width="400" height="230" id="iframe_data" src="'+url+'" allowtransparency="true"></iframe>';
	show_iframe(html,Array(400,230));
	return false;
}
function recordThis(eId){
	showAlert('Event has been added to your recording list');
	return false;
}
function addPlanner(eId,objLink,event_id){
	$.get('/tv/ajax.html?addPlanner='+eId,function(data){
	showAlert(data);
	if(data.match(/login/i)) return false;
	try{		
		if(tvg.planner == 1) {tvg.reset();	updateListPlanner();}
		tvg.progsById[event_id].plan = 1;	
	}catch(e){}		
	try{
		if(!objLink) return false;		
		$(objLink).attr('onclick',"return removePlanner('"+eId+"',this)");
		src = $('img',objLink).attr('src');
		src = src.replace(/-add/i,'-remove');
		$(objLink).attr('title','Remove from my planner');						
		$('img',objLink).attr('src',src);	
		$('#tooltip-js span').html($(objLink).attr('title'));	
	}catch(e){}
	});
	return false;
}
function closePopup(){
	$('#iframe-ajax,#iframe-overlay').fadeOut(300,function(){
		$("select, embed, object, #imgf_captions, #imgf_scrollbar, .tohide").css('visibility','visible');													   
	});
}
function removePlanner(eId,objLink,event_id){
	$.get('/tv/ajax.html?removePlanner='+eId,function(data){												  
	showAlert(data);
	try{
		if(tvg.planner == 1) {tvg.reset();	updateListPlanner();}
		tvg.progsById[event_id].plan = 0;		
	}catch(e){}	
	try{
		if(!objLink) return false;		
		$(objLink).attr('onclick',"return addPlanner('"+eId+"',this)");
		src = $('img',objLink).attr('src');
		src = src.replace(/-remove/i,'-add');
		$(objLink).attr('title','Add to my planner');
		$('img',objLink).attr('src',src);		
		$('#tooltip-js span').html($(objLink).attr('title'));			
	}catch(e){}	
	});
	return false;
}

function addFav(type,id,objLink,event_id){
	$.get('/tv/ajax.html?addFav='+id+'&type='+type,function(data){
	showAlert(data);
	if(data.match(/login/i)) return false;		
	try{
		if(!objLink) return false;
		$(objLink).attr('onclick',"return removeFav('"+type+"','"+id+"',this)");
		src = $('img',objLink).attr('src');
		src = src.replace(/-add/i,'-remove');
		$(objLink).attr('title','Remove from my favourites');
		$('img',objLink).attr('src',src);	
		$('#tooltip-js span').html($(objLink).attr('title'));	
		tvg.progsById[event_id].fav = 1;
		if(tvg.planner == 1) {updateListPlanner();}
	}catch(e){}	
	});
	return false;
}
function removeFav(type,id,objLink,event_id){
	$.get('/tv/ajax.html?removeFav='+id+'&type='+type,function(data){
	showAlert(data);
	try{
		if(!objLink) return false;		
		$(objLink).attr('onclick',"return addFav('"+type+"','"+id+"',this)");
		src = $('img',objLink).attr('src');
		src = src.replace(/-remove/i,'-add');
		$(objLink).attr('title','Add to my favourites');		
		$('img',objLink).attr('src',src);	
		$('#tooltip-js span').html($(objLink).attr('title'));
		tvg.progsById[event_id].fav = 0;
		if(tvg.planner == 1) {updateListPlanner();}
	}catch(e){}	
	});	
	return false;	
}
function sendActivation(email){
	$('#iframe-ajax,#iframe-overlay').hide();
	$.get('/tv/ajax.html?dosend_activation=1&email='+email,function(data){
	showAlert(data);
	});	
}
