var smallBunnerWidth = 201;
var padding = 36;
var promoMinMargin = 10;

var bandSpeedWithoutMouseDown = 8;
var bandSpeed = bandSpeedWithoutMouseDown;
var promoAnimatePart = 0;
var bandInterval;
var bandIntervalStatus = false;

var lastAnimate = 0;
var promoAnimate = 0;
var animateVisCount = 0;
var promoNum = 0;
var reColculate = false;

var motion = true;
var motionNow = false;
	
$(document).ready(function() {

	$(".blinkingText").hide();
	setTimeout(function(){$(".blinkingText").show();setInterval(function(){$(".blinkingText").toggleClass('notVisible');},300)},1000);	

	$("a.tooltip").tooltip({track: true, delay: 100, showBody: "|", showURL: false, opacity: 0.85});

	smallBunnerWidth = ($(".image-band .main-event")[0] != undefined)?201:194;
	//alert(smallBunnerWidth);
	
	if ($.browser.mozilla) {
		bandSpeedWithoutMouseDown = bandSpeedWithoutMouseDown * 2;
	}

	$(".image-band .motion").width(parseInt($(".image-band .motion a").length*smallBunnerWidth/2)+4+'px');
	setTimeout(function(){
		resize();
		initmb();
	},10);
	$(window).resize(resize);
	
	if ($.browser.className == 'msie6') {
		$(".promo-band .motion-inner>div").bind("mouseover", function() {
			$(this).find("span.hover").addClass("hover-ie");
		})
		$(".promo-band .motion-inner>div").bind("mouseout", function() {
			$(this).find("span.hover").removeClass("hover-ie");
		})
	}
	
	$(".promo-band .left-control, .promo-band .right-control").bind('mousedown', function() {
		bandSpeed *= 3;
	});
	
	$(".promo-band .left-control, .promo-band .right-control").bind('mouseup', function() {
		bandSpeed = bandSpeedWithoutMouseDown;
	});
	
	var leftControlActive = false;
	$(".promo-band .left-control").bind("mouseover", function(){
		if (motion && !bandIntervalStatus) {
			leftControlActive = true;
			if (promoNum>0)
			{
				leftcontrol($(this));
			}
		}
	})
	$(".promo-band .left-control").bind("mouseout", function(){
		leftControlActive = false;
	})
	function leftcontrol(target) {
		bandIntervalStatus =true;
		var $motion_inner = target.parent().find(".motion-inner");
		
		bandInterval = setInterval(function(){
			var left = $motion_inner.css('left');
			left = left=="auto" ?0:parseInt(left);
			
			promoAnimatePart += bandSpeed;
						
			if(promoAnimatePart > promoAnimate)
			{	clearInterval(bandInterval); bandIntervalStatus = false;
				promoNum--;
				promoAnimatePart -= promoAnimate;
				if (promoNum > 0 && leftControlActive)
				{
					left += bandSpeed;	$motion_inner.css({'left':left});
					target.parent().css({'background-position': left+"px 0px"});
					leftcontrol(target);
				}else{
					left += bandSpeed -promoAnimatePart; $motion_inner.css('left',left);
					promoAnimatePart = 0;
					target.parent().css({'background-position': left+"px 0px"});
				}
			}else{
				left += bandSpeed; 	$motion_inner.css({'left':left});
				target.parent().css({'background-position': left+"px 0px"});
			}
		},25);
	}
	var rightControlActive = false;
	$(".promo-band .right-control").bind("mouseover", function(){
		if (motion && !bandIntervalStatus) {
			rightControlActive = true;
			if (promoNum < $(".promo-band .motion-inner>div").length - animateVisCount)
			{
				rightcontrol($(this));
			}
		}
	})
	$(".promo-band .right-control").bind("mouseout", function(){
		rightControlActive = false;
	})
	function rightcontrol(target) {
		bandIntervalStatus =true;
		var $motion_inner = target.parent().find(".motion-inner");
		
		bandInterval = setInterval(function(){
			var left = $motion_inner.css('left');
			left = left=="auto" ?0:parseInt(left);
			
			promoAnimatePart += bandSpeed;
						
			if(promoAnimatePart > promoAnimate)
			{	clearInterval(bandInterval); bandIntervalStatus = false;
				promoNum++;
				promoAnimatePart -= promoAnimate;
				if ((promoNum < $(".promo-band .motion-inner>div").length - animateVisCount) && rightControlActive)
				{
					left -= bandSpeed;	$motion_inner.css({'left':left});
					target.parent().css({'background-position': left+"px 0px"});
					rightcontrol(target);
				}else{
					left -= bandSpeed -promoAnimatePart; $motion_inner.css('left',left);
					promoAnimatePart = 0;
					target.parent().css({'background-position': left+"px 0px"});
				}
			}else{
				left -= bandSpeed; 	$motion_inner.css({'left':left});
				target.parent().css({'background-position': left+"px 0px"});
			}
		},25);
	}
	
	$(".image-band>.other-event a").each(function(i) {
		$(this).find("img").css({"background-image":"url("+$(this).find("img")[0].src+")"})
		$(this).find("img")[0].rev = $(this).find("img")[0].src;
		$(this).find("img").bind('mouseover', function(){
			$(this)[0].src = '/Sites/expojournal/Images/select_border.png';
			
			var i = $(this);
			var a = i.parents('a');
			var s = a.find('span');
			var d = s.find('div');
			var w = parseInt(s.css('width'));
			var h = parseInt(s.css('height'));
			var l = parseInt(s.css('left'));
			if (i.width() != s.width())
				return false;
			s.css('width', w - 10 + 'px');
			s.css('height', h - 5 + 'px');
			d.css('margin-top', '2px');
			s.css('margin-left', '5px');
			s.css('bottom', '5px');
		})
		$(this).find("img").bind('mouseout', function(){
			$(this)[0].src = $(this)[0].rev;
			
			var i = $(this);
			var a = i.parents('a');
			var s = a.find('span');
			var d = s.find('div');
			var w = parseInt(s.css('width'));
			var h = parseInt(s.css('height'));
			var l = parseInt(s.css('left'));
			s.css('width', w + 10 + 'px');
			s.css('height', h + 5 + 'px');
			d.css('margin-top', '0px');
			s.css('margin-left', '0px');
			s.css('bottom', 0);
			
			removeWhiteBoxes();
		})
		//$(this).find("img")[0].src = '/Sites/expojournal/Images/select_border.png';
	});
	
	$('.other-event').bind('mouseout', function() {
		removeWhiteBoxes();
	});
	
	initRadioBox();
});

