var cwidth=300; // cover width
var cstep = 55; // space between covers
var selCovers = false;
var selCCol = false;
var showId = 0;
var speed = 700;
var speedInfo = 350;
var cfInfoIntv = false;
var cfaniIntv = false;
var cfOpenIntv = false;
var cfaniId = 0;
var cfaniIsRun = false;
var ccnt = 0;
var cfInfoId = 0;


$(document).ready(function(){
	$('#cfloader').fadeIn (10);
	//
	selCovers = $('.cfcover')
	selCovers.initCover ();
	selCClick = $('.cfclick')
	selCClick.initClick ();
	selCShad = $('.cfimgshad')
	selCShad.initShad ();
	selCCol = $('.cfimgcol')		
	$('#cfinfo p').mouseover(function(){
			if (cfaniIsRun==true) cf_stop_ani ();
		});
	$('#cfinfo p').mouseout(function(){
			if (cfaniIsRun==false) cf_start_ani ();
		});							
	$('#cfloader').fadeOut (10);
	showId = 0;
	$('#cfcover'+cfaniId+' .cfimgcol', this).hide ();
	$('#cfcover'+showId).data('colison', 0);
	openCover (0)
	fillInfo (cfdata[0])
	cf_start_ani ();
});


jQuery.fn.initCover = function(){
	$(this).each(function(){
		var myid = $(this).attr('id').substring(7, 10)
		var x = (cstep*myid);
		$(this).data('myid', myid);
		$(this).data('posx', x);
		$(this).data('isanipos', 0);
		$(this).data('isanifadeopt', 'none');
		$(this).data('colison', 1);
		$(this).css ( {'margin':'0px', left:x, 'z-index':(100-myid)} );
		$(this).prepend ('<div class="cfclick"></div>')
		ccnt++;
	});
}
jQuery.fn.initClick = function(){
	$(this).each(function(){
		$(this).click(function(){
			window.location.href = cfdata[ $(this).parent().data('myid') ].lnkmore;
			});	
		$(this).mouseover(function(){
			selectCover ($(this).parent().data('myid'), 'show')
			});
		$(this).mouseout(function(){
			selectCover ($(this).parent().data('myid'), 'hide')
			});	
	});
}
jQuery.fn.initShad = function(){
	$(this).each(function(){
		$(this).click(function(){
			prevId =  parseInt( $(this).parent().data('myid'),10) + 1
			window.location.href = cfdata[ prevId ].lnkmore;
			});	
		$(this).mouseover(function(){
			prevId =  parseInt( $(this).parent().data('myid'),10) + 1
			selectCover (prevId, 'show')
			});
		$(this).mouseout(function(){
			prevId =  parseInt( $(this).parent().data('myid'),10) + 1
			selectCover (prevId, 'hide')
			});	
	});
	// no actin for last covershadow
	$(this).last().css({'cursor':'default' })
}


var cfselcId = false;
var cfselcIntv = false;
function selectCover ( id, opt ){
	clearInterval(cfselcIntv);
	cfselcId = id
	cfselcOpt = opt
	if (opt=='show'){
		$('#cfcover'+id +' .cfimgcol').stop(true,true).fadeOut( 200 )			
		cfselcIntv = setInterval(function() { 
				clearInterval(cfselcIntv);
				selCCol.hlCover(cfselcId, 'in'); 
		},10);
	}else{
		cfselcIntv = setInterval(function() { 
				clearInterval(cfselcIntv);
				selCCol.hlCover(cfselcId, 'out'); 
		},10);		
		}
}


