/*
* Copyright (C) 2010-2011 Anderson Costa
* Licenced under the MIT license
* http://www.mangava.com.br
*/
	(function($) {
		$.fn.popupBanner = function(settings) {
			settings = $.extend({
				typeContent:'image',					//can be image, flash or html file (Ajax method)
				pathBanner:'media/popup-showhair.jpg',	//defines a path of banner content
				titleBanner:'Publicidade',				//sets the banner's title. It will be used in alt atribute of images
				timerBanner:20,							//set timout (in seconds) and after hide banner
				activeBanner:true,						//defines if user is redirected to another page or not when click
				linkBanner:'http://www.rediron.com.br/contato',//url to redirect the user when click
				closeBanner:'Fechar X',					//text of close button. To personalize this you can create an id named closeBanner (i.e: div#closeBanner) and set attributes to this
				closePosition: 'top-right',				//set a size of the banner, see classes on settings.css
				bannerOverlay:true,						//use a layer to hidde content while banner has been shown
				bannerAutoPos:true,						//if true, centralize banner on page. If false, use bannerPos data to positioning the banner
				bannerWidth:700,						//width of the banner
				bannerHeight:398,						//height of the banner
				bannerPos:{position:'absolute', right:'10px', top:'10px', width:'250px', height:'250px', zIndex:'1001'}//custom positioning of the banner
			}, settings);
	
			return $(this).each(function(){
				
					
   
					var bnnElement;
					//content validation
						if(settings.typeContent != '' || settings.pathBanner != ''){
							if(settings.bannerOverlay){
								$(this).append('<div id="popup_banner_overlay_sys">&nbsp;</div>');
								$('#popup_banner_overlay_sys').css({
											background:'#000',
											height:'100%',
											left:'0px',
											opacity:'.5',
											filter:'alpha(opacity=50)',
											position:'absolute',
											top:'0px',
											width:'100%',
											zIndex:'3'
									});
								$('#popup_banner_overlay_sys').click(function(){
										closeBanner();
									});
							}
							$(this).append('<div id="popup_banner_sys"><div id="contentBanner"></div><div id="closeBanner" style="position:absolute;">'+settings.closeBanner+'</div></div>');
							if(settings.typeContent == 'image'){
								var imgBanner = new Image();
								$(imgBanner).load(
									function(){
										$('#contentBanner').append(this);
										}
								).error(function () {
									bannerError();
								}).
								attr({
									alt: settings.titleBanner,
									src: settings.pathBanner
									}).
								css({
									width:settings.bannerWidth+'px',
									height:settings.bannerHeight+'px'
									}); 
							}else if(settings.typeContent == 'flash'){
								bnnElement = $('#contentBanner').append('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="'+settings.bannerWidth+'" height="'+settings.bannerHeight+'"><param name="wmode" value="transparent" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="'+settings.pathBanner+'" /><param name="menu" value="false" /><embed src="'+settings.pathBanner+'" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="'+settings.bannerWidth+'" height="'+settings.bannerHeight+'" wmode="transparent"></embed></object>');
							}
							else if(settings.typeContent == 'html'){
									$('#contentBanner').load(settings.pathBanner);
							}
							else{
								bannerError();
							}
									
							$('#closeBanner').click(function(){
									closeBanner();
							});
							
							$(this).keydown(function(e){
								if(e.keyCode == 27 || e.keyCode == 88){
									closeBanner();
								}
							});
							
							if(settings.activeBanner){
								$('#contentBanner').css('cursor','pointer');
								$('#contentBanner').click(function(){
									window.open(settings.linkBanner);
									});
								}
							positionCloseBanner();		
							
						}
				});
				
				//if something wrong happens
				function bannerError(){
					$('#content_banner').html('<h6>Erro ao carregar conteúdo!</h6>');
				}
	
				function positionCloseBanner() {
					switch (settings.closePosition){
						case 'top-left':
							$('#closeBanner').css({
								left:'0px',
								top:'0px'
								});
							break
							
						case 'bottom-right':
							$('#closeBanner').css({
								bottom:'0px',
								right:'0px'
								});
							break
						
						case 'bottom-left':
							$('#closeBanner').css({
								bottom:'0px',
								left:'0px'
								});
							break
							
						default:
							$('#closeBanner').css({
								right:'0px',
								top:'0px'
								});
						}
					bannerTimeOut();
					bannerPositioning();
				}
				function bannerTimeOut(){
					setTimeout(function(){
						closeBanner();
						}, (settings.timerBanner)*1000);
				}
				
				function bannerPositioning(){
					if (settings.bannerAutoPos == true){
						
						$('#popup_banner_sys').css({
								height:settings.bannerHeight,
								left:'50%',
								marginLeft:(Math.floor(Number(settings.bannerWidth/2)*-1))+'px',
								marginTop:(Math.floor(Number(settings.bannerHeight/2)*-1))+'px',
								position:'absolute',
								top:'50%',
								zIndex:1000,
								width:settings.bannerWidth
						});
					}
						
					else{
						$('#popup_banner_sys').css(settings.bannerPos);
						$('#closeBanner').css({
							background:'#2d2015',
							color:'#ffffff',
							cursor:'pointer',
							height:'20px',
							width:'80px',
							zIndex:2
							});

					}
				}
				
				function closeBanner(){
					$('#popup_banner_sys').hide();
					if(settings.bannerPos){
						$('#popup_banner_overlay_sys').hide();
						}
				}
		}
	})(jQuery);