function removeWhiteBoxes() {
	$('.image-band>.other-event a').each(function() {
		$(this).find("img")[0].src = $(this).find("img")[0].rev;
	});
}

function addTextOnImages() {
	// Remove all text overlays.
	$('.textOnImage, .inimg').remove();
	// Add text overlays for banners.
	$('.other-event a, .expo_preview a').each(function() {
		var heightDivider = 4;
		var a = $(this);
		var t = a.attr('title');
		a.css('position', 'relative');
		a.prepend('<span class="textOnImage"><div><table><tr><td>' + t + '</td></tr></table></div></span>');
		var s = a.children('span');
		var d = s.children('div');
		var i = a.children('img');
		i.css({
			'z-index': '999'
		});
		s.css({
			'height': i.height() / heightDivider + 'px',
			'width': i.css('width')
		});
		
		while (s.height() < d.height()) {
			$('.textOnImage').css('height', i.height() / --heightDivider + 'px');
		}
		
		// Trigger mouseover event on image if we move mouse over text overlay.
		s.bind('mouseover', function() {
			var a = $(this).parents('a');
			var i = a.find('img');
			i.trigger('mouseover');
		});
		s.bind('mouseout', function() {
			var a = $(this).parents('a');
			var i = a.find('img');
			if ($(this).width() != i.width())
				i.trigger('mouseout');
		});
	});
}

