if ( typeof $w == 'undefined' ) {
	$w = window.$w = window;
}

$w.scroll = function (){
    $('right_1').effect('left',{
        duration: 500,
        fps: 35,
        transition: Fx.Transitions.Cubic.easeInOut,
        onComplete: $w.right_end_scroll.pass('right_1')
    }).start(-($w.right_1_counter*scroll_amt), -(($w.right_1_counter-1)*scroll_amt));
    $('right_2').effect('left',{
        duration: 500,
        fps: 35,
        transition: Fx.Transitions.Cubic.easeInOut,
        onComplete: $w.right_end_scroll.pass('right_2')
    }).start(-($w.right_2_counter*scroll_amt), -(($w.right_2_counter-1)*scroll_amt));
    $('right_3').effect('left',{
        duration: 500,
        fps: 35,
        transition: Fx.Transitions.Cubic.easeInOut,
        onComplete: $w.right_end_scroll.pass('right_3')
    }).start(-($w.right_3_counter*scroll_amt), -(($w.right_3_counter-1)*scroll_amt));
    
    $w.setTimeout(function(){$w.scroll();}, 3000); //9000
}

if ( true /*String(document.location.href).match(/&dan=testing/)*/ ) {
	$w.scroll = function (){
		//bail if no ads??
		if ( $w.right_1_counter > 0 && $w.right_2_counter > 0 ) {
			if ( $$('#right_1 a')[$w.right_1_counter-1].href == $$('#right_2 a')[$w.right_2_counter-1] ) {
				$$('#right_2 a')[$w.right_2_counter-1].nomatch = false;
				$$('#right_2 a').each(function(e,i) {
					if ( $$('#right_2 a')[$w.right_2_counter-1].nomatch ) {
						return;
					}
					if ( i > $w.right_2_counter+1 || i < $w.right_2_counter-1 ) {
						if ( $$('#right_2 a')[$w.right_2_counter-1].href != e.href ) {
							new_vals = [e.href,e.getElements('img')[0].src];
							e.href = $$('#right_2 a')[$w.right_2_counter-1].href;
							e.getElements('img')[0].src = $$('#right_2 a')[$w.right_2_counter-1].getElements('img')[0].src;
							$$('#right_2 a')[$w.right_2_counter-1].nomatch = true;
							$$('#right_2 a')[$w.right_2_counter-1].href = new_vals[0];
							$$('#right_2 a')[$w.right_2_counter-1].getElements('img')[0].src = new_vals[1];
						}
					}
				});
			}
		}
		if ( $w.right_1_counter > 0 && $w.right_2_counter > 0 && $w.right_3_counter > 0 ) {
			if ( $$('#right_3 a')[$w.right_3_counter-1].href == $$('#right_2 a')[$w.right_2_counter-1] ||
				 $$('#right_3 a')[$w.right_3_counter-1].href == $$('#right_1 a')[$w.right_1_counter-1] ) {
				$$('#right_3 a')[$w.right_3_counter-1].nomatch = false;
				$$('#right_3 a').each(function(e,i) {
					if ( $$('#right_3 a')[$w.right_3_counter-1].nomatch ) {
						return;
					}
					if ( i > $w.right_3_counter+1 || i < $w.right_3_counter-1 ) {
						if ( $$('#right_3 a')[$w.right_3_counter-1].href != e.href ) {
							new_vals = [e.href,e.getElements('img')[0].src];
							e.href = $$('#right_3 a')[$w.right_3_counter-1].href;
							e.getElements('img')[0].src = $$('#right_3 a')[$w.right_3_counter-1].getElements('img')[0].src;
							$$('#right_3 a')[$w.right_3_counter-1].nomatch = true;
							$$('#right_3 a')[$w.right_3_counter-1].href = new_vals[0];
							$$('#right_3 a')[$w.right_3_counter-1].getElements('img')[0].src = new_vals[1];
						}
					}
				});
			}
		}
		$('right_1').effect('left',{
		    duration: 500,
        fps: 35,
		    transition: Fx.Transitions.Cubic.easeInOut,
		    onComplete: $w.right_end_scroll.pass('right_1')
		}).start(-($w.right_1_counter*scroll_amt), -(($w.right_1_counter-1)*scroll_amt));
		$('right_2').effect('left',{
		    duration: 500,
        fps: 35,
		    transition: Fx.Transitions.Cubic.easeInOut,
		    onComplete: $w.right_end_scroll.pass('right_2')
		}).start(-($w.right_2_counter*scroll_amt), -(($w.right_2_counter-1)*scroll_amt));
		$('right_3').effect('left',{
		    duration: 500,
        fps: 35,
		    transition: Fx.Transitions.Cubic.easeInOut,
		    onComplete: $w.right_end_scroll.pass('right_3')
		}).start(-($w.right_3_counter*scroll_amt), -(($w.right_3_counter-1)*scroll_amt));
	    $w.setTimeout(function(){$w.scroll();}, 9000);
	}
}

