var drag = false;
var connector;
var flashka;

if (undefined === Common) {
	var Common = {};
}

Common.Measurer = (function() {
	var funcs = {};

	var interval = 500;

	var genId =  1;

	var curHeight;

	var el;

	var isInit = false;

	var isDocReady = false;

	$(function() {
		isDocReady = true;
		isInit && initBlock();
	});

	function initBlock() {
		el = $("#measurer");
		curHeight = el.height();


		setInterval(function() {
			checkScale();
		}, interval);
		$(window).resize(callFuncs);
	}

	function checkScale() {
		var newHeight = el.height();

		if (newHeight != curHeight) {
			curHeight = newHeight;
			callFuncs();
		}
	}

	function callFuncs() {
		for (var func in funcs) {
			funcs[func]();
		}
	}

	return {
		setFunc: function(name, func) {
			if (!$.isFunction(name) && !$.isFunction(func)) {
				funcs[name] && (delete funcs[name]);

				return;
			}

			isInit = true;
			isDocReady && initBlock();

			if ($.isFunction(name)) {
				funcs[genId.toString()] = name;
				genId++;
			} else {
				funcs[name] = func;
			}
		}
	};
})();



function flashIsReady(){
	var flash = $(".flash");
	flashka = $("#gallery");
	try{
		$(flashka)[0].callMe('xyFlash', $(flash).offset().left, $(flash).offset().top);
		if($.browser.opera){
			connector = new SWFConnector("/f/1/SWFConnector.swf","connector");
			connector.addCommand('134');
		    connector.sendCommands();
		}
	}
	catch(e){
		alert(e);
	}
}

function mouseIsDown(){
	drag = true;
}

$( function() {
   var back = $("#background");

   $(back).height($("#outer").height()).show();

   Common.Measurer.setFunc(
		function(){
			$(back).height($("#outer").height());
		}
	);

$( function() {
   var back = $("#background1");

   $(back).height($("#outer").height()).show();

   Common.Measurer.setFunc(
		function(){
			$(back).height($("#outer").height());
		})
			});


/*   var faces_arr = [];
   $(".faces img").each(
   	function(i){
   		faces_arr.push(this);
   	}
   );
   for(var i=0;i<3;i++){
		var randomnumber = Math.floor(Math.random()*12);
		while($(faces_arr[randomnumber]).css("display") != 'none'){
			var randomnumber = Math.floor(Math.random()*12);
		}
		$(faces_arr[randomnumber]).show();
   }*/

   if(!$.browser.opera){
	   $(document).mousemove(
			function(e){
				if (drag){
					$(flashka)[0].callMe('xy',e.pageX,e.pageY);
				}
			}
		);
   }
   $(document).mouseup(
		function(e){
			if (drag){
				if($.browser.opera){
					try{
						connector = new SWFConnector("/f/1/SWFConnector.swf","connector");
						connector.addCommand('134',e.pageX, e.pageY);
	        			connector.sendCommands();
					}
					catch(e){
						alert(e);
					}
				}
				else{
					$(flashka)[0].flashMouseUp();
				}
				drag = false;
			}
		}
	);
}
);


/* toool tip*/
 $(function () {
        $('.bubbleInfo').each(function () {
            var distance = 0;
            var time = 100;
            var hideDelay = 50;

            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: -225,
                        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;
            });
        });
    });

/* tooltip end */