function resize() {
	var main_event_width = ($(".image-band>.main-event")[0] != undefined)?359:187; //$(".image-band>.main-event").width()
	//alert(main_event_width);
	var promo_inner_width = 117; //$(".promo-band .motion-inner>div").width();
	
	var windowWidth = $(window).width()<1000?1000:$(window).width();
	var ceilBansCount = parseInt((windowWidth-(main_event_width+1)-padding)/smallBunnerWidth);
	var innerWidth = main_event_width+ceilBansCount*smallBunnerWidth;
	
	$(".main").width(windowWidth +'px');
	$(".main>.header").width(innerWidth +'px');
	$(".main>.content").width(innerWidth +'px');
	
	$(".footer").width(windowWidth +'px');
	$(".footer>.content").width(innerWidth +'px');
	
	if ($.browser.className == 'msie6') {
		$(".image-band>.other-event").width($(".image-band").width() - main_event_width - 1 +'px');
	}
	
	var promoOriginalWidth = (promo_inner_width+promoMinMargin)*$(".promo-band .motion-inner>div").length;
	if (promoOriginalWidth < $(".promo-band>.motion").width()) {
		var inc = $(".promo-band>.motion").width() % promoOriginalWidth / ($(".promo-band .motion-inner>div").length-1);
		$(".promo-band .motion-inner>div").css({
			"margin-right":parseInt(promoMinMargin+inc+1)+"px"
		});
		$(".promo-band .motion-inner>div:last").css({
			"margin-right":"0"
		});//(parseInt(promoMinMargin+inc+1)+"px");
		motion = false;
		promoAnimate = parseInt(promoMinMargin+inc+1) + promo_inner_width;
		/*if (lastAnimate>0)
			promoReposition();
		lastAnimateVisCount = animateVisCount;
		lastAnimate = promoAnimate;*/
	} else {
		var visibleCount = parseInt($(".promo-band>.motion").width() / (promo_inner_width+promoMinMargin));
		var visibleCountOriginalWidth = (promo_inner_width+promoMinMargin)*visibleCount;
		var inc = $(".promo-band>.motion").width() % visibleCountOriginalWidth / (visibleCount-1);
		var mistake = Math.round((inc*visibleCount-parseInt(inc)*visibleCount)/visibleCount*2.4);
		//alert((inc*(visibleCount-1)-parseInt(inc)*(visibleCount-1))/(visibleCount-1)*2.49+" "+((promoMinMargin+inc)*visibleCount-parseInt(promoMinMargin+inc)*visibleCount)/visibleCount*2.49);
		$(".promo-band .motion-inner>div").css({
			"margin-right":parseInt(promoMinMargin+inc+mistake)+"px"
		});
		$(".promo-band .motion-inner>div:last").css({
			"margin-right":"0"
		});
		//alert(visibleCount);
		promoAnimate = parseInt(promoMinMargin+inc+mistake) + promo_inner_width;
		animateVisCount = visibleCount;
		if (lastAnimate>0)
			promoReposition();
		lastAnimate = promoAnimate;
	}
	$(".promo-band .motion-inner").width(promoAnimate*$(".promo-band .motion-inner>div").length);
	//alert();
	if ($.browser.className == 'msie6') {
		$(".gallery-text tr").find("td:last>div").css({
			"width":"189px"
		})
		//alert();
	}
	addTextOnImages();
}

function promoReposition() {
	//alert(parseInt($(".promo-band .motion-inner").css("left"))+(promoAnimate-lastAnimate));
	var shift = 0;
	/*if (promoNum > $(".promo-band .motion-inner>div").length - animateVisCount)
		shift = promoNum - ($(".promo-band .motion-inner>div").length - animateVisCount);*/
	var left = $(".promo-band .motion-inner").css("left");
	left = left=="auto"?0:parseInt(left);
	$(".promo-band .motion-inner").css({
		'left': left-(promoAnimate-lastAnimate)*promoNum+promoAnimate*shift+"px"
	})
}

