// ----------------------------------------------------------------------------
// DOM READY
// ----------------------------------------------------------------------------

$(document).ready(function() {

	// BIND EVENTS FOR CLICKABLE ITEMS
	$('.clickable').bind('click', function() {
		var url = $(this).find('a[href]:last').attr('href');
		var target = $(this).find('a[href]:last').attr('target');
		if(url) {
			if($('base[href]').length > 0) { // IE BUG
				url = url.replace($('base[href]').attr('href'), '');
				url = $('base[href]').attr('href') + url;
			}
			if($.data(document, 'notclickable') == false) {
				if(target == '_blank') {
					window.open(url);
				} else {
					window.location.href = url;
				}
			}
		}
	});
	
	// DISABLE RIGHT CLICK
	$(document).bind('contextmenu', function(event) {
		alert('Op deze website is copyright van toepassing.\nFoto\'s kunnen via sociale media gedeeld worden door gebruik te maken van de + knop linksonder in beeld.');
		event.preventDefault();
	}); 

	// SET VARIABLE FOR NOT CLICKABLE ITEMS
	$('.clickable .notclickable').bind('mouseenter', function() {
		$.data(document, 'notclickable', true);
	}).bind('mouseleave', function() {
		$.data(document, 'notclickable', false);
	});
	$.data(document, 'notclickable', false);

	// BIND EVENTS FOR HOVERABLE ITEMS
	$('.hoverable').bind('mouseenter mouseleave', function() {
		$(this).toggleClass('hover');
	});

	// BIND EVENTS FOR FORMS
	$('.formline, .formarea, .formselect').bind('focus blur', function() {
		$(this).toggleClass('focus');
	});
	$('.formbut').bind('mouseenter mouseleave', function() {
		$(this).toggleClass('hover');
	});

	// BIND EVENTS FOR RESIZE			
	$(window).bind('resize', function() {
		$('#tpl_picture li img').trigger('imageresize');
		$('#tpl_header_image').trigger('imageresize');
		$('#tpl_content_wrapper').trigger('wrapperresize');
		$('#tpl_thumbs_wrapper').trigger('thumbmargin');
	});

	// PREPARE PHOTOBOOK
	$('#tpl_picture').data('index', 0);
	$('#tpl_thumbs_wrapper ul').prev().remove(); // REMOVES TOOLBAR BUTTON

	// REMOVE ELEMENTS
	$('#tpl_navigation #tpl_menu ul:empty').remove();
	$('#tpl_fptext:empty').remove();
	if($('#tpl_fptext').text() == '') {
		$('#tpl_fptext').remove();
	}

	// SHOW EXTRA ON HOME
	if(jquery_pageid == '421') {
		$.getScript(jquery_coreurl+'/include/jquery/tweet.latest.js', function() {
		$.getScript(jquery_coreurl+'/include/jquery/idle.latest.js', function() {
		$.getScript(jquery_coreurl+'/include/jquery/easing.latest.js', function() {
			// SHOW TWITTER POPUP
			$('#tpl_thumbs').append('<div id="tpl_latesttweet"></div>');
			$('#tpl_latesttweet').hide().tweet({
				username: ['bastiaanhopster'],
				count: 1
			}).idle(5500, function() {
				$(this).show().slideUp(0).slideDown(750, 'easeOutExpo').delay(5000).slideUp(750, 'easeOutExpo');
			});
			// SHOW FP TEXT POPUP
			$('#tpl_fptext').idle(1000, function() {
				$(this).show().slideUp(0).slideDown(750, 'easeOutExpo').delay(5000).slideUp(750, 'easeOutExpo');
			});
		});
		});
		});
	}

	// SHOW EXTRA ON CONTACT
	if(jquery_pageid == '312') {
		// SHOW TWITTER POPUP
		$('#tpl_socialmedia .profile').removeClass('profile');
	}


	// ------------------------------------------------------------------------
	// LOAD EXTERNAL SCRIPTS / PLUGINS
	// ------------------------------------------------------------------------

	if($('#permalink').length > 0) {
		$.getScript(jquery_coreurl+'/include/jquery/zclip.latest.js', function() {
			
			// PERMALINK EVENT
			$('#permalink').bind('click', function(event) {
				event.preventDefault();
			}).zclip({
				path: jquery_coreurl + '/include/jquery/zclip.latest.swf',
				copy: function() {
					return $('#permalink').attr('href');
				},
				afterCopy: function() {
					alert('De url van deze foto is gekopieërd naar het klembord.');
				}
			});
			$('.zclip').attr('title', $('#permalink').attr('title'));
	
			$('#permalinkpage').bind('click', function(event) {
				event.preventDefault();
			}).zclip({
				path: jquery_coreurl + '/include/jquery/zclip.latest.swf',
				copy: function() {
					return $('#permalinkpage').attr('href');
				},
				afterCopy: function() {
					alert('De url van deze pagina is gekopieërd naar het klembord.');
				}
			});
			$('.zclip').attr('title', $('#permalinkpage').attr('title'));
			
		});
	}

	$.getScript(jquery_coreurl+'/include/jquery/idle.latest.js', function() {
	$.getScript(jquery_coreurl+'/include/jquery/easing.latest.js', function() {
	$.getScript(jquery_coreurl+'/include/jquery/address.latest.js?tracker=track', function() {

		// BIND MENU ANIMATION / EXPAND / COLLAPSE
		if($('#tpl_thumbs').length > 0) { // ADD MENU EVENTS FOR PHOTOBOOK
			$('#tpl_navigation').bind('mouseenter', function() {
				$('#tpl_menu a').not('.menu1selected, .menu2selected, .menu3selected').parent().stop(true, true).slideDown(750, 'easeOutExpo');
				$('#tpl_menu ul ul').stop(true, true).animate({
					marginBottom: 8
				}, 750, 'easeOutExpo');
			}).bind('mouseleave', function() {
				$('#tpl_menu a').not('.menu1selected, .menu2selected, .menu3selected').parent().stop(true, true).slideUp(750, 'easeOutExpo');
				$('#tpl_menu ul ul').stop(true, true).animate({
					marginBottom: 0
				}, 750, 'easeOutExpo');
			}).idle(2000, function() {
				$(this).trigger('mouseleave');
			});
		}

		// BIND PHOTOBOOK THUMBS ANIMATION
		$('#photoframeindex').bind('mouseenter', function() {
			var s_height = $('#tpl_thumbs_slider').height('auto').height();
			$('#tpl_thumbs_slider').stop(true, true).show().height(0).delay(0).animate({height: s_height}, 1000, 'easeOutExpo');
			$('#tpl_thumbs_title').stop(true, true).delay(0).animate({height: 0}, 1000, 'easeOutExpo');
			$('#tpl_thumbs').stop(true, true).delay(0).animate({paddingBottom: 55}, 1000, 'easeOutExpo');
		})
		$('#tpl_thumbs').bind('mouseleave', function() {
			$('#tpl_thumbs_slider').stop(true, true).delay(0).animate({height: 0}, 1000, 'easeOutExpo');
			$('#tpl_thumbs_title').stop(true, true).delay(0).animate({height: 18}, 1000, 'easeOutExpo');
			$('#tpl_thumbs').stop(true, true).delay(0).animate({paddingBottom: 18}, 1000, 'easeOutExpo');
		});

		// STOP THUMBS ANIMATION ON TITLE HOVER
		/*$('#tpl_thumbs_title a').bind('mouseenter', function() {
			$('#tpl_thumbs_slider').stop(true, true);
			$('#tpl_thumbs_title').stop(true, true);
			$('#tpl_thumbs').stop(true, true);
		}).bind('mouseleave', function() {
			$('#tpl_thumbs').trigger('mouseenter');
		});
		$('#tpl_thumbs_title').delegate('a', 'mouseenter', function() {
			$('#tpl_thumbs_slider').stop(true, true);
			$('#tpl_thumbs_title').stop(true, true);
			$('#tpl_thumbs').stop(true, true);
		}).delegate('a', 'mouseleave', function() {
			$('#tpl_thumbs').trigger('mouseenter');
		});*/

		// BIND PHOTOBOOK THUMB DYNAMIC MARGIN EVENT
		$('#tpl_thumbs_wrapper').bind('thumbmargin', function() {
			var w_width = $(this).width();
			var t_width = $(this).find('.thumb').width() + 18;
			var count = Math.floor(w_width / t_width);
			var difference = w_width - (t_width * count);
			var margin = ((difference / count) / 2) + 8; // 10 -2 for border
			$(this).find('.thumb').css({
				marginLeft: margin+'px',
				marginRight: margin+'px',
				marginBottom: (margin*2)+'px'
			});
		});

		// BIND PHOTOBOOK PICTURE ANIMATION
		if($('#tpl_picture *').length == 0) {
			$('#tpl_picture').append('<ul></ul><br clear="all" /><div id="imageload"></div>').bind('imageload', function() {
				
				// CLEAR TIMEOUT
				clearTimeout(window.timeoutId);

				// SET TIMEOUT AGAIN
				window.timeoutId = setTimeout("$('#tpl_picture').trigger('imageload')", 5000);
				
				// SHOW LOADING
				$('#imageload').fadeOut(0).fadeTo(750, 0.3);
				var index = parseInt($('#tpl_picture').data('index'));

				// SET SOURCE
				var source = $('#tpl_thumbs li img.thumb').eq(index).parent().attr('web');
				if($('#tpl_picture ul #index'+index).length > 0) {
					$('#tpl_picture ul #index'+index).remove();
				}

				// ADD PICTURE WRAPPER + TAG FOR SLIDE
				$('#tpl_picture ul').append('<li><div><img id="index'+index+'" /></div></li>');

				// BIND EVENTS FOR PRELOADING
				$('#tpl_picture ul #index'+index).bind('load', function() {
					
					// HIDE LOADING
					$('#imageload').fadeTo(750, 0);

					// ANIMATE PICTURE SLIDE
					$(this).unbind('load').bind('imageresize', function() {

						// SET ACTIVE THUMB
						$('#tpl_thumbs li').removeClass('active').eq(index).addClass('active');

						// SYNC TITLE WITH IMAGE ANIMATION
						$('body').idle(500, function() {
							
							// SET TITLE
							var title = $('#tpl_thumbs li').eq(index).find('img:last').attr('alt');
							title = title.replace(' ', '&nbsp;');
							title = '<h1>'+title+'</h1>';
							$('#tpl_thumbs_title').html(title);
							$('#tpl_thumbs li').eq(index).find('.photoattachment').clone().appendTo('#tpl_thumbs_title');
							Cufon.refresh('#tpl_thumbs_title h1');

							// SET DEEPLINK OPTIONS
							$.address.path('/' + $('#tpl_thumbs li').eq(index).find('.thumb').parent().attr('deeplink'));
							var title = $('#tpl_thumbs li').eq(index).find('.thumb').parent().attr('title');
							if(title) {
								$.address.title(title + ' | Bastiaan Hopster');
							} else {
								$.address.title('Bastiaan Hopster');
							}
							$('meta[name="description"]').attr('content', $('#tpl_thumbs li').eq(index).find('.thumb').parent().attr('description'));
	
							// UPDATE ADDTHIS URL
							if(addthis_share) {
								var title = $.address.title();
								var description = $('meta[name="description"]').attr('content');
								window.addthis_share.url = window.location.href;
								window.addthis_share.title = title;
								window.addthis_share.description = description;
								window.addthis.update();
							}

						});

						// CALCULATE FULLSCREEN IMAGE
						var iw = $(this).width();
						var ih = $(this).height();
						var ir = ih / iw;
						var pw = $('#tpl_picture').width();
						var ph = $('#tpl_picture').height();
						var pr = ph / pw;
						if (pr > ir) {
							// PORTVIEW IS HIGHER
							$(this).height(ph);
							$(this).width(ph / ir);
							$(this).css({
								marginLeft: (0-(((ph / ir) - pw) / 2)),
								marginTop: 0
							});
						} else {
							// PORTVIEW IS WIDER
							$(this).width(pw);
							$(this).height(pw * ir);
							$(this).css({
								marginLeft: 0,
								marginTop: (0-(((pw * ir) - ph) / 2))
							});
						}
						if($('#tpl_picture').data('move')) {
							var move = $('#tpl_picture').data('move');
						} else {
							var move = 'left';
						}
						$(this).parent().css(move, '0px');
						$(this).parent().parent().delay(250).css(move, '0px').animate({
							width: '100%'
						}, 2000, 'easeInOutExpo', function() {
							$(this).prev().remove();
						});
						if(move == 'right') {
							move = 'left';
						}
						$('#tpl_picture').data('move', move);
					}).trigger('imageresize');
				});
				$('#tpl_picture ul #index'+index).attr('src', source);
				$('#tpl_picture').data('index', (index+1));
				if(parseInt($('#tpl_picture').data('index')) == $('#tpl_thumbs li').length) {
					$('#tpl_picture').data('index', 0);
				}
			}).bind('imageprev', function() {
				$('#tpl_picture').data('move', 'right');
				var index = $('#tpl_thumbs_wrapper li.active').index('#tpl_thumbs_wrapper li');
				$('#tpl_thumbs_wrapper li').eq((index-1)).find('.thumb').trigger('click');
			}).bind('imagenext', function() {
				$('#tpl_picture').data('move', 'left');
				var index = $('#tpl_thumbs_wrapper li.active').index('#tpl_thumbs_wrapper li');
				index = index + 1;
				if(index == $('#tpl_thumbs_wrapper li').length) {
					index = 0;
				}
				$('#tpl_thumbs_wrapper li').eq((index)).find('.thumb').trigger('click');
			});
			
		}

		// BIND PHOTOBOOK THUMB EVENTS
		$('#tpl_thumbs_slider .thumb').parent().bind('click', function(event) {
			event.preventDefault();
		});
		$('#tpl_thumbs_slider .thumb').bind('click', function(event) {
			event.preventDefault();
			var index = $('#tpl_thumbs_slider .thumb').index($(this));
			$('#tpl_picture').data('index', index).trigger('imageload');
			$('#tpl_thumbs').trigger('mouseleave'); // HIDE THUMBS ON CLICK
		});

		// DEEPLINK FOR PHOTOBOOK
		$.address.init(function(event) {
			$('#ie78bug').address();
		}).change(function(event) {
			var location = window.location.href;
			location = location.replace('/#/','/');
			$('#permalink').attr('href', location);
		}).externalChange(function(event) {
			var href = event.path;
			if(href.charAt(0) == '/') {
				href = href.replace(/\//, '');
			}
			if($('#tpl_thumbs_wrapper li a[deeplink="'+href+'"] img').length > 0) {
				$('#tpl_thumbs_wrapper li a[deeplink="'+href+'"] img').trigger('click'); // WHEN PAGE EXISTS
			} else {
				$('#tpl_thumbs_wrapper li a[deeplink]:first img').trigger('click'); // WHEN PAGE IS RENAMED
			}
		});

		// TRIGGER IMAGE LOAD WHEN NO DEEPLINK IS DEFINED
		var pathlength = $.address.pathNames().length;
		if(pathlength == 0) {
			$('#tpl_picture').trigger('imageload');
		}

		// HOVER EFFECT ON THUMB
		$('#tpl_thumbs_slider .thumb').each(function() {
			$(this).parent().parent().bind('mouseenter', function(event) {
				$(this).addClass('hover');
			}).bind('mouseleave', function() {
				$(this).removeClass('hover');
			});
		});

		// SHOW HIDE PREV NEXT
		if($.browser.msie && $.browser.version < 7) { // 9
			if($('#tpl_thumbs').length > 0) { // ADD MENU EVENTS FOR PHOTOBOOK
				$('#tpl_picture').append('<div id="tpl_cursorblock_left" title="Vorige foto"></div><div id="tpl_cursorblock_right" title="Volgende foto"></div>');
				$('#tpl_cursorblock_left, #tpl_cursorblock_right').fadeOut(0).delay(2500).fadeTo(750, 1, function() {
					$(this).css('filter', 'none');
				}).bind('mouseenter', function() {
					//$(this).stop(true, true).fadeTo(750, 1);
				}).bind('mouseleave', function() {
					//$(this).stop(true, true).fadeTo(750, 0.3);
				});
				$('#tpl_cursorblock_left').bind('click', function() {
					$('#tpl_picture').trigger('imageprev');
				});
				$('#tpl_cursorblock_right').bind('click', function() {
					$('#tpl_picture').trigger('imagenext');
				});
			}
		} else {
			if($('#tpl_thumbs').length > 0) { // ADD MENU EVENTS FOR PHOTOBOOK
				$('#tpl_body').bind('mousemove', function(event) {
					$(this).data('posY', (event.pageY-$('#tpl_body').offset().top));
					$(this).data('posX', event.pageX);
				});
				$('#tpl_body').data('mousefollow', 'true').append('<div id="tpl_cursor_left" title="Vorige foto"></div><div id="tpl_cursor_right" title="Volgende foto"></div>').bind('mousefollow', function() {
					var posY = $(this).data('posY') + -10;
					var posX = $(this).data('posX') + -10;
					var width = $(this).width();
					var height = $(this).height();
					var scaleX = (posX / (width / 100));
					var scaleY = (posY / (height / 100));
					if((scaleX < 25 && scaleY > 25 && scaleY < 75) || (scaleX > 75 && scaleY > 25 && scaleY < 75)) {
						if($(this).data('mousefollow') == 'hibernate') {
							$(this).data('mousefollow', 'true');
						}
						if($(this).data('mousefollow') == 'true') {
							if(scaleX < 25) {
								//$('#tpl_cursor_left').css({opacity: 1, top: posY, left: posX});
								$('#tpl_cursor_left').stop().animate({opacity: 1, top: posY, left: posX}, 1500, 'easeOutCirc');
							}
							if(scaleX > 75) {
								//$('#tpl_cursor_right').css({opacity: 1, top: posY, left: posX});
								$('#tpl_cursor_right').stop().animate({opacity: 1, top: posY, left: posX}, 1500, 'easeOutCirc');
							}
						}
					} else {
						if($(this).data('mousefollow') == 'true') {
							$(this).data('mousefollow', 'hibernate');
							//$('#tpl_cursor_left').stop().animate({opacity: 0, top: (height/2), left: 300}, 1000, 'easeOutCirc');
							//$('#tpl_cursor_right').stop().animate({opacity: 0, top: (height/2), left: (width-300)}, 1000, 'easeOutCirc');
							$('#tpl_cursor_left').animate({opacity: 0}, 1000, 'easeOutCirc');
							$('#tpl_cursor_right').animate({opacity: 0}, 1000, 'easeOutCirc');
						}
					}
				}).find('#tpl_cursor_left, #tpl_cursor_right').show().css('opacity', 0).bind('mouseenter', function() {
					$('#tpl_body').data('mousefollow', 'false');
					$(this).stop();
				}).bind('mouseleave', function() {
					$('#tpl_body').data('mousefollow', 'true');
				});
				$('#tpl_cursor_left').bind('click', function() {
					$('#tpl_picture').trigger('imageprev');
				});
				$('#tpl_cursor_right').bind('click', function() {
					$('#tpl_picture').trigger('imagenext');
				});
				function mousefollowCall() {
					$('#tpl_body').trigger('mousefollow');
				}
				var mousefollowIntervalID = window.setInterval(mousefollowCall, 100);
			}
		}

	});
	});
	});


	// ------------------------------------------------------------------------
	// ONLOAD / LOAD COMPLETE
	// ------------------------------------------------------------------------

	$(window).bind('load', function() {

		// TRIGGER RESIZE EVENT
		$(this).trigger('resize');

	});

});


// SEND VISITORS TO DYNAMIC CONTENT
function jquery_include_address() {
	//alert(escape('%')); // DEBUG GOOGLE ENCODE URI
	if($('#tpl_picture *').length > 0) {
		$('#tpl_picture *').remove();
		var location = window.location.href;
		location = location.reverse();
		location = location.replace(/\//,'/#/');
		location = location.reverse();
		// GOOGLE ADJUSTMENTS
		location = encodeURI(location); // will not encode: ~!@#$&*()=:/,;?+'
		location = location.replace('(', '%28');
		location = location.replace(')', '%29');
		location = location.replace('%25', '%'); // UNDO % replacement
		// END
		window.location.href = location;
	}
}


// TINYSCROLLBAR
function jquery_include_scrollbar() {

	// ADD HTML
	$('#tpl_content_scrollbar').prepend('<div class="scrollbar"><div class="track"><div class="thumb"><div class="end"></div></div></div></div>');

	// LOAD SCRIPT
	$.getScript(jquery_coreurl+'/include/jquery/tinyscrollbar.latest.js', function() {
	
		// ADD SCROLLBAR FUNCTION
		$('#tpl_content_scrollbar').tinyscrollbar();
		
	
		// DYNAMIC HEIGHT EVENT FOR CONTENT
		$('#tpl_content_wrapper').bind('wrapperresize', function() {
			var padding = (($('#tpl_content').width() - $(this).width()) / 2);
			$(this).height(($('#tpl_content').height() - 180));
			$(this).css({
				paddingLeft: (padding + 60),
				paddingRight: (padding + 25)
			});
			$('#tpl_content_scrollbar').height(($('#tpl_content').height() - 180));
			$(this).trigger('scroll');
			$('#tpl_content_scrollbar .tpl_blog_date_large').css('left', (0-(padding-10))+'px'); // POSITION DATE OUTSIDE CONTENT
			$('#tpl_content_scrollbar').tinyscrollbar_update();
		}).trigger('wrapperresize');
	
	});
}


String.prototype.reverse = function() {
	splitext = this.split("");
	revertext = splitext.reverse();
	reversed = revertext.join("");
	return reversed;
}
