// player init start
function playerReady(player)
{
	var playerId = player.id;
	var p = document.getElementById(playerId);

	// custom autoplay
	checkPlay(playerId);
}

function openRelatedArticle(articleURL)
{

	w = window.open(articleURL);
	w.focus();
}

function openRelatedVideo(videoURL)
{

	w = window.open(videoURL);
	w.focus();
}

function checkPlay(playerId)
{
	var p = document.getElementById(playerId);
	
	if(p.getConfig().googleanalyticsmanagement == true && p.getConfig().eyewondermanagement == true){
		window.setTimeout("playVideo('" + playerId + "')", 300);				
	} else{
		window.setTimeout("checkPlay('" + playerId + "')", 100);
	}
}

function playVideo(playerId)
{
	var p = document.getElementById(playerId);

	//p.sendEvent('PLAY');
	p.playEyeWonderManagement();
	
}
// player init end

// JavaScript Document
$(document).ready(function() {

	if(isNaN(parseInt($('#channelIdentifier').html()))){
		
		channelIdentifier = String($('#channelIdentifier').html());
		var btnId = 'btnChannel' + channelIdentifier.toUpperCase().substr(0,1) + channelIdentifier.substr(1);
		
	} else{
		
		channelIdentifier = parseInt($('#channelIdentifier').html());
		var btnId = 'btnChannel' + channelIdentifier;
		
	}
	
	if(isNaN(parseInt($('#channelProductId').html()))){
		productId = String($('#channelProductId').html());
	} else{
		productId = parseInt($('#channelProductId').html());
	}
	
	$('#' + btnId).removeClass("btnInactive");
	$('#' + btnId).addClass("btnActive");
	
	if(channelIdentifier == 'search'){
				
		searchActive = true;
		searchText = $('#channelSearch').html();

		$('#searchText').attr( 'value', searchText);
		
		$('#btnSearchResult').show();
		$('#btnSearchResult').removeClass("btnInactive");
		$('#btnSearchResult').addClass("btnActive");
		
	}

	 $("#slidertop").easySlider({
		prevText:'<img src="/img/gamestar/video/btnSliderPrevious.png" width="13" height="24" />&nbsp;',
		prevId:'slidertopPrev',
		nextText:'<img src="/img/gamestar/video/btnSliderNext.png" width="13" height="24" />&nbsp;',
		nextId:'slidertopNext',
		orientation:'horizontal'
	});
	
	 $("#slider").easySlider({
		prevText:'<img src="/img/gamestar/video/arrow_left.png" width="26" height="105" />&nbsp;',
		prevId:'sliderPrev',
		nextText:'<img src="/img/gamestar/video/arrow_right.png" width="26" height="105" />&nbsp;',
		nextId:'sliderNext',
		orientation:'horizontal'
	});


	 
	 $("#searchItem  > *").click(function() {

	 	searchVideoInit(jQuery(this).html())

    });
	 
    $("a","#sliderPrev").focus(function() {
		this.blur();
    });
    $("a","#sliderNext").focus(function() {
    	this.blur();
    });
	$("a","#sliderPrev").click(function(){
		reloadStatistic('RC_VSLIDER');
	});
	$("a","#sliderNext").click(function(){		
		reloadStatistic('RC_VSLIDER');
	});

    $("a","#slidertopPrev").focus(function() {
		this.blur();
    });
    $("a","#slidertopNext").focus(function() {
    	this.blur();
    });
	$("a","#slidertopPrev").click(function(){
		reloadStatistic('RC_TSLIDER');
	});
	$("a","#slidertopNext").click(function(){		
		reloadStatistic('RC_TSLIDER');
	});
	
	$("#searchText").keypress(function(e){
		
		if(e.keyCode == 13){
		
			searchVideoInit();
		
		}
				
	});

});

var channelOverviewURL = '_misc/videos/portal/getChannelOverview.cfm'
var channelIdentifier;
var page = 1;
var productId;
var searchText;
var searchActive = false;

function searchVideoInit(s)
{
	
	if(s == undefined){
		
		searchText = $('#searchText').attr('value');
		
	} else {
		
		searchText = s;
		
	}
	
	page = 1;

	searchVideo(searchText);

}

