/***** WICKEDWEB LIGHTBOX v1.1 | COPYRIGHT WICKEDWEB 2010 *****/

var zoomed = false;

function setCSS(css) {
	try{
		document.getElementsByTagName("head")[0].appendChild(css);
	}catch(e){
		setTimeout(function(){setCSS(css)}, 100);
	}
}
var css = document.createElement("link");
css.setAttribute("href","/static/css/wwightbox.css");
css.setAttribute("rel","stylesheet");
css.setAttribute("type","text/css");
setCSS(css);
css = null;

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 animateLightBox(){
	$('#lightBoxContent').css('top', '50%').css('position', 'fixed').css('margin-top', 0);
	var mTop = (-(($('#lightBoxInner').outerHeight() + ($('#lightBoxContent').outerHeight() - $('#lightBoxContent').height())) / 2));
	var mBtm = (-(($('#lightBoxInner').outerWidth() + ($('#lightBoxContent').outerWidth() - $('#lightBoxContent').width())) / 2));
	if(($('#lightBoxInner').outerHeight() + 40) > $(window).height()){
		mTop = 0;
		$('#lightBoxContent').css('top', ($(window).scrollTop() + 20) + 'px').css('position', 'absolute').css('margin-top', 0);
	}
	$('#lightBoxContent').stop().animate({ 
		width: $('#lightBoxInner').outerWidth(),
		height: $('#lightBoxInner').outerHeight() ,
		marginTop: mTop,
		marginLeft: mBtm,
		opacity: 1
	}, 600, "easeInOutRegular", function(){
		$('#lightBoxContent').css('filter', '');
		$('#lightBoxInner').css('visibility', 'visible');
		$('#lightBoxInner, #closeLightBoxBtn').stop().fadeTo(500, 1, function(){
			$('#lightBoxContent, #closeLightBoxBtn').css('filter', '')
			//$('#lightBoxInner').attr('style', '').css('filter', '');
			$('#lightBoxInner').css('filter', '');
		});
	});
}*/

