
var $iBoxTotalOpen = 0;
var $iCurrentZIndex = 1;
var $aBoxHistory = {};
var iImageIterationCount = 0;
var iCurrentImageIterationCount = 0;
var $sCurrentId = null;
var sLastOpenUrl = null;
var bIsPhotoImage = false;

$Behavior.addDraggableToBoxes = function()
{
    $('.js_box').each(function()
    {
		if ($(this).hasClass('ui-draggable'))
    		$(this).draggable('destroy');
    	tb_draggable(this);
    });

    $('a.thickbox').click(function()
    {
		var aExtra = $(this).html().match(/userid="([0-9a-z]?)"/i);
		var sHref = this.href;
		if (aExtra != null && isset(aExtra[1]))
		{
			sHref += 'userid_' + aExtra[1] + '/';
		}

    	var bReturn = tb_show('', sHref, this);

		if (bReturn === true){
			return true;
		}

    	return false;
    });

	$('.popup').click(function() {
		var caption = ($(this).data('caption')) ? $(this).data('caption') : '';
		tb_show(caption, $(this).attr('href'), $(this));

		return false;
	});
};

function js_box_remove($oObj)
{
	$('#main_core_body_holder').show();

	$('.imgareaselect-outer').remove();
	$('#noteform').remove();
	$('.imgareaselect-border1').remove();
	$('.imgareaselect-border2').remove();
	$('.imgareaselect-border3').remove();
	$('.imgareaselect-border4').remove();
	$('.feed_share_on_item a').removeClass('active');

	var $oParent = $($oObj).parents('.js_box:first');
	var $oBoxParent = $($oObj).parents('.js_box_image_holder_full:first');
	var $sLink = $oParent.find('.js_box_history:first').html();

	if (isset($aBoxHistory[$sLink]))
	{
		delete $aBoxHistory[$sLink];
	}

	$oBoxParent.fadeOut(100, function()
	{
		$(this).remove();
	});
	$oParent.fadeOut(100, function()
	{
		$(this).remove();
	});

	$('#global_attachment_list_inline').hide();

	if (bIsPhotoImage){
		bIsPhotoImage = false;
	//	$Core.addUrlPager({'href': sLastOpenUrl.replace('/#!', '')}, true);
	}

	return false;
}

function tb_show_new_image(thisObject, sSrc, imageWidth, imageHeight, iCurrentIte)
{
	$('.js_box_image_gallery img').removeClass('is_active');
	$(thisObject).find('img').addClass('is_active');

	iCurrentImageIterationCount = iCurrentIte;

	$('#js_thickbox_core_image').attr({
		'src': sSrc,
		'width': imageWidth,
		'height': imageHeight
	});
}

function js_box_next_image()
{
	iCurrentImageIterationCount++;

	var thisObject = $('#js_next_image_thumb_' + iCurrentImageIterationCount);

	if (thisObject.length <= 0)
	{
		iCurrentImageIterationCount = 0;

		return js_box_next_image();
	}

	var aParts = explode('|', thisObject.attr('rel'));

	tb_show_new_image('#js_next_image_thumb_' + iCurrentImageIterationCount, '' + thisObject.attr('href') + '', aParts[0], aParts[1], iCurrentImageIterationCount);

	return false;
}

