/* VARS */

var IS_LIVE = true;

var height = 0;
var name = "";

/* FUNCTIONS */

function setBackgrounds() {
	
	if( $(document).height() > $(window).height() ) {
		
		height = $(document).height() + parseInt( $("#content").css("padding-bottom").substr(0, 2) );
		
	} else {
	
		height = $(window).height();
		
	}
	
//	console.log( height );
	
	//
	
	$("#wrapper").css("height", height + "px");
	$("#main").css("height", height + "px");
  	
  	/*  */
  	
	var isiPad = navigator.userAgent.match(/iPad/i) != null;
	
	if( isiPad ) {
		
		$("#wrapper").css("cssText", "position: relative; width: 1156px; height: " + height + "px; margin-left: 370px; background: 0px 0px url('http://preekvandeleek.nl/uploads/development/wrapper_background.png') repeat-y;");
		$("#main").css("cssText", "position: absolute; top: 0px; left: 88px; z-index: 10; width: 980px; height: " + height + "px; background: 0px 0px url('http://preekvandeleek.nl/uploads/development/main_background.gif') repeat-y;");
			
	}
  	
}

function setCarousel() {

	$("#carousel a[class^='fancybox']").fancybox({overlayOpacity: 0.85, overlayColor: "#000000"});
	
	//

	setInterval( "slideshowSwitch()", 5000 );

}

function slideshowSwitch() {

    var $active = $("#slideshow a.active");

    if( $active.length == 0 ) $active = $("#slideshow a:last");

    var $next =  $active.next().length ? $active.next() : $("#slideshow a:first");

    $active.addClass("last-active");

    $next.css({opacity: 0.0}).addClass("active").animate({opacity: 1.0}, 1000, function() {
    
            $active.removeClass("active last-active");
            
    } );
            
}

function setTweet() {

	if( $("#tweet").length != 0 ) {
	
		$("#tweet").tweet( {
			join_text: "auto",
			avatar_size: 30,
			count: 4,
			query: "preekvdleek",
			loading_text: "Tweets worden geladen.",
			auto_join_text_default: "ik zei,",
			auto_join_text_ed: "ik",
			auto_join_text_ing: "ik was",
			auto_join_text_reply: "ik antwoordde",
			auto_join_text_url: "ik bekeek",
			refresh_interval: 60
		} ).bind("loaded", function() {setBackgrounds();} );
		
	}
    
}

function setPhotobook() {

	if( $("#photobook").length != 0 ) {

		$("#photobook a[class^='fancybox']").fancybox({overlayOpacity: 0.85, overlayColor: "#000000"});
	
	}

}

function setPhotobookExtra() {

	if( $("#photobook_extra").length != 0 ) {

		$("#photobook_extra a[class^='fancybox']").fancybox({overlayOpacity: 0.85, overlayColor: "#000000"});
	
		//
		
		$("#jcarousel").jcarousel( {	
			auto: 5,
			scroll: 1,
			wrap: "last",
			itemVisibleInCallback: {
				onAfterAnimation: test
			}
		} );
		
	}

}

function setRollOvers() {

	if( $("#partners").length != 0 ) {
		
		$("#partners ul li").each( function() { 
		
			var array = new Array();
				array = $(this).find("img").attr("src").split(".jpg");
			
			//
						
			$(this).hover(
			
				function() {
					
					name = array[0];
					
					$(this).find("img").attr("src", name + "_over.jpg");
					
				}, 
				function() {
				
					$(this).find("img").attr("src", name + ".jpg");
					
				}

			);
		
		} );
		
	}

}

function setForms() {

	if( $("form[name=newsletter]").length != 0 ) {
	
		$("form[name=newsletter]").attr("id", "newsletter_form");
	
	}
	
	//
	
	if( $("form[name=sign_out_newsletter]").length != 0 ) {
	
		$("form[name=sign_out_newsletter]").attr("id", "sign_out_newsletter_form");
	
	}
	
}

function setMediaPlayer() {
	
	if( $("#jquery_jplayer").length != 0 ) {
		
		var	jPlayer = $("#jquery_jplayer");
		var	file = $("#jquery_jplayer").attr("title");
				
		//
		
		jPlayer.jPlayer( {
	
			ready: function() {
	
				$(this).jPlayer("setMedia", {
				
					mp3: file
				
				} );
				
				//
				
	//			$("#jp_container .jp-play").click();
	
			},
			play: function(event) {
	
				
	
			},
			pause: function(event) {
	
				
	
			},
			ended: function(event) {
	
				
	
			},
			swfPath: "http://preekvandeleek.nl/flash",
			cssSelectorAncestor: "#jp_container",
			supplied: "mp3",
			wmode: "window"
	
		} );
		
	}

}

function setVideos() {
	
	$("a.video").click(function() {
	
		$.fancybox( {
			"type" : "iframe",
			"href" : this.href.replace(new RegExp("watch\\?v=", "i"), "embed/") + "?rel=0&autoplay=1",
			"overlayShow" : true,
			"centerOnScroll" : true,
			"speedIn" : 100,
			"speedOut" : 50,
			"width" : 640,
			"height" : 480,
			"overlayOpacity": 0.85, 
			"overlayColor": "#000000"
		} );

		return false;
		
	} );
	
}

function update() {

	setPhotobook();
	
	setBackgrounds();
		
}

function test() {

//	console.log("TEST");

}

/* INIT */

$(document).ready( function() {

	setBackgrounds();
	
	setCarousel();
	
	setTweet();

	setPhotobookExtra();
	
	setRollOvers();
	
	setForms();
	
	setMediaPlayer();
	
	setVideos();
	
	//
	
	if( $("#photobooks").length > 0 ) {
	
		if( $("#photobooks .items").length > 0 ) {
	
			$("#photobooks .items .item:first-child a").click();
			
		}
	
	}
	
	//
	
	if( $("#contact_form").length > 0 ) {
	
//		console.log( $("#contact_form") );
	
	}
	
} );

$(window).load( function() {setBackgrounds();} );

$(window).resize( function() {

	
	
} );
