/*     
	02/04/2009
	Layer Popup System KLIB Javascript Library based jquery
	Copyright(C) 2009 Keon-Jun Lee(ch4feel@naver.com)

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

try {
	$k.klib;
} 
catch (e) {
	alert('Form Validator have to use kblib.core.js');
}


$k.popcorn = {
	
	corns : [],
	poplate : {
		bgColor : '#164e9c',
		bg : '',
		border : '1px solid #3989d0',
		close : '',
		x : '',
		logo : '',
		titleColor : '#fff',
		title : 'Popcorn',
		notseeColor : '#bbd0e9',
		marginLeft : 7,
		marginRight : 7,
		marginTop : 30,
		marginBottom : 30,
		notseeMsg : '동안 이창을 보지 않음'
	},
	
	setMachine : function(hashCorn) {
		jQuery.extend($k.popcorn.poplate, hashCorn);
	},
	
	eatCorn : function(cornid, name, exp) {
		if( name && exp && exp != 0) {
			try{
				$k.setCookie(name, 'done', exp);	
			} catch (e) {
				alert('쿠키생성실패');
			}
			
		}
		jQuery(cornid).remove();
		
	},
	
	pushCorn : function(name, url, width, height, exp, expMsg) {
		if(!name || name=='') {
			alert('팝업 이름이 반드시 필요합니다.');
			return;
		} else if (!url || url==''){
			alert('URL이 반드시 필요합니다.');
			return;
		} else if (!width || width==''){
			alert('가로크기가 반드시 필요합니다.');
			return;
		} else if (!height || height==''){
			alert('세로크기 반드시 필요합니다.');
			return;
		} else if (!exp || exp==''){
			exp = 0;
		} else if (!expMsg || expMsg == ''){
			expMsg = '';
		}
		
		var corn = [name, url, width, height, exp, expMsg];
		
		for(i=0; i<$k.popcorn.corns.length; i++){
			if($k.popcorn.corns[i][0] == name){
				alert('존재하는 팝업입니다.\n팝업 이름을 확인하세요.');
				return;
			}
		}
		
		if($k.getCookie(name) == '') {
			$k.popcorn.corns.push(corn);
		}
		
	},
	
	rePopCorn : function() {
		for (i = 0; i < $k.popcorn.corns.length; i++) {
			$k.popcorn.eatCorn('#PC_'+$k.popcorn.corns[i][0]);
		}
		$k.popcorn.popCorn();
	},
	
	popCorn : function() {

		var corn, html = '', cornsWidth = 0, cornsHeight = 0, positionX = 0;

		for (i = 0; i < $k.popcorn.corns.length; i++) {
			cornsWidth += ($k.popcorn.corns[i][2] + $k.popcorn.poplate.marginLeft + $k.popcorn.poplate.marginRight + 12);
			if ($k.popcorn.corns[i][3] > cornsHeight) {
				cornsHeight = ($k.popcorn.corns[i][3] + $k.popcorn.poplate.marginTop + $k.popcorn.poplate.marginBottom + 2);
			}
		}
		
		for (i = 0; i < $k.popcorn.corns.length; i++) {
	
			html = '';
			
			corn = '#PC_'+$k.popcorn.corns[i][0];
			
			jQuery('body').children(':last').after('<div id="PC_'+$k.popcorn.corns[i][0]+'"></div>');
			jQuery(corn).addClass('PC_LAYER');
			jQuery(corn).attr('expMsg', $k.popcorn.corns[i][5]);
			
			if ($k.popcorn.corns[i][5]) {
				jQuery(corn).attr('expMsg', $k.popcorn.corns[i][5]);
			} else {
				if ($k.popcorn.corns[i][4] < 0) {
					jQuery(corn).attr('expMsg', '본 팝업은 1회만 제공됩니다.');
				} else {
					switch ($k.popcorn.corns[i][4]) {
						case 0:
							jQuery(corn).attr('expMsg', '');
							break;
						case 1:
							jQuery(corn).attr('expMsg', '하루' + $k.popcorn.poplate.notseeMsg);
							break;
						case 7:
							jQuery(corn).attr('expMsg', '일주일' + $k.popcorn.poplate.notseeMsg);
							break;
						case 30:
							jQuery(corn).attr('expMsg', '한달' + $k.popcorn.poplate.notseeMsg);
							break;
						default:
							jQuery(corn).attr('expMsg', $k.popcorn.corns[i][4] + '일' + $k.popcorn.poplate.notseeMsg);
							break;
					}	
				}
			}
			
			jQuery(corn).css({
				'position' : 'absolute',
				'left' : '50%',
				'top' : '50%',
				'margin-top' : ((0 - cornsHeight) + Math.floor(cornsHeight / 2)) + 'px',
				'width' : ($k.popcorn.corns[i][2] + $k.popcorn.poplate.marginLeft + $k.popcorn.poplate.marginRight) + 'px',
				'height' : ($k.popcorn.corns[i][3] + $k.popcorn.poplate.marginTop + $k.popcorn.poplate.marginBottom) + 'px',
				'background-color' : $k.popcorn.poplate.bgColor,
				'background-image' : 'url(' + $k.popcorn.poplate.bg + ')',
				'background-repeat' : 'repeat-x',
				'border' : $k.popcorn.poplate.border,
				'cursor' : 'move',
				'z-index' : 999 - i
			});
			
			if($k.popcorn.corns.length == 1){
			  jQuery(corn).css({
				'left' : '32%',
				'top' : '38%'
				});
			}else if($k.popcorn.corns.length == 2){
			  jQuery(corn).css({
					'left' : '42%',
					'top' : '38%'
				});
			}
			
			if (i==0) {
				jQuery(corn).css({
					'margin-left' : '-' + Math.floor(cornsWidth/2) + 'px'
				});
				positionX = $k.popcorn.corns[i][2] + $k.popcorn.poplate.marginLeft + $k.popcorn.poplate.marginRight + 275;
			} else {
				jQuery(corn).css({
					'margin-left' : ((0 - Math.floor(cornsWidth/2)) + positionX) + 'px'
				});
				positionX += $k.popcorn.corns[i][2] + $k.popcorn.poplate.marginLeft + $k.popcorn.poplate.marginRight + 275;
			}
			html += '<div style="position:absolute;left:'+$k.popcorn.poplate.marginLeft+'px;top:' + Math.floor(($k.popcorn.poplate.marginTop-24)/2) + 'px;padding:' + Math.floor(($k.popcorn.poplate.marginTop-24)/2) + 'px 0 ' + Math.floor(($k.popcorn.poplate.marginTop-24)/2) + 'px ';
			if ($k.popcorn.poplate.logo != '')
				html += '60';
			else
				html += '0';
			html += 'px;background:url('+$k.popcorn.poplate.logo+') no-repeat;font:bold 11px 돋움;color:'+$k.popcorn.poplate.titleColor+';">'+$k.popcorn.poplate.title+'</div>';
			html += '<iframe src="'+$k.popcorn.corns[i][1]+'" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" width="'+$k.popcorn.corns[i][2]+'" height="'+$k.popcorn.corns[i][3]+'" style="position:absolute;left:'+$k.popcorn.poplate.marginLeft+'px;top:'+$k.popcorn.poplate.marginTop+'px;"></iframe>';
			html += '<div onclick="$k.popcorn.eatCorn(\'' + corn + '\');" style="position:absolute;right:7px;top:9px;cursor:pointer;">';
			if ($k.popcorn.poplate.x != '')
				html += '<img src="'+$k.popcorn.poplate.x+'" alt="닫기" title="닫기" />';
			else {
				html += '<span style="font:bold 12px 돋움;color:'+ $k.popcorn.poplate.titleColor +';">X</span>';
			}
			html += '</div>';
			html += '<div onclick="$k.popcorn.eatCorn(\'' + corn + '\');" style="position:absolute;right:6px;top:'+($k.popcorn.poplate.marginTop+$k.popcorn.corns[i][3]+6)+'px;cursor:pointer;">';
			if ($k.popcorn.poplate.close != '')
				html += '<img src="'+$k.popcorn.poplate.close+'" alt="닫기" title="닫기" />';
			else
				html += '<span style="font:bold 14px 돋움;color:'+ $k.popcorn.poplate.notseeColor +';">[ 창닫기 ]</span>';
			html += '</div>';
			html += '<div onclick="$k.popcorn.eatCorn(\'' + corn + '\', \'' + $k.popcorn.corns[i][0] + '\', ' + $k.popcorn.corns[i][4] +');" style="position:absolute;left:8px;top:'+($k.popcorn.poplate.marginTop+$k.popcorn.corns[i][3]+10)+'px;cursor:pointer;"><span style="font:11px 돋움;color:'+$k.popcorn.poplate.notseeColor+';">';
			html += jQuery(corn).attr('expMsg');
			html += '</span></div>';

			jQuery(corn).append(html);
			
			try {
				jQuery(corn).draggable({
					cursor : 'move',
					containment : 'document'
				});
				
				jQuery(corn).mousedown(function() {
					jQuery('.PC_LAYER').each(function() {
						jQuery(this).css({
							'z-index' : jQuery(this).css('z-index') - 1
						});
					});
					jQuery(this).css({
						'z-index' : 999
					});
					
				});
			} catch (e) {} 
		}
	}
}
jQuery.extend({
	pushCorn : $k.popcorn.pushCorn,
	popCorn : $k.popcorn.popCorn,
	rePopCorn : $k.popcorn.rePopCorn,
	setMachine : $k.popcorn.setMachine
});