jQuery.fn.hlCover = function( id, opt ){
	clearInterval(cfselcIntv);
	clearInterval(cfOpenIntv);	
	cf_stop_ani ();
	var cntCoverHl = 0;
	$(this).each(function(){
		myid = $(this).parent().data('myid');
		if (id==myid && opt=='in'){ // 
			// selected cover
			cntCoverHl++;
			// start timer for open cover
			cfOpenIntv = setInterval(function() { 
					clearInterval(cfOpenIntv);
					openCover(id); 
			},250);				
			// fade out color, but only if not faded out yet
			//$(this).hide();
		}else{
			// non selected Cover
			if (showId==myid){
			}else{
				$(this).fadeIn( speed )
			}
		}
	});
	// if no cover is hilited (rollover out of cover area
	// start animation
	if (cntCoverHl==0){
		if (cfaniIsRun==false) cf_start_ani ();
	}
}

function openCover (id){
	clearInterval(cfOpenIntv);
	if (id==showId)return; // already shown yet
	showId = parseInt(id,10);
	// stop all ongoing animations
	selCovers.stop(true,false);
	$('.cfimgcol', this).stop(true,false);
	// set new position to all covers
	infoisdone = false;
	selCovers.showCover ( showId )
	// remember for ani
	cfaniId =  showId;	
}

jQuery.fn.showCover = function( showId ){
	$(this).each(function(){
	var myId = $(this).data('myid');
		var currposx = parseInt($(this).css('left'),10);
		if (myId<showId){
			var x = showId * cstep - cwidth - (showId-myId-1)*cstep;
		}else{
			var x =myId * cstep ;
		}
		if (myId==showId) {
			$('.cfimgcol', this).fadeOut (speed/2);
		}else{

			if ( navigator.appVersion.indexOf("MSIE 6.")==-1 && navigator.appVersion.indexOf("MSIE 7.")==-1  && navigator.appVersion.indexOf("MSIE 8.")==-1) {
				$('.cfimgcol', this).fadeIn (speed);		
			}else{
				$('.cfimgcol', this).css ('opacity', 0.6);
				$('.cfimgcol', this).css ('-ms-filter', 'progid:DXImageTransform.Microsoft.Alpha(Opacity=60)');
				$('.cfimgcol', this).css ('filter', 'alpha(opacity=60)');
				$('.cfimgcol', this).show();
			}
		}
		$(this).data('posx', x);
		if (currposx!=x){ // move only, if cover is not in right position
			if ( $(this).data('isanipos')==1){
				$(this).stop (true,false)
			}
			$(this).data('isanipos',1);
			showInfo ()
			$(this).animate({"left": x+"px"}, speed,function(){
				$(this).data('isanipos',0);
//				showInfo ()
				});
		}
	});
}

function showInfo (){
		if (cfInfoId == showId) return false;
		var cfInfoIntv = setInterval(function() { 
			//alert ('loop int info')
			clearInterval(cfInfoIntv);
			$('#cfinfo').fadeOut (speedInfo, function(){
				// make sure text is visible
				if (cfdata[showId] !=undefined)	fillInfo (cfdata[showId])
				$('#cfinfo').fadeIn (speedInfo);
				
			});	
			
			$('#cfinfometa').fadeOut (speedInfo, function(){
			// make sure text is visible
			//if (cfdata[showId] !=undefined)	fillInfo (cfdata[showId])
			$('#cfinfometa').fadeIn (speedInfo);
				
			});	
		},10);
	cfInfoId = showId;
 
}

