/***** 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 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(){
	$('#lightBoxInner').prepend('<a href="/Close" id="closeLightBoxBtn" class="closeLightBox"></a>')
	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(boxID){
	$('#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', boxID);
	var Desc = querySt('desc', boxID);
	var Width = querySt('width', boxID);
	var Height = querySt('height', boxID);
	var Lat = querySt('lat', boxID);
	var Lang = querySt('lang', boxID);
	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(boxID.indexOf('?') > -1){
		boxID = boxID.slice(0, boxID.indexOf('?'));
	}
	
	r = document.location.href.toString().replace(document.location.hash.toString(), '');
    if(boxID.indexOf('?') > -1){
        q = boxID.slice(boxID.indexOf('?'));
        boxID = boxID.slice(0, boxID.indexOf('?'))
    }
    if(boxID.indexOf(r) === 0){
        boxID = boxID.replace(r, '');
    }
	if(Lat !== undefined && Lang !== undefined){
		$('#lightBoxInner').append('<div id="map" style="width:' + Width + 'px; height:' + Height +'px;"></div>');
		var latlng = new google.maps.LatLng(Lat, Lang);
		var myOptions = {
			zoom: 16,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		var map = new google.maps.Map(document.getElementById("map"), myOptions);
		//var image = 'imagesbeachflag.png';
		var beachMarker = new google.maps.Marker({
			position: latlng,
			map: map//,
			//icon: image
		});
		animateLightBox();
		lbStatus = 'content';
	}
    else if(boxID.indexOf('#') === 0) {
		$(boxID).clone(true).appendTo('#lightBoxInner').show();
		animateLightBox();
		lbStatus = 'content';
    }
	else if(boxID.endsWith('.html') || boxID.endsWith('.htm') || boxID.endsWith('.php') || boxID.endsWith('.aspx')){
		$('#lightBoxMid').addClass('loading');
		if(lbStatus == 'closed'){
			$('#lightBoxInner').width(100).height(100);
			animateLightBox();
		}
		$('#lightBoxInner').load(boxID, function(){
			$('#lightBoxMid').removeClass('loading');
			animateLightBox();
			lbStatus = 'html';
		});	
	}
	else if(boxID.endsWith('.jpg') || boxID.endsWith('.gif') || boxID.endsWith('.png') || boxID.endsWith('.bmp') || boxID.endsWith('.tiff')){
		$('#lightBoxMid').addClass('loading');
		if(lbStatus == 'closed'){
			$('#lightBoxInner').width(100).height(100);
			animateLightBox();
		}		
		var img = "img" + boxID.slice(boxID.lastIndexOf("/") + 1, boxID.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 = boxID;
		imgNo = jQuery.inArray(boxID, galleryArray);
	}
}

function showLightBox(boxID){
	$('#lightBoxInner').css('visibility', 'hidden');
	if($('#lightBoxContent:visible').size() > 0){
		detectContent(boxID);
	}
	else{
		$('#lightBox').stop().show().fadeTo(0, 0).fadeTo(500, 0.75, function(){
			$('#lightBoxContent').stop().show().fadeTo(0, 0, function(){
				detectContent(boxID);
			});
		});
	}
}

function openLightBox(boxID){
	if($('#lightBoxContent:visible').size() > 0){
		$('#lightBoxInner').stop().fadeTo(500, 0, function(){
			showLightBox(boxID);
		});
	}
	else{
		$('#lightBoxInner').fadeTo(0, 0, function(){
			showLightBox(boxID);
		});
	}
}

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');
	$('#lightBoxContent').stop().fadeTo(500, 0, function(){
		$('#lightBox').stop().fadeTo(500, 0, function(){
			$('#lightBoxContent').hide().width('100px').height('100px');
			$('#lightBox').hide();
			$('#lightBoxInner').empty();
			lbStatus = 'closed';
		});
	});
}

function initWWightBox(){
	$('body').append('<div id="lightBox"></div><div id="lightBoxContent"><div id="lbtl"></div><div id="lbtr"></div><div id="lbbl"></div><div id="lbbr"></div><div id="lightBoxMid"><div id="lightBoxInner"></div></div></div>');
	$(document).bind('mouseup', function(e){
		$('.zoomBox img').unbind('mousemove');
		$('.zoomBox img').css('cursor', 'pointer');
	});
	$(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]);
}

$('.lightBox').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 == '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();
});
