$(document).ready(function(){
	$('.imgTextScroller').each(function(){
		$('.images',this).hide();
		$('.texts',this).hide();
	});
});
/*
$.browser.mozilla
$.browser.msie
*/
$(window).load(function(){
    var i_imageMargin = 10;
    var i_imageOnRow = 3;
    var i_imageSpeed = 3000;
    var i_imageClickSpeed = 400;
    $('.imgTextScroller').each(function(){
        $('.images a',this).append('<span></span>');
        var i_imageStartTotal = $('.images a',this).length;
        for(var i_img=0; i_img<i_imageOnRow+1; i_img++){
            $('.images a',this).eq(i_img).clone().appendTo('.images',this);
            $('.images a:last-child',this).attr('cloneFrom',i_img);
        }
        for(var i_img=0; i_img<i_imageOnRow+1; i_img++){
            $('.texts a',this).eq(i_img).clone().appendTo('.texts',this);
            $('.texts a:last-child',this).attr('cloneFrom',i_img);
        }
        var i_imageTotal = $('.images a',this).length;
        if(i_imageTotal > i_imageOnRow){
            var i_imageWidth = $('.images a',this).width();
            var s_imagesHtml = $('.images',this).html();
            $('.texts a',this).hide();
            var i_imageHolderStartWidth = (i_imageStartTotal*i_imageWidth)+((i_imageStartTotal-1)*i_imageMargin);
            var i_imageHolderWidth = (i_imageTotal*i_imageWidth)+((i_imageTotal-1)*i_imageMargin);
            $('.images').attr({
                'i_imageHolderStartWidth':i_imageHolderStartWidth,
                'i_imageHolderWidth':i_imageHolderWidth,
                'i_imageHolderStartTotal':i_imageStartTotal,
                'i_imageStartTotal':i_imageStartTotal,
                'i_imageTotal':i_imageTotal,
                'i_imageWidth':i_imageWidth,
                'i_midNum':Math.ceil((i_imageOnRow-1)/2),
                'i_timeStarted':new Date().getTime(),
                'i_timePlayed':0,
                'i_timeRemaining':(i_imageHolderStartWidth/100)*i_imageSpeed,
                'i_duration':(i_imageHolderStartWidth/100)*i_imageSpeed,
                'b_isMoving':'n',
                'b_isPaused':'n',
                'b_isRunning':'y',
		'i_imageSpeed':i_imageSpeed,
		'i_imageClickSpeed':i_imageClickSpeed,
		'timeout':false
            });
            //MOUSE OVER | OUT\\
            $('.images',this).html('<div>'+s_imagesHtml+'</div>')
            $('.over',this)/*.mousemove(function(){
                var dv_images = $('.images',$(this).parent().parent().parent());
                dv_images.attr('b_isMoving','y');
            })*/.mouseover(function(){
                var dv_images = $('.images',$(this).parent().parent().parent());
                if(dv_images.attr('b_isRunning') == 'y' && dv_images.attr('b_isPaused') == 'n' && dv_images.attr('b_isMoving') == 'n'){
                    dv_images.attr('b_isPaused','y');
                    $('div',dv_images).stop();
                    var i_timeStarted = dv_images.attr('i_timeStarted')*1;
                    var i_now = new Date().getTime();
                    dv_images.attr({
                        'i_timePlayed':(dv_images.attr('i_timePlayed')*1)+(i_now-i_timeStarted),
                        'i_timeRemaining':(dv_images.attr('i_duration')*1)-(i_now-i_timeStarted)
                    });
                }
            }).mouseout(function(){
                var dv_images = $('.images',$(this).parent().parent().parent());
                if(dv_images.attr('b_isRunning') == 'y' && dv_images.attr('b_isPaused') == 'y'){
                    dv_images.attr({
                        'b_isPaused':'n',
                        'b_isMoving':'n'
                    });
                    imgTextScrollerImageAnimate($('.images',$(this).parent()));
                }
            }).click(function(){
                var dv_images = $('.images',$(this).parent());
                var i_txtNum = imgTextScrollerGetCur(dv_images,true);
                var dv_a = $('.texts a',$(this).parent()).eq(i_txtNum);//$('.texts a:nth-child('+i_txtNum+')',$(this).parent());
                var s_aHref = dv_a.attr('href');
                var s_aTarget = dv_a.attr('target');

                if(s_aTarget == '_self' || s_aTarget == '' || s_aTarget == undefined){
                    document.location.href = s_aHref;
                } else {
                    window.open(s_aHref);
                }
            });
            $('.images div',this).width(i_imageHolderWidth);
            imgTextScrollerImageAnimate($('.images',this))
		var b_mayClick = true;
            //PREV\\
            $('.prev',this).click(function(){
		if(b_mayClick){
			b_mayClick = false;
	                var dv_images = $('.images',$(this).parent());
			clearTimeout(dv_images.attr('timeout'));
	                dv_images.attr('b_isRunning','n');
        	        var dv_imagesDiv = $('div',dv_images);
                	var i_txtNum = imgTextScrollerGetCur($('.images',$(this).parent()),false);
						
			if(i_txtNum == 1){
				dv_imagesDiv.css('left',(0-($('a',dv_imagesDiv).eq(dv_images.attr('i_imageHolderStartTotal')).position().left-dv_imagesDiv.position().left+i_imageMargin))+'px');
			}
	                i_txtNum -= 2;
        	        if(i_txtNum < 0){
                	    i_txtNum = dv_images.attr('i_imageHolderStartTotal');
	                }

        	        dv_imagesDiv.stop().animate({
                	    'left':(0-$('a',dv_imagesDiv).eq(i_txtNum).position().left-(i_txtNum > 0 ? i_imageMargin : 0))+'px'
	                },{
            	    	    duration: parseInt(dv_images.attr('i_imageClickSpeed')),
	                    easing: 'swing',
            		    complete:function(){
	        	            var i_txtNum = imgTextScrollerGetCur($(this).parent(),false)-1;
        	        	    if(i_txtNum == 0){
                	        	i_txtNum = $(this).parent().attr('i_imageHolderStartTotal');
	                	        $(this).css('left',(0-($('a',dv_imagesDiv).eq(i_txtNum).position().left+(i_txtNum > 0 ? i_imageMargin : 0)))+'px');
        	            	    }
	                	    //imgTextScrollerTextShow(dv_images,false);//$(this));
				    dv_images.attr('timeout',setTimeout(function(){imgTextScrollerImageAnimate(dv_images);},5000));
				    b_mayClick = true;
			    }
	                });
        	        imgTextScrollerTextShow(dv_images,false);//$(this));
		}
            });
            //NEXT\\
            $('.next',this).click(function(){
		if(b_mayClick){
			b_mayClick = false;
	                var dv_images = $('.images',$(this).parent());
			clearTimeout(dv_images.attr('timeout'));
        	        dv_images.attr('b_isRunning','n');
                	var dv_imagesDiv = $('div',dv_images);
	                var i_txtNum = imgTextScrollerGetCur($('.images',$(this).parent()),false);
        	        //i_txtNum++;
                	dv_imagesDiv.stop().animate({
	                    'left':(0-($('a',dv_imagesDiv).eq(i_txtNum).position().left+(i_txtNum > 0 ? i_imageMargin : 0)))+'px'
 	      	        },{
            	    	    duration: parseInt(dv_images.attr('i_imageClickSpeed')),
	                    easing: 'swing',
            		    complete:function(){
                		    var i_txtNum = imgTextScrollerGetCur($(this).parent(),false)-2;
			            if(i_txtNum == $(this).parent().attr('i_imageHolderStartTotal')-1){
        	               		$(this).css('left','0px');
                		    }
			            //imgTextScrollerTextShow(dv_images,true);//$(this));
				    dv_images.attr('timeout',setTimeout(function(){imgTextScrollerImageAnimate(dv_images);},5000));
				    b_mayClick = true;
            		    }
        		});
                	imgTextScrollerTextShow(dv_images,true);//$(this));
		}
            });
        } else {
            $('.prev, .next',this).hide();
        }
	if($.browser.msie && $.browser.version < 9 || $.browser.safari){
		$('.images',this).fadeIn();
		$('.texts',this).show();
	} else {
		$('.images',this).fadeIn();
		$('.texts',this).fadeIn();
	}
    });
    //FUNCTIONS\\
   	function imgTextScrollerImageAnimate(dv_images){
		if($('div',dv_images).position().left < 0-$(dv_images).attr('i_imageHolderStartWidth')-i_imageMargin+($(dv_images).attr('i_imageWidth')/2)){
			$('div',dv_images).css('left','0px');
        	}
		imgTextScrollerTextShow(dv_images,true);
		$('div',dv_images).animate({
            		'left':Math.round($('div',dv_images).position().left-$(dv_images).attr('i_imageWidth')-i_imageMargin)+'px'
        	},{
            		duration: parseInt(dv_images.attr('i_imageSpeed')),
            		easing: 'linear',
            		complete:function(){
                		imgTextScrollerImageAnimateDone(dv_images);
            		}
        	});
   	}
 	function imgTextScrollerImageAnimateDone(dv_images){
        	if($('div',dv_images).position().left < 0-$(dv_images).attr('i_imageHolderStartWidth')-i_imageMargin+($(dv_images).attr('i_imageWidth')/2)){
			$('div',dv_images).css('left','0px');
        	}
        	imgTextScrollerImageAnimate(dv_images);
	}

    function imgTextScrollerImageAnimateOld(dv_images){
        $('div',dv_images).animate({
            'left':Math.round(0-$(dv_images).attr('i_imageHolderStartWidth')-i_imageMargin)+'px'
        },{
            duration: dv_images.attr('i_timeRemaining')*1,
            easing: 'linear',
            step: function(now, fx){
                imgTextScrollerTextShow($(this));
            },
            complete:function(){
                imgTextScrollerImageAnimateDone(dv_images);
            }
        });
        
    }
    function imgTextScrollerImageAnimateDoneOld(dv_images){
        $('div',dv_images).css('left','0px');
        dv_images.attr({
            'i_timeStarted':new Date().getTime(),
            'i_timeRemaining':Math.round(dv_images.attr('i_imageHolderStartWidth')/100)*i_imageSpeed,
            'i_duration':Math.round(dv_images.attr('i_imageHolderStartWidth')/100)*i_imageSpeed
        });
        imgTextScrollerImageAnimate(dv_images);
    }
    function imgTextScrollerGetCur(dv_images,b_fromAnimate){
        var i_pos = (Math.round(parseInt($('div',dv_images).css('left')))+'').replace('-','');
        var i_txtNum = Math.round(i_pos/$(dv_images).attr('i_imageWidth'))+(b_fromAnimate ? parseInt($(dv_images).attr('i_midNum')) : 0)+1;
        if(b_fromAnimate){
            if(i_txtNum > $(dv_images).attr('i_imageStartTotal')){
                i_txtNum = i_txtNum-$(dv_images).attr('i_imageStartTotal');
            }
        }
        return(i_txtNum);
    }
    /*function imgTextScrollerTextShow(dv_imagesDiv){
        var dv_images = $(dv_imagesDiv).parent();*/
    function imgTextScrollerTextShow(dv_images,b_fromAni){
        var i_txtNum = imgTextScrollerGetCur(dv_images,true);
	if(!b_fromAni){
		i_txtNum -= 2;
	}
	if(i_txtNum < 0){
        	i_txtNum = dv_images.attr('i_imageHolderStartTotal')-1;
	}

        if($(dv_images).attr('cur') != i_txtNum){
            $(dv_images).attr('cur',i_txtNum);
	    
	    if($.browser.msie && $.browser.version < 9 || $.browser.safari){
	            $('.texts a:visible',$(dv_images).parent()).hide();
	            $('.texts a',$(dv_images).parent()).eq(i_txtNum).show();
	    } else {
	            $('.texts a:visible',$(dv_images).parent()).fadeOut();
	            $('.texts a',$(dv_images).parent()).eq(i_txtNum).fadeIn();
	    }

	    if($.browser.msie && $.browser.version < 9 || $.browser.safari){
                $('a:visible span',dv_images).show();
                $('span',$('a',dv_images).eq(i_txtNum)).hide();
                //$('a[cloneFrom="'+(i_txtNum+(!b_fromAni ? 1 : 0))+'"] span',dv_images).hide();
		$('a[cloneFrom="'+i_txtNum+'"] span',dv_images).hide();
            } else {
                $('a:visible span',dv_images).fadeIn();
                $('span',$('a',dv_images).eq(i_txtNum)).fadeOut();
                //$('a[cloneFrom="'+(i_txtNum+(!b_fromAni ? 1 : 0))+'"] span',dv_images).fadeOut();
		$('a[cloneFrom="'+i_txtNum+'"] span',dv_images).fadeOut();
            }

        }
    }
});
