/**
 * @author Bilge Hauser
 * @date 12.05.2010
 * @company hier.com
 */

 
$(document).ready(function () {
	jQuery.fn.center = function () {
	    this.css("position","absolute");
	    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
	    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
	    return this;
	}
	// create custom animation algorithm for jQuery called "bouncy"
	$.easing.bouncy = function (x, t, b, c, d) {
	    var s = 1.70158;
	    if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
	    return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
	}
	
	// create custom tooltip effect for jQuery Tooltip
	$.tools.tooltip.addEffect("bouncy",
	
		// opening animation
		function(done) {
			this.getTip().animate({top: '+=15'}, 500, 'bouncy', done).show();
		},
	
		// closing animation
		function(done) {
			this.getTip().animate({top: '-=15'}, 500, 'bouncy', function()  {
				$(this).hide();
				done.call();
			});
		}
	);
	$('#page').center();
	$('#loader').center();
    $('#backround').css("position","absolute");
    $('#backround').css("left", "0px");
    $('#backround').css("top", "0px");

    $('#rahmen').css("position","absolute");
    $('#rahmen').css("left", "0px");
    $('#rahmen').css("top", "0px");

    $('#faustballon').css("position","absolute");
	$('#faustballon').css("left", "0px");
    $('#faustballon').css("top", "300px");

    $('#fliegender').css("position","absolute");
	$('#fliegender').css("display", "none");

    $('#schildtraeger').css("position","absolute");
    $('#schildtraeger').css("left", "135px");
    $('#schildtraeger').css("top", "400px");

    $('#posaunist').css("position","absolute");
    $('#posaunist').css("left", "195px");
    $('#posaunist').css("top", "390px");

    $('#trommler').css("position","absolute");
    $('#trommler').css("left", "260px");
    $('#trommler').css("top", "410px");

    $('#faustballon').css("position","absolute");
    $('#faustballon').css("left", "15px");
    $('#faustballon').css("top", "150px");

    $('#totenkopf').css("position","absolute");
    $('#totenkopf').css("left", "390px");
    $('#totenkopf').css("top", "420px");
	$('#totenkopf').css("display", "none");

    $('#clown01').css("position","absolute");
    $('#clown01').css("left", "450px");
    $('#clown01').css("top", "150px");

    $('#clown02').css("position","absolute");
    $('#clown02').css("left", "250px");
    $('#clown02').css("top", "-103px");

    $('#zoe').css("position","absolute");
    $('#zoe').css("left", "515px");
    $('#zoe').css("top", "600px");
	start_page();
    /*
	$('#loader').loader({
		'wrapper': $('#page'),
		'container': $('#page'),
	 	'loadingClass': 'loading',
		'callback': function(){
			start_page();
		}
	});
	*/
});

function start_page(){
    fliegender();
	animateBaloonDown();
	skull();
	animateZoeUp();
    $('#zoe_image').maphilight();
	if ($.browser.mozilla) {
		$('[title]').tooltip({
			effect: 'bouncy'
		});
	}else{
		$('[title]').tooltip({
			offset: [300, 600],
			effect: 'bouncy'
		});
	}
	soundManager.onload = function() {soundManager.play('squealer','./sounds/squealer(master).mp3');}
}


$(window).resize(function() { 
	$('#page').center();
	$('#fliegender').css("display", "none");
});

function fliegender(){
	var left = ($(window).width() - $('#page').width() ) / 2+$(window).scrollLeft();
	var top = ($(window).height() - $('#page').height() ) / 2+$(window).scrollTop();
    $('#fliegender').css("left", (left+620)+"px");
    $('#fliegender').css("top", "-60px");
	$('#fliegender').fadeIn(3000);
	$('#fliegender').animate({
		top: '+='+(300+top),
    	left: '+='+100,
    	height: 'toggle',
		width: 'toggle'
  		}, 
		5000, 
		function() {
    		fliegender();
  	});
}

function animateBaloonDown(){
	$('#faustballon').animate({
		top: '+=200'
  		}, 
		5000, 
		function() {
    		animateBaloonUp();
  	});
}

function animateBaloonUp(){
	$('#faustballon').animate({
		top: '-=200'
  		}, 
		5000, 
		function() {
    		animateBaloonDown();
  	});
}

function skull(){
	$('#totenkopf').animate({opacity: "toggle"}, 6000, skull);
}

function animateZoeUp(){
	$('#zoe').animate({
		top: '-=376'
  		}, 
		5000);
}


function openDialog(title, action, top, left, width, height){
	// loveClicked penClicked cardClicked carClicked flowerClicked
	var leftMargin = ($(window).width() - $('#page').width() ) / 2+$(window).scrollLeft();
	var topMargin = ($(window).height() - $('#page').height() ) / 2+$(window).scrollTop();
	$('#galleryDialog').html('');
	$('#galleryDialog').dialog({
		resizable: false,
		closeOnEscape: true,
		width: width,
		height: height,
		title: title,
		modal:true,
		show: 'clip',
		hide: 'clip',
		position:[left+leftMargin,top+topMargin]
  	});
    if (action == 'loveClicked') {
    	$('#galleryDialog').html('<div id="gallery"></div>');
		$("#gallery").gallery({json:"callback.php?dir=circus1&cmd=list",sw:60,sh:100,pw:570,ph:400,cw:300,mode:1,easing:"easeOutBounce"});
	}else if (action == 'penClicked') {
		$.ajax({
			url: 'pen.txt',
			success: function(data){
				$('#galleryDialog').html(data);
			}
		});
	}else if (action == 'cardClicked') {
		$.ajax({
			url: 'card.txt',
			success: function(data){
				$('#galleryDialog').html(data);
			}
		});
	}else if (action == 'flowerClicked') {
		$.ajax({
			url: 'flower.txt',
			success: function(data){
				$('#galleryDialog').html(data);
			}
		});
	}else if (action == 'carClicked') {
		$.ajax({
			url: 'car.txt',
			success: function(data){
				$('#galleryDialog').html(data);
			}
		});
	}
}
