Function.prototype.bind = function(scope, args){
 var _this = this;
 return function() {
  return _this.apply(scope, args ? args : []);
 }
};

var HT = {
	buttons:{
		left: null,
		right: null
	},
	cont: null,
	fullwidth: 100,
	width: 49,
	len: 0,
	toheight: 32,
	height: 70,
	margintop: 18,
	diff: 0,
	diff2: 0,
	diff3: 0,
	freq: 500,
	contwidth: 288,
	inprocess: false,
	leftend: null,
	rightend: null,
	rotateto: 'right',
	init: function(){
		HT.buttons['left'] = jQuery('a[class="click-left"]');
		HT.buttons['right'] = jQuery('a[class="click-right"]');
		HT.buttons['left'].click(HT.left);
		HT.buttons['right'].click(HT.right);
		HT.cont = jQuery('div[class="inner_test_logos"]');
		HT.list = HT.cont.children();
		HT.len = (HT.list.length)*HT.width+HT.fullwidth;
		setTimeout(HT.rotate.bind(this), 10000);
	},
	rotate: function(){
		if(HT.inprocess === true || HT.rightend === true){
			HT.rotateto = 'left';
		} 
		
		if(HT.inprocess === true || HT.leftend === true){
			HT.rotateto = 'right';
		}
		if(HT.rotateto == 'left'){
			HT.left();
		}
		if(HT.rotateto == 'right'){
			HT.right();
		}
		setTimeout(HT.rotate.bind(this), 10000);
	},
	left: function(){
		if(HT.inprocess === true || HT.leftend === true) return;
		if(HT.diff < 0){
			if(HT.diff2 == 0){
				HT.diff += HT.width;
				HT.cont.animate({left: HT.diff+"px"}, HT.freq);
			}else{
				HT.diff2--;
			}
		}
		
		
		var el = HT.cont.children("[class='icon_active']");
		var prev = el.prev();
		if(prev.length > 0){
			if(HT.diff >= 0){
				HT.diff3++;
			}
			HT.inprocess = true;
			HT.rightend = null;
			el.children().animate({height:HT.toheight+'px'}, HT.freq, function(_el){
				_el.removeClass("icon_active");
			}.bind(this, [el]));
			el.animate({'margin-top': HT.margintop+'px'}, HT.freq, function(_el){
			}.bind(this, [el]));
	
			
			prev.children().animate({height:HT.height+'px'}, HT.freq, function(_el){
				_el.addClass("icon_active");
			}.bind(this, [prev]));
			prev.animate({'margin-top': '0px'}, HT.freq, function(_el){
				_el.css({'margin-top': '0px'});
			}.bind(this, [prev]));
			var nm = Number(el.attr("_id"));
	        var textel = jQuery("#test_text"+nm);
	        var textelnext = textel.prev();
	        textel.hide('slide', {'direction': 'right'}, 900);
	        textelnext.effect('slide', {'direction': 'left'}, 1000, function(){HT.inprocess = false;});             
	        
		}else{
			HT.leftend = true;
			HT.diff2++;
		}

		/*var first = jQuery("a.icon_visible:first").attr("id");
        var first_number = Number(first.substr(9,first.length-9));
        if (first_number==jQuery(".comments a.icon").length) second_number = '1';
        else second_number = (first_number+1).toString();
        var next = jQuery("a.icon_visible:last").attr("id");
        var next_number = Number(next.substr(9,next.length-9));
        if (next_number==jQuery(".comments a.icon").length) next_number = '1';
        else next_number = (next_number+1).toString();
        jQuery("#test_logo"+next_number).removeClass('icon_hidden');
        jQuery("#test_logo"+next_number).addClass('icon_visible');
        jQuery("#test_logo"+next_number+" img").animate({maxWidth: "40px"},300);
        jQuery("#"+first+" img").animate({maxWidth: "1px"},300,function(){
            jQuery("#"+first).removeClass('icon_visible');
            jQuery("#"+first).addClass('icon_hidden');
        });
        var elems = jQuery("a.icon_visible");
        var imgs = jQuery("a.icon_visible img");                            
        jQuery(".inner_test_logos").animate({marginLeft: "-=45px"},500,function(){
            jQuery(".comments a.icon:first").insertAfter(".comments a.icon:last");
            var mleft = jQuery(".inner_test_logos").css('margin-left');
            mleft=(Number(mleft.substr(0,mleft.length-2))+45).toString()+'px';
            jQuery(".inner_test_logos").css('margin-left',mleft);
        });
        jQuery(imgs[1]).animate({maxWidth: "40px",opacity: 0.3},300);
        jQuery(elems[2]).animate({width: "40px"},300);
        jQuery(imgs[2]).animate({maxWidth: "40px",opacity: 0.5},300);
        jQuery(elems[3]).animate({width: "100px"},300);
        jQuery(imgs[3]).animate({maxWidth: "100px",opacity: 1},300);
        jQuery(imgs[4]).animate({maxWidth: "40px",opacity: 0.5},300);
        var main_logo = jQuery(elems[2]).attr("id");
        var main_logo_number = Number(main_logo.substr(9,main_logo.length-9));
        jQuery(".quote p").animate({opacity: 0},500,function(){
            jQuery(".quote p").hide();
            jQuery("#test_text"+main_logo_number).show();
            jQuery("#test_text"+main_logo_number).animate({opacity: 1},500);
        });
        jQuery("span.autor").animate({opacity: 0},500,function(){
            jQuery("span.autor").hide();
            jQuery("#test_title"+main_logo_number).show();
            jQuery("#test_title"+main_logo_number).animate({opacity: 1},500);                                    
        });                            
        //clearInterval(switch_interval);
        //switch_interval = setInterval("switch_test_right()", 5000);         
        */                  
    },
	right: function(){
		if(HT.inprocess === true || HT.rightend === true) return;
		if(HT.len > HT.contwidth  && (HT.len + HT.diff - HT.fullwidth) >=  HT.contwidth){
			if(HT.diff3 == 0){
				HT.diff -= HT.width;
				HT.cont.animate({left: HT.diff+"px"}, HT.freq);
			}else{
				HT.diff3--;
			}
		}else{
			HT.diff2++;
		}
		
		
		var el = HT.cont.children("[class='icon_active']");
		var next = el.next();
		if(next.length > 0){
			HT.leftend = null;
			HT.inprocess = true;
			el.children().animate({height:HT.toheight+'px'}, HT.freq, function(_el){
				_el.removeClass("icon_active");
			}.bind(this, [el]));
			el.animate({'margin-top': HT.margintop+'px'}, HT.freq, function(_el){
			}.bind(this, [el]));
	
			
			next.children().animate({height:HT.height+'px'}, HT.freq, function(_el){
				_el.addClass("icon_active");
			}.bind(this, [next]));
			next.animate({'margin-top': '0px'}, HT.freq, function(_el){
				_el.css({'margin-top': '0px'});
				
			}.bind(this, [next]));
			var nm = Number(el.attr("_id"));
	        var textel = jQuery("#test_text"+nm);
	        var textelnext = textel.next();
	        textel.hide('slide', {'direction': 'left'}, 900);
	        textelnext.effect('slide', {'direction': 'right'}, 1000, function(){HT.inprocess = false;});
	        
		}else{
			HT.rightend = true;
			HT.diff3++;
		}
		

        
		/*var first = jQuery("a.icon_visible:first").attr("id");
        var second = jQuery("a.icon_hidden:first").attr("id");
        var second_number = Number(second.substr(9,second.length-9));
        var next = jQuery("a.icon_visible:last").attr("id");
        jQuery("#"+second).removeClass('icon_hidden');
        jQuery("#"+second).addClass('icon_visible');                            
        jQuery("#"+second+" img").animate({maxWidth: "40px"},300);
        jQuery("#"+next+" img").animate({maxWidth: "1px"},300,function(){
            jQuery("#"+next).removeClass('icon_visible');
            jQuery("#"+next).addClass('icon_hidden');
        });
        var elems = jQuery("a.icon_visible");
        var imgs = jQuery("a.icon_visible img");                            
        jQuery(".inner_test_logos").animate({marginLeft: "+=45px"},500,function(){
            jQuery(".comments a.icon:last").insertBefore(".comments a.icon:first");
            var mleft = jQuery(".inner_test_logos").css('margin-left');
            mleft=(Number(mleft.substr(0,mleft.length-2))-45).toString()+'px';
            jQuery(".inner_test_logos").css('margin-left',mleft);
        });
        jQuery(imgs[0]).animate({maxWidth: "40px",opacity: 0.3},300);
        jQuery(imgs[1]).animate({maxWidth: "40px",opacity: 0.5},300);
        jQuery(elems[2]).animate({width: "100px"},300);
        jQuery(imgs[2]).animate({maxWidth: "100px",opacity: 1},300);
        jQuery(elems[3]).animate({width: "40px"},300);
        jQuery(imgs[3]).animate({maxWidth: "40px",opacity: 0.5},300);
        jQuery(imgs[4]).animate({maxWidth: "40px",opacity: 0.3},300);                            
        var main_logo = jQuery(elems[2]).attr("id");
        var main_logo_number = Number(main_logo.substr(9,main_logo.length-9));
        jQuery(".quote p").animate({opacity: 0},500,function(){
            jQuery(".quote p").hide();
            jQuery("#test_text"+main_logo_number).show();
            jQuery("#test_text"+main_logo_number).animate({opacity: 1},500);
        });
        jQuery("span.autor").animate({opacity: 0},500,function(){
            jQuery("span.autor").hide();
            jQuery("#test_title"+main_logo_number).show();
            jQuery("#test_title"+main_logo_number).animate({opacity: 1},500);                                    
        });
        //clearInterval(switch_interval);
        //switch_interval = setInterval("switch_test_right()", 5000);      
       */                      
    }
};
jQuery(document).ready(HT.init);
