// JavaScript Document

var timerval = '';
// set timeout interval for slideshow
var slideTimer = 5000;

jQuery(function() {
	// handle slidshow thumb clicks
	jQuery('#slidesBox img').click(function() {
		jQuery('#slidesBox img').removeClass('active');
		jQuery('#slidesBox img').removeClass('last-active');
		jQuery(this).addClass('active');
		jQuery(this).addClass('last-active');
		var image = $(this).attr('title');
		clearInterval(timerval);
		//alert(jQuery('#slideViewBox').attr('style'));
		jQuery('#slideViewBoxOld').attr('style',jQuery('#slideViewBox').attr('style'));
		jQuery('#slideViewBoxOld').css('display','block');
		jQuery('#slideViewBox').hide();
		$(this).onImagesLoaded(function(_this){
		  jQuery('#slideViewBox').css('background','url('+image+') center center no-repeat');
		  jQuery('#slideViewBoxOld').fadeOut('slow');
		  jQuery('#slideViewBox').fadeIn('slow');
		  timerval = setInterval( "slideSwitch()", slideTimer );
		});
	});
	var image = $('#slidesBox img:first-child').attr('title');
	jQuery('#slideViewBox').css('background','url('+image+') center center no-repeat');
	
	// run slideshow
    if($('#slidesBox img').hasClass('active')) {
	    timerval = setInterval( "slideSwitch()", slideTimer );
   };

	jQuery('#communityMenu a').click(function() {
		jQuery('#communityMenu a').removeClass('communityNavArrSel');
		jQuery(this).addClass('communityNavArrSel');
	});
	
});

jQuery.fn.onImagesLoaded = function(_cb) { 
  return this.each(function() {
 
    var $imgs = (this.tagName.toLowerCase()==='img')?$(this):$('img',this),
        _cont = this,
            i = 0,
    _done=function() {
      if( typeof _cb === 'function' ) _cb(_cont);
    };
 
    if( $imgs.length ) {
      $imgs.each(function() {
        var _img = this,
        _checki=function(e) {
          if((_img.complete) || (_img.readyState=='complete'&&e.type=='readystatechange') )
          {
            if( ++i===$imgs.length ) _done();
          }
          else if( _img.readyState === undefined ) // dont for IE
          {
            $(_img).attr('title',$(_img).attr('title')); // re-fire load event
          }
        }; // _checki \\
 
        $(_img).bind('load readystatechange', function(e){_checki(e);});
        _checki({type:'readystatechange'}); // bind to 'load' event...
      });
    } else _done();
  });
};

function displyCopyBlock(copyId){
	$('.commContentBox').css('display','none');
	$('#content'+copyId).css('display','block');
	if(copyId == 4){
			initialize();
	}
}

function slideSwitch() {
    var $active = $('#slidesBox IMG.active');
    //if ( $active.length == 0 ) $active = $('#slidesBox IMG:last');

    var $next =  ($active.length == 0 ? $('#slidesBox IMG:first') : $active.next());
	$active.removeClass('active');
    $next.addClass('active');
	var image = $($next).attr('title');

	
	if(image) {
		jQuery('#slideViewBoxOld').attr('style',jQuery('#slideViewBox').attr('style'));
		jQuery('#slideViewBoxOld').css('display','block');
		jQuery('#slideViewBox').hide();
		$($next).onImagesLoaded(function(_this){
		  jQuery('#slideViewBox').css('background','url("'+image+'") center center no-repeat');
		  jQuery('#slideViewBoxOld').fadeOut('slow');
		  jQuery('#slideViewBox').fadeIn('slow');
		});
	} else {
		slideSwitch();
	}
}