function searchVideo(s)
{
	
	searchActive = true;
	searchText = s;

	$('#searchText').attr( 'value', searchText);
	
	$(".btnActive").each(function() {
	
		$(this).removeClass('btnActive');
	
		$(this).addClass("btnInactive");
	
	})
	
	$('#btnSearchResult').show();
	$('#btnSearchResult').removeClass("btnInactive");
	$('#btnSearchResult').addClass("btnActive");
	
	// load channel
	$('#channelContentLoaderInfo').show();
	$('#channelContent').hide();
	
	jQuery.get(channelOverviewURL, {'channelName' : 'search', 'search' : searchText, 'p' : page}, loadChannelComplete, 'html');

	// track event
	reloadStatistic('RC_VIDEOBOX', 'channelSearch_' + s);

}

function loadChannelPage(p)
{

	// check document ready
	if(channelIdentifier == undefined){
		
		return;
		
	}
	
	page = p;
	
	if(searchActive){
	
		searchVideo(searchText);
		
		return;
	
	}

	if(typeof(channelIdentifier) == 'string'){
	
		var btnId = 'btnChannel' + channelIdentifier.toUpperCase().substr(0,1) + channelIdentifier.substr(1);
	
	} else {
	
		var btnId = 'btnChannel' + channelIdentifier;
	
	}
	
	loadChannel(btnId, channelIdentifier) 

}

function initChannel(btnId, ci, pId) 
{
	
	page = 1;
	
	productId = pId;

	loadChannel(btnId, ci)

}

function loadChannel(btnId, ci) 
{
	
	searchActive = false;
	
	$(".btnActive").each(function() {
	
		$(this).removeClass('btnActive');
	
		$(this).addClass("btnInactive");
	
	})
	
	$('#' + btnId).removeClass("btnInactive");
	$('#' + btnId).addClass("btnActive");

	// load channel
	$('#channelContentLoaderInfo').show();
	$('#channelContent').hide();
	
	if(typeof(ci) == 'string'){

		channelIdentifier = ci;
		
		jQuery.get(channelOverviewURL, {'channelName' : channelIdentifier, 'p' : page, 'productId': productId}, loadChannelComplete, 'html');
	
	} else {
	
		channelIdentifier = ci;
		
		jQuery.get(channelOverviewURL, {'channelId' : channelIdentifier, 'p' : page, 'productId': productId}, loadChannelComplete, 'html');
	
	}
	
	// track event
	reloadStatistic('RC_VIDEOBOX', ci);
	
}

function loadChannelComplete(responseText) 
{

	$('#channelContent').html(responseText);
	
	$('#channelContentLoaderInfo').hide();
	$('#channelContent').fadeIn(800);
 
}

function reloadStatistic (code,id) 
{
	
  var id = (!id) ? id = "" : "" + id;
  var get_rand = "d=" + (Math.random()*100000);
  var get_referer = "r=" + escape(document.referrer);
  var countIVW = new Image();
  var lowercode = code.toLowerCase();
  countIVW.src = "http://gastar.ivwbox.de/cgi-bin/ivw/CP/" + code + "?" + get_referer + "&" + get_rand;
  //var countSitestat = new Image();
  //countSitestat.src = "http://de.sitestat.com/idgcom-de/gamestar/s?ajax_elemente." + lowercode + id + ".home&" + get_rand;

}

function doVideoDownload(pk,fk,pid) 
{ 
	openWindow('/_misc/downloads/videos/detail.cfm?pk=' + pk + '&fk=' + fk + '&pid=' + pid + '&newvideo=1', 'VideoDownload','width=500,height=440,resizable=no,scrollbars=yes,menubar=no'); 
}

function doGstvDownload(videoId,pk) 
{ 
	openWindow('/_misc/downloads/videos/gstvdownload.cfm?vid=' + videoId + '&pk=' + pk  + '&newvideo=1', 'VideoDownload','width=450,height=250,resizable=no,scrollbars=yes,menubar=no'); 
}

function openWindow(theURL,wname,features) 
{ 
	window.open(theURL,wname,features);	
}

function makeVideoReputation(pk) {
	var rnd = Math.round(Math.random()*100000);
	// AJAX um die Bildbeschreibung zu ermitteln
	jQuery.ajax({
		type:'GET',
		url:'http://'+window.location.hostname+'/_misc/videos/portal/reputationxml.cfm',
		data: 'id='+pk+'&cacheconfuse='+rnd,
		dataType:'xml',
		beforeSend:function() { },
		success:function(xml) {
			if ('1' == jQuery(xml).children('reputation').children('reputationdone').text()) {
				jQuery('#reputationcount').html(jQuery(xml).children('reputation').children('count').text());
			}
		},
		error:function (XMLHttpRequest, textStatus, errorThrown) { }
	});
}
