/* 
    Document   : itemslider.js
    Created on : 28.01.2011, 14:56:35
    Author     : Tillmann David@FLOW4
    Description:
        Slider for Homepage
*/
 
 $(document).ready(function(){
        var currentPosition = 0;
        var newheight = 0;
        var newwidth = 0;
        var NewPosition = 0;
        var bgwidth = 1280;
        var bgheight = 960;
        var browserwidth  = $(window).width();
        var browserheight  = $(window).height();
        var ratio = bgheight/bgwidth;
        var slideWidth = $(window).width();
        var slideCount = 0;
        var hash = location.hash.replace("#","");
		var HashId = 0;

       
        
    	function AdSlides(){
			$('#Slides .hslide').each(function(index) {			
			    $('#slidesContainer')
		        .append( '<div class="slide" id="slide'+slideCount+'" index="'+slideCount+'" url="'+$(this).attr('url')+'">'+$(this).html()+'</div>' );
				slideCount++;
			  });
        }
				

    	//AddSlides to Slideshow
		AdSlides();
		
		/*initially checks if any URI is given or not
         * Given URI is shown on reload*/
        if(hash != "") {        	 
        	 $('.slide').each(function(index){
        		 if($(this).attr('url') == hash){
        			 currentPosition = index;        			 
        			 return false;
        		 }
        	 });        	 
        	 NewPosition = currentPosition+1;
        	 $('#slide'+(currentPosition)).show();        	         	        	
        	 
        } else {
        	var initialHash = $('#slidesContainer').find('#slide0').attr('url');
        	document.location.hash = initialHash; 
        	//console.log(initialHash);
        }

        
		function IsIpad(){
			var deviceAgent = navigator.userAgent.toLowerCase();
			return deviceAgent.match(/(ipad)/);
		 
		}
		
		
		
        
        var slides = $('.slide');
        var numberOfSlides = slides.length;


        //Insert new Wrapper
        slides
        .wrapAll('<div id="slideInner"></div>')
        .css({
          'float' : 'left',
          'width' : slideWidth
        });


       	//set wrapper width
        $('#slideInner').width(slideWidth);


        $('#fixbackgroundimg')
        .live('click', function(e){
			
			if(e.pageX<browserwidth/2){
				if (currentPosition != 0) {
					NewPosition = currentPosition - 1;
				}
				else {
					NewPosition = currentPosition + 1;
				}
			}
			else{
				
					NewPosition = currentPosition+1;
				
			}			
			
			setCursor(e);			
			
            //NewPosition = currentPosition+1;
            
            browserwidth  = $(window).width();
            
            $('#slideInner').width(browserwidth*(NewPosition+1));

            //add new slides
            if($('.slide').length <= NewPosition)
            	AdSlides();

            $('#slide'+(NewPosition)).show();
            
            var initialHash = $('#slide'+(NewPosition)).attr('url');
        	document.location.hash = initialHash;
            
            calcPositions(currentPosition);
          
            $('#slide'+(NewPosition)+' .content').css('margin-top' , browserheight+"px");                        
            
            $('#fixbackgroundimg').hide();
			
			var old_video = $('#slide'+(currentPosition)+' .player');
			var ak_video =  $('#slide'+(NewPosition)+' .player');
			
			if (old_video.length>0) {
				old_video.get(0).pause();
			}
            
            $('#slide'+(currentPosition)+' .content').animate({
                'margin-top' : browserheight+"px"
            },200,function(){
				if (ak_video.length>0) {
					ak_video.show();
				}			
	            $('#slidesContainer').animate({
	                'left' : (slideWidth*(-NewPosition))+30
	            }, 500,"swing", function() {
	            	$('#slidesContainer').animate({
		                'left' : slideWidth*(-NewPosition)
		            },100,"swing", function() {
	            		$('#fixbackgroundimg').fadeIn("fast", function() {
		                    $('#slide'+(NewPosition)+' .content').animate({
		                        'margin-top' : browserheight-(parseInt($('#slide'+(NewPosition)+' .content .content_box').outerHeight()))+"px"
		                    },200); 
							 if (old_video.length>0) {
								old_video.hide();
							}
							if (ak_video.length>0) {
								ak_video.get(0).play();
							}
	            		})            		
		            })
	            })
            });

            currentPosition = NewPosition;            
        });

        HashId = $('#Slides').find('div[url='+hash+']').index();
		if (HashId >= 0) {
			currentPosition = parseInt(HashId);
			NewPosition = parseInt(HashId);			
			calcPositions(currentPosition);			
		} else {
			calcPositions(currentPosition);
		}	


		//set Container height
        $("#slidesContainer").height(browserheight);
        $('#slide'+(NewPosition)+' .backgroundimg').width(browserwidth);

        $(window).resize(function(){
			
            calcPositions(currentPosition);

        });

        

        function calcBGZeroPoint(ratio){
            var coordinates=new Array;

            if (($(window).height() / $(window).width()) > ratio) {
                coordinates['height'] = $(window).height();
                coordinates['width'] = Math.round($(window).height() / ratio);
            } else {
                coordinates['height'] = Math.round($(window).width() * ratio);
                coordinates['width'] = $(window).width();
            }

            coordinates['left'] = Math.round(($(window).width() - coordinates['width']) / 2);
            coordinates['top'] = Math.round(($(window).height() - coordinates['height']) / 2);

            return coordinates;
        }

        function calcPositions(currentPosition){
            
            browserheight  = $(window).height();
            browserwidth  = $(window).width();
			

            var bgPos = calcBGZeroPoint(ratio);

            $('.backgroundimg').css('z-index','-2');
            $('#slide'+(NewPosition)+' .backgroundimg').css({
                'width' : bgPos['width'],
                'height' : bgPos['height'],
                'marginLeft' : bgPos['left']+'px',
                'marginTop' : bgPos['top']+'px',
                'z-index' : '-1'
            });

            slideWidth = browserwidth;
            $(".slide").width(browserwidth);
            if(IsIpad())
            	$('#slideInner').width( browserwidth*(currentPosition+1)+1);//alert(browserwidth*(currentPosition+1));
            else
				$('#slideInner').width( browserwidth*($(".slide").length));//alert(browserwidth*(currentPosition+1));
            
		
            $("#slidesContainer").height(browserheight);
            $('#slidesContainer').css('left',slideWidth*(-currentPosition));

			//set Control position
            $(".control").css('top',(browserheight/2)-($(".control").height()/2)+"px");

            //set Footer position
            $("#footer").css('top',browserheight-($("#footer").outerHeight())-5+"px");
            
            //4 vertical scrolling
            //$('#slide'+(NewPosition)+' .content').height(browserheight-40);
            //$('#slide'+(NewPosition)+' #wrapper').height(browserheight);

			$(".slide .content").hide();
			$('#slide'+(NewPosition)+' .content').show();
			
            //set Contentbox position
            $('#slide'+(NewPosition)+' .content').css('margin-top' , browserheight-(parseInt($('#slide'+(NewPosition)+' .content .content_box').outerHeight()))+"px");
            $('#slide'+(NewPosition)+' .content').css('margin-left' , browserwidth-(parseInt($('#slide'+(NewPosition)+' .content .content_box').outerWidth())+32)+"px");
            //set Logo position
            $('#logo').css('left',(browserwidth-(114+30))+"px");

        }

		//fadein slide0
		
        var bgPos = calcBGZeroPoint(ratio);

        $('.backgroundimg').css('z-index','-2');
        $('#slide'+(currentPosition)+' .backgroundimg').css({
            'width' : bgPos['width'],
            'height' : bgPos['height'],
            'marginLeft' : bgPos['left']+'px',
            'marginTop' : bgPos['top']+'px',
            'z-index' : '-1'
        });		
        $('#slide'+currentPosition+' .content').fadeIn('slow');
        $('#slide'+currentPosition).fadeIn('slow');
        $('#slide'+currentPosition+' .content').show();
        $('#slide'+currentPosition+' .content').css('margin-left' , browserwidth-(parseInt($('#slide'+(NewPosition)+' .content .content_box').outerWidth())+32)+"px");
        $('#slide'+currentPosition+' .content').css('margin-top' , browserheight+"px");
        $('#slide'+currentPosition+' .content').animate({
            'margin-top' : browserheight-(parseInt($('#slide'+(NewPosition)+' .content .content_box').outerHeight())+16)+"px"
        },200); 

      //FF hack to set the tooltip and title for mozilla as cursor image is not supported by Moz-browser
	  var ua = $.browser;
	  	
	  	
		if(ua.mozilla || (ua.msie && (ua.version.slice(0,1) <= 9)))
		{	
			$('#fixbackgroundimg').css("margin-left","-4px");		
			$('#fixbackgroundimg').attr("title","&raquo; weiter");
			$('#fixbackgroundimg').tooltip({ 
	            track: true, 
	            delay: 0, 
	            showURL: false, 
	            showBody: " - ", 
	            fade: 150 
	        });				
		}
      
		HashId = $('#Slides').find('div[url='+hash+']').index();
		if (HashId >= 0) {
			currentPosition = parseInt(HashId);
			NewPosition = parseInt(HashId);			
			calcPositions(currentPosition);	

		}	
		
		function setCursor(e){
			if (numberOfSlides > 1) { 
				if (e.pageX < browserwidth / 2) {
					if (currentPosition != 0) {
						$('#fixbackgroundimg').css('cursor', "url('img/arrow_left.png'),hand");
						$('#tooltip').find('h3').html('zur&uuml;ck &laquo;');
					}
					else {
						$('#fixbackgroundimg').css('cursor', "url('img/arrow_right.png'),hand");
						$('#tooltip').find('h3').html('&raquo; weiter');
					}
				}
				else {
					if (currentPosition != numberOfSlides - 1) {
						$('#fixbackgroundimg').css('cursor', "url('img/arrow_right.png'),hand");
						$('#tooltip').find('h3').html('&raquo; weiter');
					}
					else {
						$('#fixbackgroundimg').css('cursor', "url('img/arrow_left.png'),hand");
						$('#tooltip').find('h3').html('zur&uuml;ck  &laquo;');
					}
				}
			}
		}
		
		$('#fixbackgroundimg').mousemove(function(e){
			setCursor(e);
				//console.log(e.pageX);
		});		
		
    });    
