$(document).ready(function(){
    initLayout();
    animateLines();
    
    $(window).resize(function() { 
        initLayout();
    });
	/*$("h1").click(function(){
		//document.location.href = "/#/home.html";
		var url = $(this).attr('href').replace('/start.html', '');
		load_Content('./#/home.html');
	});*/
	$('#refcontainer .thumbs a').lightBox();
	
    // Event handlers
     $.address.init(function(event) {
        $('nav a[target!="_blank"], h1 a').address(function() {
        	var url = $(this).attr('href').replace(location.pathname, '');
        	var urlWoD = url.replace('./', '');
            return urlWoD;
        });
     }).change(function(event) {
        load_Content('.'+event.value);
     });
     //setInterval('animateNavButtonA()', 2000);
     //setInterval("animateNavButtonB()",2500);
     pReady();
     playBgVideo();
});

var nextBgA = '-76px -11px';
function animateNavButtonA(){
	
	$(".slogan a.bild").animate({
		backgroundPosition: nextBgA
	}, 800, function(){
		if(nextBgA != '-76px 0'){
			nextBgA = '-76px 0';
		}else{
			nextBgA = '-76px -11px';
		}
	});
	
	/*$(".slogan a.bild").clearQueue();
	$(".slogan a.bild").addClass('active').delay(500).queue(function(){
		$(this).removeClass('active');
	});*/
}

var nextBgB = '-137px -11px';
function animateNavButtonB(){
	$(".slogan a.ton").animate({
		backgroundPosition: nextBgB
	}, 800, function(){
		if(nextBgB != '-137px 0'){
			nextBgB = '-137px 0';
		}else{
			nextBgB = '-137px -11px';
		}
	});
	/*
	$(".slogan a.ton").clearQueue();
	$(".slogan a.ton").addClass('active').delay(500).queue(function(){
		$(this).removeClass('active');
	});*/
}

/*
function animateNavButtonB(){
	$(".slogan a.ton").fadeOut(1500, function () {
        $(this).addClass("active").fadeIn(1500, function () {
            $(this).removeClass("active");
            alert('remove');
            //animateNavButtonB();
        });
    });
}*/

function clickFirst(){
	$("#refcontainer .thumbs a:first-child").click();
}

function initLayout(){
    var browserW = $("body").width();
    var pageOffset = $("#page").offset();
    var rFadeoutW = browserW - pageOffset.left - $("#wrap").width();
    $("#klammer_lu div.atpage").css("left", pageOffset.left-255);
    $("#klammer_ro div.atpage").css("left", pageOffset.left+1100);
    $("#klammer_ro div.atpage").css("width", pageOffset.left+100);
}

function playBgVideo(){
	playVideo('', false, true);
}

function playVideo(video, hideelements, playlist){
	if(hideelements){
		$('#content').fadeOut();
		$('nav').fadeOut();
		$('#sidebar').fadeOut();
		$('#topline .img').animate({
	        width: '0'
	    }, 1000, function(){
	        $(".kontakt.imglink").animate({
	            opacity: 0
	        }, 1000);
			$('#bgvideonetz').fadeOut();
			$('#page').append('<div id="close_bg"></div>');
			jwplayer('bgvideo').setup({
			    'flashplayer': '/js/player.swf',
   			    'skin': '/js/glow.zip',
			    'file': video,
			    'autostart': 'true',
			    'repeat': 'always',
			    'controlbar': 'over',
			    'width': '1200',
			    'height': '678'
			  });
			$('#close_bg').click(function(){
				$('#bgvideonetz').show();
				animateLines();
				if($('#bgvideo_wrapper').length > 0){
					$('#bgvideo_wrapper').empty();
					$('#bgvideo_wrapper').attr('id', 'bgvideo');
				}else{
					$('#bgvideo').empty();
				}
				$('#content').fadeIn();
				$('nav').fadeIn();
				$('#sidebar').fadeIn();
				playBgVideo();
				$(this).remove();
			});
	    });
		    
	    $('#bottomline').animate({
		        width: '0'
	    }, 1000, function(){
		        $("#content").animate({
		            opacity: 0
		        }, 1000);
	    });
	}else if(playlist){
		jwplayer('bgvideo').setup({
		    'flashplayer': '/js/player.swf',
		    'playlistfile': '/js/playlist.xml',
		    'skin': '/js/glow.zip',
		    'controlbar': 'none',
		    'dock': 'false',
		    'autostart': 'true',
		    'icons': 'false',
		    'repeat': 'list',
		    'stretching': 'fill',
		    'width': '1200',
		    'height': '678'
		  });
	}else{
		jwplayer('bgvideo').setup({
		    'flashplayer': '/js/player.swf',
		    'skin': '/js/glow.zip',
		    'file': video,
		    'autostart': 'true',
		    'repeat': 'always',
		    'controlbar': 'over',
		    'width': '1200',
		    'height': '678'
		  });
	}
}

function pReady(){
    $(' .thumbs a').lightBox();
    
    if($('.portfolios').length > 0){
    	$('.portfolios .portfolio').click(function(){
    		playVideo($(this).data('background'), true);
    	})
    }
    //$("#asterisk").append('<a name="fb_share" type="button" href="http://www.facebook.com/sharer.php">Teilen</a><script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script>');
    //FB.Share.renderAll();
}
                
function animateLines(){
    $('#topline .img').animate({
        width: '634px'
    }, 1000, function(){
        $(".kontakt.imglink").animate({
            opacity: 1
        }, 1000);
    });
    
    $('#bottomline').animate({
        width: '865px'
    }, 1000, function(){
        $("#content").animate({
            opacity: 1
        }, 1000);
    });
}

function load_Content(content){
    $.ajax({
        url: content,
        cache: false,
        dataType: 'json',
        success: function(html){
            $content = $("#content .textcontainer");
            $content.hide();
            $content.html(html.content);
            $content.slideDown("fast");
            setTitleAndMenu(html);
            pReady();
        }
    });   
}

String.prototype.startsWith = function (str){
    return this.indexOf(str) == 0;
};


function setTitleAndMenu(html){
    document.title = html.title;
    $('nav li.active').removeClass('active');
    $('nav li.'+html.current).addClass('active');
    if(html.current.startsWith("portfolio")){
    	$('nav li.portfolio').addClass('active');
    }
    /*switch (content.current) {
    case "home": 			title = "Werbefilme  - Filmmusik  - Corporate Sound - ton99";
    			 			break;
 
    case "kommunikation": 	title = "Werbung - Kommunikation - Design - ton99";
                     		break;
 
    case "bild": 			title = "Werbefilme - Unternehmensfilme - Produktfilme - ton99";
    						break;
 
    case "ton": 			title = "Filmmusik - Soundtracks - Corporate Sound - ton99";
                       		break;
 
    case "kontakt": 		title = "Kontakt - ton99";
                    		break;
                    		
    case "impressum":		title = "Impressum - ton99";
                    		break;
 
    default: 				title = "Werbefilme  - Filmmusik  - Corporate Sound - ton99";
  }*/
}



