// JavaScript Document
var hoverenabled = true;
$jq = jQuery.noConflict();

$jq(document).ready(function () {
	
	var flashvars = {};
	var params = {};
	var attributes = {};

	params.flvurl = 'http://streaming.interlake.net/fp;streaming/20100201144631/KNM;internetimagefilm_knm.flv;stream;fwd.flv';
	params.scriptaccess = 'always';
	attributes.name = "flashcont";

	swfobject.embedSWF("include/flvgridplayer.swf", "flashcont", "585", "330", "10","include/expressInstall.swf", flashvars, params, attributes);
	$jq('#imagefilm').hide();
							
	if ($jq.browser.msie && $jq.browser.version < 7) {
		var iemode = true;
		$jq('.gridcell').css({'width':147, 'height':147});
	}
	
	$jq('#cover').css({
		//Set opacity property with jQuery to enable IE-compatibility
		'opacity' : 0,
		'display' : 'block'
	});
	
	$jq('#gridbox').hover(
		function() {
			if (hoverenabled) {
				//MouseOver
				$jq('#cover')
					.stop(true)
					.animate({ 
						opacity: 0.7
					}, 500 );
			}
		},
		function() {
			if (hoverenabled) {
				//MouseOut
				$jq('#cover')
					.stop(true)
					.animate({ 
					opacity: 0
				}, 500 );
			}
		}
	);
	
	$jq('.textbox').each(function(tboxindex,tboxobj) {
		
		$jq(tboxobj).css({'display' : 'block', 'opacity' : 0.6});
		
		if (tboxindex < 5) {
			$jq(tboxobj).css({
				'top' : '-147px'
			})
		} else {
			$jq(tboxobj).css({
				'top' : '441px'
			})	
		}
	});
	
	$jq('.gridlink').each(function(linkindex,linkobj) {
		
		var cellReference =  $jq('#c' + linkindex);
		var triggersPicture = $jq(this).hasClass('picture');
		var triggersText = $jq(this).hasClass('text');
		var triggersVideo = $jq(this).hasClass('video');
		var textReference =  $jq('#t' + linkindex);		
		var imageReference =  $jq('#c' + linkindex + ' img');
		var embedReference =  $jq('#c' + linkindex + ' ');
		
		//Falls entsprechendes Textfeld existiert, CSS-Eigenschaften festlegen
		if (textReference.length) {
			var t_cl = parseInt(cellReference.css('left'));
			var t_ct = parseInt(cellReference.css('top'));			
			if (t_cl > 294) {
				//Textfeld muss nach links öffnen
				textReference.css({'left' : t_cl - 294 + 'px', 'top' : t_ct});
			} else {
				//Textfeld nach rechts öffnen
				textReference.css({'left' : t_cl  + 147 + 'px', 'top' : t_ct});
			};
			textReference.css({'height' : '0px', 'overflow' : 'hidden', 'opacity' : 0});
		}
		
		$jq(linkobj)
		.data('cell',{
			  cellDOMRef: cellReference,
			  cellPosL: cellReference.css('left'),
			  cellPosR: cellReference.css('right'), 
			  cellPosT: cellReference.css('top'),
			  cellPosB: cellReference.css('bottom'),
			  imgDOMRef: imageReference,
			  imgPosT: imageReference.css('top'),
			  imgPosL: imageReference.css('left'),
			  txtDOMRef: textReference,
			  txtPosT: textReference.css('top'),
			  triggersPicture: triggersPicture,
			  triggersText: triggersText,
			  triggersVideo: triggersVideo
			  }
		)
		
		.click(function(){
			if ($jq(this).data('cell').triggersVideo) {
				
				var swf = document.getElementById("flashcont");
				swf.handleFlashClick();
			}
		})
		
		.hover(
			function() { 
				if (hoverenabled) {
				//MouseOver
				
					if ($jq(this).data('cell').triggersPicture) {
									
						var leftOffset = 0;
						
						var t_cl = parseInt($jq(this).data('cell').cellPosL);
						if (t_cl >= 3 * 147) {leftOffset = 294;} else if (t_cl == 294) {leftOffset = 147};
				
						var cellAnimProperties = {
							'top' : 0,
							'bottom' : 0,
							'left' : parseInt($jq(this).data('cell').cellPosL) - leftOffset,
							'right' : parseInt($jq(this).data('cell').cellPosR) - 294 + leftOffset
						}

						if (iemode) {cellAnimProperties.width = cellAnimProperties.height = 441;}

						var imgAnimProperties = {
							'top' : 0,
							'left' : 0
						}
				
						$jq(this).data('cell').cellDOMRef
							.css({'z-index': 250})
							.stop(1)
							.animate(cellAnimProperties, 400);
					
						$jq(this).data('cell').imgDOMRef
							.stop(1)
							.animate(imgAnimProperties, 400);
					
					} else if ($jq(this).data('cell').triggersText) {
				
						$jq(this).data('cell').cellDOMRef.css({'z-index': 250});
					
						var txtAnimProperties = {
							'opacity' : 0.75,
							'height' : 147
						}
					
						$jq(this).data('cell').txtDOMRef
							.stop(1)
							.animate(txtAnimProperties, 800);
					
					} else if ($jq(this).data('cell').triggersVideo) {
					
						var cellAnimProperties = {
							'top' : 55,
							'bottom' : 55,
							'left' : 75,
							'right' : 75
						}
	
						if (iemode) {cellAnimProperties.width = 585; cellAnimProperties.height = 330;}
	
					
						$jq(this).data('cell').cellDOMRef
							.css({'z-index': 250})
							.stop(1)
							.animate(cellAnimProperties, 400,
									 function() {
										 $jq('#imagefilm').show();
									 });					
						}
					}
			},
			function() {
				if (hoverenabled) {
					//MouseOut
				
					if ($jq(this).data('cell').triggersPicture) {

						var cellAnimProperties = {
							'top' : $jq(this).data('cell').cellPosT,
							'bottom' : $jq(this).data('cell').cellPosB,
							'left' : $jq(this).data('cell').cellPosL,
							'right' : $jq(this).data('cell').cellPosR					
						}

						if (iemode) {cellAnimProperties.width = cellAnimProperties.height = 174;}
								
						var imgAnimProperties = {
							'top' : $jq(this).data('cell').imgPosT,
							'left' : $jq(this).data('cell').imgPosL
						}

						$jq(this).data('cell').imgDOMRef
							.stop(1)
							.animate(imgAnimProperties, 400);
					
						$jq(this).data('cell').cellDOMRef
							.css({'z-index': 225})
							.stop(1)
							.animate(cellAnimProperties,400,
							function() {
								//callback to reset z-index on end of transition
								$jq(this).css({'z-index': 100});	
							});	
										
					} else if ($jq(this).data('cell').triggersText) {
			
						$jq(this).data('cell').cellDOMRef.css({'z-index': 100});
				
						var txtAnimProperties = {
							'opacity' : 0,
							'height' : 0
						}
					
						$jq(this).data('cell').txtDOMRef
							.stop(1)
							.animate(txtAnimProperties, 800);
			
					} else if ($jq(this).data('cell').triggersVideo) {
						
						$jq('#imagefilm').hide();
						
						var cellAnimProperties = {
							'top' : $jq(this).data('cell').cellPosT,
							'bottom' : $jq(this).data('cell').cellPosB,
							'left' : $jq(this).data('cell').cellPosL,
							'right' : $jq(this).data('cell').cellPosR					
						}

						if (iemode) {cellAnimProperties.width = cellAnimProperties.height = 174;}
								
					
						$jq(this).data('cell').cellDOMRef
							.css({'z-index': 225})
							.stop(1)
							.animate(cellAnimProperties,400,
							function() {
								//callback to reset z-index on end of transition
								$jq(this).css({'z-index': 100});	
							});	
				
					}
				}
					
			});
		
	});
	
});

function lockgrid() {
	hoverenabled = false;
}

function unlockgrid() {
	hoverenabled = true;
}
