/***** 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;

String.prototype.endsWith = function (b) {
    return (this.match(b + "$") == b)
};

function animateLightBox(){
	$('#lightBoxInner').prepend('<a id="closeLightBoxBtn" class="closeLightBox" href="Close"><img src="/static/images/close_btn.gif" width="11" height="11" /></a>');
	$('#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').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);
	myVideo = querySt('video', boxID);
	myxml = querySt('xml', boxID);
	myID = querySt('id', boxID);
	myEnhanced = querySt('enhanced', 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(myDesc);
	}
	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 (myID !== undefined){
		if(boxID == '#formlink'){
			$(boxID).appendTo('#lightBoxInner').show();
		}
		else{
			$(boxID).clone(true).appendTo('#lightBoxInner').show();
			$('#lightBoxInner h1:first').prependTo('#lightBoxInner');
		}
		animateLightBox();
	}
    else if (boxID.indexOf('#') === 0) {
		$(boxID).clone(true).appendTo('#lightBoxInner').show();
		animateLightBox();
    }
	else if(Boolean(myZoom) || Boolean(my360) || Boolean(myVideo)){
		$('#lightBoxInner').append('<div class="zoomWrap"><div class="zoomBox"></div><div class="zoomControls"><span class="zoom arrow">Click to zoom</span></div></div>');
		if(Boolean(my360)){
			f360 = $.flash.create({swf: boxID,width:546,height:468,wmode:'transparent',expressInstaller:'flash/expressInstall.swf',flashvars:{}});
			$('#lightBoxInner .zoomBox').html(f360);
			$('#lightBoxInner .zoomControls .zoom').fadeTo(0, 0.3).addClass('disabled');
		}
		else if(Boolean(myVideo)){
			f360 = $.flash.create({swf: boxID,width:546,height:468,wmode:'transparent',expressInstaller:'flash/expressInstall.swf',flashvars:{}});
			$('#lightBoxInner .zoomBox').html('<iframe width="100%" height="100%" src="' + boxID + '" frameborder="0" allowfullscreen></iframe>');
			$('#lightBoxInner .zoomControls .zoom').fadeTo(0, 0.3).addClass('disabled');
		}
		else{
			loadImage('#lightBoxInner .zoomBox', boxID, true);
		}
		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').append('<div class="zoomThumbs"><div class="thumbWrap"><ul class="slider nobar">' + $('.slider').html() + '</ul></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');
			//initSlider('#lightBoxInner');
		}
		else{
			//$('#lightBoxInner .zoomThumbs').addClass('zoomScroll');
			$('#lightBoxInner .thumbWrap').css('margin-left',  (Math.round(696 - $('#lightBoxInner .thumbWrap').width()) / 2) + 29) + 'px';
		}
		animateLightBox();
	}
	else if(boxID.endsWith('.swf')){
		flashGallery = $.flash.create({swf: boxID,width:myWidth,height:myHeight,wmode:'transparent',expressInstaller:'flash/expressInstall.swf',flashvars:{useFlickr:'false', xmlURL:myxml, maxJPGSize:640, useReloadButton:'false', columns:3, rows:3, showFlipButton:'false', langGoFull:''}});
		$('#lightBoxInner').append(flashGallery).find('> :last').addClass('galleryFlash');
		animateLightBox();
	}
	else if(boxID.endsWith('.html') || boxID.endsWith('.html') || boxID.endsWith('.php') || boxID.endsWith('.aspx')){
		if($('#lightBoxContent:visible').size() <= 0){
			animateLightBox();
		}
		$('#lightBoxInner').load(boxID, '', function(){
			animateLightBox();
		});	
	}
	else if(boxID.endsWith('.jpg') || boxID.endsWith('.gif') || boxID.endsWith('.png') || boxID.endsWith('.bmp') || boxID.endsWith('.tiff')){
		if($('#lightBoxContent:visible').size() <= 0){
			animateLightBox();
		}		
		var img = "img" + boxID.slice(boxID.lastIndexOf("index.html") + 1, boxID.lastIndexOf("."));
		eval[img] = new Image();
		eval[img].onload = function (evt) {
			$('#lightBoxInner').fadeTo(0, 0, function(){
				$('#lightBoxInner').append(eval[img]);
				animateLightBox();
			});
		};
		eval[img].onerror = function (evt) {
			$('#lightBoxInner').fadeTo(0, 0, function(){
				$(this).removeClass("loading").append('<p>There was an error loading this image... <a href="' + boxID + q + '" class="lightBox">Click here</a> to try again.</p>');
				animateLightBox();
			});
		};
		eval[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').fadeTo(0, 0, function(){
			showLightBox(boxID);
		});
	}
}

function closeLightBox(){
	$('.zoomBox iframe').remove();
	$('#lightBoxContent').stop().fadeTo(500, 0, function(){
		$('#lightBox').stop().fadeTo(500, 0, function(){
			$('#lightBoxContent').hide().width('100px').height('100px');
			$('#lightBox').hide();
			if($('#lightBoxInner #formlink').size() > 0){
				$('#lightBoxInner #formlink').appendTo('body').hide();
			}
			$('#productimage iframe').show();
			$('#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).bind('mouseup', function(e){
		$('.zoomBox img').unbind('mousemove');
		$('.zoomBox img').css('cursor', 'pointer');
	});
	$(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 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;
});

$('.termsLink').live('click', function(e){
	openLightBox('#terms?width=760&height=550&id=terms');
	return false;
});

$('.reviewsdesigner').live('click', function(e){
	openLightBox('#reviews?width=700&id=reviews');
	return false;
});

$('.formLink').live('click', function(e){
	openLightBox('#formlink?width=560&id=formlink');
	return false;
});

$('.enlarge').live('click', function(e){
	if($(this).attr('href').indexOf('http://www.youtube.com/') > -1 || $('#productimage iframe').size() > 0){
		player.stopVideo();
	$('#productimage iframe').hide();
		openLightBox($(this).attr('href') + '?' + $(this).attr('rel') + '&video=true');
	}
	else{
		openLightBox($(this).attr('href') + '?' + $(this).attr('rel'));
	}
	if(typeof(zoomImage) == 'undefined'){
		window.zoomImage = querySt('z', '?' + $(this).attr('rel'));
	}
	return false;
});

$('#productControls .f360').live('click', function(e){
	zoomed = false;
	openLightBox($(this).attr('href') + '?' + $('#productControls .enlarge:first').attr('rel') + '&360=true');
	return false;
});

$('.#lightBoxInner .f360').live('click', function(e){
	zoomed = false;
	f360 = $.flash.create({swf: $(this).attr('href'),width:546,height:468,wmode:'transparent',expressInstaller:'flash/expressInstall.swf',flashvars:{}});
	$('#lightBoxInner .zoomBox').html(f360);
	$('#lightBoxInner .zoomControls .zoom').fadeTo(0, 0.3).addClass('disabled');
	$('#lightBoxInner .zoom').text('Click to zoom');
	return false;
});

$('#lightBoxInner .zoomThumbs a').live('click', function(){
	zoomed = false;
	var largeImage = querySt('l', '?' + $(this).attr('rel'));
	var zoomImage = querySt('z', '?' + $(this).attr('rel'));
	var imgURL = $(this).attr('href');
	if(imgURL.indexOf('http://www.youtube.com/') > -1){
		window.zoomImage = undefined;
		$('#lightBoxInner .zoomBox').html('<iframe width="100%" height="100%" src="' + imgURL + '" frameborder="0" allowfullscreen></iframe>');
	}
	else{
		loadImage('#lightBoxInner .zoomBox', largeImage, true);
		window.zoomImage = zoomImage;
		$('#lightBoxInner .zoomControls .zoom').fadeTo(0, 1).removeClass('disabled');
		$('#lightBoxInner .zoom').text('Click to zoom');
	}
	return false;
});

$('.closeLightBox').live('click', function(){
	closeLightBox()
	return false;
});

$(document).keypress(function(event){
	if($('#lightBoxContent:visible').size() > 0 && event.keyCode == '27'){
		event.preventDefault();
		closeLightBox();
	}
});

var newInterval = "";
$('#lightBoxInner .zoom, #lightBoxInner .zoomBox img:first').live('click', function(e){
	if(zoomed == false && $('#lightBoxInner .zoomBox img').size() > 0){
		if($('#lightBoxInner .zoomBox img').size() < 2){
			var img = new Image();
			img.onload = function(evt){
				$('#lightBoxInner .zoomBox').append(img);
				$('#lightBoxInner .zoomBox img:last').addClass('zoomedImage');
			}
			img.src = zoomImage;
		}
		$('#lightBoxInner .zoomBox img.zoomedImage').css('cursor', 'pointer');
		$('#lightBoxInner .zoomBox img:first').stop().animate({ 
			width: 2002,
			height: 1716,
			left: -parseInt(1001 - (546/2)),
			top: -parseInt(858 - (468/2))
		}, 600, "easeInOutRegular", function(){
			newInterval = window.setInterval(function(){
				if($('#lightBoxInner .zoomBox img').size() > 1){
					window.clearInterval(newInterval);
					$('#lightBoxInner .zoomBox img.zoomedImage').removeAttr('style');
					$('#lightBoxInner .zoomBox img:first').fadeOut(500);
				}
			}, 100)
			zoomed = true;
		});
		$(this).text('Click to zoom out');
	}
	else{
		$('#lightBoxInner .zoomBox img:last').stop().animate({ 
			width: 546,
			height: 468,
			left: 0,
			top: 0
		}, 600, "easeInOutRegular", function(){
			$('#lightBoxInner .zoomBox img:first').removeAttr('style').fadeIn(500);
		});
		zoomed = false;
		$(this).text('Click to zoom');
	}
	return false;
});
$('#lightBoxInner .zoomBox img').live('mousemove', function(e){
	return false;
});
$('#lightBoxInner .zoomBox img').live('mousedown', function(e){
	if(zoomed == true){
		$('#lightBoxInner .zoomBox img.zoomedImage').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;
});


/***** ON READY FUNCTIONS *****/

$(document).ready(function(){
	initWWightBox();
});