$w.right_1_count;
$w.right_2_count;
$w.right_3_count;
// used to keep track of the current image
$w.right_1_counter;
$w.right_2_counter; 
$w.right_3_counter;
var scroll_amt = 180;


$w.right_end_scroll = function(type_id, count){
    $w[type_id+'_counter']--;
    if($w[type_id+'_counter']==0){
        new Fx.Style(type_id,'left').set(-($w[type_id+'_counter']*scroll_amt));
        $w[type_id+'_counter']=$w[type_id+'_count'];
    }
}

window.addEvent('domready', function() {
	//if ( String(document.location.href).match(/&dan=testing/) ) {
		//remove smaller ads for businesses which have a big ad
		//get big ad links
		var large_ads = $$('#right_3 a');
		var links = {};
		for ( i=0,m=large_ads.length; i<m; i++ ) {
			links[large_ads[i].href] = true;
		}
		smaller_ads = $$('#right_1 a, #right_2 a');
		for ( i=0,m=smaller_ads.length; i<m; i++ ) {
			if ( typeof links[smaller_ads[i].href] != 'undefined' ) {
				$(smaller_ads[i]).remove();
			}
		}
	//}
	//correct right col pos in ie6
	if ( window.ie6 == true ) {
		$('rightCol').setStyle('margin','10px 1px 0 316px');
	}
    //set these to the number of images to scroll through
    $w.right_1_count = $('right_1').getElementsByTagName('a').length ;
    $w.right_2_count = $('right_2').getElementsByTagName('a').length ;
    $w.right_3_count = $('right_3').getElementsByTagName('a').length ;
    $('right_1').setStyle('width', (($w.right_1_count+2)*scroll_amt)+'px');
    $('right_2').setStyle('width', (($w.right_2_count+2)*scroll_amt)+'px');
    $('right_3').setStyle('width', (($w.right_3_count+2)*scroll_amt)+'px');

    // used to keep track of the current image
    $w.right_1_counter=$w.right_1_count;
    $w.right_2_counter=$w.right_2_count;
    $w.right_3_counter=$w.right_3_count;

    new Fx.Style('right_1','left').set(-($w.right_1_counter*scroll_amt));
    new Fx.Style('right_2','left').set(-($w.right_2_counter*scroll_amt));
    new Fx.Style('right_3','left').set(-($w.right_3_counter*scroll_amt));
    
    //correct content height
    center_col = $$('#centreCol');
    if ( typeof center_col.getCoordinates()[0] != 'undefined' ) {
	if ( center_col.getCoordinates()[0].height <= 615 ) {
		//center_col.setStyle('height',630);
		var spacer = new Element('div');
		spacer.id = "content_overlay_spacer";
		center_col[0].appendChild(spacer);
		$$('#content_overlay_spacer').setStyle('height',630-center_col.getCoordinates()[0].height);
	}
    }
    
    $w.scroll();

    //$w.setInterval($w.scroll, 9000);
});
