$(document).ready(
	function() {
		$('.portfolioThumb').click(
            function() {
                $(this).fadeTo('fast', 0.5, function() {
					$(this).fadeTo('fast', 1.0);
				});
				$('#overlay').height(19200);
				$('#overlay').fadeIn();
				//$('#portfolioLoader').show();
				
				var id = $(this).attr('portfolioId');
				var url = config.actionroot+'galleries/view_scroller/'+id;
				
				$.get(url, 
					function(data) {
						//$('#portfolioLoader').hide();
						$('#portfolioGalery').html(data);
						
						var arrPageSizes = getPageSize();
						var arrPageScroll = getPageScroll();
						// Calculate top and left offset for the jquery-lightbox div object and show it
						
						console.log(arrPageSizes[0]);
						
						$('#portfolioGaleryContainer').css({
							left:	'5%',
							width: '90%'
						});
						$('#portfolioGaleryContainer').animate({
							top:	arrPageScroll[1] + (arrPageSizes[3] / 10) + 50
						});
					}
				);
			}
		);
		
		$('#portfolioGaleryClose').click(
			function(e) {
				e.preventDefault();
				if ($('#portfolioGaleryContainer').is(':visible')) {
					closePortfolioScrollContainer();
					$('#overlay').fadeOut();
				}
			}
		);
		
		$('#overlay').click(
			function(e) {
				if ($('#portfolioGaleryContainer').is(':visible')) {
					closePortfolioScrollContainer();
				} 
			}
		);
		
		$('.categorymenu_link').click(
			function(e) {
				e.preventDefault();
				$(this).next().slideToggle();
			}
		);
		
		//if (!checkMobile()) {
			$(".lightbox").lightBox(
				{
					overlayBgColor: '#'+config.lightbox_overlay_bg,
					containerResizeSpeed: 150,
					overlayOpacity: 0.6,
					imageLoading:  config.imgroot+'lightbox/lightbox-ico-loading.gif',
					imageBtnClose: config.imgroot+'lightbox/close.gif',
					imageBtnPrev:  config.imgroot+'lightbox/prev.png',
					imageBtnNext:  config.imgroot+'lightbox/next.png'
				}
			);
		//}
		
		if (checkMobile()) {
			$('#lightbox-image').css('border', '5px solid #000').height(300);
			var useragent = navigator.userAgent||navigator.vendor||window.opera;
			//alert('hello mobile browser: '+useragent);	
		}
		
		$('#sharebtn').click(
			function(e) {
				var offset = $(this).offset();
				var left = offset.left -20;
				var top = offset.top -20;
				$('#sharebox').css({
					left: left+'px',
					top: top+'px'
				}).fadeIn();
			}
		);
		
		$('#sharebox').mouseleave(
			function() {
				$(this).fadeOut();
			}
		)
		
	}
);

function closePortfolioScrollContainer() {
	$('#portfolioGaleryContainer').css({
		top: -800
	});
	$('#portfolioGalery').html('');
	//$('#overlay').fadeOut();
}

function checkMobile() {
	var useragent = navigator.userAgent||navigator.vendor||window.opera;
	var strgrep = /android|avantgo|blackberry|blazer|compal|elaine|fennec|hiptop|iP(hone|od|pad)|iris|kindle|lge |maemo|midp|mmp|mobile|o2|opera mini|palm( os)?|plucker|pocket|pre\/|psp|smartphone|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce; (iemobile|ppc)|xiino/i;
	if(strgrep.test(useragent)) { 
		return true;
	}
}


/**
 / THIRD FUNCTION
 * getPageSize() by quirksmode.com
 *
 * @return Array Return an array with page width, height and window width, height
 */
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){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};
/**
 / THIRD FUNCTION
 * getPageScroll() by quirksmode.com
 *
 * @return Array Return an array with x,y page scroll values.
 */
function getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
};



function _set_interface() {
	// Apply the HTML markup into body tag
	$('body').append('<div id="jquery-overlay"></div><div id="jquery-lightbox"><div id="lightbox-container-image-box"><div id="lightbox-container-image"><img id="lightbox-image"><div style="" id="lightbox-nav"><a href="#" id="lightbox-nav-btnPrev"></a><a href="#" id="lightbox-nav-btnNext"></a></div><div id="lightbox-loading"><a href="#" id="lightbox-loading-link"><img src="' + settings.imageLoading + '"></a></div></div></div><div id="lightbox-container-image-data-box"><div id="lightbox-container-image-data"><div id="lightbox-image-details"><span id="lightbox-image-details-caption"></span><span id="lightbox-image-details-currentNumber"></span></div><div id="lightbox-secNav"><a href="#" id="lightbox-secNav-btnClose"><img src="' + settings.imageBtnClose + '"></a></div></div></div></div>');	
	// Get page sizes
	var arrPageSizes = getPageSize();
	// Style overlay and show it
	$('#jquery-overlay').css({
		backgroundColor:	settings.overlayBgColor,
		opacity:			settings.overlayOpacity,
		width:				arrPageSizes[0],
		height:				arrPageSizes[1]
	}).fadeIn();
	// Get page scroll
	var arrPageScroll = ___getPageScroll();
	// Calculate top and left offset for the jquery-lightbox div object and show it
	$('#jquery-lightbox').css({
		top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
		left:	arrPageScroll[0]
	}).show();
	// Assigning click events in elements to close overlay
	$('#jquery-overlay,#jquery-lightbox').click(function() {
		_finish();									
	});
	// Assign the _finish function to lightbox-loading-link and lightbox-secNav-btnClose objects
	$('#lightbox-loading-link,#lightbox-secNav-btnClose').click(function() {
		_finish();
		return false;
	});
	// If window was resized, calculate the new overlay dimensions
	$(window).resize(function() {
		// Get page sizes
		var arrPageSizes = getPageSize();
		// Style overlay and show it
		$('#jquery-overlay').css({
			width:		arrPageSizes[0],
			height:		arrPageSizes[1]
		});
		// Get page scroll
		var arrPageScroll = ___getPageScroll();
		// Calculate top and left offset for the jquery-lightbox div object and show it
		$('#jquery-lightbox').css({
			top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
			left:	arrPageScroll[0]
		});
	});
}