function tb_show(caption, url, thisObject, sForceMessage, bForceNoCilck, sType)
{
	$('.js_box').unbind('click');
	var baseURL;
	if (url.indexOf("?")!==-1)
	{
		baseURL = url.substr(0, url.indexOf("?"));
	}
	else
	{
		baseURL = url;
	}

	var urlString = /\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$/;
	var urlType = baseURL.toLowerCase().match(urlString);

	if (urlType == '.jpg' || urlType == '.jpeg' || urlType == '.png' || urlType == '.gif' || urlType == '.bmp')
	{
		imgPreloader = new Image();
		imgPreloader.onload = function()
		{
			imgPreloader.onload = null;

			var pagesize = tb_getPageSize();
			var x = pagesize[0] - 150;
			var y = pagesize[1] - 150;
			var imageWidth = imgPreloader.width;
			var imageHeight = imgPreloader.height;

			if (imageWidth > x)
			{
				imageHeight = imageHeight * (x / imageWidth);
				imageWidth = x;
				if (imageHeight > y)
				{
					imageWidth = imageWidth * (y / imageHeight);
					imageHeight = y;
				}
			}
			else if (imageHeight > y)
			{
				imageWidth = imageWidth * (y / imageHeight);
				imageHeight = y;
				if (imageWidth > x)
				{
					imageHeight = imageHeight * (x / imageWidth);
					imageWidth = x;
				}
			}

			TB_WIDTH = imageWidth + 30;
			TB_HEIGHT = imageHeight + 60;

			$('.js_box_image_holder').remove();
			$('.js_box_image_holder').unbind('click');

			var sHtml = '';
			sHtml += '<div class="js_box_image_holder"><div class="js_box ' + (oParams['bJsIsMobile'] ? 'mobile_js_box' : '') + ' js_box_image" style="display:block;"><div class="js_box_content">';

			if ($(thisObject).parents('.js_box_thumbs_holder').length > 0)
			{
				var sCurrentSource = $(thisObject).find('img:first').attr('src');
				var sNewSource = '';
				var SubimgPreloaderImageWidth = '';
				var SubimgPreloaderImageHeight = '';
				var SubArrayWidth = new Array();
				var SubArrayHeight = new Array();
				var sSubHtml = '';
				var aUrlParts = new Array();
				var aUrlPartsNew = new Array();
				// p('Current: ' + sCurrentSource);

				iImageIterationCount = 0;
				iCurrentImageIterationCount = 0;

				sHtml += '<div>';
				$(thisObject).parents('.js_box_thumbs_holder').find('.js_box_image_holder_thumbs').find('.thickbox').each(function()
				{
					iImageIterationCount++;
					sNewSource = $(this).find('img').attr('src');

					aUrlParts = explode('_', sCurrentSource);
					aUrlPartsNew = explode('_', sNewSource);

					if (aUrlParts[0] == aUrlPartsNew[0])
					{
						iCurrentImageIterationCount = iImageIterationCount;
					}

				//	if (sCurrentSource != sNewSource)
					{
						SubimgPreloader = new Image();

						SubimgPreloader.src = $(this).attr('href');

						SubimgPreloaderImageWidth = SubimgPreloader.width;
						SubimgPreloaderImageHeight = SubimgPreloader.height;

						if (SubimgPreloaderImageWidth > x)
						{
							SubimgPreloaderImageHeight = SubimgPreloaderImageHeight * (x / SubimgPreloaderImageWidth);
							SubimgPreloaderImageWidth = x;
							if (imageHeight > y)
							{
								SubimgPreloaderImageWidth = SubimgPreloaderImageWidth * (y / SubimgPreloaderImageHeight);
								imageHeight = y;
							}
						}
						else if (imageHeight > y)
						{
							SubimgPreloaderImageWidth = SubimgPreloaderImageWidth * (y / SubimgPreloaderImageHeight);
							SubimgPreloaderImageHeight = y;
							if (SubimgPreloaderImageWidth > x)
							{
								SubimgPreloaderImageHeight = SubimgPreloaderImageHeight * (x / SubimgPreloaderImageWidth);
								SubimgPreloaderImageWidth = x;
							}
						}

						SubArrayWidth.push(SubimgPreloaderImageWidth);
						SubArrayHeight.push(SubimgPreloaderImageHeight);

						sSubHtml += '<a rel="' + SubimgPreloaderImageWidth + '|' + SubimgPreloaderImageHeight + '" id="js_next_image_thumb_' + iImageIterationCount + '" href="' + $(this).attr('href') + '" onclick="tb_show_new_image(this, \'' + $(this).attr('href') + '\', ' + SubimgPreloaderImageWidth + ', ' + SubimgPreloaderImageHeight + ', ' + iImageIterationCount + '); return false;"><img src="' + sNewSource + '" alt="" ' + (aUrlParts[0] == aUrlPartsNew[0] ? ' class="is_active" ' : '') + ' /></a>';
					}
				});

				TB_WIDTH = ((Math.max.apply(Math, SubArrayWidth)) + 30);
				TB_HEIGHT = ((Math.max.apply(Math, SubArrayHeight)));

				sHtml += '<div class="js_box_image_holder_browse">';
				sHtml += '<div class="js_box_image_gallery_display" style="height:' + TB_HEIGHT + 'px; line-height:' + TB_HEIGHT + 'px;">';
				sHtml += '<a href="#" onclick="return js_box_next_image();"><img src="' + url + '" width="' + imageWidth + '" height="' + imageHeight + '" alt="" id="js_thickbox_core_image" /></a>';
				sHtml += '</div>';
				sHtml += '<div class="js_box_image_gallery">' + sSubHtml + '</div>';
				sHtml += '</div>';

				sHtml += '</div>';
			}
			else
			{
				sHtml += '<a href="#" onclick="$(\'.js_box_image_holder\').remove(); return false;"><img src="' + url + '" width="' + imageWidth + '" height="' + imageHeight + '" alt="" id="js_thickbox_core_image" style="min-width: '+imageWidth+'px;min-height:'+imageHeight+'px;"/></a>';
			}

			sHtml += '</div><div class="js_box_close"><a href="#" onclick="return js_box_remove(this);">' + oTranslations['close'] + '</a></div></div></div>';

			$('body').prepend(sHtml);

			$('.js_box_image').css(
			{
				top: (($(window).height() - $('.js_box_image').outerHeight()) / 2) + "px",
        		left: (($(window).width() - $('.js_box_image').outerWidth()) / 2) + "px",
        		display: 'block'
			});

			var bCanCloseImageBox = true;

			$('.js_box').click(function()
			{
				bCanCloseImageBox = false;
			});

			$('.js_box_image_holder').click(function()
			{
				if (!bCanCloseImageBox)
				{
					bCanCloseImageBox = true;
				}
				else
				{
					$(this).remove();
				}
			});

		    if ($.browser.msie && $.browser.version == '7.0')
			{
		    	$Behavior.ie7FixZindex();
			}
		};

		imgPreloader.src = url;

		return false;
	}

	var bIsAlreadyOpen = false;
	if ($(thisObject).hasClass('photo_holder_image') && !empty($(thisObject).attr('rel')))
	{
		return;
	}
	else
	{
		if ($Core.exists('.js_box_image_holder_full')){
			js_box_remove($('.js_box_image_holder_full').find('.js_box_content:first'));
		}

		if (url.indexOf('#') != -1)
		{
			var aParams = url.split('#');
			url = '#' + aParams[1];
		}

		var queryString = url.replace(/^[^\?]+\??/,'');
		var params = tb_parseQuery(queryString);

	}
	if (!bIsPhotoImage && isset($aBoxHistory[params['' + getParam('sGlobalTokenName') + '[call]']]))
	{
		return false;
	}

	if (!bIsAlreadyOpen)
	{
		$iBoxTotalOpen++;
		$iCurrentZIndex++;

		$aBoxHistory[params[getParam('sGlobalTokenName') + '[call]']] = true;

		$sCurrentId = 'js_box_id_' + $iBoxTotalOpen;
	}

	if (caption === null)
	{
		caption = '';
	}

	var bIsFullMode = false,
		_w = $(window).width(),
		_h = $(window).height();


	if (params['width'] == 'full')
	{
		params['width'] = _w;
		params['height'] = _h;
		bIsFullMode = true;
	}
	else if (params['width'] == 'scan')
	{
		params['width'] = (_w - (oCore['core.is_admincp'] ? 100 : 150));
		params['height'] = (_h - (oCore['core.is_admincp'] ? 100 : 150));
	}

	if(_w < 600){
		params['width'] = _w-20;
	}

	TB_WIDTH = (!empty(params['width']) ? ((params['width']*1) + 30) : 630);
	TB_HEIGHT = (params['height']*1) + 40 || 440;

	var pagesize = tb_getPageSize();
	if (TB_HEIGHT > pagesize[1])
	{
		TB_HEIGHT = (pagesize[1] - 75);
	}

	if (TB_WIDTH > pagesize[0])
	{
		TB_WIDTH = (pagesize[0] - 75);
	}


	var ajaxContentW =TB_WIDTH - 30,
		ajaxContentH = TB_HEIGHT - 45;

	if(_w < 600){
		TB_WIDTH = _w-20;
		ajaxContentW =  TB_WIDTH-10;
	}

	console.log(TB_WIDTH, ajaxContentH);



	if (!bIsAlreadyOpen)
	{
		var sHtml = '';

		if (bIsPhotoImage){
			$('.js_box_image_holder').remove();
			$('.js_box_image_holder').unbind('click');
			sHtml += '<div class="js_box_image_holder_full">';
			sHtml += '<div class="js_box_image_holder_full_loader" style="position:absolute; top:50%; left:50%;"><img src="' + oJsImages['loading_animation'] + '" alt="" /></div>';
			sHtml += '<div style="display:none;" id="' + $sCurrentId + '" class="js_box' + (oParams['bJsIsMobile'] ? ' mobile_js_box' : ' ') + (isset(params['no_remove_box']) ? ' js_box_no_remove' : '') + '" style="width:' + ajaxContentW + 'px;">';
			sHtml += '<div class="js_box_content"></div>';
			sHtml += '<div class="js_box_close"><a href="#" onclick="return js_box_remove(this);">' + oTranslations['close'] + '</a><span class="js_box_history">' + params[getParam('sGlobalTokenName') + '[call]'] + '</span></div>';
			sHtml += '</div>';
			sHtml += '</div>';
		}
		else{
			if (bIsPhotoImage)
			{
				$('.js_box_image_holder').remove();
				$('.js_box_image_holder').unbind('click');
				sHtml += '<div class="js_box_image_holder_full">';
			}

			if (bForceNoCilck){
				sHtml += '<div class="js_box_image_holder">';
			}

      var extraClass = ($(thisObject).attr('rel')) ? ' ' + $(thisObject).attr('rel') : ' ';
			sHtml += '<div id="' + $sCurrentId + '" class="js_box' + extraClass + (oParams['bJsIsMobile'] ? ' mobile_js_box' : ' ') + (isset(params['no_remove_box']) ? ' js_box_no_remove' : '') + '" style="width:' + ajaxContentW + 'px;">';
			// sHtml += '<div class="js_box_cover"></div>';
			if (!bIsPhotoImage)
			{
				sHtml += '<div class="js_box_title">' + caption + '';
				sHtml += '</div>';
			}

			sHtml += '<div class="js_box_close"><a href="#" onclick="return js_box_remove(this);"><i class="fa fa-close"></i></a><span class="js_box_history">' + params[getParam('sGlobalTokenName') + '[call]'] + '</span></div>';
			sHtml += '<div class="js_box_content"><span class="js_box_loader"><i class="fa fa-spin fa-spinner"></i></span></div>';
			sHtml += '</div>';

			if (bIsPhotoImage)
			{
				sHtml += '</div>';
			}

			if (bForceNoCilck){
				sHtml += '</div>';
			}
		}

		$('body').prepend(sHtml);

		var $oNew = $('#' + $sCurrentId + '');

		tb_position($oNew, (bIsFullMode ? bIsFullMode : ''));

		if (!bIsPhotoImage){
			$oNew.show();
		} else {
			$('.js_box_image_holder_full_loader').css({
				'margin-left': '-' + ($('.js_box_image_holder_full_loader').find('img:first').width() / 2) + 'px',
				'margin-top': '-' + ($('.js_box_image_holder_full_loader').find('img:first').height() / 2) + 'px'
			});
		}
	}
	else
	{
		$oNew = $('.js_box_image_holder_full').find('.js_box:first');
	}

	if (getParam('bJsIsMobile')){
		queryString += '&js_mobile_version=true';
	}
	if(url.indexOf('TB_inline') != -1){

					if (params['type'])
					{
						switch(params['type'])
						{
							case 'delete':
								var sHtml = '';
                var sMainUrl = url + '&do_delete=true';
								sHtml += '<div id="js_inline_delete">';
								if (!params['call'])
								{
									sHtml += '<form method="post" action="' + sMainUrl + '">';
									sHtml += '<div><input type="hidden" name="' + getParam('sGlobalTokenName') + '[security_token]" value="' + getParam('sSecurityToken') + '" />';
								}
								sHtml += '<div>';
								sHtml += getPhrase('are_you_sure');
								sHtml += '<div class="t_center p_4">';
								sHtml += ' <input type="hidden" name="item_id" value="' + params['itemId'] + '" id="js_inline_delete_id" /> ';
								if (!params['call'])
								{
									sHtml += ' <input class="btn" type="submit" value="' + getPhrase('yes') + '" class="button" /> ';
								}
								else
								{
									sHtml += ' <input class="btn" type="button" value="' + getPhrase('yes') + '" class="button" onclick="$(\'#js_inline_delete_id\').ajaxCall(\'' + params['call'] + '\', \'' + queryString + '\'); tb_remove();" /> ';
								}
								sHtml += ' <input class="btn btn-success" type="button" value="' + getPhrase('no') + '" class="button" onclick="tb_remove();" /> ';
								sHtml += '</div>';
								sHtml += '</div>';
								if (!params['call'])
								{
									sHtml += '</form>';
								}
								sHtml += '</div>';

								break;
						}

						$oNew.find('.js_box_content').html('');
						$oNew.find('.js_box_content').html(sHtml);
					}
					else
					{
						var sHtml = $('#' + params['inlineId']).children();
					}

		$oNew.find('.js_box_content').html('');
		$oNew.find('.js_box_content').html(sHtml);

		return;
	}
	else if (isset(params['TB_iframe'])){
		var sIframe = '';
		$oNew.find('.js_box_content').html('');
		$oNew.find('.js_box_content').html(sIframe);
	}
	else{
		if (!empty(sForceMessage)){
			$oNew.find('.js_box_content').html('');
			$oNew.find('.js_box_content').html(sForceMessage);
			return;
		}

		var sAjaxType = 'GET';
		if ( (params['' + getParam('sGlobalTokenName') + '[call]'] == 'share.popup') || sType == 'POST'){
			sAjaxType = 'POST';
		}

		if (thisObject instanceof jQuery && thisObject.hasClass('popup')) {
			if (thisObject.data('custom-class')) {
				$oNew.addClass(thisObject.data('custom-class'));
				$oNew.attr('style', '');
			}

			$.ajax({
				url: thisObject.attr('href'),
				contentType: 'application/json',
				success: function(e) {
					if (typeof(e.goto) == 'string') {
						window.location.href = e.goto;
						return;
					}

					if (typeof(e.error) == 'string' && typeof(e.content) != 'string') {
						$oNew.find('.js_box_content').html('<div class="error_message">' + e.error + '</div>');
						return;
					}

					$oNew.find('.js_box_title:first').html(((e.h1_clean && e.h1_clean.length) ? e.h1_clean : e.actual_title));
					$oNew.find('.js_box_content').html('');
					$oNew.find('.js_box_content').html(e.content);
					$oNew.find('.js_box_title:first').show();
					$oNew.find('.js_box_close:first').show();
					$Core.loadInit();
				}
			});

			return;
		}

		$.ajax(
			{
				type: sAjaxType,
				dataType: 'html',
				url: getParam('sJsAjax'),
				data: queryString,
				success: function(sMsg)
				{
					$oNew.find('.js_box_content').html('');
					$oNew.find('.js_box_content').html(sMsg);
					if (!empty($oNew.find('.js_box_title_store:first').html()))
					{
						$oNew.find('.js_box_title:first').html($oNew.find('.js_box_title_store:first').html());
					}
					$oNew.find('.js_box_title:first').show();
					$oNew.find('.js_box_close:first').show();

					if (bIsFullMode){
						$oNew.find('.js_box_content').height(ajaxContentH);
					}
				}
			});
	}
}

