﻿// JavaScript Document
// 2010-04-02 by xing@xc

//参数:
// id:跑马灯的窗口id(必填),time:时间(1000=1秒)值越大速度越慢,dir:方向(left/top)


//<div id="Marqu_Parent" style="height:150px; width:100px;float:left; overflow: auto; border:1px solid">
//  <div style="width:200px; height:50px; float:left;">
//    <div style="width:50px; height:50px;float:left;">aaa</div>
//    <div style="width:50px; height:50px;float:left;">bbb</div>
//    <div style="width:50px; height:50px;float:left;">ccc</div>
//    <div style="width:50px; height:50px;float:left;">ddd</div>
//  </div>
//</div>

// 用法:var demo = new Marquee('Marqu_Parent',1000,'left')

function Marquee()
{
	this.id = arguments[0];
	this.time = arguments[1];
	this.dir =	arguments[2];
	
	if (isNaN(this.time))
	{
		this.time = 1
	}
	
	if ($.trim(this.dir) == '')
	{
		this.dir = 'left';
	}
	
	var temp_table;
	
	if (this.dir == 'left')
	{
		temp_table = '<table><tr><td id="'+this.id+'_1">temp_maqu_html</td><td id="'+this.id+'_2">temp_maqu_html</td></tr></table>'
	}
	else
	{
		temp_table = '<table><tr><td id="'+this.id+'_1">temp_maqu_html</td></tr><tr><td id="'+this.id+'_2">temp_maqu_html</td></tr></table>'
	}
	this.temp_html = $('#'+this.id).html();
	//this.temp_html = this.temp_html.replace('_1','_2')
	temp_table = temp_table.replace(/temp_maqu_html/g,this.temp_html);
	
	$('#'+this.id).empty().append(temp_table);
}

Marquee.prototype.Start = function ()
{
	var Start = this;

	Start.Go = function () 
	{
		Start.Scroll();
	}
	
	//Start.Scroll();
	Start.OK = setInterval(Start.Go, parseInt(Start.time));
	
	$('#'+Start.id).mousemove(
		function ()
		{
			clearInterval(Start.OK);
		}
	);
	
	$('#'+Start.id).mouseout(
		function ()
		{
			clearInterval(Start.OK);
			Start.OK = setInterval(Start.Go, Start.time);
		}
	);
}

Marquee.prototype.Scroll = function ()
{
	var Scroll = this;

	//var ment = $('.Marqu_Content');
	if (Scroll.dir == 'left')
	{
		Scroll.Left();
	}
	
	if (Scroll.dir == 'top')
	{
		Scroll.Top();
	}
}

Marquee.prototype.Left = function ()
{
	var id0 = document.getElementById(this.id);
	var id1 = document.getElementById(this.id+'_1');
	var id2 = document.getElementById(this.id+'_2');
	
 	if(id2.offsetWidth - id0.scrollLeft <= 0)
  {
		id0.scrollLeft -= id1.offsetWidth;
	}
	else
	{
		id0.scrollLeft++;
	}	
}

Marquee.prototype.Top = function ()
{
	var id0 = document.getElementById(this.id);//$('#'+this.id);
	var id1 = document.getElementById(this.id+'_1');//$('.'+this.id+'_Marqu_Content');
	var id2 = document.getElementById(this.id+'_2');//$('.'+this.id+'_Marqu_Content_ch');

 	if(id2.offsetTop - id0.scrollTop <= 0)
  {
		id0.scrollTop -= id1.offsetHeight;
	}
	else
	{
		id0.scrollTop++;
	}	
}

/*
function Marquee()
{
	this.id = arguments[0];
	this.time = arguments[1];
	this.dir =	arguments[2];
	
	if (isNaN(this.time))
	{
		this.time = 20
	}
	
	if ($.trim(this.dir) == '')
	{
		this.dir = 'left';
	}
		
	$('#'+this.id).css({'position':'relative','z-index':'10'});
	$('#'+this.id+'>div').addClass('Marqu_Content');
	$('#'+this.id+'>.Marqu_Content').css({'position':'relative','z-index':'-999','left':'0px','top':'0px'});
	
	this.temp_width = parseInt($('#'+this.id+'>.Marqu_Content').css('width').substring(0,$('#'+this.id+'>.Marqu_Content').css('width').length-2));
	this.temp_height = parseInt($('#'+this.id+'>.Marqu_Content').css('height').substring(0,$('#'+this.id+'>.Marqu_Content').css('height').length-2));
	this.temp_html = $('#'+this.id).html();
}

Marquee.prototype.Start = function ()
{
	var Start = this;

	Start.Go = function () 
	{
		if ($('#'+Start.id+'>.Marqu_Content').length < 2)
		{
			$('#'+Start.id).append(Start.temp_html);
		} 
		Start.Scroll();
	}
	
	Start.OK = setInterval(Start.Go, parseInt(Start.time));
	
	$('#'+Start.id).mousemove(
		function ()
		{
			clearInterval(Start.OK);
		}
	);
	
	$('#'+Start.id).mouseout(
		function ()
		{
			clearInterval(Start.OK);
			Start.OK = setInterval(Start.Go, Start.time);
		}
	);
}

Marquee.prototype.Scroll = function ()
{
	var Scroll = this;

	if (Scroll.dir == 'left')
	{
		Scroll.Left();
	}
	
	if (Scroll.dir == 'top')
	{
		Scroll.Top();
	}
}

Marquee.prototype.Left = function ()
{
	var Left = this;
	
	var ment = $('#'+Left.id+'>.Marqu_Content');
	
	for (var i = 0; i < ment.length; i++)
	{
		$(ment[i]).css('left',$(ment[i]).css('left').substring(0,$(ment[i]).css('left').length-2)-1);
	}

	var j = parseInt($(ment[0]).css('left').substring(0,$(ment[0]).css('left').length-2));
	var k = parseInt($(ment[1]).css('left').substring(0,$(ment[1]).css('left').length-2));

	if (j > k)
	{
		if ($(ment[1]).css('left').substring(0,$(ment[1]).css('left').length-2)-1 < 0 - 2 * Left.temp_width)
		{
			$(ment[1]).css('left',k + Left.temp_width * 2);
		}
	}
	else
	{
		if ($(ment[0]).css('left').substring(0,$(ment[0]).css('left').length-2)-1 < 0 - Left.temp_width)
		{
			$(ment[0]).css('left',j + Left.temp_width * 2);
		}
	}
}

Marquee.prototype.Top = function ()
{
	var Top = this;
	var ment = $('#'+Top.id+'>.Marqu_Content');
	
	for (var i = 0; i < ment.length; i++)
	{
		$(ment[i]).css('top',$(ment[i]).css('top').substring(0,$(ment[i]).css('top').length-2)-1);
	}

	var j = parseInt($(ment[0]).css('top').substring(0,$(ment[0]).css('top').length-2));
	var k = parseInt($(ment[1]).css('top').substring(0,$(ment[1]).css('top').length-2));

	if (j > k)
	{
		if ($(ment[1]).css('top').substring(0,$(ment[1]).css('top').length-2)-1 < 0 - 2 * Top.temp_height)
		{
			$(ment[1]).css('top',k + Top.temp_height * 2);
		}
	}
	else
	{
		if ($(ment[0]).css('top').substring(0,$(ment[0]).css('top').length-2)-1 < 0 - Top.temp_height)
		{
			$(ment[0]).css('top',j + Top.temp_height * 2);
		}
	}
}
*/
