// JavaScript Document

//document main init
$(document).ready (onInit);



//global init function
function onInit (e) {
	SWFAddress.addEventListener(SWFAddressEvent.CHANGE, mm.onChange);
	
	$(window).resize(pz.onResize);
	pz.init();
	mm.init();
	
	
	$('#pr-home > ul > li').hover (
		function () {
			$(this).find('.pr-glow').stop().animate({'backgroundPosition':"0px 0px"},400);			
		},  
		function () {
			$(this).find('.pr-glow').stop().animate({'backgroundPosition':"0px -643px"},800);
		}).click(function () {
			var newIndex = parseInt($(this).attr('index'));
			pr.productGoTo(newIndex);
		});



	
	$('.pr-contentframe > ul').each(function(index, element) {        
		var length = $(this).find('li').length;
		$(this).find('li').css('width',(100/length)+'%');
    });


	$('a[rel="plink"]').click(pr.openProduct);
	$('.tooltip').tooltip();
	
	
	$('.name')
			.fadeTo(0,0)
			.parent()
			.hover(function () {
					$(this).find('.name').stop().fadeTo(400,1);
				}, function () {
					$(this).find('.name').stop().fadeTo(400,0);
				});
	
	
	$('a[rel="imb"]').fancybox({overlayColor:'#000000', overlayOpacity:0.8, padding:0, margin:0});
}





//* menu and animation manager **/
var mm = {

	currentIndex:0,

	init:function () {
		$('#navi > ul > li').hover(mm.onOver,mm.onOut);
	},
	
	
	onChange:function () {
		/** geht the new link from swf adress **/
		var nl = SWFAddress.getPathNames();
		if (nl == "") nl = "home";
		
		$('#navi > ul > li')
		.removeClass('selected')
		.each(function(index, element) {
			$a = $(this).find('a');
			var href = $a.attr('href');
			if (('#'+nl) == href) {
				mm.currentIndex = index;
				$(this).addClass('selected').trigger('mouseover');
			}
        }).trigger('mouseout');
		
		mm.scollToPosition();
	},
	
	
	scollToPosition:function (fxOff) {
		
		if (fxOff)
			$.fx.off = true;
		
		var newPos = (pz.middelHeight+5)*mm.currentIndex;
		$('#middel').stop().animate({scrollTop:newPos},1500,'easeInOutSine',function () {
			$.fx.off = false;	
		});
		
		
	},
	
	
	onOver:function () {
		$(this)
			.stop()
			.animate({'background-color':'#ffffff'},500)
			.find('a')
			.stop()
			.animate({'color':'#000000'},500);
			
		
	},
	
	
	onOut:function () {
		if ($(this).hasClass('selected') == false)
			$(this)
				.stop()
				.animate({'background-color':'#000000'},500)
				.find('a')
				.stop()
				.animate({'color':'#ffffff'},500);
	}
	

}





var pz = {
	
	topHeight:null,
	bottomHeight:null,
	topMaring:null,
	middelHeight:null,
	
	
	init:function () {
		pz.topHeight = parseInt($('#top').height());
		pz.topMaring = parseInt($('#top').css('margin-bottom'));
		pz.bottomHeight = parseInt($('#bottom').height());
		pz.onResize();
	},
	
	
	
	
	
	onResize:function () {
		var totalHeight = parseInt($('#page-size').height());
		pz.middelHeight = totalHeight - pz.topHeight - pz.bottomHeight - pz.topMaring;
		$('#middel').height(pz.middelHeight);
		mm.scollToPosition(true);
				
		$('#pr-frame').find('.pr-contentframe').each(function(index, element) {
			$(this).css({'left':$('#page-size').width()*index, width:$('#page-size').width()});
        });
		
	}
}



var pr = {
	
	productGoTo:function (index) {
		$('#pr-frame').stop().animate({scrollLeft:$('#page-size').width()*index},800);
	},
	
	openProduct: function () {
		
		
		var href = $(this).attr('href');
		var cat = href.split('/')[0];
		var prc = href.split('/')[1];
		
		var pr = prd[cat][prc];
	
	
		$('#pr-detail-imge').html('<img src="'+pr.image+'">');
		$('#pr-detail-text-inner').html('<h1>'+pr.headline+'</h1>'+pr.text);
		
		
		
		if (pr.video) {
			$('#pr-detail-video-container').html('<a href="#" rel="videobn" onclick="return false"><img src="images/pr-video-bn.jpg" /></a>');
			$('a[rel="videobn"]').click(function () {
				$('#pr-detail-video-container').html('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="336" height="270" id="FLVPlayer"><param name="wmode" value="transparent"/><param name="movie" value="FLVPlayer_Progressive.swf" /><param name="quality" value="high" /><param name="wmode" value="opaque" /><param name="scale" value="noscale" /><param name="salign" value="lt" /><param name="FlashVars" value="&amp;MM_ComponentVersion=1&amp;skinName=Clear_Skin_2&amp;streamName='+pr.video+'&amp;autoPlay=true&amp;autoRewind=false" /><param name="swfversion" value="8,0,0,0" /><object type="application/x-shockwave-flash" data="FLVPlayer_Progressive.swf" width="336" height="270"><param name="quality" value="high" /><param name="wmode" value="opaque" /><param name="scale" value="noscale" /><param name="salign" value="lt" /> <param name="FlashVars" value="&amp;MM_ComponentVersion=1&amp;skinName=Clear_Skin_2&amp;streamName='+pr.video+'&amp;autoPlay=true&amp;autoRewind=false" /> <param name="swfversion" value="8,0,0,0" /><param name="expressinstall" value="Scripts/expressInstall.swf" /><param name="wmode" value="transparent"/></object></object>');
			});
		}else{
			$('#pr-detail-video-container').html('<img src="images/pr-video-bn-no.jpg" />');
		}
		
		
		$('#pr-overlay')
			.css('display','block')
			.fadeTo(0,0)
			.fadeTo(300,0.8);
		
		
		$('#pr-detail-wrapper')
			.css('display','block')
			.fadeTo(0,0)
			.delay(400)
			.fadeTo(300,1);
			
			
			$('#tooltip').css({
					left: 0,
					top: 0,
					display:'none'
				});
						
		return false;
	},
	
	
	
	closePorduct: function () {
		$('#pr-detail-video-container').html('');
		$('#pr-overlay')
			.fadeTo(0,500,function () {
				$(this).css('display','none');	
			});
			
		$('#pr-detail-wrapper')
			.fadeTo(300,0, function () {
				$(this).css('display','none');	
			});	
	}
	
}





$.fn.tooltip = function () {
	
	$(this).bind({
			mouseenter: function (e) {
				
				
				var newLeft = $(this).offset().left + $(this).width()/2 - $('#tooltip').width()/2;
				
				
				
				if (newLeft + $('#tooltip').width() > $(window).width())
					newLeft = $(window).width() - $('#tooltip').width();
					
				if (newLeft < 0 )
					newLeft = 0;
				
			
				
				$('#tooltip').css({
					left: newLeft,
					top: $(this).offset().top - $('#tooltip').height(),
					display:'block'
				});
				
				$('#tooltip-content').html($(this).attr('tooltip'));
				
			},
			mouseleave: function (e) {	
				$('#tooltip').css({
					left: 0,
					top: 0,
					display:'none'
				});
								
			}});
}





//**** IE Array indeOf fix ..... ****/
if(!Array.indexOf){
	Array.prototype.indexOf = function(obj){
		for(var i=0; i<this.length; i++){
			if(this[i]==obj){
				return i;
			}
		}
		return -1;
	}
}
 
