var $j = jQuery;//.noConflict();

initPreloader = function(){
    var headerImages = [
        {
            src: '/img/visual/visual1.jpg',
            next: 1,
            prev: 7
        },
        {
            src: '/img/visual/visual2.jpg',
            next: 2,
            prev: 0
        },
        {
            src: '/img/visual/visual3.jpg',
            next: 3,
            prev: 1
        },
        {
            src: '/img/visual/visual4.jpg',
            next: 4,
            prev: 2
        },
        {
            src: '/img/visual/visual5.jpg',
            next: 5,
            prev: 3
        },
        {
            src: '/img/visual/visual6.jpg',
            next: 6,
            prev: 4
        },
        {
            src: '/img/visual/visual7.jpg',
            next: 7,
            prev: 5
        },
        {
            src: '/img/visual/visual8.jpg',
            next: 0,
            prev: 6
        }
    ];
	var nextTimer = undefined;
	var nowImg = 0;
    var prevImage = function(id){
        jQuery( '#headerImg' + id ).animate({'opacity': 0}, 1000, 'swing', function(){
			nowImg = headerImages[id].prev;
            jQuery( '#headerImg' + nowImg ).animate({'opacity': 1}, 1000, 'swing', function(){
                nextTimer = setTimeout(function(){ prevImage( nowImg ); }, 5000);
            });
        });
    }
    var nextImage = function(id){
        jQuery( '#headerImg' + id ).animate({'opacity': 0}, 1000, 'swing', function(){
			nowImg = headerImages[id].next;
            jQuery( '#headerImg' + nowImg ).animate({'opacity': 1}, 1000, 'swing', function(){
                nextTimer = setTimeout(function(){ nextImage( nowImg ); }, 5000);
            });
        });
    }
    var imgsOut = '';
    for ( var i = headerImages.length-1; i >= 0; i-- ){
        imgsOut = imgsOut + '<img id="headerImg' + i + '" src="' + headerImages[i].src + '" alt="">';
    }
    jQuery('#pageHeader>.photoBlock:first').append('<div id="animateHeader">'+imgsOut+'</div>');
    setTimeout(function(){
        //jQuery('#pageHeader>.visual:first').css({'backgroundImage': 'url(/assets/templates/main/images/visual.jpg)'});
        jQuery('#animateHeader img').not('#headerImg0').css({'opacity': 0});
        jQuery('#animateHeader').css({'visibility': 'visible'});
		$j('#pageHeader>.photoBlock:first>img').remove();
        setTimeout(function(){ nextImage(nowImg); }, 5000);
    }, 300);
	
	jQuery('#photoPanel').each(function(){
		var left = jQuery('.left', this);
		var pause = jQuery('.pause', this);
		var right = jQuery('.right', this);
		left.click(function(){
			clearTimeout(nextTimer);
			prevImage(nowImg);
		});
		right.click(function(){
			clearTimeout(nextTimer);
			nextImage(nowImg);
		});
		pause.click(function(){
			clearTimeout(nextTimer);
		});
	});
}

$j(document).ready(function(){
	$j('#pageHeader>.photoBlock:first').each(function(){
		initPreloader();
	});

	$(document).ajaxStop(function(){
		$(document).find('a').each(function(){
			if (this.href.match(/http\:\/\/www\.coral/gi) ) {
				$(this).attr('href', 'javascript://');
				$(this).attr('target', '');
				$(this).bind('click', function(){
					return false;
				});
			}
		});
	});
});


