var mySwiper ;
function eviewer(url)
{
	try
	{
		var evt = event || window.event;
    	if (evt.stopPropagation) { evt.stopPropagation(); } else { evt.cancelBubble = true; }
    	if (evt.preventDefault) { evt.preventDefault(); } else { evt.cancelBubble = true; }
	}catch (e) {}
	
	$(".eviewer_body .swiper-wrapper").html('');
	$(".eviewer_body .swiper-wrapper").append('<div class="swiper-slide" ontouchmove="return false;"><div class="cell"><img src="' + url + '" /></div></div>');
	if(typeof(RTP)!='undefined')
	{
		$('.eviewer_body .cell').each(function (){
            new RTP.PinchZoom($(this),{});
        });
	}

	
	//mySwiper.updateSlidesSize();
    //mySwiper.updatePagination();
	mySwiper.slideTo(0, 0, false);
	mySwiper.update();
	$(".eviewer_body").css({"z-index": "19891111","opacity": "1","visibility": "visible"});
	bdbodyevent();
}
function eviewer_childimg(par)
{
	var img=$(par).find("img");
	if(img.length>0)
	{
		eviewer_click(img.get(0));
	}
}
function isPhone() 
{
    var userAgentInfo = navigator.userAgent.toLowerCase();
    var Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone","iPad", "iPod", "Windows CE", "Windows Mobile", "UCWeb", "Midp"];
    var flag = false;
    for(var i = 0; i < Agents.length; i++) 
	{
        if (userAgentInfo.indexOf(Agents[i].toLowerCase()) > -1) 
		{
            flag = true;
            break;
        }
    }
    return flag;
};
function getEventObject()
{
	var evt,src;
	if(window.event)
	{
		evt=window.event;
		src = evt.srcElement;
	}
	else
	{
		var o = arguments.callee;
		var e;
		while(o != null)
		{
			e = o.arguments[0];
			if(e && (e.constructor == Event || e.constructor == MouseEvent))
			{
				evt=e;
				src=evt.target; 
				break;
			}
			o = o.caller;
		}
	}
	return src;
};
function eviewer_close()
{
	$(".eviewer_body").css({"z-index": "-1","opacity": "0","visibility": "hidden"});
}
function eviewer_click(obj)
{
	var evt = event || window.event;
    if (evt.stopPropagation) { evt.stopPropagation(); } else { evt.cancelBubble = true; }
    if (evt.preventDefault) { evt.preventDefault(); } else { evt.cancelBubble = true; }
	var par=$(obj).parents(".eimages_view:first");
	$(".eviewer_body .swiper-wrapper").html('');
	var imgs=par.find('img');	
	var i= imgs.index(obj);	
	imgs.each(function(i,el){
		$(".eviewer_body .swiper-wrapper").append('<div class="swiper-slide" ontouchmove="return false;"><div class="cell"><img src="' + $(this).attr("src") + '" /></div></div>');
	});	
	if(typeof(RTP)!='undefined')
	{
		$('.eviewer_body .cell').each(function (){
            new RTP.PinchZoom($(this),{});
        });
	}
	mySwiper.updateSlidesSize();
    mySwiper.updatePagination();
    $(".eviewer_body").css({"z-index": "19891111","opacity": "1","visibility": "visible"});
    mySwiper.slideTo(i, 0, false);
	//$(".eviewer_body").on("click", function(){$(this).css({"z-index": "-1","opacity": "0"});});
	bdbodyevent();
}
function bdbodyevent()
{
	if(isPhone()){
		$(".eviewer_body").unbind("click").bind("click", function(){
			var src = getEventObject();
			if($(src).hasClass("swiper-pagination-bullet")){return;}
			if($(src).hasClass("swiper-button-next")){return;}
			if($(src).hasClass("swiper-button-prev")){return;}
			if($(src).hasClass("swiper-button-disabled")){return;}
			//$(this).css({"z-index": "-1","opacity": "0","visibility": "hidden"});
			eviewer_close();
		});
	}
	else
	{
		$(".eviewer_body").unbind("dblclick").bind("dblclick", function(){
			var src = getEventObject();
			if($(src).hasClass("swiper-pagination-bullet")){return;}
			if($(src).hasClass("swiper-button-next")){return;}
			if($(src).hasClass("swiper-button-prev")){return;}
			if($(src).hasClass("swiper-button-disabled")){return;}
			//$(this).css({"z-index": "-1","opacity": "0","visibility": "hidden"});
			eviewer_close();
		});
	}

	//$(".eviewer_body").unbind("click").click(function(e){alert(5); });
}
function eviewer_init()
{
	//if(top!=self){return;}
	if(typeof($)!="function"){return;}
	if(typeof(Swiper)!="function"){return;}
	//alert($('.eimages_view img').length);
		
	$('.eimages_view').each(function(i,el){
		var ebinded=$(this).attr("ebinded");
		if(typeof(ebinded)!='undefined'){return true;}		
		$(this).attr("ebinded","true");	
		var imgs=$(this).find("img");
		if(imgs.length==0){return true;}
		imgs.css("cursor","pointer");// cursor:pointer;		
		imgs.on("click", function(){eviewer_click(this);});

	});	
	var html='<div class="eviewer_body" ontouchmove="return false;">';
    html+='<div class="swiper-container2">';
	if(!isPhone()){html+='<div class="eviewer-close" onclick="eviewer_close();"></div>';}
    html+='<div class="swiper-wrapper"></div>';
    html+='</div>';
    html+='<div class="swiper-pagination2"></div>';
	if(isPhone())
	{
		html+='<div class="swiper-button-next swiper-button-white"></div>';
    	html+='<div class="swiper-button-prev swiper-button-white"></div>';
	}
	else{
    	html+='<div class="swiper-button-next swiper-button-white"></div>';
    	html+='<div class="swiper-button-prev swiper-button-white"></div>';
	}
	html+='</div>';
	$(document.body).append(html);
	if($(".eviewer-close").css("position")=='static'){return;}
	mySwiper = new Swiper('.swiper-container2', {pagination: '.swiper-pagination2',nextButton: '.swiper-button-next',prevButton: '.swiper-button-prev',paginationType: 'fraction',loop: false,grabCursor: true, paginationClickable: true, 

    });
	
}
if(window.addEventListener){window.addEventListener("load",function(){eviewer_init();}, false);}else{window.attachEvent("onload",function(){eviewer_init();});}