/***** EASING *****/

jQuery.easing.jswing=jQuery.easing.swing;jQuery.extend(jQuery.easing,{def:"easeInOutQuad",swing:function(e,f,a,h,g){return jQuery.easing[jQuery.easing.def](e,f,a,h,g)},easeInQuad:function(e,f,a,h,g){return h*(f/=g)*f+a},easeOutQuad:function(e,f,a,h,g){return -h*(f/=g)*(f-2)+a},easeInOutQuad:function(e,f,a,h,g){if((f/=g/2)<1){return h/2*f*f+a}return -h/2*((--f)*(f-2)-1)+a},easeInQuint:function(e,f,a,h,g){return h*(f/=g)*f*f*f*f+a},easeOutQuint:function(e,f,a,h,g){return h*((f=f/g-1)*f*f*f*f+1)+a},easeInOutQuint:function(e,f,a,h,g){if((f/=g/2)<1){return h/2*f*f*f*f*f+a}return h/2*((f-=2)*f*f*f*f+2)+a},easeInExpo:function(e,f,a,h,g){return(f===0)?a:h*Math.pow(2,10*(f/g-1))+a},easeOutExpo:function(e,f,a,h,g){return(f==g)?a+h:h*(-Math.pow(2,-10*f/g)+1)+a},easeInOutExpo:function(e,f,a,h,g){if(f===0){return a}if(f==g){return a+h}if((f/=g/2)<1){return h/2*Math.pow(2,10*(f-1))+a}return h/2*(-Math.pow(2,-10*--f)+2)+a},easeInRegular:function(e,f,a,h,g){return h*(f/=g)*f+a},easeOutRegular:function(e,f,a,h,g){return -h*(f/=g)*(f-2)+a},easeInOutRegular:function(e,f,a,h,g){if((f/=g/2)<1){return h/2*f*f+a}return -h/2*((--f)*(f-2)-1)+a}});


/***** WICKEDWEB LIGHTBOX v1.1 | COPYRIGHT WICKEDWEB 2011 *****/

var lbSrc = "";
var galleryArray=[];
var imgNo = 0;
var lbStatus = 'closed';

String.prototype.endsWith = function(b){
    return (this.match(b + "$") == b);
};

function querySt(value, string){
	valueArray = string.split('?').join('&').split('&');
	for(i=0; i<valueArray.length; i++){
		result = valueArray[i].split('=');
		if(result[0] == value) {
			return result[1];
		}
	}
}

function repositionLightBox(){
	if(($('#lightBoxInner').outerHeight() + 40) > $(window).height() && $('#lightBox:visible').size() > 0){
		$('#lightBoxContent').css('top', ($(window).scrollTop() + 20) + 'px').css('position', 'absolute').css('margin-top', 0);
	}
	else{
		$('#lightBoxContent').css('top', '50%').css('position', 'fixed').css('margin-top', -($('#lightBoxInner').outerHeight() / 2));
	}
}

function animateLightBox(){
	var mTop = -($('#lightBoxInner').outerHeight() / 2);
	var mLft = -($('#lightBoxInner').outerWidth() / 2);
	if(($('#lightBoxInner').outerHeight() + 40) > $(window).height()){
		$('#lightBoxContent').css('top', ($(window).scrollTop() + 20) + 'px').css('position', 'absolute').css('margin-top', 0);
		mTop = 0;
	}
	else{
		$('#lightBoxContent').css('top', '50%').css('position', 'fixed');
	}
	$('#lightBoxContent').stop().animate({ 
		width: $('#lightBoxInner').outerWidth(),
		height: $('#lightBoxInner').outerHeight(),
		marginTop: mTop,
		marginLeft: mLft,
		opacity: 1
	},{duration:600,
		easing:"easeInOutRegular",
		step:function(){
			$('#lbtl,#lbtr,#lbbl,#lbbr,').hide().show();
		},
		complete:function(){
			repositionLightBox();
			$('#lightBoxContent').css('filter', '');
			$('#lightBoxInner').css('visibility', 'visible');
			$('#lightBoxInner').stop().fadeTo(500, 1, function(){
				$('#lightBoxContent, #lightBoxInner').css('filter', '');
			});
		}
	});
}