$Behavior.thickboxBrowser = function(){

	if ($Core.exists('.js_box_image_holder_full')){
		$(document).keyup(function(e){
			if (e.keyCode == 27){
				js_box_remove($('.js_box_image_holder_full').find('.js_box_content'));
			}
		});
	}
};

function tb_get_active()
{
	var $aAllBoxIndex = new Array();
	var $aAllBoxIndexHolder = new Array();
	$('.js_box').each(function()
    {
    	$aAllBoxIndex[parseInt($(this).css('z-index'))] = $(this).attr('id');
    	$aAllBoxIndexHolder.push(parseInt($(this).css('z-index')));
    });

    var $iFocusBox = parseInt(Math.max.apply(Math, $aAllBoxIndexHolder));

    if (isset($aAllBoxIndex[$iFocusBox]) && $('#' + $aAllBoxIndex[$iFocusBox]).length > 0)
   	{
   		return $aAllBoxIndex[$iFocusBox];
   	}

   	return null;
}

function tb_remove()
{
	$('#main_core_body_holder').show();

	var $aAllBoxIndex = new Array();
	var $aAllBoxIndexHolder = new Array();
	$('.js_box').each(function()
    {
    	$aAllBoxIndex[parseInt($(this).css('z-index'))] = $(this).attr('id');
    	$aAllBoxIndexHolder.push(parseInt($(this).css('z-index')));
    });

   	var $iFocusBox = parseInt(Math.max.apply(Math, $aAllBoxIndexHolder));

   	if (isset($aAllBoxIndex[$iFocusBox]) && $('#' + $aAllBoxIndex[$iFocusBox]).length > 0)
   	{
    	var $sLink = $('#' + $aAllBoxIndex[$iFocusBox]).find('.js_box_history:first').html();

   		$('#' + $aAllBoxIndex[$iFocusBox]).fadeOut('100', function() {$(this).remove();});
		$('#' + $aAllBoxIndex[$iFocusBox]).parent('.js_box_image_holder').fadeOut('100', function() {$(this).remove();});

    	delete $aBoxHistory[$sLink];
   	}

   	$('#global_attachment_list_inline').hide();

	return false;
}