function initRadioBox() {

	if ($(".radio-group")[0] != undefined ) {

		$(".radio-group input[type=radio], .radio-group input[type=checkbox]").each(function(i) {

			if ($(this).css("display") != 'none') {
				// Do some nice html-markup around simple form.

				$(this).css({"display": "none"});

				$(this).after($('<div id="' + $(this).attr('name') + '-element' + i + '" class="decore-point"></div>'));

				$(this).parent().after('<div class="float-clear"><!-- --></div>');

				$($(this).parent().find("span")).css({

					"position": "relative",

					"top": "2px"

				});
				// Click handler.

				$(this).parent().click(function() {

					$(this).parent().find("div").removeClass('select');
					$(this).parent().find("input[type=radio], input[type=checkbox]").attr('checked', false);

					$(this).find("div").addClass('select');
					$(this).find("input[type=radio], input[type=checkbox]").attr('checked', true);

				});
				// "Check" decorate-div if corresponding radio-button is checked.

				if ($(this).attr('checked'))

					$(this).parent().find("div").addClass('select');

			}

		});

	}

}

// Modal Dialog Box
// copyright 8th July 2006 by Stephen Chapman
// http://javascript.about.com/
// permission to use this Javascript on your web page is granted
// provided that all of the code in this script (including these
// comments) is used without any alteration

function pageWidth() {
	return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
}

function pageHeight() {
	return window.innerHeight != null ? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null ? document.body.clientHeight : null;
}

function posLeft() {
	return typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
}

function posTop() {
	return typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
}

function $0(x) {
	return document.getElementById(x);
}

function scrollFix() {
	var obol = $0('ol');
	obol.style.top = posTop() + 'px';
	obol.style.left = posLeft() + 'px'
}

function sizeFix() {
	var obol = $0('ol');
	obol.style.height = pageHeight() + 'px';
	obol.style.width = pageWidth() + 'px';
}

function kp(e) {
	ky = e ? e.which : event.keyCode;
	if (ky == 88 || ky == 120) hm();
	return false
}

function inf(h) {
	tag = document.getElementsByTagName('select');
	for (i = tag.length - 1; i >= 0; i--) tag[i].style.visibility = h;
	tag = document.getElementsByTagName('iframe');
	for (i = tag.length - 1; i >= 0; i--) tag[i].style.visibility = h;
	tag = document.getElementsByTagName('object');
	for (i = tag.length - 1; i >= 0; i--) tag[i].style.visibility = h;
}

function sm(obl, wd, ht) {
	var h = 'hidden';
	var b = 'block';
	var p = 'px';
	var obol = $0('ol');
	var obbxd = $0('mbd');
	obbxd.innerHTML = $0(obl).innerHTML;
	obol.style.height = pageHeight() + p;
	obol.style.width = pageWidth() + p;
	obol.style.top = posTop() + p;
	obol.style.left = posLeft() + p;
	obol.style.display = b;
	var tp = posTop() + ((pageHeight() - ht) / 2) - 12;
	var lt = posLeft() + ((pageWidth() - wd) / 2) - 12;
	var obbx = $0('mbox');
	obbx.style.top = (tp < 0 ? 0 : tp) + p;
	obbx.style.left = (lt < 0 ? 0 : lt) + p;
	obbx.style.width = wd + p;
	obbx.style.height = ht + p;
	inf(h);
	obbx.style.display = b;
	return false;
}

function hm() {
	var v = 'visible';
	var n = 'none';
	$0('ol').style.display = n;
	$0('mbox').style.display = n;
	inf(v);
	document.onkeypress = ''
}

function initmb() {
	var ab = 'absolute';
	var n = 'none';
	var obody = document.getElementsByTagName('body')[0];
	var frag = document.createDocumentFragment();
	var obol = document.createElement('div');
	obol.setAttribute('id', 'ol');
	obol.style.display = n;
	obol.style.position = ab;
	obol.style.top = 0;
	obol.style.left = 0;
	obol.style.zIndex = 998;
	obol.style.width = '100%';
	frag.appendChild(obol);
	var obbx = document.createElement('div');
	obbx.setAttribute('id', 'mbox');
	obbx.style.display = n;
	obbx.style.position = ab;
	obbx.style.zIndex = 999;
	var obl = document.createElement('span');
	obbx.appendChild(obl);
	var obbxd = document.createElement('div');
	obbxd.setAttribute('id', 'mbd');
	obl.appendChild(obbxd);
	frag.insertBefore(obbx, obol.nextSibling);
	obody.insertBefore(frag, obody.firstChild);
}