function animateLightBox() {
    $("#lightBoxInner").prepend('<a id="closeLightBoxBtn" class="closeLightBox" href="Close"><img src="/static/images/close_btn.gif" width="11" height="11" /></a>');
	var height = $("#lightBoxInner").outerHeight();
	var width = $("#lightBoxInner").outerWidth()
    var mTop = -(height / 2);
    var mBtm = -(width / 2);
    if((height + 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: width,
        height: height,
        marginTop: mTop,
        marginLeft: mBtm,
        opacity: 1
    }, 600, "easeInOutRegular", 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');
	myTitle = querySt('title', boxID);
	myDesc = querySt('desc', boxID);
	myWidth = querySt('width', boxID);
	myHeight = querySt('height', boxID);
	myZoom = querySt('zoom', boxID);
	my360 = querySt('360', boxID);
	myShowcaseText = querySt('sctext', boxID);
	myShowcaseLink = querySt('sclink', boxID);
	if(myWidth !== undefined){
		$('#lightBoxInner').width(parseInt(myWidth));
	}
	if(myHeight !== undefined){
		$('#lightBoxInner').height(parseInt(myHeight));
	}
	if(myTitle !== undefined){
		$('#lightBoxInner').append('<h2>' + myTitle + '</h2>')
	}
	if(myDesc !== undefined){
		$('#lightBoxInner').append('<p><strong>' + myDesc + '</strong></p>')
	}
	if(boxID.indexOf('?') > -1){
		boxID = boxID.slice(0, boxID.indexOf('?'));
	}
	if(boxID.indexOf('#') > -1){
		/*if(boxID.indexOf('?') > -1){
			boxID = boxID.slice(boxID.indexOf('#'), boxID.indexOf('?'));
		}
		else{
			boxID = boxID.slice(boxID.indexOf('#'));
		}*/
		$(boxID).clone(true).appendTo('#lightBoxInner').show();
		animateLightBox();
	}
	else if(boxID.indexOf('.html') > -1 || boxID.indexOf('.htm') > -1 || boxID.indexOf('.php') > -1 || boxID.indexOf('.aspx') > -1){
		if($('#lightBoxContent:visible').size() <= 0){
			animateLightBox();
		}
		$('#lightBoxInner').load(boxID, '', function(){
			animateLightBox();
		});	
	}
	else if(boxID.indexOf('.jpg') > -1 || boxID.indexOf('.gif') > -1 || boxID.indexOf('.png') > -1|| boxID.indexOf('.bmp') > -1){
		if($('#lightBoxContent:visible').size() <= 0){
			animateLightBox();
		}
		if(myZoom == 'true'){
			$('#lightBoxInner').append('<div class="zoomWrap"><div class="zoomBox"></div><div class="zoomControls"><span class="zoom arrow">Click to zoom</span></div></div>');
			$('#lightBoxInner .zoomBox').html('<img src="' + boxID + '" />');
			if($('.f360').size() > 0){
				$('#lightBoxInner .zoomControls').append('<a class="arrow f360" href="' + $('.f360').attr('href') + '">' + $('.f360').text() + '</a>');
			}
			if($('.showcase').size() > 0){
				$('#lightBoxInner .zoomControls').append('<a class="arrow" href="' + $('.showcase').attr('href') + '">' + $('.showcase').text() + '</a>');
			}
			$('#lightBoxInner').append('<div class="zoomThumbs"><div class="arrowL"></div><div class="thumbWrap"><ul class="slider">' + $('.slider').html() + '</ul></div><div class="arrowR"></div></div>');
			$('#lightBoxInner .slider').css('width', 'auto');
			if($('#lightBoxInner .zoomThumbs li').size() > 7){
				$('#lightBoxInner .thumbWrap').width(696);
				$('#lightBoxInner .thumbWrap ul').width(($('#lightBoxInner .thumbWrap ul li').size() * 102) + 'px');
			}
			else{
				$('#lightBoxInner .zoomThumbs').addClass('zoomScroll');
				$('#lightBoxInner .thumbWrap').css('margin-left',  (Math.round(696 - $('#lightBoxInner .thumbWrap').width()) / 2) + 29) + 'px';
			}
			animateLightBox();
		}
		else{
			var img = new Image();
			img.onload = function(evt){
				$('#lightBoxInner').fadeTo(0, 0, function(){
					$('#lightBoxInner').append(img);
					animateLightBox();
				});
			}
			img.src = boxID;
		}
	}
}

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, #closeLightBoxBtn').fadeTo(0, 0, function(){
			showLightBox(boxID);
		});
	}
}

function closeLightBox(){
	$('#lightBoxContent').stop().fadeTo(500, 0, function(){
		$('#lightBox').stop().fadeTo(500, 0, function(){
			$('#lightBoxContent').hide().width('100px').height('100px');
			$('#lightBox').hide();
			$('#lightBoxInner').empty();
		});
	});
}

function initWWightBox(){
	$('body').append('<div id="lightBox" class="closeLightBox"></div><div id="lightBoxContent"><div id="lightBoxMid"><div id="lightBoxInner"></div></div></div>');
	$(document).mouseup(function(e){
	$('.zoomBox img').unbind('mousemove');
	$('.zoomBox img').css('cursor', 'pointer');
		return false;
	});
	$(window).resize(function(){
		repositionLightBox();
	});
}

/*function repositionLightBox(){
	if(($('#lightBoxInner').outerHeight() + 40) > $(window).height()){
		if($('#lightBoxContent:visible').size() > 0 && $('#lightBoxContent:visible').css('top') == '50%'){
			mTop = 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() + ($('#lightBoxContent').outerHeight() - $('#lightBoxContent').height())) / 2)));
	}
}*/

