var allplayers = [];

function highlightBox(whichOne){
		//alert(whichOne);
		var that = $('#'+whichOne);
		if($(that).hasClass("active")) return;


var cacheDivs = {
		'd1': '100',
		'd2': '200',
		'd3': '400',
		'd4': '300'
	};


if($('#mv_logo').is(":visible")){} else{$('#mv_logo').fadeIn(2000);}
var tempZ = $(that).css('z-index');
if (whichOne == 'd3'){
 $('.left_header').hide();
}
else{
 $('.left_header').show();
}
$(that).css({'z-index':600});
$("div.active").css({'z-index':cacheDivs[$("div.active").attr('id')]}).removeClass("active");

var actualHash = window.location.hash;
var actualHashArr = actualHash.split("?");
actualHash  = actualHashArr[0].replace( /^#/, '' ); 


var refdiv="";
if( actualHashArr[1]){

var refparams = actualHashArr[1].split("=");
refdiv = refparams[1];


	
}


//alert(refdiv);


var anidelay = 0;

if(!refdiv && actualHash != '/Pages/Mediavent/Referenzen.php' ) anidelay = 1000;


$(that).switchClass("","active",anidelay,function(){







 $(that).css({'z-index':500});

if (whichOne == 'd2'){
 if (jQuery.browser.msie && jQuery.browser.version <= 8) {
   $('#d2').uncorner();
   $('#d2').corner("20px cc:#CBCFC1");

$('#d3').uncorner();
$('#d3').corner("tl bl br 20px cc:#CBCFC1").corner("tr 20px cc:#C47E7E");
	$('#d3 .box_header').corner("tr tl 20px cc:#CBCFC1");
	$('.left_header').corner("tl 20px cc:#CBCFC1");


if (jQuery.browser.msie && jQuery.browser.version <= 7) {

$('.left_header').addClass('roundedie').corner("tr 20px cc:#CBCFC1");

}	
 }
}
else if (whichOne == 'd3'){
	 if (jQuery.browser.msie && jQuery.browser.version <= 8) {
		$('#d3').uncorner().corner("tl bl br 20px cc:#CBCFC1").corner("tr 20px cc:#C47E7E");
		$('#d3 .box_header').corner("tr tl 20px cc:#CBCFC1");
		$('#d2').uncorner();
		$('#d2').corner("tr bl br 20px cc:#CBCFC1").corner("tl 20px cc:#BCF2EA");
	 }

}
else{
	 if (jQuery.browser.msie && jQuery.browser.version <= 8) {
		$('#d3').uncorner();
		$('#d3').corner("tl bl br 20px cc:#CBCFC1").corner("tr 20px cc:#C47E7E");
		$('#d3 .box_header').corner("tr tl 20px cc:#CBCFC1");
		$('.left_header').corner("tl 20px cc:#CBCFC1");
		$('#d2').uncorner();
		$('#d2').corner("tr bl br 20px cc:#CBCFC1").corner("tl 20px cc:#BCF2EA");
	}
	if (jQuery.browser.msie && jQuery.browser.version == 8) {
		$('.left_header').css({'background-color':'#E5EEF5'});
	}

	if (jQuery.browser.msie && jQuery.browser.version <= 7) {
	
		$('.left_header').addClass('roundedie').corner("tr 20px cc:#CBCFC1");
	
	}

}



});


//alert("hier");

//alert($(that).find( '.dyncontent' ).children( ':visible' ).hide());

//setTimeout(function() {$(that).find( '.dyncontent' ).children( ':visible' ).hide();}, 1200 );

/*		$("div.active").removeClass("active");
		$(that).toggleClass("active",1);
		$( '.dyncontent' ).children( ':visible' ).hide();
		var options = {};
		var layer = $(that).attr('id');		
		if ( layer === "d1" ) {options = {direction: "up"};} 
		else if ( layer === "d2" ) {options = {direction: "right"};} 
		else if ( layer === "d3" ) {options = {direction: "left"};}
		else {options = {direction: "down"};}
		//setTimeout(function() {$("div.active").effect("slide",options, 100 );}, 20 );

                $("div.active").stop().effect("slide",options, 100 );


*/

//		setTimeout(function() {$("div.active").effect("slide",options, 100 ).effect("bounce",{distance:20}, 400 );}, 20 );
		
}

function inputDefaults(){

$('form input[type=text], form textarea').each(function() {
    var default_value = this.value;
    $(this).focus(function() {
      if(this.value == default_value) {
        this.value = '';
      }
    });
    $(this).blur(function() {
      if(this.value == '') {
        this.value = default_value;
      }
    });
  });

$("div.text_block a[href^='http://']").attr('target', '_blank');
$('a, button').focus(function(){ this.blur();} );
 $('a.contactme').each(function() {
            var RealMail = $(this).attr('rel').replace('_MV_','@');
            $(this).attr('href','mailto:' + RealMail);
            $(this).text(RealMail);
        });

}



$(document).ready(function(){


//$("#contact_form").validate();

$('#social_media a').fadeTo('fast', 0.5);
$('#social_media a').hover(
function () {
 $(this).fadeTo('fast', 1);
},
function () {
 $(this).fadeTo('fast', 0.5);
}
);


//$('.box').corner("20px");
  if (jQuery.browser.msie && jQuery.browser.version <= 8) {
	$('#d4').corner("tr 20px cc:#842A2A").corner("tl 20px cc:#A7C7DE").corner("bottom 20px cc:#CBCFC1");
	$('#d3').corner("tl bl 20px cc:#CBCFC1").corner("tr 20px cc:#C47E7E");
	$('#d1').corner("tl bl tr 20px cc:#CBCFC1").corner("br 20px cc:#E18D5F");
	$('#d2').corner("br bl tr 20px cc:#CBCFC1").corner("tl 20px cc:#BCF2EA");
	$('#d3 .box_footer').corner("br 20px cc:#CBCFC1");
	$('#d3').corner("br 20px cc:#E18D5F");

	$('#d3 .box_header').corner("tr tl 20px cc:#CBCFC1");
	$('.left_header').corner("tl 20px cc:#CBCFC1");
if (jQuery.browser.version == 8) {
    $('.left_header').css({'background-color':'#E5EEF5'});
}

 }
else{
Cufon.replace('.blue .left_header span'); 
}

  if (jQuery.browser.msie && jQuery.browser.version <= 7) {
$('.left_header').addClass('roundedie').corner("tr 20px cc:#CBCFC1");

var oldbrowserhint = $( '<div id="facebox"/>' );
$('body').prepend(oldbrowserhint);
$(oldbrowserhint).prepend("<div><h2>Veralteter Browser!</h2> <p>Sie benutzen einen veralteten Browser, der moderne Standards wie CSS, Ajax und HTML5 nur unzureichend unterstützt. Unsere Seite wird deshalb nicht korrekt dargestellt. Wenn Sie eine schönere optische Qualität wünschen, dann installieren Sie bitte einen aktuellen Browser:</p><p>Firefox - Win, Mac, Linux -> http://www.mozilla.org/de/firefox/<br />Internet Explorer - Win, Mac (Link) --> http://windows.microsoft.com/de-DE/internet-explorer<br />Safari  - Mac, Win -> http://www.apple.com/de/safari/<br />Chroome  - Win, Mac, Linux -> http://www.google.com/chrome/<br /></p><p>Vielen Dank für Ihr Verständnis</p><p><span class='close'>Schließen</span></p></div>");
$(oldbrowserhint).overlay({
	top: 260,
	mask: {
		color: '#fff',
		loadSpeed: 200,
		opacity: 0.5
	},
	closeOnClick: true,
	load: true
});
}


 inputDefaults();

	/*
	$(".navi").click(function(){
		$(".dyncontent").load($(this).attr('href'));
		return false;
	}); // $(".navi").click()
	*/

	 // 

$('.actnext').live({
   click: function() {
    $(this).closest(".maincontainer").find(".scrollable:last").animate({"left": "-=90px"}, "fast");
    return false;
  }
});


$('.main_images_item div a.gallerypic').live({
   click: function() {
    var zwischenSpeicher = $(this).parents().html();
    jwplayer($(this).parents().attr('id')).setup({
        flashplayer: "/DAM/swf/player.swf",
        file: $(this).attr('href'),
        image: $(this).find('img').attr('src'),
	plugins: {"gapro-1": {
"trackstarts": true,
        "trackpercentage": true,
        "trackseconds": true,
        "idstring": "||mediaid||-||title||-||file||",
"accountid":"UA-24280836-1"
      }},

        stretching:'fill',
        skin:'/DAM/swf/lulu.zip',
        height: 355,
        width: 635,
        events: {
                onComplete: function() {
                $(this).parents().html(zwischenSpeicher);
                }
        }
    }).play();
	allplayers.push($(this).parents().attr('id'));
    return false;
  }
});

$('.actprev').live({
   click: function() {
    $(this).closest(".maincontainer").find(".scrollable:last").animate({"left": "+=90px"}, "fast");
    return false;
  }
});



$('.blue img.weEditmodeStyle').live({
  mouseover: function() {

 // E-Mail Adresse anzeigen
        var elementMail = $(this).next().children('a');
        var RealMail =  $(elementMail).attr('rel').replace('_MV_','@');
            $(elementMail).attr('href','mailto:' + RealMail);
            $(elementMail).text(RealMail);
    $('.ueberuns_info').html($(this).next().html());
  }
});
	$(".scrollable").scrollable({ items: '.items', next:'.actnext', prev :'.actprev'});
	
	$(".scrollable .clickable div div.link").live({
	   click: function() {
			// see if same thumb is being clicked
		   if(allplayers.length >=1){
			   for (var playerr in allplayers){
				//jwplayer(allplayers[playerr]).stop() 
				jwplayer(allplayers[playerr]).stop() 
			  }
			}

			if ($(this).hasClass("aktive")) { return false; }
			if(!$(this).closest(".maincontainer").find(".scrollable:first").data("scrollable")){
				$('.box.active').find(".content-item .maincontainer:visible .scrollable").scrollable({ items: '.items', next:'.actnext', prev :'.actprev'}).index();
			}
			$(this).closest(".items").find("div.aktive").removeClass("aktive");
			var myNumberis = $(this).parent().index();
			
						
				$(this).closest(".maincontainer").find(".scrollable:first").data("scrollable").seekTo(myNumberis);
							
			

$(this).addClass("aktive");
			if ($(this).children('.reef_desc').length) { // implies *not* zero
				$('div#referenzen_layer').html($(this).children('.reef_desc').html());
$("div#referenzen_layer a[href^='http://']").attr('target', '_blank');
$('a, button').focus(function(){ this.blur();} );
if ($('div#referenzen_layer .tab-container').length){

				$('div#referenzen_layer .tab-container').easytabs({updateHash:false,animate:false});
} 
				
// HIER MUSS DIE URL MANIPULIERT WERDEN
				window.location.hash = "/Pages/Mediavent/Referenzen.php?ref="+$(this).attr('id');
			} else {
			}
			
			
			
			
			
			return false;
			// when page loads simulate a "click" on the first image  
	  }
	});


$(".scrollable .clickable > div:first-child").each(function(){
		$(this).children().click();
	 });


	 $(".scrollable .clickable").each(function(){
		if($(this).children().size() < 8){
			$(this).parent().next().hide();
		}
	 });




if ($('.tab-container').length){
	$('.tab-container').easytabs({updateHash:false,animate:false});

}
	//$('.blue .left_header').rotate(-90);
	$(".box").click(function(){
		//highlightBox($(this).attr('id'));
		if(!$(this).hasClass('active')){
			window.location.hash = $( '#'+$(this).attr("id")+' .box_header ul li a:first' ).attr('href').replace('#','');
		}
		//alert("hier");
		//window.location.hash = "TEST";

	}); // $(".box").click()
	// hashchange BBQ
	// Keep a mapping of url-to-container for caching purposes.
	var cache = {
		// If url is '' (no fragment), display this div's content.
		'': $('.default-item')
	};
  // Bind an event to window.onhashchange that, when the history state changes,
  // gets the url from the hash and displays either our cached content or fetches
  // new content to be displayed.
  $(window).bind( 'hashchange', function(e) {
	// Get the hash (fragment) as a string, with any leading # removed.

var actualHash = window.location.hash;

_gaq.push(['_trackPageview', actualHash]);

var actualHashArr = actualHash.split("?");
actualHash  = actualHashArr[0].replace( /^#/, '' ); 

var refdiv="";
if( actualHashArr[1]){

var refparams = actualHashArr[1].split("=");
refdiv = refparams[1];


	
}

var url = actualHash;
//var url = $.param.fragment();
//alert($.param.fragment());

    // Remove .current class from any previously "current" link(s).
    $( '.box_header ul li a.current' ).removeClass( 'current' );


//alert(url);


    // Hide any visible ajax content.
   $('.box_header ul li a[href="#' + url + '"]' ).parents('.box').find( '.dyncontent' ).children(  ).hide();


		
    // Add .current class to "current" nav link(s), only if url isn't empty.
    //url && $( '.box_header ul li a[href="#' + url + '"]' ).addClass( 'current' );

//window.location.hash="";
if(url !=""){
	$( '.box_header ul li a[href="#' + url + '"]' ).addClass( 'current' );
	
	
	
}else{
	//$( '.box_header ul li a.first' ).addClass( 'current' );
	//window.location.hash=$( '.box_header ul li a.first' ).attr('href').replace('#','');
}   
 if ( cache[ url ] ) {
      // Since the element is already in the cache, it doesn't need to be
      // created, so instead of creating it again, let's just show it!
		
		
if(!refdiv){
		cache[ url ].fadeIn(2000);
		var sonderfall = cache[ url ].find('a.gallerypic_ueberuns');
		if(sonderfall.length >0){
			jwplayer($(sonderfall).parents().attr('id')).setup({
			flashplayer: "/DAM/swf/player.swf",
			file: $(sonderfall).attr('href'),
			//image: $(sonderfall).find('img').attr('src'),
				plugins: {"gapro-1": {
        "idstring": "||mediaid||-||title||-||file||",
"accountid":"UA-24280836-1"
      }},

			stretching:'fill',
			skin:'/DAM/swf/lulu.zip',
			height: 190,
			width: 630,
			repeat:'always',
			'controlbar.position':'none'
		}).play();
		allplayers.push($(sonderfall).parents().attr('id'));
		}


}
else{
		cache[ url ].show();
		var sonderfall = cache[ url ].find('a.gallerypic_ueberuns');
		if(sonderfall.length >0){
			jwplayer($(sonderfall).parents().attr('id')).setup({
			flashplayer: "/DAM/swf/player.swf",
			file: $(sonderfall).attr('href'),
			//image: $(sonderfall).find('img').attr('src'),
				plugins: {"gapro-1": {
        "idstring": "||mediaid||-||title||-||file||",
"accountid":"UA-24280836-1"
      }},

			stretching:'fill',
			skin:'/DAM/swf/lulu.zip',
			height: 190,
			width: 630,
			repeat:'always',
			'controlbar.position':'none'
		}).play();
		allplayers.push($(sonderfall).parents().attr('id'));
		}
}		
		//alert("hier");


		if(url !=""){
			highlightBox($('.box_header ul li a[href="#' + url + '"]' ).parents('.box').filter(':first').attr('id'));
			
			//alert("url!=''");
		}

    } else {
      // Show "loading" content while AJAX content loads.
      $( '.loading' ).show();
      // Create container for this url's content and store a reference to it in
      // the cache.
	  cache[ url ] = $( '<div class="content-item"/>' )
        // Append the content container to the parent container.
        .appendTo($('.box_header ul li a[href="#' + url + '"]' ).parents('.box_header').next().children('.dyncontent') ).css('display','none')
        // Load external content via AJAX
        .load( url + '?ajax=true', function(){
          // Content loaded, hide "loading" content.
inputDefaults();


		var sonderfall = cache[ url ].find('a.gallerypic_ueberuns');
		if(sonderfall.length >0){
			jwplayer($(sonderfall).parents().attr('id')).setup({
			flashplayer: "/DAM/swf/player.swf",
			file: $(sonderfall).attr('href'),
			//image: $(sonderfall).find('img').attr('src'),
				plugins: {"gapro-1": {
        "idstring": "||mediaid||-||title||-||file||",
"accountid":"UA-24280836-1"
      }},

			stretching:'fill',
			skin:'/DAM/swf/lulu.zip',
			height: 190,
			width: 630,
			repeat:'always',			
			'controlbar.position':'none'
		}).play();
		allplayers.push($(sonderfall).parents().attr('id'));
		}
			$( '.loading' ).hide();
$(this).fadeIn(2000);
			 $(".scrollable .clickable").each(function(){
				if($(this).children().size() < 8){
					$(this).parent().next().hide();
				}
			 });



			$('.box_header ul li a[href="#' + url + '"]' ).parents('.box_header').next().children('.dyncontent').children('.default-item').hide();
if ($('.tab-container').length){			
 $('.tab-container').easytabs({updateHash:false,animate:false});
 
}
if(refdiv!=""){

			
			

			refdiv='#'+refdiv.toString();
//alert(refdiv);

			//var thats = $('.box_header ul li a[href="#' + url + '"]' ).parents('.box:eq(0)').filter(':first');
			//$(thats).find(".content-item .maincontainer:visible .scrollable").scrollable({ items: '.items', next:'.actnext', prev :'.actprev'}).index();
			//$(thats).find(".content-item .maincontainer:visible .scrollable .clickable > div").each(function(){
					
				$(refdiv).children().click();
			//});
			refdiv="";
			



			

}else{
var thats = $('.box_header ul li a[href="#' + url + '"]' ).parents('.box:eq(0)').filter(':first');
			$(thats).find(".content-item .maincontainer:visible .scrollable").scrollable({ items: '.items', next:'.actnext', prev :'.actprev'}).index();
			$(thats).find(".content-item .maincontainer:visible .scrollable .clickable > div:first-child").each(function(){
					$(this).children().click();
			 });

			
}			





			
			
        });
		highlightBox($('.box_header ul li a[href="#' + url + '"]' ).parents('.box:eq(0)').filter(':first').attr('id'));
		
		
    }
  })
  // Since the event is only triggered when the hash changes, we need to trigger
  // the event now, to handle the hash the page may have loaded with.
  $(window).trigger( 'hashchange' );



});
