module("Fotobox.UI.Component", (function($) {
function Component() {
}
var constructor = Component;
var methods = {
init : function(selector) {
var that = this;
this.selector = selector;
this.postAnimateShowFuns = [];
Fotobox.UI.Common.registerObject(selector, this);
$(function() {
that.componentReady();
});
Fotobox.UI.Common.registerCloseFlyoutIfOpen();
},
componentReady : function() {
this.moveObjectToElement();
this.ready();
this.autoAnimateShow();
},
ready : function() {
},
moveObjectToElement : function() {
this.$this = $(this.selector);
Fotobox.UI.Common.moveObjectToElement(this.selector, this.$this, this);
},
addPostAnimateShowFun : function(fun) {
this.postAnimateShowFuns.push(fun);
},
setAnimateShowQueueSelector : function(animateShowQueueSelector) {
this.animateShowQueueSelector = animateShowQueueSelector;
},
getAnimateShowQueueSelector : function() {
return this.animateShowQueueSelector || this.selector;
},
animateShowSelf : function($queue) {
$queue.dequeue();
},
autoAnimateShow : function() {
var that = this;
if (this.getAnimateShowQueueSelector() === this.selector) {
this.$this.queue(function() {
that.animateShow();
});
}
},
animateShow : function() {
var that = this;
var $queue = $(this.getAnimateShowQueueSelector());
 var queueArr = $queue.queue();
var idx = queueArr.length > 0 && queueArr[0] === "inprogress" && 1 || 0;
queueArr.splice(idx++, 0, function() {
that.animateShowSelf($queue);
});
$.each(this.postAnimateShowFuns, function(fidx, fun) {
queueArr.splice(idx++, 0, function() {
fun($queue);
});
});
$queue.queue(queueArr);
$queue.dequeue();
}
};
$.extend(constructor.prototype, methods);
return constructor;
})(jQuery));
module("Fotobox.UI.Common", function() {
 function registerObject(selector, obj) {
$(document).data("Fotobox.Obj_" + selector, obj);
}
function moveObjectToElement(selector, $this, obj) {
$(document).removeData("Fotobox.Obj_" + selector);
$this.data("Fotobox.Obj", obj);
}
function getObject(selector) {
return $(selector).data("Fotobox.Obj") || $(document).data("Fotobox.Obj_" + selector);
}
	function setContentPaddingTop() {
$('#fbox_elements').css('padding-top', ($('#fbox_heading').height()+1) + 'px');
}
function centerVertically($element) {
var top = $(window).height() / 2 + $(window).scrollTop();
$element.css('top', "" + top + "px" );
}
function showLoader(loaderUrl) {
var loader = '<div id="loader"><img id="img_loader" src="' + loaderUrl + '" /></div>';
var $loader = $(loader);
centerVertically($loader.eq(0));
$('body').append($loader);
$loader.queue(function() {
lockBackground();
$(this).dequeue();
});
$loader.topZIndex();
$loader.animate({'opacity': 1}, "fast");
}
function isLoading() {
return $('#loader').length > 0 ? true : false;
}
function lockBackground() {
if (isBackgroundLocked()) {
$('#bodyBackground').topZIndex();
return;
}
var $background = $('<div id="bodyBackground" class="pf t0 l0 fw fh opa0"></div>');
 $background.css('height', $(document).height());
$('body').append($background);
$background.topZIndex();
$background.animate({'opacity': 0.5}, "fast");
}
function unlockBackground() {
if (!isBackgroundLocked()) {
return;
}
var $background = $('#bodyBackground');
$background.animate({'opacity': 0}, "fast", function(){
$background.remove();
});
}
function isBackgroundLocked() {
return $('#bodyBackground').size() > 0;
}
function removeLoader(mustUnlockBackground) {
var $loader = $('#loader');
$loader.animate({'opacity': 0}, "fast", function(){
$loader.remove();
if (mustUnlockBackground && isBackgroundLocked()) {
unlockBackground();
}
});
}
function showError($element, errMsg) {
var $error = $element.next('.error_validation');
if ($error.length !== 0) {
return;
}
$error = $('<div class="error_validation"></div>');
$error.html(errMsg);
$error.insertAfter($element);
$error.css({'opacity' : 1, 'display': 'inline-block'});
$element.trigger('errorAdded');
}
function removeError($element) {
var $error = $element.next('.error_validation');
if ($error.length === 0) {
return;
}
if (Fotobox.UI.Browser.ie7 || Fotobox.UI.Browser.ie8){
 } else{
$error.css({'opacity' : 0});
}
$error.remove();
$element.trigger('errorRemoved');
}
function WindowSize($window) {
var width = $window.width(), height = $window.height();
this.hasChanged = function($window) {
return $window.height() !== height || $window.width() !== width;
}
}

function onWindowResize(winResizeHandler) {
var $window = $(window), windowSize = new WindowSize($window);
var resizeTimeoutId;
$(window).bind('resize', function() {
if (!Fotobox.UI.Browser.ie7 && !Fotobox.UI.Browser.ie8 && !Fotobox.UI.Browser.ie9) {
winResizeHandler.apply(this);
} else {
if (windowSize.hasChanged($window)) {
windowSize = new WindowSize($window);
clearTimeout(resizeTimeoutId);  resizeTimeoutId = window.setTimeout(winResizeHandler, 100);
}
}
});
}
  function isMobileView(){
if($('#fbox_body.externalOverview').length > 0) return true; else return false;
}
return {
init : function (){
setContentPaddingTop();
$('#fbox_heading, #fbox_elements').addClass('no-select');
},
onWindowResize: onWindowResize,
resizeBoxThumbs : function($img, w, h){
if(!$img.hasClass('box_img')) {
Fotobox.UI.Common.setBoxImgClipping($img.parent(), w, h);
}else{
Fotobox.UI.Common.setBoxImgClipping($img, w, h);
}
Fotobox.UI.Common.maxImageSize($img, w, h);
},
maxImageSize : function($obj, w, h){
if($obj.height() < h){
$obj.css('height', h + 'px');
}
if($obj.width() < w){
$obj.css('width', w + 'px');
}
$obj.css({
'display': 'inline-block'
});
},
setBoxImgClipping : function($obj, w, h){
 var borderW = 3;
 var clipHeightOffset = parseInt(($obj.height() - h) * 0.5);
if($obj.height() < h) $obj.css({'clip': 'rect(0px, ' + w + 'px, ' + h + 'px, 0px)', 'margin-top': borderW + 'px', 'margin-left' : borderW + 'px'});
else $obj.css({'clip': 'rect(' + clipHeightOffset + 'px, ' + w + 'px, ' + (h + clipHeightOffset) + 'px, 0px)', 'margin-top': ((clipHeightOffset * -1) + borderW) + 'px', 'margin-left' : borderW + 'px' });
},
registerCloseFlyoutIfOpen : function (){
},
registerObject : registerObject,
moveObjectToElement : moveObjectToElement,
getObject : getObject,
setContentPaddingTop : setContentPaddingTop,
centerVertically : centerVertically,
showLoader: showLoader,
removeLoader: removeLoader,
isLoading: isLoading,
showError: showError,
removeError: removeError,
lockBackground: lockBackground,
unlockBackground: unlockBackground,
isMobileView : isMobileView,
isBackgroundLocked : isBackgroundLocked,
pixelIndependentSize : function ($refImg, origImgWidth, piPixelRatio){
 if(piPixelRatio > 1) {
 var percentVal = Math.round((1 / piPixelRatio)*100)/100;
 var pixelVal = Math.round(origImgWidth * percentVal)+1;
 $refImg.width(pixelVal);
}
}
};
}());
(function($) {
$(function() {
Fotobox.UI.Common.init();
});
})(jQuery);

