// VisioScroller
jQuery.fn.VScroller = function() {

	var obj = $(this);
	
	
	var picO = $(this).find('#scroll li');

	var numItems = $(picO).size();
	var wi = $(picO).css('width').replace('px', '');
	var he = $(picO).css('height').replace('px', '');
	var padd = Number($(picO).css('paddingLeft').replace('px', '')) + Number($(picO).css('paddingRight').replace('px', ''));
	var marg = Number($(picO).css('marginLeft').replace('px', '')) + Number($(picO).css('marginRight').replace('px', ''));
	
	var picWidth = Number(wi) + Number(padd) + Number(marg);
	var newWidth = picWidth * numItems;
	
	var currPos = 0;

	$(this).find('#scroll').css('width', newWidth +'px');
	
	$(this).find('.arrow.l').click(function(){
		
		if (currPos >= 0) return;
		
		currPos += picWidth;
		
		$(obj).find('#scroll').animate({
			left: '+=' + picWidth
		  }, 500, function() {
			// Animation complete.
		  });
	});
	$(this).find('.arrow.r').click(function(){

		if (currPos <= (-newWidth + Number($(obj).find('#scrollArea').css('width').replace('px', ''))) ) return;
		
		
		currPos -= picWidth;
		
		$(obj).find('#scroll').animate({
			left: '-=' + picWidth
		  }, 500, function() {
			// Animation complete.
		  });
	});

};


jQuery.fn.ImageRotator = function() {

	var is_interval_running = true;
	var obj = $(this);
	var curr = 0;
	var length = $(obj).find('img').length;

	if (length == 1) return;
	
	$(window).focus(function () {
		is_interval_running = true;
    }).blur(function () {
        is_interval_running = false;
    });

	
	$(obj).find('img').css({position: 'absolute', display: 'none', top: '0px', right: '0px'});
	$(obj).find('img:eq(0)').css({display: 'block'});
	$(obj).css({position: 'relative'});

	$(obj).bind('slide', function(){
		if (!is_interval_running) return;
		if ($(obj).find('img:eq(' + curr + ')').length == 1)
		{
			$(obj).find('img:eq(' + curr + ')').fadeOut();
			curr++;
			if (curr == length) curr = 0;
			
			$(obj).find('img:eq(' + curr + ')').fadeIn();
		}
	});

	var inter = setInterval(function(){ $(obj).trigger('slide'); }, 4000);

};

jQuery.fn.bubble = function(text) {

	$(this).each(function(){
		
		var obj = $(this);
		var position = $(this).position();
	
		var newDate = new Date;
		var id = newDate.getTime();
		
		position.top -= 94;
	
	
		$(this).after('<div id="bubble_' + id + '" class="bubble" style="left:' + position.left + 'px; top:' + position.top + 'px;">' + text + '</div>');
		
		$(this).hover(function(){ $('#bubble_' + id).show(); }, function(){ $('#bubble_' + id).stop().hide(); });
	});
};

var bubbleClicked = false;
jQuery.fn.bubbleWin = function() {

	$(this).each(function(){
		var name = $(this).attr('name');
		var win = $('.container[name=' + name + ']');

		$(win).addClass('bubbleWin_win');
		$(win).css({position: 'absolute', zIndex: 10000});
		$(win).prepend('<div class="bubbleArrow top"></div>').hide();
		if (!$(win).hasClass('noOpacity')) $(win).css('opacity', 0.8);
		
		$(win).click(function(e){ bubbleClicked = true; });
		
		$(this).click(function(e){
			e.stopPropagation();
			ret = false;
			if ($(win).is(':visible')) ret = true;
			
			$('.bubbleWin.selected').each(function(){
							
				$(this).removeClass('selected');
				$('.container[name=' + $(this).attr('name') + ']').hide();
				return;
			});
			
			if (ret) return;
			
			
			$(this).addClass('selected');
			
			$(win).show();
			pos = $(win).offset();
			pos2 = $(this).offset();

			if ((pos.top - $(this).width()) > pos2.top)
			{
				plc = ($(this).width() / 2) - (25 / 2);
				$('.bubbleArrow', win).addClass('top').offset({left: (pos2.left + plc)});
			}
			else if ((pos.top + $(win).height()) < pos2.top)
			{
				plc = ($(this).width() / 2) - (25 / 2);
				$('.bubbleArrow', win).addClass('bot').offset({left: (pos2.left + plc)});
			}
			else if (pos.left > pos2.left)
			{
				plc = ($(this).height() / 2) - (25 / 2);
				$('.bubbleArrow', win).addClass('l').offset({top: (pos2.top + plc)});
			}
			else
			{
				plc = ($(this).height() / 2) - (25 / 2);
				$('.bubbleArrow', win).addClass('r').offset({top:(pos2.top + plc)});
			}
			
		});
	});
};

function CloseBubble()
{
	$('.bubbleWin.selected').each(function(){
							
		$(this).removeClass('selected');
		$('.container[name=' + $(this).attr('name') + ']').hide();
		return;
	});
}

$(document).ready(function(){

	$.ajaxSetup({ cache: false });

	var $_GET = [];

	document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () {
		function decode(s) {
			return decodeURIComponent(s.split("+").join(" "));
		}
	
		$_GET[decode(arguments[1])] = decode(arguments[2]);
	});
	
	$('.fancy').live('click', function(e){
		var obj = $(this);
		var url = $(this).attr('href');
		var title = ((title == undefined || title == "") ? $(this).attr('title') : title);
		var footer = ((footer == undefined || footer == "") ? $(this).attr('footer') : footer);

		$.MyFancy(url, null, title, null, footer);
		return false;
	});

	
	$("img").livequery('error', function () {
	  $(this).unbind("error").attr("src", "Templates/Ilusion/images/px.png");
	});
	
	$(document).click(function (event) {
		if(bubbleClicked) { bubbleClicked=false;}
		else CloseBubble();
	});
	
	
	$('a[rel=blank]').attr('target', '_blank');
	
	$('.bubbleWin').bubbleWin();

	$('#pics .inner').ImageRotator();
	
});
