$(document).ready( function() {
	if ($('#coda-slider-1').length > 0) {
		$('#coda-slider-1').codaSlider({
			dynamicArrows: false,
			dynamicTabs: false
		});
	}
	initLinks();
	set_bg_image();
});

function set_bg_image() {
	var sliderfolie = parseInt(location.hash.substring(1)); 
	if (!sliderfolie) {
		change_bg(1);
	} else {
		change_bg(sliderfolie);
	}
}


var agent = navigator.userAgent.toLowerCase();
var mobile_browser = ((agent.indexOf('ipad') != -1) || (agent.indexOf('iphone') != -1));

// used to preload the images for the backgrounds

(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.imagePreloader = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
    	if (arguments[i]) {
	      var cacheImage = document.createElement('img');
	      cacheImage.src = arguments[i];
    	  cache.push(cacheImage);
    	}
    }
  }
})(jQuery);

// Array shuffle used to randomize background images

(function($){
  $.fn.shuffle = function() {
    return this.each(function(){
      var items = $(this).children();
      return (items.length)
        ? $(this).html($.shuffle(items))
        : this;
    });
  }
  $.shuffle = function(arr) {
    for(
      var j, x, i = arr.length; i;
      j = parseInt(Math.random() * i),
      x = arr[--i], arr[i] = arr[j], arr[j] = x
    );
    return arr;
  }
})(jQuery);


function slider_toggle() {
	$(".coda-slider-wrapper").slideToggle()
}


function vplink_fadein() {
	$("#vplaylink").animate({ opacity: 1 }, 600, function() {
		vplink_fadeout();
	});
}
function vplink_fadeout() {
	$("#vplaylink").animate({ opacity: 0.6 }, 600, function() {
		vplink_fadein();
	});
}

function change_bg(the_index) {
	var the_img = $(".coda-slider .panel").slice(the_index - 1, the_index).find('div.imagecontainer').html();
	var the_video = $(".coda-slider .panel").slice(the_index - 1, the_index).find('div.videocontainer').html();

	if (the_video) {
		// show the play button and trigger the colorbox to the button
		
		var play_button = "";
		if (mobile_browser) {
			play_button = "<a id='vplaylink' href='/agentur/youtube_de/mobile_video/"+ the_video + "/' title='&nbsp;'><img id='vplaybutton' src='/agentur/images/playvideo_icon.png' alt='Play the video' /></a>";		
		} else {
			play_button = "<a id='vplaylink' href='http://www.youtube.com/v/"+ the_video + "&autoplay=1' title='&nbsp;'><img id='vplaybutton' src='/agentur/images/playvideo_icon.png' alt='Play the video' /></a>";		
		}
		$("#bgcontainer").html(play_button);
		$("#vplaylink").colorbox({iframe:true, innerWidth:"70%", innerHeight:"70%"});				

		if (the_img) {
			if ($('#bgimg').attr("src") != the_img) {
				$('#bgimg').fadeOut('fast', function() {
					$('#bgimg').attr("src", the_img);	
					$('#bgimg').fadeIn('fast');
				});
			}
		}

		vplink_fadeout();

	} else {
		if ($('#map').length == 0) {
			$('#bgcontainer').html("");
			if (the_img) {
				if ($('#bgimg').attr("src") != the_img) {
					$('#bgimg').fadeOut('fast', function() {
						$('#bgimg').attr("src", the_img);	
						$('#bgimg').fadeIn('fast');
					});
				}
			}
		}
	}
	// check the height of the slider!
	if ($('#coda-slider-1').length > 0) {
		if (the_index == '1') {
			panelHeight = $('.panel:eq(' + the_index + ')').height();
			sliderHeight = $('#coda-slider-1').height();
			//alert("panelHeight: "+panelHeight+" SliderHeight: "+sliderHeight);
			if (panelHeight > sliderHeight) {
				$('#coda-slider-1').css({ height: panelHeight });
			}
		}
	}
}
function initLinks()
{
	$("a[href$='.pdf'], a[href$='.PDF']").addClass("pdf");
	$("a[href$='.docx'], a[href$='.DOCX'], a[href$='.doc'], a[href$='.DOC'], a[href$='.txt'], a[href$='.TXT'], a[href$='.rft'], a[href$='.RTF']").addClass("doc");
	$("a[href$='.xls'], a[href$='.XLS']").addClass("xls"); 
	$("a[href$='.zip'], a[href$='.tar'], a[href$='.gz']").addClass("zip");
	$("a[href$='.jpg'], a[href$='.JPG'], a[href$='.jpeg'], a[href$='.JPEG']").addClass("jpg");
}

/* FACEBOOK */

// check if slider #1 is high enough

function setsliderheight() {
	if ($('#coda-slider-1').length > 0) {
		var sliderfolie = parseInt(location.hash.substring(1));
		if (!sliderfolie || sliderfolie=='#1') {
			// we are on the first tab
			panelHeight = $('#pan1').height();
			sliderHeight = $('#coda-slider-1').height();
			if (panelHeight > sliderHeight) {
				$('#coda-slider-1').css({ height: panelHeight });
			}
		}
	}
	setTimeout("setsliderheight()", 3000 );
}


setTimeout("setsliderheight()", 3000 );


