
jQuery.BugScrollDiv = {
active : []
};
jQuery.fn.BugScrollDiv = function(settings)
{
settings = jQuery.extend(
{
scrollbarWidth : 2,
scrollbarMargin : 5,
wheelSpeed : 18,
showArrows : false,
arrowSize : 0,
animateTo : false,
dragMinHeight : 59,
dragMaxHeight : 59,
animateInterval : 100,
animateStep: 3,
maintainPosition: true,
scrollbarOnLeft: false
}, settings
);
return this.each(
function()
{
var $this = jQuery(this);

if (jQuery(this).parent().is('.BugScrollDivContainer')) {
var currentScrollPosition = settings.maintainPosition ? $this.offset({relativeTo:jQuery(this).parent()[0]}).top : 0;
var $c = jQuery(this).parent();
var paneWidth = $c.innerWidth();
var paneHeight = $c.outerHeight();
var trackHeight = paneHeight;
if ($c.unmousewheel) {
$c.unmousewheel();
}
jQuery('>.BugScrollDivTrack, >.jScrollArrowUp, >.jScrollArrowDown', $c).remove();
$this.css({'top':0});
} else {
var currentScrollPosition = 0;
this.originalPadding = $this.css('paddingTop') + ' ' + $this.css('paddingRight') + ' ' + $this.css('paddingBottom') + ' ' + $this.css('paddingLeft');
this.originalSidePaddingTotal = (parseInt($this.css('paddingLeft')) || 0) + (parseInt($this.css('paddingRight')) || 0);
var paneWidth = $this.innerWidth();
var paneHeight = $this.innerHeight();
var trackHeight = paneHeight;
$this.wrap(
jQuery('<div></div>').attr(
{'className':'BugScrollDivContainer'}
).css(
{
'height':paneHeight+'px', 
'width':paneWidth+'px'
}
)
);
jQuery(document).bind(
'emchange', 
function(e, cur, prev)
{
$this.BugScrollDiv(settings);
}
);
}
var p = this.originalSidePaddingTotal;

var cssToApply = {
'height':'auto',
'width':paneWidth - settings.scrollbarWidth - settings.scrollbarMargin - p + 'px'
}

if(settings.scrollbarOnLeft) {
cssToApply.paddingLeft = settings.scrollbarMargin + settings.scrollbarWidth + 'px';
} else {
cssToApply.paddingRight = settings.scrollbarMargin + 'px';
}

$this.css(cssToApply);

var contentHeight = $this.outerHeight();
var percentInView = paneHeight / contentHeight;

if (percentInView < .99) {
var $container = $this.parent();
$container.append(
jQuery('<div></div>').attr({'className':'BugScrollDivTrack'}).css({'width':settings.scrollbarWidth+'px'}).append(
jQuery('<div></div>').attr({'className':'BugScrollDivDrag'}).css({'width':settings.scrollbarWidth+'px'}).append(
jQuery('<div></div>').attr({'className':'BugScrollDivDragTop'}).css({'width':settings.scrollbarWidth+'px'}),
jQuery('<div></div>').attr({'className':'BugScrollDivDragBottom'}).css({'width':settings.scrollbarWidth+'px'})
)
)
);

var $track = jQuery('>.BugScrollDivTrack', $container);
var $drag = jQuery('>.BugScrollDivTrack .BugScrollDivDrag', $container);

if (settings.showArrows) {

var currentArrowButton;
var currentArrowDirection;
var currentArrowInterval;
var currentArrowInc;
var whileArrowButtonDown = function()
{
if (currentArrowInc > 4 || currentArrowInc%4==0) {
positionDrag(dragPosition + currentArrowDirection * mouseWheelMultiplier);
}
currentArrowInc ++;
};
var onArrowMouseUp = function(event)
{
jQuery('html').unbind('mouseup', onArrowMouseUp);
currentArrowButton.removeClass('jScrollActiveArrowButton');
clearInterval(currentArrowInterval);
//console.log($(event.target));
//currentArrowButton.parent().removeClass('jScrollArrowUpClicked jScrollArrowDownClicked');
};
var onArrowMouseDown = function() {
//console.log(direction);
//currentArrowButton = $(this);
jQuery('html').bind('mouseup', onArrowMouseUp);
currentArrowButton.addClass('jScrollActiveArrowButton');
currentArrowInc = 0;
whileArrowButtonDown();
currentArrowInterval = setInterval(whileArrowButtonDown, 100);
};
$container
.append(
jQuery('<a></a>')
.attr({'href':'javascript:;', 'className':'jScrollArrowUp'})
.css({'width':settings.scrollbarWidth+'px'})
.html('Scroll up')
.bind('mousedown', function()
{
currentArrowButton = jQuery(this);
currentArrowDirection = -1;
onArrowMouseDown();
this.blur();
return false;
}),
jQuery('<a></a>')
.attr({'href':'javascript:;', 'className':'jScrollArrowDown'})
.css({'width':settings.scrollbarWidth+'px'})
.html('Scroll down')
.bind('mousedown', function()
{
currentArrowButton = jQuery(this);
currentArrowDirection = 1;
onArrowMouseDown();
this.blur();
return false;
})
);
var $upArrow = jQuery('>.jScrollArrowUp', $container);
var $downArrow = jQuery('>.jScrollArrowDown', $container);
if (settings.arrowSize) {
trackHeight = paneHeight - settings.arrowSize - settings.arrowSize;
$track
.css({'height': trackHeight+'px', top:settings.arrowSize+'px'})
} else {
var topArrowHeight = $upArrow.height();
settings.arrowSize = topArrowHeight;
trackHeight = paneHeight - topArrowHeight - $downArrow.height();
$track
.css({'height': trackHeight+'px', top:topArrowHeight+'px'})
}
}

var $pane = jQuery(this).css({'position':'absolute', 'overflow':'visible'});

var currentOffset;
var maxY;
var mouseWheelMultiplier;
// store this in a seperate variable so we can keep track more accurately than just updating the css property..
var dragPosition = 0;
var dragMiddle = percentInView*paneHeight/2;

// pos function borrowed from tooltip plugin and adapted...
var getPos = function (event, c) {
var p = c == 'X' ? 'Left' : 'Top';
return event['page' + c] || (event['client' + c] + (document.documentElement['scroll' + p] || document.body['scroll' + p])) || 0;
};

var ignoreNativeDrag = function() {	return false; };

var initDrag = function()
{
ceaseAnimation();
currentOffset = $drag.offset(false);
currentOffset.top -= dragPosition;
maxY = trackHeight - $drag[0].offsetHeight;
mouseWheelMultiplier = 2 * settings.wheelSpeed * maxY / contentHeight;
};

var onStartDrag = function(event)
{
initDrag();
dragMiddle = getPos(event, 'Y') - dragPosition - currentOffset.top;
jQuery('html').bind('mouseup', onStopDrag).bind('mousemove', updateScroll);
if (jQuery.browser.msie) {
jQuery('html').bind('dragstart', ignoreNativeDrag).bind('selectstart', ignoreNativeDrag);
}
return false;
};
var onStopDrag = function()
{
jQuery('html').unbind('mouseup', onStopDrag).unbind('mousemove', updateScroll);
dragMiddle = percentInView*paneHeight/2;
if (jQuery.browser.msie) {
jQuery('html').unbind('dragstart', ignoreNativeDrag).unbind('selectstart', ignoreNativeDrag);
}
};
var positionDrag = function(destY)
{
destY = destY < 0 ? 0 : (destY > maxY ? maxY : destY);
dragPosition = destY;
$drag.css({'top':destY+'px'});
var p = destY / maxY;
$pane.css({'top':((paneHeight-contentHeight)*p) + 'px'});
$this.trigger('scroll');
if (settings.showArrows) {
$upArrow[destY == 0 ? 'addClass' : 'removeClass']('disabled');
$downArrow[destY == maxY ? 'addClass' : 'removeClass']('disabled');
}
};
var updateScroll = function(e)
{
positionDrag(getPos(e, 'Y') - currentOffset.top - dragMiddle);
};

var dragH = Math.max(Math.min(percentInView*(paneHeight-settings.arrowSize*2), settings.dragMaxHeight), settings.dragMinHeight);

$drag.css(
{'height':dragH+'px'}
).bind('mousedown', onStartDrag);

var trackScrollInterval;
var trackScrollInc;
var trackScrollMousePos;
var doTrackScroll = function()
{
if (trackScrollInc > 8 || trackScrollInc%4==0) {
positionDrag((dragPosition - ((dragPosition - trackScrollMousePos) / 2)));
}
trackScrollInc ++;
};
var onStopTrackClick = function()
{
clearInterval(trackScrollInterval);
jQuery('html').unbind('mouseup', onStopTrackClick).unbind('mousemove', onTrackMouseMove);
};
var onTrackMouseMove = function(event)
{
trackScrollMousePos = getPos(event, 'Y') - currentOffset.top - dragMiddle;
};
var onTrackClick = function(event)
{
initDrag();
onTrackMouseMove(event);
trackScrollInc = 0;
jQuery('html').bind('mouseup', onStopTrackClick).bind('mousemove', onTrackMouseMove);
trackScrollInterval = setInterval(doTrackScroll, 100);
doTrackScroll();
};

$track.bind('mousedown', onTrackClick);

// if the mousewheel plugin has been included then also react to the mousewheel
if ($container.mousewheel) {
$container.mousewheel(
function (event, delta) {
initDrag();
ceaseAnimation();
var d = dragPosition;
positionDrag(dragPosition - delta * mouseWheelMultiplier);
var dragOccured = d != dragPosition;
return !dragOccured;
},
false
);					
}
var _animateToPosition;
var _animateToInterval;
function animateToPosition()
{
var diff = (_animateToPosition - dragPosition) / settings.animateStep;
if (diff > 1 || diff < -1) {
positionDrag(dragPosition + diff);
} else {
positionDrag(_animateToPosition);
ceaseAnimation();
}
}
var ceaseAnimation = function()
{
if (_animateToInterval) {
clearInterval(_animateToInterval);
delete _animateToPosition;
}
};
var scrollTo = function(pos, preventAni)
{
if (typeof pos == "string") {
$e = jQuery(pos, this);
if (!$e.length) return;
pos = $e.offset().top - $this.offset().top;
}
ceaseAnimation();
var destDragPosition = -pos/(paneHeight-contentHeight) * maxY;
if (preventAni || !settings.animateTo) {
positionDrag(destDragPosition);
} else {
_animateToPosition = destDragPosition;
_animateToInterval = setInterval(animateToPosition, settings.animateInterval);
}
};
$this[0].scrollTo = scrollTo;

$this[0].scrollBy = function(delta)
{
var currentPos = -parseInt($pane.css('top')) || 0;
scrollTo(currentPos + delta);
};

initDrag();

scrollTo(-currentScrollPosition, true);

jQuery.BugScrollDiv.active.push($this[0]);

} else {
$this.css(
{
'height':paneHeight+'px',
'width':paneWidth-this.originalSidePaddingTotal+'px',
'padding':this.originalPadding
}
);
// remove from active list?
}

}
)
};