function fillInfo (mydata){
	var islogin = $('#jsislogin').val();
	$('#cfinforub').html (mydata.rub)
	$('#cfinfotitle').html (mydata.title)
	$('#cfinfodescr').html (mydata.descr)
	$('#cfinfolnkmore').attr('href', mydata.lnkmore )
	$('#cfinfolnktitle').attr('href', mydata.lnkmore )
	if (mydata.lnktrailer=='' || mydata.lnktrailer===false)
	{
		$('#cfinfotrailer').attr('href', '#' )		
		$('.cfinfotrailer').hide()
	}else{
		$('#cfinfotrailer').attr('href', mydata.lnktrailer )
		$('.cfinfotrailer').removeClass('hide')
		$('.cfinfotrailer').show()
	}

	if ( typeof mydata.lnkorder =='undefined' || mydata.lnkorder=='' || mydata.lnkorder===false)
	{
		$('#cfinfolnkorder').attr('href', '#' )		
		$('#cfinfoorder').hide()
		$('#cfinfolnkorder').html ('');
	}else{
		if (islogin!='1') {
			$('#cfinfolnkorder').attr('href','Javascript:'+mydata.lnkorder)
		}else{
			$('#cfinfolnkorder').attr('href','Javascript:'+mydata.lnkorder )
		}

		if (mydata.pricedvd=='' || mydata.pricedvd===false)
		{
			$('#cfinfolnkorder').html ('DVD kaufen');

		}else{

			$('#cfinfolnkorder').html ('DVD kaufen');			
			$('#cfinfolnkorderprice').html (mydata.pricedvd+' &euro;');			

		}
		$('#cfinfoorder').removeClass('hide')
		$('#cfinfoorder').show()
	}
	if (typeof mydata.lnkvod =='undefined' ||  mydata.lnkvod=='' || mydata.lnkvod===false)
	{
		$('#cfinfolnkvod').attr('href', '#' )		
		$('#cfinfovod').hide()
		$('#cfinfolnkvod').html ('');
	}else{
	//---------------------		
			 
		if (typeof mydata.datevod !='undefined' && mydata.datevod!='')
		{
			// show info with date
			
			$('#vodc1').removeClass ('col1')
			$('#vodc2').removeClass ('col2')
			$('#vodc1').addClass ('col1data')
			// add inactive class
			$('#cfinfolnkvod').removeClass ('inactive')
			$('#cfinfolnkvod').attr('href', '#' )
			//
			var txtdate = '<span class="date">' +  mydata.datevod +'</span>'			
			$('#cfinfolnkvod').html ('online ansehen2 ' + txtdate);	
			$('#vodc2').html ('');	
			$('#vodc2').hide();
		}else{
			$('#vodc1').removeClass ('col1')
			$('#vodc2').removeClass ('col2')
			$('#vodc2').show();
			$('#cfinfolnkvod').removeClass ('inactive')
			// show info with price
			if (islogin!='1')
			{ 
				$('#cfinfolnkvod').attr('href',"javascript:load_ajaxpage('login')" )
			}else{		
				$('#cfinfolnkvod').attr('href', mydata.lnkvod  )
			}	
			//
			if (typeof mydata.pricevod =='undefined' || mydata.pricevod=='' || mydata.pricevod===false)
			{
				var txtprice = '--';	
			}else{
				var txtprice = mydata.pricevod+' &euro;';

			}	
			$('#cfinfolnkvod').html ('online ansehen');
			$('#vodc2').html (txtprice);
			$('#cfinfolnkvodprice').html (mydata.pricevod+' &euro;');		
				
		}		
	//---------------------		
	// show info		
	$('#cfinfovod').removeClass('hide')
	$('#cfinfovod').show()
	}

	
	if (typeof mydata.lnkpresse =='undefined' || mydata.lnkpresse=='' || mydata.lnkpresse==false)
	{
		
		$('#cfinfolnkpresse').attr('href', '#' )		
		$('#cfinfopress').hide()
	}else{
		if (islogin!='1') {
			$('#cfinfolnkpresse').attr('href',"javascript:load_ajaxpage('login')" )
		}else{
			$('#cfinfolnkpresse').attr('href','Javascript:'+mydata.lnkpresse )
		}
		
		$('#cfinfopress').removeClass('hide')
		$('#cfinfopress').show()
	}	
	
}
function cf_start_ani (){
	clearInterval(cfaniIntv);
	cfaniIsRun = true;
	cfaniIntv = setInterval('cf_do_ani()',4000);
}
function cf_stop_ani (){
	clearInterval(cfaniIntv);
	cfaniIsRun = false;	
}
function cf_do_ani (){
	cfaniId ++;
	if (cfaniId>=ccnt) cfaniId=0;
	openCover (cfaniId);
	}