function repositionLightBox() {
    if (($("#lightBoxInner").outerHeight() + 40) > $(window).height()) {
        $("#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 getXY(e){
	pX = e.pageX;
	pY = e.pageY;
	var x = 0;
	var y = 0;
	$('#lightBoxContent').each(function(e){
		window.x = pX - this.offsetLeft;
		window.y = pY - this.offsetTop;
	})
}

$('.lightBox').live('click', function(e){
	openLightBox($(this).attr('href'));
	return false;
});

$('.enlarge').live('click', function(e){
	openLightBox($(this).attr('href') + '?' + $(this).attr('rel'));
	return false;
});

$('.closeLightBox').live('click', function(){
	closeLightBox()
	return false;
});

$(document).keypress(function(event){
	if($('#lightBoxContent:visible').size() > 0 && event.keyCode == '27'){
		event.preventDefault();
		closeLightBox();
	}
});

$('.zoom').live('click', function(e){
	if(zoomed == false){
		$('.zoomBox img').css('cursor', 'pointer');
		$('.zoomBox img').stop().animate({ 
			width: 2002,
			height: 1716,
			left: -parseInt((2002/2) - (546/2)),
			top: -parseInt((1716/2) - (468/2))
		}, 600, "easeInOutRegular", function(){
			zoomed = true;
		});
	}
	else{
		$('.zoomBox img').css('cursor', 'default');
		$('.zoomBox img').stop().animate({ 
			width: 546,
			height: 468,
			left: 0,
			top: 0
		}, 600, "easeInOutRegular");
		zoomed = false;
	}
	return false;
});

$('.zoomBox img').live('mousedown', function(e){
	if(zoomed == true){
		$('.zoomBox img').css('cursor', 'move');
		getXY(e);
		var moveX = parseInt($(this).css('left')) - window.x;
		var moveY = parseInt($(this).css('top')) - window.y;
		$(this).mouseenter(function(e){
			getXY(e);
			moveX = parseInt($(this).css('left')) - window.x;
			moveY = parseInt($(this).css('top')) - window.y;
		});
		$(this).mousemove(function(e){
			getXY(e);
			if((moveX + window.x) < 0 && (moveX + window.x) > -($(this).width() - 546)){
				$(this).css('left', (moveX + window.x) + 'px');
			}
			else if((moveX + window.x) > 0){
				$(this).css('left', '0');
				moveX = parseInt($(this).css('left')) - window.x;
			}
			else if((moveX + window.x) < -($(this).width() - 546)){
				$(this).css('left', -($(this).width() - 546) + 'px');
				moveX = parseInt($(this).css('left')) - window.x;
			}
			if((moveY + window.y) < 0 && (moveY + window.y) > -($(this).height() - 468)){
				$(this).css('top', (moveY + window.y) + 'px');
			}
			else if((moveY + window.y) > 0){
				$(this).css('top', '0');
				moveY = parseInt($(this).css('top')) - window.y;
			}
			else if((moveY + window.y) < -($(this).height() - 468)){
				$(this).css('top', -($(this).height() - 468) + 'px');
				moveY = parseInt($(this).css('top')) - window.y;
			}
			return false;
		});
	}
	return false;
});

$('.#lightBoxInner .f360').live('click', function(e){
	zoomed = false;
	f360 = $.flash.create({swf: $(this).attr('href'),width:546,height:468,expressInstaller:'flash/expressInstall.swf',flashvars:{},params:{wmode:'transparent'}});
	$('#lightBoxInner .zoomBox').html(f360);
	$('#lightBoxInner .zoom').fadeTo(200, 0.5);
	return false;
});

$('.zoomThumbs a').live('click', function(){
	zoomed = false;
	$('#lightBoxInner .zoomBox').html('<img src="' + $(this).attr('rel') + '" />');
	$('#lightBoxInner .zoom').fadeTo(200, 1);
	return false;
});


/***** ON READY FUNCTIONS *****/

$(document).ready(function(){
	initWWightBox();
});

