
$(document).ready(function() {

	$('a.intuitive').fancybox({ 'hideOnContentClick': true, 'overlayShow': true, 'overlayOpacity': 0, 'zoomSpeedIn': 500, 'zoomSpeedOut' : 500 });
	$('a.friendly').fancybox({ 'hideOnContentClick': true, 'overlayShow': true, 'overlayOpacity': 0, 'zoomSpeedIn': 500, 'zoomSpeedOut' : 500 });
	$('a.smart').fancybox({ 'hideOnContentClick': true, 'overlayShow': true, 'overlayOpacity': 0, 'zoomSpeedIn': 500, 'zoomSpeedOut' : 500 });
	$('a.discrete').fancybox({ 'hideOnContentClick': true, 'overlayShow': true, 'overlayOpacity': 0, 'zoomSpeedIn': 500, 'zoomSpeedOut' : 500 });
	

	$('#title .name').click(function (){$('#menu_content .pos img[@name=icon_3]').parent().click();});

	var interval = 5000; //interval posle kog se menjaju selektovani naslovi
	var speed = 750; //brzina rotiranja menija
	var slideSpeed = 750; //brzina menjanja slajdova
	var filesSpeed = 300; //brzina menjanja fajlova na slide_2, 200 = jQuery fast, 600 = jQuery slow
	var positions = new Array();
	var pos;
	for (i=1;i<=5;i++) {
		pos = $('#pos_'+i);
		positions[i] = new Object();
		positions[i].left = pos.css('left');
		positions[i].top = pos.css('top');
		positions[i].zIndex = pos.css('zIndex');
		positions[i].width = pos.css('width');
		positions[i].height = pos.css('height');
		positions[i].opacity = pos.css('opacity');
	}
	
	function rotate(direction,speed) {
		//alert('rotating');
		var increment = (direction=='left')?(-1):(+1);
		var pos;
		var newId;
		var newPos;
		setTimeout(function() {
			$('div.pos').each(function() {
				pos = parseInt($(this).attr('id').substr(4));
				/*newPos = pos+increment;
				if (newPos == 0)
					newPos = 5;
				else if (newPos == 6)
					newPos = 1;*/
				$(this).css('z-index',positions[pos].zIndex);
			});
		}, speed/2);
		$('div.pos').each(function() {
			pos = parseInt($(this).attr('id').substr(4));
			newPos = pos+increment;
			if (newPos == 0)
				newPos = 5;
			else if (newPos == 6)
				newPos = 1;
			//alert(newPos);
			var currPos = new Object();
			var posDiv = $(this);
			currPos.left = posDiv.css('left');
			currPos.top = posDiv.css('top');
			//currPos.zIndex = posDiv.css('zIndex');
			currPos.width = posDiv.css('width');
			currPos.height = posDiv.css('height');
			currPos.opacity = posDiv.css('opacity');
			var posRelative = new Object();
			posRelative.left = positions[newPos].left;
			posRelative.top = positions[newPos].top;
			//posRelative.zIndex = positions[newPos].zIndex;
			posRelative.width = positions[newPos].width;
			posRelative.height = positions[newPos].height;
			posRelative.opacity = positions[newPos].opacity;
			//alert(posRelative.left);
			posRelative.left = '+='+(parseInt(posRelative.left) - parseInt(currPos.left)+'px');
			posRelative.top = '+='+(parseInt(posRelative.top) - parseInt(currPos.top)+'px');
			//posRelative.zIndex = '+='+(parseInt(posRelative.zIndex) - parseInt(currPos.zIndex));
			posRelative.width = '+='+(parseInt(posRelative.width) - parseInt(currPos.width)+'px');
			posRelative.height = '+='+(parseInt(posRelative.height) - parseInt(currPos.height)+'px');
			posRelative.opacity = '+='+(parseFloat(posRelative.opacity) - parseFloat(currPos.opacity));
			//alert(posRelative.zIndex);
			$(this).animate(posRelative, { queue:false, duration:speed });
			/*setTimeout(function() {
				posDiv.css('z-index',positions[newPos].zIndex);
			}, speed/2);*/
			//$(this).animate(positions[newPos], { queue:true, duration:speed });
			//alert(steps);
			/*$(this).animate(positions[newPos], 700, 'linear', function() {
				if (steps>1 && newPos==3)
					rotate(direction,steps-1);
			});*/
			//alert(newId);
			$(this).attr('id','pos_'+(newPos));
		});
		/*setTimeout(function() {
			$('div.pos').each(function() {
				pos = parseInt($(this).attr('id').substr(4));
				//alert(pos);
				newPos = pos+increment;
				if (newPos == 0)
					newPos = 5;
				else if (newPos == 6)
					newPos = 1;
				$(this).attr('id','pos_'+(newPos));
			});
		}, speed);*/
	}
	
	imgArray = $('div.fade img');	
	var startingImgIndex = Math.floor(Math.random()*imgArray.length);
	$(imgArray[startingImgIndex]).show();
	
	function changeHighlight() {
		//alert('changing highlight');
		var selectedLine = $('ul#lines li.selected').removeClass('selected');
		//alert(parseInt(selectedLine.attr('id').substr(5))+1);
		//alert($('li#line_'+(parseInt(selectedLine.attr('id').substr(5))+1)).attr('id'));
		var oldId = parseInt(selectedLine.attr('id').substr(5))
		var newId = oldId+1;
		if (newId==0)
			newId=5;
		else if (newId==6)
			newId=1;
		$('li#line_'+newId).addClass('selected');
		$('#screenshot_image_'+oldId).fadeOut('fast',function() {
			$('#screenshot_image_'+newId).fadeIn('fast');
		});
	}
	
	function changeFiles() {
		//alert(imgArray);
		//alert('changing file');
		var indexToShow = Math.floor(Math.random()*imgArray.length);
		while ($(imgArray[indexToShow]).is(':visible'))
			indexToShow = Math.floor(Math.random()*imgArray.length);
		$('div.fade img:visible').fadeOut('slow', function() {
			$(imgArray[indexToShow]).fadeIn('slow');
		});
	}
	
	$('div.pos').click(function() {
		thisId = $(this).attr('id');
		//if (thisId!='pos_3' && $('#'+thisId+':animated').attr('id')!=thisId) {
		if (thisId!='pos_3' && !$(this).is(':animated')) {
			//meni - rotacija
			var pos = parseInt(thisId.substr(4));
			var timeout = 0;
			/*if (pos!=1 || pos!=5)
				setTimeout(function() {
					$('div.pos').each(function() {
						//alert('stopping');
						($this).stop();
					});
				}, speed/2-100);*/
			while (pos!=3) {
				//alert(timeout);
				if (pos>3) {
					setTimeout(function() {
						rotate('left',speed/(pos-3));
					}, timeout);
					pos--;
				} else if (pos<3) {
					setTimeout(function() {
						rotate('right',speed/(3-pos));
					}, timeout);
					pos++;
				}
				timeout+=speed/2;
			}
			//slide/fade out
			pos = parseInt(thisId.substr(4));
			var moveTo = (pos>3)?960:-960;
			var divToSlideOut = $('div.slide:visible');
			divToSlideOut.animate({ 'left':-moveTo,'opacity':'hide' },{ duration:slideSpeed });
			if (divToSlideOut.attr('id')=='slide_3')
				clearInterval(intervalId);
			else if (divToSlideOut.attr('id')=='slide_2')
				clearInterval(filesIntervalId);
			var titleSpan = $('span.slide');
			var newTitle = $(this).find('img').attr('alt');
			titleSpan.fadeOut(slideSpeed/2, function() {
				titleSpan.html(newTitle);
			});
			//slide/fade in
			var slide = $('#slide_'+$(this).find('img').attr('name').substr(5));
			slide.css('left',moveTo).show().animate({ 'left':'0'},{ duration:slideSpeed });
			if (slide.attr('id')=='slide_3') {
				$('ul#lines li.selected').removeClass('selected');
				$('#line_1').addClass('selected');
				intervalId = setInterval(changeHighlight,interval);
			}
			else if (slide.attr('id')=='slide_2') {
				filesIntervalId = setInterval(changeFiles,filesSpeed);
			}
			titleSpan.fadeIn(slideSpeed);
		}
	});
	
	var tooltip = $('#tooltip');
	$('div.pos').hover(function() {
		var posDiv = $(this);
		tooltip.stop();
		tooltip.html(posDiv.find('img').attr('alt')).css('opacity',0).show().fadeTo('fast', 1.00);
	}, function() {
		tooltip.stop();
		tooltip.fadeOut('fast', function() {
			tooltip.html('&nbsp;');
		});
	});
	
	$('ul#lines li').click(function() {
		var selectedLine = $('ul#lines li.selected');
		lineId = $(this).attr('id');
		if (selectedLine.attr('id')!=lineId) {
			selectedLine.removeClass('selected');
			$(this).addClass('selected');
			$('div.screenshot img:visible').fadeOut('fast',function() {
				$('#screenshot_image_'+lineId.substr(5)).fadeIn('fast');
			});
			clearInterval(intervalId);
			intervalId = setInterval(changeHighlight,interval);
		}
	});
	
	var intervalId = setInterval(changeHighlight,interval);
	
	function fadeSpotlightIn(selector) {
		$(selector).fadeTo('slow',1,function() {
			fadeSpotlightOut(selector);
		});
	}
	
	function fadeSpotlightOut(selector) {
		$(selector).fadeTo('slow',0,function() {
			fadeSpotlightIn(selector);
		});
	}
	
	/*function resetSpotlight(spotlight,opacity) {
		spotlight.stop();
		spotlight.css('opacity',opacity);
	}*/
	
	fadeSpotlightOut($(this).find('img#spotlight'));
	fadeSpotlightIn($(this).find('img#spotlight2'));
	
	/*$('div.action').hover(
		function() {
			fadeSpotlightOut($(this).find('img#spotlight'));
			fadeSpotlightIn($(this).find('img#spotlight2'));
		},
		function() {
			resetSpotlight($(this).find('img#spotlight'),1);
			resetSpotlight($(this).find('img#spotlight2'),0);
		}
	);*/











	$('.bubbleInfo').each(function () {
		var distance = 10;
		var time = 250;
		var hideDelay = 500;
		
		var hideDelayTimer = null;
		
		var beingShown = false;
		var shown = false;
		var trigger = $('.trigger', this);
		var info = $('.popup', this).css('opacity', 0);
		
		
		$([trigger.get(0), info.get(0)]).mouseover(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			if (beingShown || shown) {
				// don't trigger the animation again
				return;
			} else {
				// reset position of info box
				beingShown = true;
				
				info.css({
					top: -60,
					left: 0,
					display: 'block'
				}).animate({
					top: '-=' + distance + 'px',
					opacity: 1
				}, time, 'swing', function() {
					beingShown = false;
					shown = true;
				});
			}
				
			return false;
		}).mouseout(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				info.animate({
					top: '-=' + distance + 'px',
					opacity: 0
				}, time, 'swing', function () {
					shown = false;
					info.css('display', 'none');
				});
				
			}, hideDelay);
			
			return false;
		});
	});
















});