var currentScreenSize = '1024x768'
var setScreenSize = function(){
	function setCookie(c_name,value,expiredays){
		var exdate=new Date();
		exdate.setDate(exdate.getDate()+expiredays);
		document.cookie=c_name+ "=" +escape(value)+
		((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
	}
	var operaFix = window.opera && (document.compatMode == "CSS1Compat") && ($.browser.version >= 9.3);
	var h = operaFix ? document.documentElement.clientHeight : $(document).height();
	var w = $(document).width();
	var screenSizes = [[1024,768],[1500,800]];
	var imgSizeNames = ['1024x768','1920x1200']
	var currentSize = 0;
	for (var i=1;i<screenSizes.length;i++){
		if (screenSizes[i][0] < w && screenSizes[i][1] < h)
			currentSize = i;
	}
	setCookie('ScreenSize', imgSizeNames[currentSize], 30);
	currentScreenSize = imgSizeNames[currentSize];
};
$(document).ready(setScreenSize);
$(window).bind('resize', setScreenSize);
var useCanvas = (($.browser.mozilla && (parseFloat($.browser.version) < 1.9) && (parseFloat($.browser.version) >= 1.8))) || window.opera;
jQuery.fn.imageshift = function(opts) {
	var imgs = this;
	if (opts.position == false){
		opts.positionx = opts.positionx || false;
		opts.positiony = opts.positiony || false;
	}
	var options = $.extend({
		resize:false,
		positionx:true,
		positiony:true,
		useBorder:false,
		shiftTitle:false,
		detectScreenChange:true,
		swapImagesSizes:true
	}, opts);
	var cropversions = {};
	var resizeTimer = null;
	var moveIt = function(){
		if (this == null)
			return;
		var img = this;
		var focalpoint = [.5,.5];
		if (img.getAttribute('focalpoint') != '[,]')
			focalpoint = eval(img.getAttribute('focalpoint') || '[.5,.5]');
		var canvas;
		if (useCanvas){
			canvas = $('canvas',img.parentNode);
			canvas.css('display', 'none');
			$(img).css('display', '');
		}
		var parent = img.offsetParent;
		if (parent == null)
			return;
		var extrah = img.offsetHeight - img.clientHeight , extraw = img.offsetWidth - img.clientWidth;
		var target = {w:parent.clientWidth - extraw,h:parent.clientHeight - extrah};
		var iw=img.clientWidth,ih=img.clientHeight;
		if (options.resize) {
			var srcAspect = iw/ih;
			var targetAspect = target.w/target.h;

			var useWidth = options.useBorder ? targetAspect<srcAspect : targetAspect>srcAspect;
			if ((useWidth && !(options.resizeFor == 'height')) || options.resizeFor == 'width'){
				iw = target.w * (options.widthMultiplier || 1);
				ih = Math.round(iw / srcAspect);
			}else{
				ih = target.h * (options.heightMultiplier || 1);
				iw = Math.round(ih * srcAspect);
			}
			if (img.tagName == 'IMG'){
				if (img.src.match(/(\d+x\d+).jpg/) && options.swapImagesSizes)
					if (img.src.match(/(\d+x\d+).jpg/)[1] != currentScreenSize)
						img.src = img.src.replace(/\d+x\d+.jpg/, currentScreenSize + '.jpg');
				if (!useCanvas){
					img.width=iw;
					img.height=ih;
				}else{
					canvas.remove();
					canvas = $('<canvas />').insertAfter(img);
					canvas.attr('width', iw).attr('height', ih);
					canvas = canvas[0];
					canvas.getContext('2d').drawImage(img, 0, 0, iw, ih);
					$(img).css('display', 'none');
				}
			}else{
				$(img).css({'width':iw,'height':ih});
			}
		}
		if (options.positionx || options.positiony){
			var left=0,top=0;
			var fpx = iw * focalpoint[0];
			var fpy = ih * focalpoint[1];

			if (iw > target.w){
				left = (target.w/2) - fpx;
				if (iw-fpx<target.w/2)
					left += (target.w/2)-(iw-fpx);
				left = Math.min(left,0);
			}else{
				left = Math.round((target.w - iw) /2);
			}
			if (ih > target.h) {
				top = (target.h/2) - fpy;
				if (ih-fpy<target.h/2)
					top += (target.h/2)-(ih-fpy);
				top = Math.min(top, 0);
			}else{
				top = Math.round((target.h - ih) /2);
			}
			if (options.resize){
				//top -= (extrah/2);
				//left -= (extraw/2);
			}
			var posob = {'position':'absolute'};
			if (options.positionx)
				posob.left = left+'px';
			if (options.positiony)
				posob.top = top+'px';
			$(img).css(posob);
			if (canvas)
				$(canvas).css(posob);
			if (options.shiftTitle)
				$('.LinkName a', $(img).parent().parent().parent()).css('padding-left',left+'px');
		}
	};
	if (options.detectScreenChange){
		$(window).bind('resize', function() {
			if (resizeTimer)
				clearTimeout(resizeTimer);
			resizeTimer = setTimeout(function(){
				$.each(imgs, function(){
					this.moveIt();
				});
			}, 100);
		});
	}
	return imgs.each(function(i){
		this.moveIt = moveIt;
		if (this.complete || this.tagName != 'IMG') {
			this.moveIt();
			if (!useCanvas)
				$(this).css('visibility', 'visible');
			if (options.backgroundRemove)
				options.backgroundRemove[i].style.backgroundImage = 'none';
		} else {
			$(this).load(function(){
				this.moveIt();
				if (!useCanvas){
					$(this).css('visibility', 'visible');
					$(this).hide();
					$(this).fadeIn();
					if (options.backgroundRemove)
						options.backgroundRemove[i].style.backgroundImage = 'none';
				}
			});
		}
	});
};
jQuery.fn.slidescroll = function(slideHouser) {
	if (this.length == 0)
		return;
	var ul = $(this[0]);
	slideHouser = $(slideHouser);
	slideHandle = $('.ContentSliderHandle', slideHouser);
	if (ul.height() == 0)
		ul.height(ul.parent().height());
	var isScrolling = false;
	var scrollTimeout;
	var startSlider = function(){
		isScrolling = false;
		slideHandle.height(Math.min(((ul.height()*100)/ul.attr("scrollHeight")),100) + '%');
		slideHouser.slider({
			orientation:'vertical',
			handle: '.ContentSliderHandle',
			change: function(e, ui){
				var maxScroll = ul.attr("scrollHeight") - ul.height();
				ul.attr({scrollTop: ui.value * (maxScroll / 100) });
			},slide: function(e, ui){
				var maxScroll = ul.attr("scrollHeight") - ul.height();
				ul.attr({scrollTop: ui.value * (maxScroll / 100) });
			}
		});
	};
	var scrollMove = function(event, delta){
		if (isScrolling)
			return;
		clearTimeout(scrollTimeout);
		//isScrolling = true;
		if (!ul.hasClass('GalleryMoving'))
			ul.addClass('GalleryMoving');
		delta = delta / Math.abs(delta);
		var maxScroll = ul.attr("scrollHeight") - ul.height();
		var newval = $('.ContentSlider').slider('value') - ((ul.height()/ul.attr("scrollHeight")) * delta * 30);
		newval = Math.max(newval, 0);
		newval = Math.min(newval, maxScroll);
		$('.ContentSlider').slider('moveTo', newval);
		scrollTimeout = setTimeout(function(){ul.removeClass('GalleryMoving');}, 100);
		return false;
	} 
	$('.GalleryContainer').mousewheel(scrollMove);
	var checkKey = function(e){
		 switch (e.keyCode) {
			case 40:
				//alert('down');
				scrollMove(e, -1);
				break;
			case 38:
				//alert('up');
				scrollMove(e, 1);
				break;
		}
	}
	$('.ContentSlider').slider('moveTo', 0);
	//if ($.browser.mozilla) {
	//	$(document).keypress (checkKey);
	//} else {
		$(document).keydown(checkKey);
	//}
	startSlider();
	$(window).bind('resize', startSlider);
}