/**
 *
 * Set the background image according to the available view port
 *
 */
function setBg()
{

	var IMG_PATH = 'img/bg/';
	var aImgSizes = [[1024,768],[1280,1024],[1920,1480]];
	var bFoundImage = false;

	var screenDims = getAvailableViewPort();

	for(var i=0; i<aImgSizes.length; i++)
	{
		if(aImgSizes[i][0] >= screenDims.iWidth && aImgSizes[i][1] >= screenDims.iHeight)
		{
			bFoundImage = true;
			break;
		}
	}

	if(bFoundImage)
	{
		// $('#body').css({'background-image': 'url(' + IMG_PATH + aImgSizes[i][0] + 'x' + aImgSizes[i][1] + '.jpg)'});
		$('#bgImg').attr('src', IMG_PATH + aImgSizes[i][0] + 'x' + aImgSizes[i][1] + '.jpg');
	}
	else
	{
		for(var i=0; i<aImgSizes.length; i++)
		{
			if(aImgSizes[i][0] >= screenDims.iWidth)
			{
				// $('#body').css({'background-image': 'url(' + IMG_PATH + aImgSizes[i][0] + 'x' + aImgSizes[i][1] + '.jpg)'});
				$('#bgImg').attr('src', IMG_PATH + aImgSizes[i][0] + 'x' + aImgSizes[i][1] + '.jpg');
				break;
			}
		}
	}
}


/**
 *
 * reads the available screen dimensions
 * @return array
 */
function getAvailableViewPort()
{
	var viewPort = {};
	var bIsIE = (-1 != navigator.userAgent.indexOf('MSIE')) ? true : false;

	if(!bIsIE)
	{
		// var iHeight = (window.innerHeight >= document.body.offsetHeight) ? window.innerHeight : document.body.offsetHeight;
		// var iWidth = (window.innerWidth >= document.body.offsetWidth) ? window.innerWidth : document.body.offsetWidth;
		var iHeight = window.innerHeight;
		var iWidth = window.innerWidth;
	}
	else
	{
		// var iHeight = (document.documentElement.clientHeight >= document.body.offsetHeight) ? document.documentElement.clientHeight : document.body.offsetHeight;
		// var iWidth = (document.documentElement.clientWidth >= document.body.offsetWidth) ? document.documentElement.clientWidth : document.body.offsetWidth;
		var iHeight = document.documentElement.clientHeight;
		var iWidth = document.documentElement.clientWidth;
		
	}
	
	viewPort.iHeight = iHeight;
	viewPort.iWidth = iWidth;

	return viewPort;
}

function initScrollPane()
{
	$('.scroll-pane').jScrollPane({showArrows:true, scrollbarWidth : 26});
}

/**
 * Triggers the updateOption function of a js module
 *
 * @param psSelector
 * @param sKey
 * @param sValue
 * @return void
 */
function triggerOptionUpdate(psSelector, sKey, sValue)
{
	$(psSelector).module('updateOption', sKey, sValue);
}


$(document).ready(function(){
	setBg();
	$(window).resize(function(){
		setBg();
	});

	$('.sprite.mouseover').live('mouseover', function(){
		$(this).addClass('hover');
	});

	$('.sprite.mouseover').live('mouseout', function(){
		$(this).removeClass('hover');
	});
	
	$('img.detachable').live('mouseover', function(){

		var borderWidth = 3;

		var oDrag = $('<div class="detachableDragBorder" style="'+
			'width:'+$(this).width()+'px;'+
			'height:'+$(this).height()+'px;'+
			'top:'+($(this).offset().top-borderWidth)+'px;'+
			'left:'+($(this).offset().left-borderWidth)+'px;"></div>').draggable();


		$('body#body').append(oDrag);

//		oDrag.trigger('mousedown');
//		oDrag.trigger('click');
//		oDrag.trigger('start');

	});
	
	$('img.detachable').live('mouseout', function(){

		//$('div.detachableDragBorder').remove();

	});

	$('img.detachable').live('mouseup', function(){
		//console.log('img up');
	});

	$('div.detachableDragBorder').live('mousedown', function(){
		//console.log('down');
		var currentImgPos = $('span.imagePosition', '.galleryShowCase').text();
		if($('img.detachable', 'li.jcarousel-item-' + currentImgPos).get(0))
		{
			var currentImgSrc = $('img.detachable', 'li.jcarousel-item-' + currentImgPos).attr('src');
			$(this).css('opacity', 0.6);
			$(this).css('background-image', 'url(' + currentImgSrc + ')');
		}
	});

	$('div.detachableDragBorder').live('mouseout', function(){
		//console.log('up');
		$(this).remove();
	});
	
	$('div.detachableDragBorder').live('mouseup', function(){
		//console.log('up');
		$(this).remove();
	});
});