﻿(function($){
	
	$.fn.cc_jq_overlay_remove = function(opt) {
		
		if(!$.fn.cc_jq_overlay.global.has_finish) {			
			if (window.stop !== undefined) {
				window.stop();			
			} else if (document.execCommand !== undefined) {
				document.execCommand("Stop", false) ;
			}
		}
		
		$('body').find("#cc_overlay_all").remove();
		
		if(opt.endRemove != '') {
			if(opt.endRemoveParams != '') {
				var args = [];
				$.each(opt.endRemoveParams, function(i, item) {
					args.push(item);
				});
				opt.endRemove.apply(this, args);			
			} else {
				opt.endRemove();
			}
		}
		$.fn.cc_jq_overlay.global.has_finish = false;
	}
	
	$.fn.cc_jq_overlay_build = function(opt) {
		
		var overlay = $('body').find("#cc_overlay_all");
		overlay.find("#cc_overlay").css({
			'background' : opt.bgOverlay,
			'opacity' : opt.bgOpacity
		});
		
		overlay.find("#cc_modal").css({
			'width' : opt.width,
			'height' : opt.height,
			'marginTop': (opt.height/2)*-1,
			'marginLeft' : (opt.width/2)*-1			
		});
		var _bgModal = '';
		if(opt.bgModal.indexOf('#') >= 0) {
			_bgModal = opt.bgModal;
		} else {
			_bgModal = 'url('+opt.bgModal+') no-repeat top center';
		}
		overlay.find("#content_cc_modal").css({
			'background' : _bgModal
		});
		
		$('#ifrm_cc_modal').load(function() {			
			overlay.find('img#loader_cc_modal').remove();
			overlay.find('#content_cc_modal').fadeIn('fast', function() {
				overlay.find('a#close_cc_modal').show();
			});			
			$.fn.cc_jq_overlay.global.has_finish = true;
		});		
		
		overlay.find('a#close_cc_modal').click(function(e) {
			e.preventDefault();
			$.fn.cc_jq_overlay_remove(opt);
		});
		
		$(document).bind('keydown',function(e){
			if(e.which == 27) {
				e.preventDefault();
				if(opt.escToExit) { $.fn.cc_jq_overlay_remove(opt); }
			}
		});
		
		$('body').find("#cc_overlay_all, #cc_overlay").click(function() {		
			if(opt.clickToOverlay) { 
				if($.fn.cc_jq_overlay.global.has_finish) {	
					$.fn.cc_jq_overlay_remove(opt); 
				}
			}
		});
	}
	
	
	//INIT PLUGIN	
	$.fn.cc_jq_overlay = function(options) {
		
		var opt = $.extend({}, $.fn.cc_jq_overlay.defaults, options);		

		var modal = '<div id="cc_overlay_all"><div id="cc_overlay"></div>';
			modal += '<div id="cc_modal">';
				modal += '<a href="close" id="close_cc_modal" class="'+opt.classBtClose+'">X</a>';
				modal += '<img id="loader_cc_modal" src="img/loaderCoca.gif" />';
				modal += '<div id="content_cc_modal">';
				if(opt.type == 'iframe') {			
					modal += '<iframe frameborder="0" hspace="0" src="'+opt.url+'" width="100%" height="100%" scrolling="no" allowtransparency="true" id="ifrm_cc_modal"></iframe>';
				}
				modal += '</div>';
			modal += '</div>';
		modal += '</div>';
		
		$('body').append(modal);
		$.fn.cc_jq_overlay_build(opt);
		
		return this;
		
	};
	
	$.fn.cc_jq_overlay.global = {
		has_finish : false
	}	
	
	//DEFAULTS CONFIG	
	$.fn.cc_jq_overlay.defaults = {
	
		url : '',
		type : 'iframe',
		width : 400,
		height : 400,
		bgModal : '#ffffff', 
		bgOverlay : '#000000',
		bgOpacity : 0.5,
		classBtClose : '',
		endRemove : '',
		endRemoveParams : '',
		clickToOverlay : true,
		escToExit : true
		
	};
	
})(jQuery);
