$(document).ready(function(){
	$("a[rel='external']").attr({
		target: "_blank"
	});
	
	// CSS Hack for Safari browsers
	var userAgent = navigator.userAgent.toString().toLowerCase();
	if ((userAgent.indexOf('safari') != -1) && !(userAgent.indexOf('chrome') != -1)) 
	{
		$("#header ul li").css("font-weight","normal");
	}
	clearinput("#search");
	var itstart=-1;
	if (typeof(howmany)=="undefined") {
    howmany=0;
  }
  else
  {
    if (location.hash)
    {
      lasti=$(".fixgallery .center div:last").attr('rel');
      //     location.hash='#'+(lasti-needshow);
           
      itstart=lasti-location.hash.substring(1)-1//-2;
              /*   alert(itstart);    */
      $(".standard .center div:visible").hide();
      $(".standard .center div[rel="+(lasti-location.hash.substring(1)+1)+"]").show();
    }
  //  
  //  start
  }

    obj=$('#flcarousel').jcarousel({
        /*auto: 4,*/
        scroll:1, 
        start:itstart,
        visible:howmany,
        wrap:'circular',
        buttonNextHTML: '<a class="button next jcarousel-next" href="#" title="Next">Next</a>',
        buttonPrevHTML: '<a class="button back jcarousel-prev" href="#" title="Back">Back</a>',
        initCallback: mycarousel_initCallback,
        itemVisibleInCallback: {
            onAfterAnimation:  mycarousel_itemVisibleInCallbackAfterAnimation
        }

    });
    


$('.jcarousel-next').bind('click', function() {
  return false;
});
$('.jcarousel-prev').bind('click', function() {
  //carousel.prev();
  return false;
});

});

function clearinput(el) {
	var inpval = $(el).val();
	
	$(el).focus(function(){
		if ($(this).val() == inpval) {
			$(this).val("");
		}
	});
	$(el).blur(function(){
		if ($(this).val() == "") {
			$(this).val(inpval);
		}
	});
}


function mycarousel_itemVisibleInCallbackAfterAnimation(carousel, item, idx, state) {
   // alert(idx);
};


function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
   /* carousel.buttonNext.bind('click', function() {
        clearTimeout(carousel.timer);
        carousel.startAuto(3);
    });
    carousel.buttonPrev.bind('click', function() {
        clearTimeout(carousel.timer);
        carousel.startAuto(3);
    });       */
    
       

    // Pause autoscrolling if the user moves with the cursor over the clip.
    /*carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });  */ 
    
    /* $('.fixgallery .center div').bind('hover', function() {
        carousel.stopAuto();
    }); */ 
          
    $('#flcarouselout ul li a').bind('click', function() {
       // clearTimeout(carousel.timer);
        firsti=carousel.first;
        lasti=carousel.last;
        /*
        if (howmany>3)
        {
          middlei=lasti-2;
        }
        else {
          middlei=lasti-1;        
        }       */
        centr=Math.ceil((lasti-firsti)/2);
        //alert(firsti+':'+lasti+'='+));
        middlei=firsti+centr;
        
        nowi=$(this).parent().attr('jcarouselindex');
        //alert(middlei);
        //needshow=$(this).attr('rel');     
        if (middlei+1<nowi)
        {
          carousel.options.scroll=2
          carousel.next();     
          carousel.options.scroll=1;                    
          //changebigim(needshow); 
        }
        else if (middlei<nowi){
          carousel.next();            
          //changebigim(needshow);   
        }
        else if (middlei-1>nowi){
          carousel.options.scroll=2;
          carousel.prev();   
          carousel.options.scroll=1;
          //changebigim(needshow);  
        }        
        else if (middlei>nowi){
          carousel.prev();              
          //changebigim(needshow); 
        }   

        //alert(carousel.options.size);
      return false;
    });
};

function changebigim(needshow)
{
  $(".standard .center div:visible").fadeOut(
      300   ,  function () {
         $(".standard .center div[rel="+needshow+"]").fadeIn(300,function(){
           lasti=$(".fixgallery .center div:last").attr('rel');
           location.hash='#'+(lasti-needshow+1);
         }    
         );
      }
  );
       
  /*
  $(".standard .center div:visible").hide();
  $(".standard .center div[rel="+needshow+"]").show();  */
}
