﻿ var percentage =0;
 
$(document).ready(function(){

  $(".external").click(function(){
     window.open($(this).attr('href'));
     return false;
  });
  if( $('#homeslides li').size()>0){
  
      $('#homeslides li').each(function(){
         $(this).css("display","none");
         bgvis=$(this).find(".visualurl").text();
         $(this).find(".slide").css("background-image","url("+bgvis+")");
      });
      startHomeFade();
  }
  $('#homeslides').bind('mouseenter',function(){
       clearTimeout(fadeTimeout);
  });
 
  $('#homeslides').bind('mouseleave',function(){
       fadeTimeout = setTimeout("homeCycle()",fadeTimer);

  });
    $('#homebuttons').bind('mouseenter',function(){
       clearTimeout(fadeTimeout);
  });
 
  $('#homebuttons').bind('mouseleave',function(){
       fadeTimeout = setTimeout("homeCycle()",fadeTimer);

  });
  $('#buttonlinks a').click(function(){
    index = parseInt($(this).find("span").text())-1;
    prev = currentIndex;
    currentIndex = index;
   $('#homeslides li:eq('+prev+')').css("z-index","10");
   $('#homeslides li:eq('+currentIndex+')').css("z-index","9");
   $('#homeslides li:eq('+prev+')').css("display","none");
   $('#homeslides li:eq('+currentIndex+')').css("display","block");
   $('#buttonlinks a:eq('+prev+')').removeClass("selected");
   $('#buttonlinks a:eq('+currentIndex+')').addClass("selected");
    return false;
  });
  
   $("#caselist ul").css("width",($("#caselist ul li").size()*320)+"px");
    $('#scrollhandle').draggable({ axis: 'x', containment: "parent" });
    $('#scrollhandle').bind('dragstart', function(event, ui) {
    });
    $('#scrollhandle').bind('drag', function(event, ui) {
      var pwidth =$(this).parent().width();
      var xpos = parseInt($(this).css('left'));
       percentage =  (xpos/(pwidth-197-20));
       if(percentage>1){
       percentage =1;
       }
      var newxPos = parseInt(($("#caselist ul").width()-((320*3)))*percentage,10);
      if(newxPos <0){
        newxPos = 0;
      }
       $("#caselist ul").css("left","-"+newxPos+"px");
    });
    $('#scrollhandle').bind('dragstop', function(event, ui) {
  // alert( $("#caselist ul").css("left"));
    // alert( $("#caselist ul").css("width"));
     //alert(percentage);
   });
   $('#scrollbar').click(function(e){
      var x = e.pageX - $(this).position().left;
       x=x-16-98;
       if(x <0){
         x=0;
       }
       if(x > $('#scrollbar').width()-$('#scrollhandle').width()){
        x= $('#scrollbar').width()-$('#scrollhandle').width();
       }
      $('#scrollhandle').css("left",x+"px");
      var pwidth = $('#scrollhandle').parent().width();
      var xpos = parseInt( $('#scrollhandle').css('left'));
       percentage =  (xpos/(pwidth-197-20));
       if(percentage>1){
       percentage =1;
       }
      var newxPos = parseInt(($("#caselist ul").width()-((320*3)))*percentage,10);
      if(newxPos <0){
        newxPos = 0;
      }
       $("#caselist ul").css("left","-"+newxPos+"px");
      
       
     // $('#scrollhandle').drag();
   });
  // $('#caselist a').lightBox();
  $(".caselinkbox").click(function(){
  
        $(this).find("#overimg").remove();
  });
       $(".caselinkbox").bind("mouseenter",function(){
       
         img = $(this).find("span");
         post = $(this).position();
        /* if(  img.css("position") != "absolute"){
             img.css("position","absolute");
             img.css("top",post.top+"px");
             img.css("left",post.left+"px");
             img.css("z-index",3999);
         }*/
          newImage = $(this).append('<img src="/images/case_over.png" id="overimg"/>');
          $(this).find("#overimg").css("position", "absolute");
           $(this).find("#overimg").css("top",post.top+13+"px");
           $(this).find("#overimg").css("left",post.left+13+"px");
          $(this).find("#overimg").css("z-index", 4000);
          
       // alert($(this).html());
        //img.css("float","relative");
       // alert('enter');
       });
       $(".caselinkbox").bind("mouseleave",function(){
        // alert('exit');
        $(this).find("#overimg").remove();
       // alert($(this).html());
       });
 //$(document).pngFix(".readmore");
});
Shadowbox.init();
var currentIndex = 0;
var fadeTimer = 8000;
var fadeTimeout = null;
function startHomeFade(){
  $('#homeslides li:eq(0)').css("display","block");
  
 $('#buttonlinks a:eq(0)').addClass("selected");
 fadeTimeout = setTimeout("homeCycle()",fadeTimer);
}
function homeCycle(){
  prev = currentIndex;
   if(currentIndex+1 <$('#homeslides li').size()){
     currentIndex++;
   }else{
  
     currentIndex = 0;
   }
   $('#homeslides li:eq('+prev+')').css("z-index","10");
   $('#homeslides li:eq('+currentIndex+')').css("z-index","9");
    $('#homeslides li:eq('+prev+')').fadeOut(3000);
    $('#homeslides li:eq('+currentIndex+')').fadeIn(1000);
   $('#buttonlinks a:eq('+prev+')').removeClass("selected");
   $('#buttonlinks a:eq('+currentIndex+')').addClass("selected");
   
    
 fadeTimeout = setTimeout("homeCycle()",fadeTimer);

}