// clean up the scrollTo expandos
jQuery(window)
.bind('unload', function() {
var els = jQuery.BugScrollDiv.active; 
for (var i=0; i<els.length; i++) {
els[i].scrollTo = els[i].scrollBy = null;
}
}
);




(function($) {

$.fn.Modal = function(settings) {

settings = jQuery.extend({

overlayBgColor: 		'#000',		
overlayOpacity:			0,		
fixedNavigation:		false,		
imageLoading:			'http://christmas.cre8tivebug.com/images/modal-ico-loading.gif',		
imageBtnPrev:			'http://christmas.cre8tivebug.com/images/modal-btn-prev.png',			
imageBtnNext:			'http://christmas.cre8tivebug.com/images/modal-btn-next.png',			
imageBtnClose:			'http://christmas.cre8tivebug.com/images/modal_close.png',		
imageBlank:				'http://christmas.cre8tivebug.com/images/modal-blank.gif',			
containerBorderSize:	10,			
containerResizeSpeed:	400,		
txtImage:				'Image',	
txtOf:					'of',		

keyToClose:				'c',		
keyToPrev:				'p',		
keyToNext:				'n',		

imageArray:				[],
activeImage:			0
},settings);

var jQueryMatchedObj = this; 

function _initialize() {
_start(this,jQueryMatchedObj); 
return false; 
}

function _start(objClicked,jQueryMatchedObj) {

$('embed, object, select').css({ 'visibility' : 'hidden' });

_set_interface();

settings.imageArray.length = 0;

settings.activeImage = 0;

if ( jQueryMatchedObj.length == 1 ) {
settings.imageArray.push(new Array(objClicked.getAttribute('href'),objClicked.getAttribute('rev')));
} else {

for ( var i = 0; i < jQueryMatchedObj.length; i++ ) {
settings.imageArray.push(new Array(jQueryMatchedObj[i].getAttribute('href'),jQueryMatchedObj[i].getAttribute('rev')));
}
}
while ( settings.imageArray[settings.activeImage][0] != objClicked.getAttribute('href') ) {
settings.activeImage++;
}

_set_image_to_view();
}

function _set_interface() {

$('body').append('<div id="jquery-overlay"></div><div id="jquery-Modal"><div id="Modal-container-image-box"><div id="Modal-secNav-btnClose"><img src="' + settings.imageBtnClose + '"></div><div id="Modal-container-image"><img id="Modal-image"><div style="" id="Modal-nav"><a href="#" id="Modal-nav-btnPrev"></a><a href="#" id="Modal-nav-btnNext"></a></div><div id="Modal-loading"><a href="#" id="Modal-loading-link"><img src="' + settings.imageLoading + '"></a></div></div></div><div id="Modal-container-image-data-box"><div id="Modal-container-image-data"><div id="Modal-image-details"><span id="Modal-image-details-caption"></span></div><div id="Modal-secNav"></div></div></div></div>');	

var arrPageSizes = ___getPageSize();

$('#jquery-overlay').css({
backgroundColor:	settings.overlayBgColor,
opacity:			settings.overlayOpacity
/*width:				arrPageSizes[0],
height:				arrPageSizes[1]*/
}).fadeIn();

$('.overlay').show();	

var arrPageScroll = ___getPageScroll();

$('#jquery-Modal').css({
top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
left:	arrPageScroll[0]
}).show();

/*$('#jquery-overlay,#jquery-Modal').click(function() {
_finish();									
});*/

$('#Modal-secNav-btnClose').click(function() {
_finish();
return false;
});

$(window).resize(function() {

var arrPageSizes = ___getPageSize();

/*$('#jquery-overlay').css({
width:		arrPageSizes[0],
height:		arrPageSizes[1]
});*/

var arrPageScroll = ___getPageScroll();

$('#jquery-Modal').css({
top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
left:	arrPageScroll[0]
});
});
}

function _set_image_to_view() { 

$('#Modal-loading').show();
if ( settings.fixedNavigation ) {
$('#Modal-image,#Modal-container-image-data-box,#Modal-image-details-currentNumber').hide();
} else {

$('#Modal-image,#Modal-nav,#Modal-nav-btnPrev,#Modal-nav-btnNext,#Modal-container-image-data-box,#Modal-image-details-currentNumber').hide();
}

var objImagePreloader = new Image();
objImagePreloader.onload = function() {
$('#Modal-image').attr('src',settings.imageArray[settings.activeImage][0]);

_resize_container_image_box(objImagePreloader.width,objImagePreloader.height);

objImagePreloader.onload=function(){};
};
objImagePreloader.src = settings.imageArray[settings.activeImage][0];
};

function _resize_container_image_box(intImageWidth,intImageHeight) {

var intCurrentWidth = $('#Modal-container-image-box').width();
var intCurrentHeight = $('#Modal-container-image-box').height();

var intWidth = (intImageWidth + (settings.containerBorderSize * 2)); 
var intHeight = (intImageHeight + (settings.containerBorderSize * 2)); 

var intDiffW = intCurrentWidth - intWidth;
var intDiffH = intCurrentHeight - intHeight;

$('#Modal-container-image-box').animate({ width: intWidth, height: intHeight },settings.containerResizeSpeed,function() { _show_image(); });
if ( ( intDiffW == 0 ) && ( intDiffH == 0 ) ) {
if ( $.browser.msie ) {
___pause(250);
} else {
___pause(100);	
}
} 
$('#Modal-container-image-data-box').css({ width: intImageWidth });
$('#Modal-nav-btnPrev,#Modal-nav-btnNext').css({ height: intImageHeight + (settings.containerBorderSize * 2) });
};

function _show_image() {
$('#Modal-loading').hide();
$('#Modal-image').fadeIn(function() {
_show_image_data();
_set_navigation();
});
_preload_neighbor_images();
};

function _show_image_data() {
$('#Modal-container-image-data-box').slideDown('fast');
$('#Modal-image-details-caption').hide();
if ( settings.imageArray[settings.activeImage][1] ) {
$('#Modal-image-details-caption').html(settings.imageArray[settings.activeImage][1]).show();
}

if ( settings.imageArray.length > 1 ) {
$('#Modal-image-details-currentNumber').html(settings.txtImage + ' ' + ( settings.activeImage + 1 ) + ' ' + settings.txtOf + ' ' + settings.imageArray.length).show();
}		
}

function _set_navigation() {
$('#Modal-nav').show();


$('#Modal-nav-btnPrev,#Modal-nav-btnNext').css({ 'background' : 'transparent url(' + settings.imageBlank + ') no-repeat' });


if ( settings.activeImage != 0 ) {
if ( settings.fixedNavigation ) {
$('#Modal-nav-btnPrev').css({ 'background' : 'url(' + settings.imageBtnPrev + ') left 50% no-repeat' })
.unbind()
.bind('click',function() {
settings.activeImage = settings.activeImage - 1;
_set_image_to_view();
return false;
});
} else {

$('#Modal-nav-btnPrev').unbind().hover(function() {
$(this).css({ 'background' : 'url(' + settings.imageBtnPrev + ') left 50% no-repeat' });
},function() {
$(this).css({ 'background' : 'transparent url(' + settings.imageBlank + ') no-repeat' });
}).show().bind('click',function() {
settings.activeImage = settings.activeImage - 1;
_set_image_to_view();
return false;
});
}
}


if ( settings.activeImage != ( settings.imageArray.length -1 ) ) {
if ( settings.fixedNavigation ) {
$('#Modal-nav-btnNext').css({ 'background' : 'url(' + settings.imageBtnNext + ') right 50% no-repeat' })
.unbind()
.bind('click',function() {
settings.activeImage = settings.activeImage + 1;
_set_image_to_view();
return false;
});
} else {

$('#Modal-nav-btnNext').unbind().hover(function() {
$(this).css({ 'background' : 'url(' + settings.imageBtnNext + ') right 50% no-repeat' });
},function() {
$(this).css({ 'background' : 'transparent url(' + settings.imageBlank + ') no-repeat' });
}).show().bind('click',function() {
settings.activeImage = settings.activeImage + 1;
_set_image_to_view();
return false;
});
}
}

_enable_keyboard_navigation();
}

function _enable_keyboard_navigation() {
$(document).keydown(function(objEvent) {
_keyboard_action(objEvent);
});
}


function _disable_keyboard_navigation() {
$(document).unbind();
}


function _keyboard_action(objEvent) {

if ( objEvent == null ) {
keycode = event.keyCode;
escapeKey = 27;

} else {
keycode = objEvent.keyCode;
escapeKey = objEvent.DOM_VK_ESCAPE;
}

key = String.fromCharCode(keycode).toLowerCase();

if ( ( key == settings.keyToClose ) || ( key == 'x' ) || ( keycode == escapeKey ) ) {
_finish();
}

if ( ( key == settings.keyToPrev ) || ( keycode == 37 ) ) {

if ( settings.activeImage != 0 ) {
settings.activeImage = settings.activeImage - 1;
_set_image_to_view();
_disable_keyboard_navigation();
}
}

if ( ( key == settings.keyToNext ) || ( keycode == 39 ) ) {

if ( settings.activeImage != ( settings.imageArray.length - 1 ) ) {
settings.activeImage = settings.activeImage + 1;
_set_image_to_view();
_disable_keyboard_navigation();
}
}
}


function _preload_neighbor_images() {
if ( (settings.imageArray.length -1) > settings.activeImage ) {
objNext = new Image();
objNext.src = settings.imageArray[settings.activeImage + 1][0];
}
if ( settings.activeImage > 0 ) {
objPrev = new Image();
objPrev.src = settings.imageArray[settings.activeImage -1][0];
}
}


function _finish() {
$('#jquery-Modal').remove();
$('#jquery-overlay').fadeOut(function() { $('#jquery-overlay').remove(); });
$('.overlay').fadeOut('slow');
$('embed, object, select').css({ 'visibility' : 'visible' });
}


function ___getPageSize() {
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){ 
xScroll = document.body.scrollWidth;
yScroll = document.body.scrollHeight;
} else { 
xScroll = document.body.offsetWidth;
yScroll = document.body.offsetHeight;
}
var windowWidth, windowHeight;
if (self.innerHeight) {	
if(document.documentElement.clientWidth){
windowWidth = document.documentElement.clientWidth; 
} else {
windowWidth = self.innerWidth;
}
windowHeight = self.innerHeight;
} else if (document.documentElement && document.documentElement.clientHeight) { 
windowWidth = document.documentElement.clientWidth;
windowHeight = document.documentElement.clientHeight;
} else if (document.body) { 
windowWidth = document.body.clientWidth;
windowHeight = document.body.clientHeight;
}	

if(yScroll < windowHeight){
pageHeight = windowHeight;
} else { 
pageHeight = yScroll;
}

if(xScroll < windowWidth){	
pageWidth = xScroll;		
} else {
pageWidth = windowWidth;
}
arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
return arrayPageSize;
};


function ___getPageScroll() {
var xScroll, yScroll;
if (self.pageYOffset) {
yScroll = self.pageYOffset;
xScroll = self.pageXOffset;
} else if (document.documentElement && document.documentElement.scrollTop) {	 
yScroll = document.documentElement.scrollTop;
xScroll = document.documentElement.scrollLeft;
} else if (document.body) {
yScroll = document.body.scrollTop;
xScroll = document.body.scrollLeft;	
}
arrayPageScroll = new Array(xScroll,yScroll);
return arrayPageScroll;
};




function ___pause(ms) {
var date = new Date(); 
curDate = null;
do { var curDate = new Date(); }
while ( curDate - date < ms);
};

return this.unbind('click').click(_initialize);
};
})(jQuery); 
