(function($){

	$.fn.lightbox = function(options){
		// initalize the lightbox
		$.fn.lightbox.initialize();
		return this.each(function(){
			$(this).click(function(){
				$(this).lightbox.start(this);
				return false;
			});
		});
	};

	// lightbox functions
	$.fn.lightbox.initialize = function(){ 
		$('#overlay').remove();
		$('#lightbox').remove();

		var outerImage = '<div id="outerImageContainer"></div>';

		var string;
	  	string = '<div id="overlay"></div><div id="lightbox">' + outerImage + '</div>';
	  	$("body").append(string);

		$("#overlay").click(function(){ $.fn.lightbox.end(); }).hide();
		$("#lightbox").hide();
		$('#outerImageContainer').width(250).height(250);
	};

	$.fn.lightbox.getPageSize = function(){
		var xScroll, yScroll;

		if (window.innerHeight && window.scrollMaxY) {
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}

		var windowWidth, windowHeight;

		if (self.innerHeight) { // all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth;
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}

		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else {
			pageHeight = yScroll;
		}


		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){
			pageWidth = xScroll;
		} else {
			pageWidth = windowWidth;
		}

		var arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
		return arrayPageSize;
	};


	$.fn.lightbox.getPageScroll = function(){
		var xScroll, yScroll;

		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
			xScroll = self.pageXOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){  // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
			xScroll = document.documentElement.scrollLeft;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
			xScroll = document.body.scrollLeft;
		}

		var arrayPageScroll = new Array(xScroll,yScroll);
		return arrayPageScroll;
	};

	$.fn.lightbox.start = function(){

		$("select, embed, object").hide();
		var arrayPageSize = $.fn.lightbox.getPageSize();
		$("#overlay").hide().css({width: '100%', height: arrayPageSize[1]+'px', opacity: 0.8}).fadeIn();

		// calculate top and left offset for the lightbox
		var arrayPageScroll = $.fn.lightbox.getPageScroll();
		var lightboxTop = arrayPageScroll[1] + (arrayPageSize[3] / 10);
		var lightboxLeft = arrayPageScroll[0];
		$('#lightbox').css({top: lightboxTop+'px', left: lightboxLeft+'px'}).show();

		$.fn.lightbox.resizeImageContainer(400,250);

	};

	$.fn.lightbox.end = function(){
		$('#lightbox').hide();
		$('#overlay').fadeOut();
		$('select, object, embed').show();
	};

	$.fn.lightbox.resizeImageContainer = function(imgWidth, imgHeight){
		// get current width and height
		widthCurrent = document.getElementById('outerImageContainer').offsetWidth;
		heightCurrent = document.getElementById('outerImageContainer').offsetHeight;

		// get new width and height
		var widthNew = (imgWidth);
		var heightNew = (imgHeight);

		// scalars based on change from old to new
		xScale = ( widthNew / widthCurrent) * 100;
		yScale = ( heightNew / heightCurrent) * 100;

		// calculate size difference between new and old image, and resize if necessary
		wDiff = widthCurrent - widthNew;
		hDiff = heightCurrent - heightNew;

		$('#outerImageContainer').animate({width: widthNew},350,'linear',function(){
			$('#outerImageContainer').animate({height: heightNew},350,'linear',function(){
				$.fn.lightbox.showMailbox();
			});
		});
	};

	$.fn.lightbox.showMailbox = function() {
		var form_str;
		form_str = '<div id="mailbox">';
		form_str+= '<h4 class="c_video"><a href="#">X</a>NUSIŲSKITE VIDEO NUORODĄ DRAUGUI</h4>';
		form_str+= '<form id="mailboxForm" method="post" onsubmit="return false">';
		form_str+= '	<input type="hidden" name="video_id" value="'+String(window.comments_id)+'" />';
		form_str+= '	<div>';
		form_str+= '		<span><input type="text" name="draugo_elpastas" value="" />Draugo el.paštas: </span>';
		form_str+= '		<span><input type="text" name="jusu_vardas" value="" />Jūsų vardas: </span>';
		form_str+= '		<input type="button" value="Siųsti" id="submitForm" />';
		form_str+= '	</div>';
		form_str+= '	<div class="pvz">';
		form_str+= '		<h5>Laiško pavyzdys</h5>';
		form_str+= '		<p>Sveiki!</p>';
		form_str+= '		<p>[Jūsų vardas] išsiuntė Tau video nuorodą:<br/>';
		form_str+= '		http://www.betabu.lt/video/'+String(window.comments_id)+'</p>';
		form_str+= '	</div>';
		form_str+= '</form></div>';
		$('#outerImageContainer').html(form_str);
		$("#mailbox h4 a").click(function(){ $.fn.lightbox.end(); return false });
		
		$("#mailbox #submitForm").click(function() {
			if ( String($("#mailbox #mailboxForm input[name=jusu_vardas]").val()).length<3 ) {
				var inputfield = $("#mailbox #mailboxForm input[name=jusu_vardas]");
				inputfield.addClass('error').focus();
				inputfield.keyup(function(){ $(this).removeClass('error'); $(this).unbind(); });
				var errors=1;
			}
			if ( String($("#mailbox #mailboxForm input[name=draugo_elpastas]").val()).length<6 || !validateEmail($("#mailbox #mailboxForm input[name=draugo_elpastas]").val()) ) {
				var inputfield = $("#mailbox #mailboxForm input[name=draugo_elpastas]");
				inputfield.addClass('error').focus();
				inputfield.keyup(function(){ $(this).removeClass('error'); $(this).unbind(); });
				var errors=1;
			}
			if ( errors ) { return false; }
			$("#mailboxForm").ajaxSubmit({
				url: '/ajax/share/',
				beforeSubmit: function() { $("#mailbox #submitForm").before('<img src="/img/loader_white.gif"/>').remove(); },
				success: function() { $.fn.lightbox.end(); }
			});
		});
	};

})(jQuery);


function mailbox() {
	$.fn.lightbox.initialize();
	$.fn.lightbox.start();
}
