$(document).ready(function() {

	var nUa=navigator.userAgent;
	var uaAr=nUa.indexOf('Linux; U; Android ')!=-1;
	var uaIPo=nUa.indexOf('iPod; U')!=-1;
	var uaIPh=nUa.indexOf('iPhone; U')!=-1;
	var uaIPa=nUa.indexOf('iPad; U')!=-1;
		
		
	$(".paging").show();
	$(".paging a:first").addClass("active");
	var imageWidth = $(".window").width();
	var imageSum = $(".image_reel img").size();
	var imageReelWidth = imageWidth * imageSum;
	var nowTarget = 0;
	rotate = function(){	
		var triggerID = $active.attr("rel") - 1; 
		var image_reelPosition = triggerID * imageWidth; 
		$(".paging a").removeClass('active'); 
		$active.addClass('active'); 
		if(triggerID == nowTarget){
			return;
		}
		$(".image_reel a :eq("+nowTarget+")").animate({ 
			'opacity': 0 }, 700 , null , function(){
				$(".image_reel a :eq("+triggerID+")").css('opacity', 0);
				$(".image_reel").css('left', -image_reelPosition);
				$(".image_reel a :eq("+triggerID+")").animate({ 'opacity': 1 }, 700 );
				nowTarget = triggerID
		});
	}; 
	rotateSwitch = function(){		
		play = setInterval(function(){ 
			$active = $('.paging a.active').next();
			if ( $active.length === 0) { 
				$active = $('.paging a:first'); 
			}
			rotate(); 
		}, 7000); 
	};
	rotateSwitch(); 
	$(".image_reel a").hover(function() {
		clearInterval(play); 
	}, function() {
		rotateSwitch(); 
	});	
	$(".paging a").click(function() {	
		$active = $(this); 
		clearInterval(play); 
		rotate(); 
		rotateSwitch(); 
		return false; 
	});
	$(".image_reel a").click(function(){
		var tag = $(this).attr('youtube');
		if(tag){
			if(uaIPo||uaIPh||uaIPa){ 
				var id  = tag.match(/src="(.+?)"/);
				id = id[1];
				id = id.replace('http://www.youtube.com/embed/','http://www.youtube.com/watch?v=');
				id = id.replace('?rel=0' , '');
				location.href=id
			}else{
				var width  = tag.match(/width="(\d+?)"/);
				var height = tag.match(/height="(\d+?)"/);
				width  = (width && width.length  >= 2) ? width[1] : 427;
				height = (width && height.length >= 2) ? height[1] : 350;
				
				var Win1 = window.open('', '_blank', 'width='+width+', height='+height+', menubar=no, toolbar=no, scrollbars=no');
				
				if(navigator.appVersion.charAt(0)>=3){Win1.focus()};
				Win1.document.clear();
				Win1.document.write('<html><head><meta http-equiv=Content-Type content="text/html; charset=UTF-8"><title>YouTube video player</title><style type="text/css">*{margin:0;padding:0;}</style></head>');
				Win1.document.write("<body>");
				Win1.document.write(tag);
				Win1.document.write("</body></html>");
				Win1.document.close();
			}
		}
	})
});
