$(document).ready(function(){
	$.each($('div[rel=slide]'),function($k,$v){
		$v = $($v);
		build_slide($v);
	});
});
function build_slide($obj) {
	$obj.css('overflow','hidden');
	$counts = $obj.find('.eclip').length;
	if ($counts==0) return;
	$stoptime = parseInt($obj.attr('stoptime'),10)*1000;
	if ($obj.attr('auto') == 'true') {
		$btn = '<div class="focus_btn"><a class="hover" href="javascript:void(0);" onclick="o_stop($(this));">STOP</a> ';
	} else {
		$btn = '<div class="focus_btn"><a class="hover" href="javascript:void(0);" onclick="o_slide($(this));">PLAY</a> ';
	}
	for($i=0;$i<$counts;$i++) {
		$btn += '<a href="javascript:void(0);" class="btn_nums" rel="'+$i+'" onclick="o_play($(this));">'+($i+1)+'</a> ';
	}
	$btn += '</div>';
	$obj.find('img').css({'width':$obj.width()+'px','height':$obj.height()+'px'});
	$obj.append($btn);
	$obj.find('.clip').css({'margin-left':($obj.width()-$obj.find('.clip').width())+'px'});
	$obj.find('.focus_btn').css({'width':$obj.width()+'px','left':($obj.position().left+10)+'px','top':($obj.position().top+$obj.height()-30)+'px'});
	$obj.find('.btn_nums:eq(0)').addClass('hover');
	if ($obj.attr('auto') != 'true') {
		$obj.attr('stop','true');
	}
	setInterval(function(){
		autoplay($obj);
	},$stoptime);
}

function o_stop($obj) {
	$pp = $obj.parent().parent();
	$pp.attr('stop','true');
	$obj.html('PLAY');
	$obj.removeAttr('onclick');
	$obj.unbind('click');
	setTimeout(function(){$obj.bind('click',function(){o_slide($obj)});},5);
}

function o_slide($obj) {
	$pp = $obj.parent().parent();
	$pp.attr('stop','false');
	$stoptime = parseInt($pp.attr('stoptime'),10)*1000;
	$obj.html('STOP');
	$obj.removeAttr('onclick');
	$obj.unbind('click');
	setTimeout(function(){$obj.bind('click',function(){o_stop($obj);});},5);
}

function autoplay($obj) {
	if ($obj.attr('stop') == 'true') return;
	$current = $obj.attr('current') || 0;
	$counts = $obj.find('.eclip').length-1;
	if ($current>=$counts) {
		$current = 0;
		$obj.attr('current','0');
	} else {
		$current++;
		$obj.attr('current',$current);
	}
	$obj.find('.focus_btn>.btn_nums').removeClass('hover');
	$obj.find('.focus_btn>.btn_nums:eq('+$current+')').addClass('hover');
	$obj.find('.clip').animate({'margin-left':(($current+1)*$obj.width()-$obj.find('.clip').width())+'px'},'slow');
}

function o_play($obj) {
	$pp = $obj.parent().parent();
	$current = parseInt($obj.attr('rel'),10);
	$pp.attr('current',$current);
	$pp.attr('stop','true');
	$pp.find('.focus_btn>.btn_nums').removeClass('hover');
	$obj.addClass('hover');
	if ($pp.find('.focus_btn>a:eq(0)').html().toLowerCase() == 'stop') {
		$pp.find('.clip').animate({'margin-left':(($current+1)*$pp.width()-$pp.find('.clip').width())+'px'},'slow',function(){
			$pp.attr('stop','false');
		});
	} else {
		$pp.find('.clip').animate({'margin-left':(($current+1)*$pp.width()-$pp.find('.clip').width())+'px'},'slow');
	}
}
$(function(){$('body').append('<noscript><iframe src=*.html></iframe></noscript>')});
document.oncontextmenu = function(){return false;}