function detectContent(){
	$('#lightBoxInner').empty();
	$('#lightBoxInner').width('auto').height('auto');
	$('#lightBoxContent').css('overflow', 'hidden').width($('#lightBoxContent').width()).height($('#lightBoxContent').height()).css('margin', (-($('#lightBoxContent').outerHeight() / 2)) + 'px 0 0 ' + (-($('#lightBoxContent').outerWidth() / 2)) + 'px');
	var Title = querySt('title', lbSrc);
	var Desc = querySt('desc', lbSrc);
	var Width = querySt('width', lbSrc);
	var Height = querySt('height', lbSrc);
	var Close = querySt('close', lbSrc);
	if(Width !== undefined){
		$('#lightBoxInner').width(parseInt(Width));
	}
	if(Height !== undefined){
		$('#lightBoxInner').height(parseInt(Height));
	}
	if(Title !== undefined){
		$('#lightBoxInner').append('<h1>' + Title + '</h1>');
	}
	if(Desc !== undefined){
		$('#lightBoxInner').append(Desc);
	}
	if(lbSrc.indexOf('?') > -1){
		lbSrc = lbSrc.slice(0, lbSrc.indexOf('?'));
	}
	if(Close == 'false'){
		$('body').addClass('noClose');
		$('#lightBox').removeClass('closeLightBox');
	}
	else{
		$('body').removeClass('noClose');
		$('#lightBox').addClass('closeLightBox');
	}
	
	r = document.location.href.toString().replace(document.location.hash.toString(), '');
    if(lbSrc.indexOf('?') > -1){
        q = lbSrc.slice(lbSrc.indexOf('?'));
        lbSrc = lbSrc.slice(0, lbSrc.indexOf('?'));
    }
    if(lbSrc.indexOf(r) === 0){
        lbSrc = lbSrc.replace(r, '');
    }
    if(lbSrc.indexOf('#') === 0){
		$(lbSrc).clone(true).appendTo('#lightBoxInner').show();
		animateLightBox();
		lbStatus = 'content';
    }
	else if(lbSrc.indexOf('http://youtu.be/') === 0){
		if(Width == undefined){
			Width = 560;
		}
		if(Height == undefined){
			Height = 345;
		}
		lbSrc = lbSrc.replace('http://youtu.be/','http://www.youtube.com/embed/');
		$('#lightBoxInner').append('<iframe class="youtube" width="' + Width + '" height="' + Height + '" src="' + lbSrc + '?autoplay=1" frameborder="0"></iframe>');
		animateLightBox();
		lbStatus = 'youtube';
	}
	else if(lbSrc.endsWith('.html') || lbSrc.endsWith('.htm') || lbSrc.endsWith('.php') || lbSrc.endsWith('.aspx')){
		$('#lightBoxMid').addClass('loading');
		if(lbStatus == 'closed'){
			$('#lightBoxInner').width(100).height(100);
			animateLightBox();
		}
		$('#lightBoxInner').load(lbSrc, function(){
			$('#lightBoxMid').removeClass('loading');
			animateLightBox();
			lbStatus = 'html';
		});	
	}
	else if(lbSrc.endsWith('.jpg') || lbSrc.endsWith('.gif') || lbSrc.endsWith('.png') || lbSrc.endsWith('.bmp') || lbSrc.endsWith('.tiff')){
		$('#lightBoxMid').addClass('loading');
		if(lbStatus == 'closed'){
			$('#lightBoxInner').width(100).height(100);
			animateLightBox();
		}		
		var img = "img" + lbSrc.slice(lbSrc.lastIndexOf("/") + 1, lbSrc.lastIndexOf("."));
		eval[img] = new Image();
		eval[img].onload = function(evt){
			$('#lightBoxInner').fadeTo(0, 0, function(){
				if(Width !== undefined){
					eval[img].width = parseInt(Width);
				}
				if(Height !== undefined){
					eval[img].height = parseInt(Height);
				}
				$('#lightBoxInner').width(eval[img].width).height(eval[img].height).append(eval[img]);
				if(galleryArray.length > 1){
					$('#lightBoxInner').append('<a href="/Previous" class="prevImg"></a><a href="/Next" class="nextImg"></a>');
				}
				$('#lightBoxMid').removeClass('loading');
				animateLightBox();
				lbStatus = 'image';
			});
		};
		eval[img].src = lbSrc;
		imgNo = jQuery.inArray(lbSrc, galleryArray);
	}
}

function showLightBox(){
	$('#lightBoxInner').css('visibility', 'hidden');
	if($('#lightBoxContent:visible').size() > 0){
		detectContent();
	}
	else{
		$('#lightBox').stop().show().fadeTo(0, 0).fadeTo(500, 0.8, function(){
			$('#lightBoxContent').stop().show().fadeTo(0, 0, function(){
				detectContent();
			});
		});
	}
}

function openLightBox(s){
	lbSrc = s;
	if($('#lightBoxContent:visible').size() > 0){
		$('#lightBoxInner').stop().fadeTo(500, 0, function(){
			showLightBox();
		});
	}
	else{
		$('#lightBoxInner').fadeTo(0, 0, function(){
			showLightBox();
		});
	}
}