function tb_draggable($oObj)
{
    if ($($oObj).parent().hasClass('js_box_image_holder_full'))
    {
    	return false;
    }

	$($oObj).draggable(
    {
    	handle: '.js_box_title',
    	opacity: 0.35,
    	zIndex: 3000,
    	start: function(event, ui)
    	{
   			if ($('#global_attachment_list_inline').length > 0)
   			{
    			$('#global_attachment_list_inline').hide();
   			}
    	},
    	stop: function(event, ui)
    	{
    		if ($('#global_attachment_list_inline').length > 0)
   			{
	    		$Core.updateInlineBox();
	    		$('#global_attachment_list_inline').show();
   			}
    	}
    });
}

function tb_position($oObj, bFull)
{
    if (bFull !== true)
    {
		if (!$.ui)
	    {
	    	$Core.loadStaticFile(getParam('sJsHome') + 'static/jscript/jquery/ui.js');
	    }

	   tb_draggable('.js_box');
    }

	var $aAllBoxIndex = new Array();
	$('.js_box').each(function()
    {
    	$aAllBoxIndex.push(parseInt($(this).css('z-index')));
    });

    if (bFull === true)
    {
    	$($oObj).css(
	    {
	        top: getPageScroll()[1],
	        left: '50%',
	        'margin-left': '-' + (($($oObj).width() / 2) + 12) + 'px',
	        'z-index': (parseInt(Math.max.apply(Math, $aAllBoxIndex)) + 1)
	    });
    }
    else
    {
		$($oObj).css(
	    {
	        top: getPageScroll()[1] + (getPageHeight() / 5),
	        left: '50%',
	        'margin-left': '-' + (($($oObj).width() / 2) + 0) + 'px',
	        'z-index': (parseInt(Math.max.apply(Math, $aAllBoxIndex)) + 1)
	    });
    }

    if ($.browser.msie && $.browser.version == '7.0')
	{
    	$Behavior.ie7FixZindex();
	}
}


function tb_parseQuery(query)
{
   var Params = {};
	if (!query)
   {
		return Params;
   }

   var Pairs = query.split(/[;&]/);
   for (var i = 0; i < Pairs.length; i++)
   {
      var KeyVal = Pairs[i].split('=');
      if (!KeyVal || KeyVal.length != 2)
      {
		continue;
      }
      var key = unescape(KeyVal[0]);
      var val = unescape(KeyVal[1]);
      val = val.replace(/\+/g, ' ');

      Params[key] = val;
   }

   return Params;
}

function tb_getPageSize()
{
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	arrayPageSize = [w,h];

	return arrayPageSize;
}
