$(document).ready(function(){

	// init soundmanager
	soundManager.debugMode = false; // disable debug output
	soundManager.url = 'flash/soundmanager2.swf'; // path to movie

	soundManager.onload = function(){
		soundManager.createSound('loading','sounds/computer_prompt.mp3');
		soundManager.createSound('click','sounds/click_sound.mp3');
	};		
			
	var div_top = document.createElement('div');
	$(div_top).attr('id', 'top').addClass("left-float");
	var left_of_handler = document.createElement('div');
	$(left_of_handler).attr('id', 'left-of-handler').addClass("left-float");
	var handler = document.createElement('div');
	$(handler).attr('id', 'handler').addClass("left-float").mouseover(function(){$(this).css('cursor', 'move');});
	var right_of_handler = document.createElement('div');
	$(right_of_handler).attr('id', 'right-of-handler').addClass("left-float");
	var left_menu = document.createElement('div');
	$(left_menu).attr('id', 'left-menu').addClass("left-float");
	
	$('#window').prepend(left_menu).prepend(right_of_handler).prepend(handler).prepend(left_of_handler).prepend(div_top);
	
	var right_menu = document.createElement('div');
	$(right_menu).attr('id', 'right-menu').addClass("left-float");
	var left = document.createElement('div');
	$(left).attr('id', 'left').addClass("left-float").height($('#content').outerHeight());
	
	$('#menu').after(left).after(right_menu);
	
	var right = document.createElement('div');
	$(right).attr('id', 'right').addClass("left-float").height($('#content').outerHeight());
	var bottom = document.createElement('div');
	$(bottom).attr('id', 'bottom').addClass("left-float");
	
	$('#content').after(bottom).after(right);
	
	//var despre_noi_poza = $('<img src="img/despre-noi-bg.jpg">');
	//$('#p_despre_noi>h2').after(despre_noi_poza).hide();
	
	var first_height = $("#p_despre_noi").outerHeight();
	
	if($.browser.msie) {
	
		$("#left, #right").remove();
		
		var cur = $("#window");
		var shadow = $('<div id="ie-shadow"></div>'); cur.after(shadow);
		
		//Figure the base height and width
		var baseWidth = cur.outerWidth() - 18;
		//var baseHeight = $("#p_despre_noi").outerHeight() - 48;
		var baseHeight = 1200;
		
		//get the offset
		var position = cur.position();
		var stack = 0;
		//Determine the stack order (attention: the zIndex will get one higher!)
		if(!cur.css("zIndex") || cur.css("zIndex") == "auto") {
			//var stack = 0;
			cur.css("position", (cur.css("position") == "static" ? "relative" : cur.css("position"))).css("z-index", "1");
		} else {
			stack = parseInt(cur.css("zIndex"));
			cur.css("zIndex", stack + 1);
		}
		
		//Copy the original z-index and position to the clone
		//alert(shadow); If you insert this alert, opera will time correctly!!
		shadow.css({
			position: "absolute",
			display: "block",	
			zIndex: stack,
			left: position.left + 4,
			top: position.top + 60,
			width: baseWidth,
			height: baseHeight,
			marginLeft: cur.css("marginLeft"),
			marginRight: cur.css("marginRight"),
			marginBottom: cur.css("marginBottom"),
			marginTop: cur.css("marginTop")
		   ,filter: "progid:DXImageTransform.Microsoft.alpha(opacity=60) progid:DXImageTransform.Microsoft.Blur(pixelradius=5)"
		});
	}
	
	// draggable stuff
	//$("#handler").mouseover(function(){$(this).css('cursor', 'move')});
	
	$("#window").draggable({
		zIndex: 20, 
		ghosting: false, 
		opacity: 0.6,
		revert: false,
		handle: $('#handler'),
		start: function(e, ui) {
			$('#ie-shadow').css({
				visibility: 'hidden'
			});
		},	
		drag: function(e, ui) {
			//fuckin' sure
		},
		stop: function(e, ui) {
			$("#window").animate({top: '80', left: '400'}, {
				easing: 'easeOutBounce', 
				duration: 1200, 
				complete: function() {
					$('#ie-shadow').css({
						display: 'block',
						visibility: 'visible',
						left: $("#window").position().left + 4, 
						top: $("#window").position().top + 60
					});
				}
			});
		}
	});
	
	// fix the pngs
	$('img[@src$=.png], #top, #left-of-handler, #right-of-handler, #bottom, #right-menu, #left-menu, .contact-info, .nice_bottom').ifixpng();
	
	// hide all divs, except the first one ( in that case: #despre_noi )
	$("#content")
		.find("div.data_a")
			.hide()
			.filter("#p_despre_noi")
				.height(first_height)
				.addClass("current")
				.show()
			.end()
		.end();
	
	// helper function for smooth effect	
	var setHeight = function(prev, next) {
		//var hideHeight = prev.outerHeight(), showHeight = next.outerHeight(), difference = showHeight - hideHeight;
		//console.info(prev.outerHeight());
		$('#content, #right, #left, #ie-shadow').animate({ height: next.outerHeight() + 50 }, { duration: 800 });
		soundManager.play('loading');
		prev.fadeOut(100).removeClass("current");
		next.fadeIn(900).addClass("current");
		
		return next;
	};

	// on menu items click, change the content
	$("#menu")
		.find("ul>li>a.ir")
			.click(function(e){
				$($.find("div.data_a"))
					.hide()
					.filter("div#p_" + this.id)
						setHeight($(".current"), $("div#p_" + this.id))
					.end();
				e.preventDefault()
			})
			.mouseover(function(e){
				soundManager.play('click');
			})
		.end();
		
	$("#iqnet, #srac")
		.hover(function(e){
			$(this).animate({opacity: '1'}, {duration: 600});
		},
			function(e) {
			$(this).animate({opacity: '.6'}, {duration: 600});	
		})
		.end();
		
	$("#whomadewho>a")
		.hover(function(){
			$("#euromed-tooltip").fadeIn(500);
		}, function(){
			$("#euromed-tooltip").fadeOut(500);
		})
		.end();
	
	$(".contact-info").each(function(i){
		$(this)
			.css({zIndex:++i})
			.parent().css({top: 10*(i + 1) + (70 * i)}).end()
			.hover(function(e){
				$(this).animate({top: 0}, {duration: 400});
			}, function(e) {
				$(this).animate({top: 60}, {duration: 400});
			})
			.next().css({zIndex:i+1})
		.end()
	});
	//.end();
		
	$.ImageBox.init(
			{
				loaderSRC: 'img/loading.gif',
				closeHTML: '<img src="img/close.jpg" />'
			}
		);	
});