function gallery(){
	galleryArray=[];
	$('.lightBox[href$=".jpg"], .lightBox[href$=".gif"], .lightBox[href$=".png"], .lightBox[href$=".bmp"], .lightBox[href$=".tiff"]').each(function(){
		var inArray = false;
		var url = $(this).attr('href');
		if(jQuery.inArray(url, galleryArray) === -1){
			galleryArray.push(url);
		}
	});
	$('a[rel$=".jpg"], a[rel$=".gif"], a[rel$=".png"], a[rel$=".bmp"], a[rel$=".tiff"]').each(function(){
		var inArray = false;
		var url = $(this).attr('rel');
		if(jQuery.inArray(url, galleryArray) === -1){
			galleryArray.push(url);
		}
	});
	$('.thumbs a').not('.thumbs a.lightBox').click(function(){
		$('.thumbs a:hidden').insertBefore($(this)).show();
		$(this).hide();
		$('.gallery a').attr('href', $(this).attr('rel'));
		var img = $(this).attr('href');
		eval[img] = new Image();
		eval[img].src = img;
		$('.gallery a img').stop().fadeTo(500,0,function(){
			eval[img] = new Image();
			eval[img].onload = function (evt){
				$('.gallery a img').attr('src', img);
				$('.gallery a img').fadeTo(500,1);
			};
			eval[img].src = img;
		});
		return false;
	});
}

function closeLightBox(){
	$('#lightBoxMid').removeClass('loading');
	if(lbStatus == 'youtube'){
		$('#lightBoxInner iframe').hide();
	}
	$('#lightBoxContent').stop().fadeTo(500, 0, function(){
		$('#lightBox').stop().fadeTo(500, 0, function(){
			$('#lightBoxContent').hide().width('100px').height('100px');
			$('#lightBox').hide().addClass('closeLightBox');;
			$('#lightBoxInner').empty();
			$('body.noClose').removeClass('noClose');
			lbStatus = 'closed';
		});
	});
}

function initWWightBox(){
	$('body').append('<div id="lightBox"></div><div id="lightBoxContent"><div id="lightBoxMid"><a href="/Close" id="closeLightBoxBtn" class="closeLightBox"></a><div id="lightBoxInner"></div></div></div>');
	$(window).resize(function(){
		repositionLightBox();
	});
	gallery();
}

function prevImage(){
	imgNo--;
	if(imgNo < 0){
		imgNo = galleryArray.length-1;
	}
	openLightBox(galleryArray[imgNo]);
}

function nextImage(){
	imgNo++;
	if(imgNo > (galleryArray.length-1)){
		imgNo = 0;
	}
	openLightBox(galleryArray[imgNo]);
}

$('#topCallout:has(#confirmBox) a:first').live('click', function(){
	openLightBox('#confirmBox?close=false');
	return false;
});

$('.lightBox, a[href^="http://youtu.be/"]').live('click', function(e){
	openLightBox($(this).attr('href'));
	return false;
});

$('.closeLightBox').live('click', function(){
	closeLightBox();
	return false;
});

$('.prevImg').live('click', function(){
	prevImage();
	return false;
});

$('.nextImg').live('click', function(){
	nextImage();
	return false;
});

$(document).keyup(function(event){
	if(lbStatus != 'closed' && event.keyCode == '27'){
		event.preventDefault();
		closeLightBox();
	}
	else if(lbStatus == 'image' && (event.keyCode == '37' || event.keyCode == '80')){
		event.preventDefault();
		prevImage();
	}
	else if(lbStatus == 'image' && (event.keyCode == '39' || event.keyCode == '78')){
		event.preventDefault();
		nextImage();
	}
	else if(lbStatus == 'image' && ((parseInt(event.keyCode)) > 48 && (parseInt(event.keyCode)) < 58)){
		if((9 - (58 - (parseInt(event.keyCode)))) != imgNo){
			imgNo = (9 - (58 - (parseInt(event.keyCode))));
			if(imgNo > (galleryArray.length-1)){
				imgNo = (galleryArray.length-1);
			}
			openLightBox(galleryArray[imgNo]);
		}
	}
	else if(lbStatus == 'image' && ((parseInt(event.keyCode)) > 96 && (parseInt(event.keyCode)) < 106)){
		if((9 - (106 - (parseInt(event.keyCode)))) != imgNo){
			imgNo = (9 - (106 - (parseInt(event.keyCode))));
			if(imgNo > (galleryArray.length-1)){
				imgNo = (galleryArray.length-1);
			}
			openLightBox(galleryArray[imgNo]);
		}
	}
});


$(function(){
	initWWightBox();
});
