jQuery.fn.beonoPopUp = function(options) 
{ 

	// If BullShit 8 and lower
	var isIE = false;
	if (jQuery.browser.msie) {
		if(parseInt(jQuery.browser.version) < 9) {
			var isIE = true;
		}
	}

	var settings = {
		faderOpacity : 0.7,
		faderShowTime : 300,
		popUpShowTime : 300,
		animatePosition : false,
		isIE : isIE,
		navigationEnabled : false,
		navigationCtrlDisable : false,
		navigationPrevText : "Назад", 
		navigationNextText : "Вперед",
		cssClassName : false,
		js: false,
		css : false
	};
	
	settings = jQuery.extend(settings, options);

	$(this).click(beonoAccentStart);
		
	function beonoAccentStart (el)  
	{
		if($(el).attr("class")) {
			var $this = el;		
		} else {
			var $this = $(this);	
		}

		if($("div.beono-accent-fader").size() == 0) {
			//$("body").append('<div class="beono-accentFader"></div><div class="beono-accentLoading"></div><div class="beono-accentPopUp"><div class="beono-accentPopUpWrapper"><div class="beono-accent-popup-header"> <div class="beono-accent-popup-header-wrapper"><span class="beono-accentClose">a??</span><div class="clear-both"></div></div></div><div class="beono-accentPopUpContent"></div></div></div>');
			$("body").append(
				'<div class="beono-accent-fader"></div>'+
				'<div class="beono-accent-loading"></div>'+
				'<table class="beono-accent-popup">'+
					'<tr>' +
						'<td>' +
							'<div class="beono-accent-popup-wrapper">' +
								'<div class="beono-accent-popup-header">' +
									'<div class="beono-accent-popup-header-wrapper">' +
									'&nbsp;</div>' +
								'</div>' +
								'<div class="beono-accent-popup-content">' +
								'</div>' +
								'<a href="#" onclick="return false;" class="beono-accent-close">&nbsp;</a>' +
							'</div>' +
						'</td>' +
					'</tr>' +
				'</table>'
			);
	
		}

		var $beonoAccentFader = $("div.beono-accent-fader");
		var $beonoAccentShadow = $("img.beono-accent-shadow");
		var $beonoAccentPopUp = $(".beono-accent-popup");
		var $beonoAccentPopUpContent = $("div.beono-accent-popup-content");
		var $beonoAccentLoading = $("div.beono-accent-loading"); 
		var $beonoAccentPopUpHeaderWrapper = $("div.beono-accent-popup-header-wrapper");
		
		$beonoAccentPopUpHeaderWrapper.text(" ");

		// IE. We need to store html overflow to restore it on exit
		// TODO: Need to set overflow-x too;
		if (settings.isIE || (jQuery.browser.msie && parseInt(jQuery.browser.version) < 8)) {
			var htmlOverflow = $("html").css("overflow-y");
			//$("html").css("overflow-y", "hidden");			
				
			$beonoAccentFader.css("position", "absolute");
			$beonoAccentLoading.css("position", "absolute");
			$beonoAccentPopUp.css("position", "absolute");

		}
		
		if (settings.navigationEnabled && $("div.beono-accent-popup-additional-wrapper").size() == 0) {

	 		$beonoAccentPopUpContent.after('<div class="beono-accent-popup-additional">' +
												'<div class="beono-accent-popup-additional-wrapper">' +
												'</div>' +
											'</div>');			
		}

		var $beonoAccentPopUpAdditionalWrapper = $("div.beono-accent-popup-additional-wrapper");

		if(settings.cssClassName) {
		
			$beonoAccentPopUp.addClass(settings.cssClassName);
		
		}
		
		// Show Fader
		beonoAccentFaderShow($beonoAccentFader, settings.faderOpacity, settings.faderShowTime);

		// :TODO: fix IE absolute position
		
		$beonoAccentLoading
			.css("z-index", ($beonoAccentFader.css("z-index") + 1))
			.animate({ opacity: "show"}, 100);
			
		if (settings.isIE) {
		
			if (!windowWidth || !windowHeight) {
				var windowSize = beonoGetPageSize();
				var windowWidth	 = windowSize[0];
				var windowHeight = windowSize[1];
			}

			if (document.documentElement.scrollTop) {
				var windowScrollTop = document.documentElement.scrollTop;	
			} else {			
				var windowScrollTop = document.body.scrollTop;
			}
			
			$beonoAccentLoading.css("top", windowScrollTop + (windowHeight/2) - ($beonoAccentLoading.height()/2));	
		}

		var urlString = /\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$/;
		var urlType = $this.attr("href").toLowerCase().match(urlString);
	 		
	 	// If image
		if (urlType == '.jpg' || urlType == '.jpeg' || urlType == '.png' || urlType == '.gif' || urlType == '.bmp') {
	 			
	 		beonoAccentPopUpLoadImage($this.attr("href"), $beonoAccentPopUpContent, $beonoAccentPopUp, $beonoAccentLoading, $beonoAccentPopUpHeaderWrapper, $this);
	 		
	 		if (!settings.navigationDisable) {
	 			beonoAccentPopUpNavigation($this, $beonoAccentPopUpAdditionalWrapper, $beonoAccentPopUp);
	 		}
	 			
	 	} else if($this.attr("href") != "#") {
	 	
	 		beonoAccentPopUpLoadUri($this.attr("href"), $beonoAccentPopUpContent, $beonoAccentPopUp, $beonoAccentLoading);
	 	
	 	} else {
	 	
	 		beonoAccentPopUpLoadElement(settings.popUpElement, $beonoAccentPopUpContent, $beonoAccentPopUp, $beonoAccentLoading);
	 			
		}


				
		// On Esc press
		$(window).keydown(function(event){
			switch (event.keyCode) {
			  case 27:
			  	$(".beono-accent-close").click();
			  	break;
			}
		});	 
		
		// On Close click
		$(".beono-accent-close").bind("click", function () 
		{
			$beonoAccentPopUp.animate({ opacity: "hide" }, settings.popUpShowTime);
			$beonoAccentFader.animate({ opacity: "hide" }, settings.faderShowTime);
			setTimeout('$(".'+$beonoAccentPopUpContent.attr("class")+'").html("")', settings.popUpShowTime);
			setTimeout('$(".beono-accent-popup").remove()', settings.popUpShowTime);
			setTimeout('$(".beono-accent-fader").remove()', settings.faderShowTime);
			setTimeout('$(".beono-accent-loading").remove()', settings.faderShowTime);
 
			if (settings.isIE || (jQuery.browser.msie && parseInt(jQuery.browser.version) < 8)) {
				$("html").css("overflow-y", htmlOverflow);
			}						
		});
		
		// On window resize redraw some elements
		$(window).resize(function(){
			beonoAccentPopUpInitPosition($beonoAccentPopUp, false, false, settings.animatePosition);	
			beonoAccentFaderInitSize($beonoAccentFader);					
		});

		
		// Apply user css
		if (settings.css) { 
			jQuery.each(settings.css, function(selector, properties) {		
				jQuery.each(properties[0], function(property, value) {
					$(selector).css(property, value);
				});
			});	
		}
		
		//Apply user js
		/*
		if (settings.js) { 
			jQuery.each(settings.js, function(js, bla) {																	
				eval('('+js+')');
			});	
		}*/
		
		
		return false;
		
		// Doesn't work! See up! a??
		/*
		function beonoAccentClose () {
		
			$beonoAccentPopUp.animate({ opacity: "hide" }, settings.popUpShowTime);
			$beonoAccentFader.animate({ opacity: "hide" }, settings.faderShowTime);
			$("."+$beonoAccentPopUpContent.attr("class")).html("");
			$beonoAccentPopUp.remove();
			$beonoAccentFader.remove();
			$beonoAccentLoading.remove();
	
			if (settings.isIE) {
				$("html").css("overflow-y", htmlOverflow);
			}
		
		}
			*/
				
		function beonoAccentPopUpNavigation ($this, $beonoAccentPopUpAdditionalWrapper, $beonoAccentPopUp) {
		
			var elementIndex = $("." + $this.attr("class")).index($this);
			var $prevElement = $("." + $this.attr("class")).eq(elementIndex-1);
			var $nextElement = $("." + $this.attr("class")).eq(elementIndex+1);
			
			if (!$prevElement.attr("href") && $nextElement.attr("href")) {
			
				$beonoAccentPopUpAdditionalWrapper.html(				
					'<div class="beono-accent-popup-navigation">' +
						'<span class="beono-accent-popup-navigation-prev"></span> <a title="'+ $nextElement.attr("title") +'" href="' + $nextElement.attr("href") + '" class="beono-accent-popup-navigation-next">'+ settings.navigationNextText +'</a>' + 
						'<div style="clear:both;"></div>' +
					'</div>'
				);
				
			} else if ($prevElement.attr("href") && !$nextElement.attr("href")) {
			
				$beonoAccentPopUpAdditionalWrapper.html(				
					'<div class="beono-accent-popup-navigation">' +
						'<a title="'+ $prevElement.attr("title") +'" href="'+ $prevElement.attr("href") +'" class="beono-accent-popup-navigation-prev">'+ settings.navigationPrevText +'</a> <span class="beono-accent-popup-navigation-next"></span>' +
						'<div style="clear:both;"></div>' +
					'</div>'
				);
			
			} else if ($prevElement.attr("href") && $nextElement.attr("href")) {
			
				$beonoAccentPopUpAdditionalWrapper.html(				
					'<div class="beono-accent-popup-navigation">' +
						'<a title="'+ $prevElement.attr("title") +'" href="'+ $prevElement.attr("href") +'" class="beono-accent-popup-navigation-prev">'+ settings.navigationPrevText +'</a> <a title="'+ $nextElement.attr("title") +'" href="' + $nextElement.attr("href") + '" class="beono-accent-popup-navigation-next">'+ settings.navigationNextText +'</a>' +
						'<div style="clear:both;"></div>' +
					'</div>'
				);
						
			}
			
			$(window).keydown(function(event){	
				if (event.ctrlKey || settings.navigationCtrlDisable) {
					switch (event.keyCode ? event.keyCode : event.which ? event.which : null) {
					  case 0x25:
					  	if ($("a.beono-accent-popup-navigation-prev").attr("href")) {
						  	$(window).unbind('keydown');
						  	$("a.beono-accent-popup-navigation-prev").click();
						}
						break;
					  	
					  case 0x27:
						if ($("a.beono-accent-popup-navigation-next").attr("href")) {
						  	$(window).unbind('keydown');
						  	$("a.beono-accent-popup-navigation-next").click();
						}
						break;
					}
				}				
			});					
			
			$("a.beono-accent-popup-navigation-prev").click(function () 
			{	
				$beonoAccentPopUp.hide();									
				var elementIndex = $("." + $this.attr("class")).index($this);
				var $prevElement = $("." + $this.attr("class")).eq(elementIndex-1);	
				//$(".beono-accentClose").click();
				if (settings.isIE || (jQuery.browser.msie && parseInt(jQuery.browser.version) < 8)) {
					$("html").css("overflow-y", htmlOverflow);
				}					
				beonoAccentStart($prevElement);	
				return false;
				
			});
			
			$("a.beono-accent-popup-navigation-next").click(function () 
			{	
				$beonoAccentPopUp.hide();						
				var elementIndex = $("." + $this.attr("class")).index($this);
				var $nextElement = $("." + $this.attr("class")).eq(elementIndex+1);	
				//$(".beono-accentClose").click();
				if (settings.isIE || (jQuery.browser.msie && parseInt(jQuery.browser.version) < 8)) {
					$("html").css("overflow-y", htmlOverflow);
				}					
				beonoAccentStart($nextElement);					
				return false;
				
			});			
		
		}
				
		function beonoAccentPopUpLoadImage(imageSrc, $beonoAccentPopUpContent, $beonoAccentPopUp, $beonoAccentLoading, $beonoAccentPopUpHeaderWrapper, $this) {
						
			$beonoAccentPopUpContent.html("<img />");
			$("."+$beonoAccentPopUpContent.attr("class")+" img").load(function () {
	
				$(this).fadeIn(settings.popUpShowTime);
				beonoAccentPopUpShow($beonoAccentPopUp);
				$beonoAccentLoading.hide();
				$beonoAccentPopUp.removeAttr("width"); 
				$beonoAccentPopUp.attr("width", $beonoAccentPopUp.outerWidth()); 
				$beonoAccentPopUpHeaderWrapper.text($this.children().attr("alt"));
			
			}).attr("src", imageSrc);
						
		}
		
		function beonoAccentPopUpLoadUri(uri, $beonoAccentPopUpContent, $beonoAccentPopUp, $beonoAccentLoading) {
					
	/*
			$beonoAccentPopUpContent.html('<iframe frameborder="0"></iframe>');
			
			$("."+$beonoAccentPopUpContent.attr("class")+" iframe").load(function () {
					
				beonoAccentPopUpShow($beonoAccentPopUp);
				$beonoAccentLoading.hide();
				$beonoAccentPopUp.attr("width", $beonoAccentPopUp.outerWidth()); 
				$beonoAccentPopUpHeaderWrapper.text($this.attr("title"));
			}).attr("src", uri);	
	*/
	
			$beonoAccentPopUpContent.html('<iframe src="" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></iframe>');
			
			$beonoAccentPopUpHeaderWrapper.text($this.attr("title"));
			beonoAccentPopUpShow($beonoAccentPopUp);
			
			$("."+$beonoAccentPopUpContent.attr("class")+" iframe").load(function () {
			
			$beonoAccentLoading.hide();
			$beonoAccentPopUp.attr("width", $beonoAccentPopUp.outerWidth());
			$("."+$beonoAccentPopUpContent.attr("class")+" iframe").css("visibility", "visible");
			}).attr("src", uri);
 
						
		}
		
		function beonoAccentPopUpLoadElement(popUpElement, $beonoAccentPopUpContent, $beonoAccentPopUp, $beonoAccentLoading) {
						
			$($($(popUpElement))).clone().appendTo($beonoAccentPopUpContent).show();
	
			beonoAccentPopUpShow($beonoAccentPopUp);
			$beonoAccentLoading.hide();
			$beonoAccentPopUpHeaderWrapper.text($this.attr("title"));
					
		}
		
		function beonoAccentPopUpFadeIn ($beonoAccentPopUp) {
		
			$beonoAccentPopUp.css("display", "block");
			$beonoAccentPopUp.css("opacity", "0");
			$beonoAccentPopUp.animate({ opacity:1 }, settings.popUpShowTime);
			setTimeout('$(".beono-accent-popup").css("opacity", "")', settings.popUpShowTime + 20);
		
		}
		
		function beonoAccentPopUpShow($beonoAccentPopUp){
		
			$beonoAccentPopUp.show();
			$beonoAccentPopUp.hide();	 	 
			$beonoAccentPopUp.css("z-index", ($("div.beono-accent-fader").css("z-index") + 2));	
			
			beonoAccentPopUpInitPosition($beonoAccentPopUp);	    
			beonoAccentPopUpFadeIn($beonoAccentPopUp);			
		}
	
		function beonoAccentPopUpInitPosition($beonoAccentPopUp, windowWidth, windowHeight, animatePosition) {
			
			if (!windowWidth || !windowHeight) {
				var windowSize = beonoGetPageSize();
				var windowWidth	 = windowSize[0];
				var windowHeight = windowSize[1];
			}
					
			beonoAccentPopUpLeft = (windowWidth - $beonoAccentPopUp.width())/2;
			beonoAccentPopUpTop 	= (windowHeight/2) - ($beonoAccentPopUp.height()/2);
			
			if (settings.isIE || $beonoAccentPopUp.css("position") == "absolute") {
			
				if (document.documentElement.scrollTop) {
					var windowScrollTop = document.documentElement.scrollTop;	
				} else {			
					var windowScrollTop = document.body.scrollTop;
				}
				
				beonoAccentPopUpTop = beonoAccentPopUpTop + windowScrollTop;	
				
				if (windowHeight < $beonoAccentPopUp.height()) {
				
					beonoAccentPopUpTop = windowScrollTop;
					
				} 	
			}
	
			// If window resized and PopUp already displayed then slide it to the position
			if ((parseInt($beonoAccentPopUp.css("left")) || parseInt($beonoAccentPopUp.css("top"))) && animatePosition) {
						
					$beonoAccentPopUp.animate({ left: beonoAccentPopUpLeft+"px", top: beonoAccentPopUpTop+"px"}, settings.faderShowTime);	
					//alert(animatePosition);
				
			} else {
			
				$beonoAccentPopUp.css("left", beonoAccentPopUpLeft);
				$beonoAccentPopUp.css("top", beonoAccentPopUpTop);
				
			}	
						
		}
						
		function beonoAccentFaderInitSize($beonoAccentFader, windowWidth, windowHeight){
		
			if (!windowWidth || !windowHeight) {
				var windowSize = beonoGetPageSize();
				var windowWidth	 = windowSize[0];
				var windowHeight = windowSize[1];
			}
			
			
			if (settings.isIE) {
			
				if (document.documentElement.scrollTop) {
					var windowScrollTop = document.documentElement.scrollTop;	
				} else {			
					var windowScrollTop = document.body.scrollTop;
				}
				
				$beonoAccentFader.css("top", 0);	
				$beonoAccentFader.css("height",  $(document).height()+"px");
				
			} else if(parseInt($beonoAccentFader.css("width")) < windowWidth || parseInt($beonoAccentFader.css("height")) < windowHeight) {
				
				/*
				$beonoAccentFader.css("width", windowWidth);
				$beonoAccentFader.css("height", windowHeight);
				*/

				$beonoAccentFader.css("top", 0);	
				$beonoAccentFader.css("height",  $(document).height()+"px");
			}	
		
		}
		
		function beonoAccentFaderShow($beonoAccentFader){
		
			if($beonoAccentFader.css("display") != "block") {
			
				beonoAccentFaderInitSize($beonoAccentFader);
				$beonoAccentFader.css("display", "block");
				$beonoAccentFader.css("opacity", "0");
				$beonoAccentFader.animate({opacity: settings.faderOpacity}, settings.faderShowTime);
			}
		
		}		
					
		function beonoGetPageSize(){
		
			var de = document.documentElement;
			var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
			var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
			arrayPageSize = [w,h];
			return arrayPageSize;
			
		}
	
	}
			


};