// JavaScript Document
// JavaScript Document
window.homepage = {};
homepage.initialize = function(){
        //init flash player
       var videoID = "FzgwwzrILIw"
        // Lets Flash from another domain call JavaScript\
        var firstVdideo = $("#videoscreen ul li").first().attr("data-ytVideoID");
        var VideoObj = new videoSwitch("v-startvideo_screen",firstVdideo,680,356);     
        $("#videoscreen ul li").mouseenter(function(){
                if($(this).hasClass("active")) return;
                $("#videoscreen ul li").removeClass("active");
                $(this).addClass("active");
                var videoID = $(this).attr("data-ytVideoID");
                //console.log(VideoObj)
                if(videoID){
                    VideoObj.switchMovie(videoID,true)
                    }
                })
                
            $(".rankMember").hide()
            $(".rankMember").first().show();
            $(".prepinac a").first().addClass("active");
            window.actualrankings = $(".rankMember").get();
            window.actualRankingIndex = 0;
            $(".prepinac a").click(function(e){
               e.preventDefault();
               var num = this.href.split("/").pop();
               if(num<=$(".rankMember").length){
                   $(".rankMember").hide();
                  $(".prepinac a").removeClass("active");
                   $(".prepinac a").eq(num-1).addClass("active")
                   $(".rankMember").eq(num-1).show();
                   window.actualRankingIndex = num-1;
                 }
            })

        $(".ranking").hover(function(){
            if(window.rankingTimer){
               $(".ranking .stopped").show();
                window.clearInterval(rankingTimer)
             }
            },
            function(){
                if($(".rankMember").length>1){
                     $(".ranking .stopped").hide();
                    window.rankingTimer = window.setInterval(rankingSlider,4500);
                }
            }
         )

        function rankingSlider(){
            var nextIndex = 0;
            if(window.actualRankingIndex>=$(".rankMember").length-1){
                nextIndex = 0;
            } 
            else{
                nextIndex = window.actualRankingIndex+1; 
            }
            $(".rankMember").eq(window.actualRankingIndex).fadeOut(400,function(){
                $(".rankMember").eq(nextIndex).fadeIn();
                $(".prepinac a").removeClass("active");

                $(".prepinac a").eq(nextIndex).addClass("active");
                window.actualRankingIndex=nextIndex;
            });  
	  }
        
        if($(".rankMember").length>1){
            window.rankingTimer = window.setInterval(rankingSlider,4500);
        }
        
        
        
        
        $(".noviny input").keydown(function(e){
            if(e.which==13)  $(".noviny .button").click();
            
        })
        
        $(".noviny input")[0].defaultval = $(".noviny input").val();
        $(".noviny input").focusin(function(){
            if(jQuery.trim(this.value)==this.defaultval){
                this.value = "";
                $(this).removeClass("default");
            }
         })
         
         $(".noviny input").focusout(function(){
             if(!jQuery.trim(this.value)){
                this.value = this.defaultval;
                $(this).addClass("default");
                 $(".noviny .status div").hide();
             }
         })
         
        
        $(".noviny .button").click(function(e){
            e.preventDefault();
            var email = $(".noviny input").val();
            var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
            if(reg.test(email)==false){
                    $(".noviny .status .nomail").show();
                     
            }
            else{
                if(window.subscribeNewslettersSignal){
                  
                    $.post(window.subscribeNewslettersSignal,{email:email},function(data){
                          if(data.success){
                              $(".noviny .status div").hide();
                              if(data.success=="ok"){
                                  $(".noviny .status .success").show();
                                  $(".noviny input").val( $(".noviny input")[0].defaultval)
                                  $(".noviny input").addClass("default");
                              }
                              else if(data.success=="noemail"){
                                  $(".noviny .status .nomail").show()
                              }
                              else{
                                  $(".noviny .status .error").show()
                                  $(".noviny .status .nomail").hide()
                              }
                          }
                        })
                }
            }
        })
    
      //hiiding routes which goes over selected viewPort
      var topRoutesOffset = $(".toproutes div:first").offset().top;
      var maxAllowedOffset = $(".toproutes div:first").outerHeight(true);
      var toRemove =[];
      $(".toproutes tr").each(function(){
          var offset = $(this).offset().top;
          var height = $(this).outerHeight(true);
          var value = (offset+height-5) - topRoutesOffset;
          if(value>maxAllowedOffset) {
              
              toRemove.push(this);
          }
          
      })
     
       var newRoutesOffset = $(".newroutes div:first").offset().top;
      var maxAllowedOffset = $(".newroutes div:first").outerHeight(true);
      $(".newroutes tr").each(function(){
          var offset = $(this).offset().top;
          var height = $(this).outerHeight(true);
          var value = (offset+height-2) - newRoutesOffset;
       
          if(value>maxAllowedOffset)  toRemove.push(this);
      })
     // console.log(toRemove)
      for(var arg in toRemove){
          
          $(toRemove[arg]).remove()
      }
      
  }


//initialiying javscript code for people view
$(document).ready(homepage.initialize)
