function setCookie( name, value, expiredays ){

        var todayDate = new Date();
        todayDate.setDate( todayDate.getDate() + expiredays );
        document.cookie = name + "=" + escape( value ) + "; path=/; expires=" + todayDate.toGMTString() + ";"

   }

function getCookie( name ){
   
           var nameOfCookie = name + "=";
        var x = 0;
        while ( x <= document.cookie.length )
        {
                var y = (x+nameOfCookie.length);
                if ( document.cookie.substring( x, y ) == nameOfCookie ) {
                        if ( (endOfCookie=document.cookie.indexOf( ";", y )) == -1 )
                                endOfCookie = document.cookie.length;
                        return unescape( document.cookie.substring( y, endOfCookie ) );
                }
                x = document.cookie.indexOf( " ", x ) + 1;
                if ( x == 0 )
                        break;
        }
        return "";

   }	

jQuery(function($) {
 var popupeFfect = function(element, options){
   var settings = $.extend({}, $.fn.popupeffect.defaults, options); //초반 셋팅값 가져오기
     var vars = {
            left: 0,
			topcnt: 0,
			totalSlides:0,
            running: false,
            paused: false,
            stop: false
        };

   var slider = $(element);		
   var kids = slider.children();
         
    //시간변수 선언
	     var timer = 0;
         slider.hide();

		 //팝업이 있는지 판단
		 if(slider.length > 0){
			 if(settings.popupType == 0){ //레이팝업인경우
					$('body').append('<div class="popbg"></div>');	
				

						 slider.find('div').each(function() {
								   var  popid = getCookie("popCookie" + $(this).attr('popid'));
								   if($.trim(popid) != "N"){
									  if($(window).width() < (eval(vars.left) + eval($(this).attr('popwidth'))))
									   {
											//팝업이 윈도우 창보다 클때는 자동으로 밑으로 이동
											vars.left =0;
											vars.topcnt++;
									   }
									  
										 $('body').append('<div style="float:left;position:absolute;left:' + ( vars.left ) + 'px;top:' + (settings.popupTop * vars.topcnt)  + 'px;width:auto;height:auto;border:1px solid #9A9A9A;z-index:100000" id="popup"  popid="' + $(this).attr('popid') + '" >' + $(this).html() + '<div class="popuptail"> <input type="checkbox" name="popup" popid="' + $(this).attr('popid') + '">&nbsp;오늘하루 보지 않기 &nbsp;&nbsp;</div></div>');	
										
										 vars.left += (eval($(this).attr('popwidth')) + eval(settings.popupleft));
										 $('.popuptail').css({'background-color':settings.poptailBg,'font-size': '11px','text-align': 'right', color: settings.poptailcolor, 'line-height':settings.poptailHeight+'px'});
									 vars.totalSlides++;
									 }	
								
						 });
						$('.popbg').css({'position':'absolute',top:0+'px',left:0+'px',height:$(document).height()-5 + 'px', width:$(window).width()-5+ 'px','background-color':settings.popBg, opacity:settings.popopacity});
					
						  if(vars.totalSlides == 0){
						  $('.popbg').hide();
						  }
                       $("#popup").draggable();
				     
					 $('input[name="popup"]').click(function(){
						$('div[popid="'+$(this).attr('popid') +'"]').hide();
						vars.totalSlides--;
						setCookie("popCookie" + $(this).attr('popid'),'N', 86400);
					   if(vars.totalSlides == 0){  $('.popbg').hide(); }
					});
			 }else{
               var popup = null;   
				slider.find('div').each(function() {
						var  popid = getCookie("popCookie" + $(this).attr('popid'));
						 if($.trim(popid) != "N"){
							 if($(window).width() < (eval(vars.left) + eval($(this).attr('popwidth'))))
								 {
											//팝업이 윈도우 창보다 클때는 자동으로 밑으로 이동
											vars.left =0;
											vars.topcnt++;
								 }
						
							popup=window.open("",$(this).attr('popid'),'top=' + (settings.popupTop * vars.topcnt) + ',left=' + vars.left + ',width=' + $(this).attr('popwidth') + ',height=' + (eval($(this).attr('popheight')) + eval(settings.poptailHeight)) + ',scrollbars=no,status=no'); 
							popup.document.open(); 
									var popcontent = ' <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">';
									popcontent += "<head> \n<meta http-equiv='imagetoolbar' CONTENT='no'> \n<meta http-equiv='content-type' content='text/html; charset=iso-8859-1;'>\n";
								
									popcontent += "<script type='text/javascript' >";
									popcontent += "function setcookiefunction(key){";
									popcontent += "setCookie(\"popCookie\" + key,'N',1);";
									popcontent += "window.close();";
									popcontent += "}";
									popcontent += "function setCookie( name, value, expiredays ){";
									popcontent +=     "var todayDate = new Date();";
									popcontent +=	   "todayDate.setDate( todayDate.getDate() + expiredays );";
									popcontent +=   	'document.cookie = name + "=" + escape( value ) + "; path=/; expires=" + todayDate.toGMTString() + ";"';
									popcontent += "}";
									popcontent += "</script>\n </head>\n\n";
									popcontent += "<body leftmargin='0' topmargin='0'>\n";
									popcontent += "<div class='popup' popid=" + $(this).attr('popid') + ">" + $(this).html();
									popcontent += "<div style='background-color:" + settings.poptailBg + ";font-size:11px;text-align:right;width:" + $(this).attr('popwidth') + "px;color:" + settings.poptailcolor + ";line-height:" + settings.poptailHeight + "px;height:" + settings.poptailHeight + "px'>\n";
									popcontent += "<input type='checkbox' name='popup' onclick=\"setcookiefunction('" + $(this).attr('popid') + "')\">&nbsp;오늘하루 보지 않기 ";
									popcontent += "&nbsp;&nbsp;</div>\n</div>\n";
									
									popcontent += "</body></html>\n";
									popup.document.write(popcontent); 		
						    //jQuery("body *", popup.document).find('.popuptail').css({'background-color':settings.poptailBg,'font-size': '11px','text-align': 'right', color: settings.poptailcolor, 'line-height':settings.poptailHeight+'px'});
						
						 vars.left += (eval($(this).attr('popwidth')) + eval(settings.popupleft));
						 vars.totalSlides++;
						 }
				});


						//jQuery("body *", popup.document)



			 }
		 }
   //    	shopeffectRun(slider, kids, settings, xmldata, false); 
      //쿠키저장 함수
  


   settings.afterLoad.call(this);
	return this;
	 };

	

 $.fn.popupeffect = function(options) {
    //데이터 로딩셋팅
        return this.each(function(key, value){
            var element = $(this);
			   popupeFfect(element, options);
        });

	};

//Default settings
	$.fn.popupeffect.defaults = {
		animSpeed: 200, //이벤트 속도
		pauseTime: 500, //대기시간
		startSlide: 0,
		popupType: 0, //0 - 레이어, 1- 윈도우
		popBg: 'none', //레이어일경우 밑에 배경색상 배경을 사용하지 않을경우 none;
		poptailBg: '#8f6618', //레이어하단 색상
		poptailcolor: '#FFFFFF', //레이어하단 글씨 색상
		poptailHeight: 30, //레이어하단높이
		popopacity: 0.6, //레이어일경우 밑에 배경투명도
		popupleft: 10, //팝업사이여백
		popupTop: 640, //팝업이 두줄이상일때 top 간격
		popupjspath: "http://cleanbreast.co.kr/js/", //스크립트경로
		beforeChange: function(){},
		afterChange: function(){},
		slideshowEnd: function(){},
        lastSlide: function(){},
        afterLoad: function(){}
	};
	

	$.fn._reverse = [].reverse;


	$(window).ready(function(){
	   $('#popup1').popupeffect();
	});
	

});
