$(document).ready(function(){
	$.fn.alternateScroll=function(options){
		var settings={removing:true};
		if(options){$.extend(settings, options);}
		$.fn.extend({
			buttonPosition:function(posY){
				var $this = this;
				posY -= Math.round(Number($this.scrollButton.height/2));
				if(posY<0) posY = 0;
				if(posY+$this.scrollButton.height>$this.scrollWrap.height) posY = $this.scrollWrap.height-$this.scrollButton.height;
				$this.scrollButton.css({top:posY+'px'});
				$this.moveMenu();
			},
			scrollWraper:function(value){
				var $this = this;
				var top = Math.round(((100-value) * ($this.wraper.h-$this.height()))/100);
				$this.wraper.css({top:'-'+top+'px'});
			}
		});
		$(this).each(function(){
			var $this = $(this);
			$this.wraper = $this.find('div.scrollWrap').css('position','relative');
			$this.wraper.h = $this.wraper.height();
			if($this.wraper.children().find(':last-child').hasClass('.cms')) $this.wraper.h -= 50;
			// Hack on subscription form, where need to add back the 50px to scroll down
			// the page
			if($('body .informationBlock').length > 0) $this.wraper.h+=70;
			if($this.wraper.h < $this.height() && settings.removing){
				$this.next('.scrollBar').remove();
				$this.wraper.removeAttr('style');
				return $this;
			}
			
			$.fn.extend({
				mouseScroll:function(event){
					if(!event) event=window.event;
					if(event.wheelDelta) wheelDelta = event.wheelDelta/120;
					else if (event.detail) wheelDelta =- event.detail/3;
					$this.scrollBar.slider('value', $this.scrollBar.slider('value')+(wheelDelta*10));
					//event.preventDefault()
				}
			});
			$this.addClass('withScroll');
			$this.scrollBar=$this.next('.scrollBar');
			if(!$this.next('.scrollBar').length){
				$this.scrollBar = $('<div class="scrollBar"><div class="scrollTop"><a href="#"><span>&uarr;</span></a></div><div class="scrollBottom"><a href="#"><span>&darr;</span></a></div><div class="scrollSlider"> </div></div>')
					.insertAfter($this);
			}
			$this.topPos = $this.wraper.h-$this.height();
			$this.scrollBar = $this.scrollBar.find('.scrollSlider');
			$this.scrollBar.slider('destroy').slider({
				orientation: "vertical",
				value: 100,
				change: function(event, ui) {
					$this.scrollWraper(ui.value)
				},
				slide: function(event, ui) {
					$this.scrollWraper(ui.value)
				}
			});
			if($this.get(0).addEventListener) $this.get(0).addEventListener('DOMMouseScroll', $this.mouseScroll, false);
			$this.get(0).onmousewheel = $this.mouseScroll;
			$this.scrollBar.parent().find('.scrollTop a').click(function(event){
				event.preventDefault();
				$this.scrollBar.slider('value', $this.scrollBar.slider('value')+10);
			});
			$this.scrollBar.parent().find('.scrollBottom a').click(function(event){
				event.preventDefault();
				$this.scrollBar.slider('value', $this.scrollBar.slider('value')-10);
			});
		});
		return $(this);
	};
	

	$('form a.submitLink').click(function(e){
		e.preventDefault();
		$(this).parents('form').submit();
	});

	$("#forgetPassword").click(function() {
		$("#dialogPassword").dialog('open');
	});

	$("#submitInput").click(function(){
		$(".loginForm").submit();
	});

	if($('#checkNextSession').length && $('#checkNextSessionDouble').length){
		var $ch1=$('#checkNextSession').removeAttr('checked'),
			$ch2=$('#checkNextSessionDouble').removeAttr('checked');
		$ch1.click(function(){
			$ch2.removeAttr('checked');
		});
		$ch2.click(function(){
//			$ch1.attr('checked','checked');
			$ch1.removeAttr('checked');
		});
	}
	$('div.pcontent:has(a.openNyroModal)').each(function(){
		if($(this).find('a.openNyroModal').length>1){
			$(this).find('a.openNyroModal').not(':first').hide()
		}
	});

	/*if (!$('#box div').hasClass('scrollHere')) {
	 $('#footer').addClass('no-padding')
	 }*/
	contentResize();
});

var $bgBlock, $bgImg=$('#mainBackground img'), $purple, $cont, imgCount=0;

function imageResize(){
	$bgImg.removeAttr('style');
	if($bgImg.width()<$bgBlock.width()){
		$bgImg.width($bgBlock.width()).height('auto');
		if($bgImg.height()<$bgBlock.height()) $bgImg.height($bgBlock.height()).width('auto');
	}
	if($bgImg.height()<$bgBlock.height()){
		$bgImg.height($bgBlock.height()).width('auto');
		if($bgImg.width()<$bgBlock.width()) $bgImg.width($bgBlock.width()).height('auto');
	}

	if($bgImg.height()>$bgBlock.height()) $bgImg.css('top',Math.round(Number(($bgBlock.height()-$bgImg.height())/2))+'px');
	else $bgImg.css('top','0');

	if($bgImg.width()>$bgBlock.width()) $bgImg.css('left',Math.round(Number(($bgBlock.width()-$bgImg.width())/2))+'px');
	else $bgImg.css('left','0');
}

function setPageSize(){
	if($bgBlock.height()<710) $bgBlock.attr('style','height:710px');
	if($bgBlock.height()<$('html').height()) $bgBlock.attr('style','height:'+$('html').height()+'px');
	$purple.height($cont.height());
}

function contentResize(){
	var wh=Number(window.innerHeight) || Number(document.body.clientHeight);

	if(!$('#content div.scrollBar').length){
		$('div.scrollHere').alternateScroll({removing:false});
	}

	var $blocks_1 = $('#content div.scrollHere, #content div.scrollBar');
	var $blocks_2 = $('#content div.scrollSlider');
	var h = 0;
	var headerHeight = parseInt($('#main').css('padding-top'));
	var topNavigationHeight = $('#main div.topNavigation').height() + parseInt($('#main div.topNavigation').css('padding-bottom'));
	var footerHeight = $('#footer').height() + parseInt($('#footer').css('padding-top'));
	h = wh - headerHeight - topNavigationHeight - footerHeight;
	
	if($('#loginBottomBlock').length) h-=$('#loginBottomBlock').height() + parseInt($('#loginBottomBlock').css('margin-top'));
	
	if($('#content div.smallScroll').length) h -= 180;
	if(h < 70) h = 70;
	$blocks_1.height(h);
	$blocks_2.height(h-50);

	if($('#content div.smallScroll').length) $blocks_2.height(h-32);
	$('div.scrollHere').alternateScroll();
}

function changeBg(){
	if (bgImages.length) {
		$('#backGround img').attr('src',''+bgImages[imgCount++]);
		if(imgCount==bgImages.length) {
			imgCount=0;
		}
	}
}

var tt = setInterval('changeBg()', 5000);

window.onload = function() {
	$bgBlock=$('#mainBackground');
	$bgImg=$('#mainBackground img');
	$purple=$('#all div.purpleBg');
	$cont=$('#container');
	if($.browser.msie && parseInt($.browser.version)<7) setPageSize();
	imageResize();
	//contentResize();
};

window.onresize = function(){
	if($.browser.msie && parseInt($.browser.version)<7) setPageSize();
	imageResize();
	contentResize();
};

$(window).unload(function(){
	$('#content').remove();
});
