/*
* Cache van javascripts.script.js
* Created: 2012-02-01 16:25:45
*/

/*
$Author: sjors $
$LastChangedDate: 2006-10-19 14:05:31 +0200 (do, 19 okt 2006) $
$Id: javascripts.js 28 2006-10-19 12:05:31Z sjors $
$Rev: 28 $
*/

var google = 'UA-7586154-1';
$(document).ready(function() {
	$('body').addClass('js');

	if (google) {
		$.trackPage(google);
		$.fn.track.defaults.debug = true;
	}

	$(document).click(function(e) {
		var $target = $(e.target).closest('a');
		if ($target && $target.attr('href')) {
			var href = $target.attr('href');
			if ($target.hasClass('newwindow') || $target.hasClass('externlink')) {
				window.open(href, '_blank');
				return false;
			}

			if (
				(href.substring(0, 7) == 'http://' || href.substring(0, 8) == 'https://') &&
				href.indexOf('http://www.youngguns.nl')
			) {
				window.open(href, '_blank');
				return false;
			}

			var downloadsurl = '/downloads/';
			if (href.substring(0, downloadsurl.length) == downloadsurl) {
				window.open(href, '_blank');
				return false;
			}
		}
	});
	
	$('.quote').each(function() {
		var words = $(this).text().split(" ");
		var quote_width = parseInt($(this).width());
		var line = '';
		var $span = null;
		var $quote = $(this);
		$(this).html('');
		$.each(words, function(i, word) {
			if (!$span) {
				$span = $('<span />').css('white-space', 'nowrap').appendTo($quote);
			}
			$span.html($span.text() + word + ' ');
			if (parseInt($span.width()) > quote_width) {
				$span.html(line);
				$span = $('<span>' + word + ' </span>').css('white-space', 'nowrap').appendTo($quote);
				line = '';
			}

			line += word + ' ';			 
		});
		$(this).find('span').css('background-color', $(this).css('background-color'));
		$(this).find('span').css('background-image', $(this).css('background-image'));
		$(this).css({
			'background-color': 'transparent',
			'background-image': 'none'
		});
	});

	/*$('h1').each(function() {
		var txt = $(this).text();
		var spacepos = txt.indexOf(' ');
		if (spacepos > 0) {
			$(this).html('<strong>' + txt.substr(0, spacepos) + '</strong> ' + txt.substr(spacepos));
		}
	});*/

	$('input[name^="contactby-"]').change(function() {
		if ($(this).attr('checked')) {
			$('.' + $(this).attr('name')).slideDown();
		} else {
			$('.' + $(this).attr('name')).slideUp();
		}		
	});
	$('input[name^="contactby-"]').each(function() {
		$('.' + $(this).attr('name')).css('display', $(this).attr('checked') ? 'block' : 'none');
	});

	if (jQuery.browser.mobile) {
		var minHeight = $('#header').height();
		var hcHeight = $('#headerclearer').height();
		var h1Top = parseInt($('h1').css('top'));
		$('body').addClass('mobile-device');
		if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod') {
			$('body').addClass('ipad');
		};
		var updateMenu = function() {
			$('#mainmenu').children('li').css('height', 'auto');			
			$('#mainmenu').children('li').css('height', $('#header').height());
			$('#headerclearer').css('height', hcHeight + $('#header').height() - minHeight);
			$('h1').css('top', h1Top + $('#header').height() - minHeight);
		};
		updateMenu();
		setInterval(function() {
			updateMenu();
		}, 1000);
	} else {
		(function (header, menu) {
			var keepopen = false;
			var inheader = false;
			var menuseen = $.cookie('menu') ? true : false;
			if (header.length == 0 || menu.length == 0) return;

			menu.find('ul').css('display', 'block');
			var minHeight = header.height();
			header.css({
				'height': 'auto',
				'overflow': 'visible'
			});
			var maxHeight = header.height();
			header.css({
				'height': minHeight,
				'overflow': 'hidden'
			});
			menu.children('li').css('height', maxHeight);

			header.find('input').focus(function() {
				keepopen = true;
			});
			header.find('input').blur(function() {
				keepopen = false;
			});

			header.mouseenter(function() {
				inheader = true;
				menuseen = true;
				$(this).stop().animate({'height': maxHeight}, 1000, 'easeOutBounce');
			});
			header.mouseover(function() {
				if (!inheader) {
					header.mouseenter();
				}
			});
			header.mouseleave(function() {
				inheader = false;
				if (!keepopen) {
					$(this).stop().animate({'height': minHeight}, 2000, 'easeOutQuart');
				}
			});

			$(document).click(function(e) {
				if (e.pageY > maxHeight) {
					header.mouseleave();
				}
			});

			if (!menuseen) {
				$.cookie('menu', 1, {expires: 30});
				header.css('height', maxHeight);
				function menutease() {
					if (menuseen) return;
					header.stop().animate({'height': minHeight}, 3000, 'easeOutQuart');
				}

				setTimeout(function (){menutease();}, 1500);
			}

		})($('#header'), $('#mainmenu'));
	}

	$('#header form').submit(function() {
		document.location.href = $(this).attr('action') + '?' + $(this).serialize();
		return false;
	});

	if ('younggunstweets') {
		$('div.twitter').each(function() {
			var redknot = $(this).attr('id') == 'twitter_redknot';
			$(this).getTwitter({
				userName: redknot ? 'redknothosting' : 'younggunstweets',
				numTweets: 5,
				slideIn: redknot,
				showHeading: false,
				showProfileLink: false,
				showTimestamp: true
			});
		});
	}

	$("a.zoom").fancybox({
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">' + $.i18n._('Image %s / %s', [currentIndex + 1, currentArray.length + (title.length ? ' &nbsp; ' + title : '')]) + '</span>';
		}
	});

	$('.accordion').each(function() {
		$(this).accordion({
			autoHeight: false,
			collapsible: true,
			active: $(this).find('.current').closest('ul').closest('li').children('h4')
		});
	});

	$('.references-wrapper').each(function() {
		var $this = $(this);
		var cols = $this.find('.col');
		var wrapper = $this.find('.references');
		var numcols = 0;
		var maxscroll = cols.last().position($(wrapper)).left - $this.width();
		var currentCol = cols.index($this.find('.current').closest('.col'));

		while (maxscroll > $(cols[numcols]).position(wrapper).left + $(cols[numcols]).width()) {
			numcols++;
		}
		numcols++;
		if (currentCol >= 0) {
			if (currentCol > numcols) {
				currentCol = numcols;
			}
			currentCol--;
		}
		$(wrapper).css({'left': -1 * $(cols[currentCol+1]).position(wrapper).left});

		$this.append('<a href="#" class="prev" />').append('<a href="#" class="next" />');
		$this.children('.prev, .next').hide();
		$this.children('.prev').click(function() {
			$(this).show();
			currentCol--;
			if (cols[currentCol]) {
				$(wrapper).animate({'left': -1 * $(cols[currentCol]).position(wrapper).left});
				if (currentCol == 0) {
					$(this).hide('fade');
				}
			} else {
				$(this).hide('fade');
			}
			$this.children('.next').show('fade');
			return false;
		});
		$this.children('.next').click(function() {
			$(this).show();
			currentCol++;
			if (cols[currentCol]) {
				$(wrapper).animate({'left': -1 * $(cols[currentCol]).position(wrapper).left});
				if (currentCol + 1 > numcols) {
					$(this).hide('fade');
				}
			} else {
				$(this).hide('fade');
			}
			if (currentCol > 0) {
				$this.children('.prev').show('fade');
			}
			return false;
		});
		$this.children('.next').click();
	});

	$('.portfolio-home').each(function() {
		var related = [];
		var $this = $(this);
		var inrequest = 0;
		var curItem = 0;
		var interval;
		
		$this.closest('.sidebar').append('<div class="portfolio-home-nav"></div>');
		var $nav = $this.closest('.sidebar').find('.portfolio-home-nav');
		$nav.click(function(e) {
			var $target = $(e.target).closest('a');
			if ($target) {
				clearTimeout(interval);
				if ($target.hasClass('next')) {curItem++;}
				if ($target.hasClass('prev')) {curItem--;}
				if (curItem < 0) {curItem = related.length - 1;}
				if (curItem > related.length - 1) {curItem = 0;}

				$this.css('margin-bottom', 12);
				$this.children('li').css('margin-bottom', 0);

				var data = related[curItem];
				if (!data.image) {
					$('#headerimg').slideUp();
				} else {
					$('#headerimg').slideDown();
					var curimg = $('#headerimg img').last();
					curimg.prevAll().remove();
					$('#headerimg').css('height', curimg.height());
					curimg.css({
						'z-index': 1,
						'position': 'absolute'
					});
					$('#headerimg').append('<img src="'+data.image+'" />');
					$('#headerimg img').last().css({
						'z-index': 2,
						'position': 'absolute'
					}).hide().delay(250).fadeIn(1000);
				}
				var curDesc = $this.children('li').last();
				curDesc.prevAll().remove();
				$this.append('<li>'+data.description+'</li>');
				curDesc.slideUp();
				$this.children('li').last().hide().delay(200).slideDown();
			}

			return false;
		});

		function nexthomeportfolio() {
			clearTimeout(interval);
			curItem++;
			if (curItem > related.length - 1) {curItem = 0;}
			$this.css('margin-bottom', 12);
			$this.children('li').css('margin-bottom', 0);

			var data = related[curItem];
			if (!data.image) {
				$('#headerimg').slideUp();
			} else {
				$('#headerimg').slideDown();
				var curimg = $('#headerimg img').last();
				curimg.prevAll().remove();
				$('#headerimg').css('height', curimg.height());
				curimg.css({
					'z-index': 1,
					'position': 'absolute'
				});
				$('#headerimg').append('<img src="'+data.image+'" />');
				$('#headerimg img').last().css({
					'z-index': 2,
					'position': 'absolute'
				}).hide().delay(250).fadeIn(1000);
			}
			var curDesc = $this.children('li').last();
			curDesc.prevAll().remove();
			$this.append('<li>'+data.description+'</li>');
			curDesc.css({'z-index': 1});
			$this.children('li').last().css({'z-index': 2}).hide().delay(250).fadeIn(1000);
			
			interval = setTimeout(nexthomeportfolio, 7500);
		}

		if ($this.find('.related').length > 1) {
			$this.find('.related').each(function(i) {
				inrequest++;
				$.ajax($(this).attr('href'), {
					success: function(data) {
						if (data.image) {
							$.preLoadImages(data.image);
						}
						related[i] = data;
					},
					complete: function() {
						inrequest--;
						if (inrequest == 0) {
							$nav.append('<a href="#" title="" class="prev"></a>').append('<a href="#" title="" class="next"></a>');
							interval = setTimeout(nexthomeportfolio, 3000);
						}
					},
					data: 'type=json&pos=home',
					dataType: 'json'
				});
			});
		}
	});

	if ($('.blocks').length > 0) {
		var portfolio_cache = [];
		var containerWidth;
		var currentPortfolio = -1;
		function showPortfolioItem(id) {
			$('html, body').animate({
				scrollTop: 35
			}, 150);

			var idx = $('.sidebar .portfolio-'+id).parent().index();
			var direction = 1;
			if (idx == currentPortfolio) {
				return;
			}
			if (idx < currentPortfolio) {
				direction = -1;
			}
			currentPortfolio = idx;			

			var data = portfolio_cache[id];
			if (!data.image) {
				$('#headerimg').slideUp();
			} else {
				$('#headerimg').slideDown();
				var curimg = $('#headerimg img').last();
				curimg.prevAll().remove();
				$('#headerimg').css('height', curimg.height());
				curimg.css({
					'z-index': 1,
					'position': 'absolute'
				});
				$('#headerimg').append('<img src="'+data.image+'" />');
				$('#headerimg img').last().css({
					'z-index': 2,
					'position': 'absolute'
				}).hide().delay(250).fadeIn(1000);
			}

			$('.thumbs').fadeOut(700, function() {
				if (data.thumbs) {
					$('.thumbs').html(data.thumbs).fadeIn(500);

					$(".thumbs a.zoom").fancybox({
						'transitionIn'		: 'elastic',
						'transitionOut'		: 'elastic',
						'titlePosition' 	: 'over',
						'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
							return '<span id="fancybox-title-over">' + $.i18n._('Image %s / %s', [currentIndex + 1, currentArray.length + (title.length ? ' &nbsp; ' + title : '')]) + '</span>';
						}
					});
				}
			});

			var curDesc = $('.portfolio-item').last();
			curDesc.prevAll().remove();
			$('.portfolio-details-container').append(data.description);
			curDesc.animate({'left': -1*direction*containerWidth});
			$('.portfolio-item').last().css({'left': direction*containerWidth}).animate({'left': 0});

			$('.sidebar .portfolio a').removeClass('current');
			$('.sidebar .portfolio-'+id).addClass('current');
			var prev = $('.sidebar .portfolio-'+id).parent().prev('li');
			var next = $('.sidebar .portfolio-'+id).parent().next('li');
			if (prev.length > 0) {
				$('.portfolio-details .prev .portfolio-nav')
					.attr('title', prev.children('a').text().trim())
					.attr('href', prev.children('a').attr('href'))
					.fadeIn();
				preloadPortfolio($('.portfolio-details .prev .portfolio-nav'));
			} else {
				$('.portfolio-details .prev .portfolio-nav').fadeOut();
			}
			if (next.length > 0) {
				$('.portfolio-details .next .portfolio-nav')
					.attr('title', next.children('a').text().trim())
					.attr('href', next.children('a').attr('href'))
					.fadeIn();
				preloadPortfolio($('.portfolio-details .next .portfolio-nav'));
			} else {
				$('.portfolio-details .next .portfolio-nav').fadeOut();
			}			
		}
		function preloadPortfolio(me, show) {
			if (!$(me).attr('href')) {
				return;
			}
			var id = $(me).attr('href').substring($(me).attr('href').lastIndexOf('/'));
			id = id.substring(1, id.indexOf('-'));

			if (!portfolio_cache[id]) {
				$.ajax($(me).attr('href'), {
					success: function(data) {
						if (data.image) {
							$.preLoadImages(data.image);
						}
						portfolio_cache[id] = data;
						if (show) {
							showPortfolioItem(id);
						}
					},
					data: 'type=json',
					dataType: 'json'
				});
			}
		}
		$('.portfolio-details-container').each(function() {
			containerWidth = parseInt($(this).width());
			currentPortfolio = $('.sidebar .portfolio .current').index();
		})
		$('.portfolio-details .next, .portfolio-details .prev').each(function() {
			if ($(this).find('a').length == 0) {
				$(this).html('<a class="portfolio-nav" title="" style="display: none"></a>');
			}
			preloadPortfolio($(this).find('.portfolio-nav'));
		});
		$('.portfolio-nav').click(function(e) {
			var $target = $(e.target).closest('a');
			var id = $target.attr('href').substring($target.attr('href').lastIndexOf('/'));
			id = id.substring(1, id.indexOf('-'));
			if (!portfolio_cache[id]) {
				preloadPortfolio($target, true);
			} else {
				showPortfolioItem(id);
			}

			return false;
		});
	}
	
	/* Placeholder fix */
	$('form').submit(function() {
		$(this).find('input[placeholder').each(function() {
			if ($(this).attr('placeholder') == $(this).val()) {
				$(this).val('');
			}
		});
	});
	var input = document.createElement('input');
	if (!('placeholder' in input)) {
		$('input').each(function() {
			var $el = $(this);
			var ph = $el.attr('placeholder');
			if (ph) {
				$el.focus(function() {
					if ($el.attr('value') == ph) {
						$el.attr('value', '');
						$el.removeClass('default');
					}
				});
				$el.blur(function() {
					if ($el.attr('value') == '') {
						$el.attr('value', ph);
						$el.addClass('default');
					}
				});
				$el.trigger('blur');
			}
		});
	}

	$('.imgfader').innerfade({
		speed: 1500,
		timeout: 7500,
		type: 'sequence'
	});

	var addthis_config = {"data_track_clickback":true};
	$.getScript('http://s7.addthis.com/js/250/addthis_widget.js#username=younggunsnl');

	$.getJSON('http://api.flickr.com/services/feeds/photos_public.gne?id=54020453@N04&lang=en-us&format=json&jsoncallback=?', function(data){
		$.each(data.items.slice(0, 10), function(i,item){
			$("<img/>").attr("src", item.media.m).appendTo("#flickr_badge_wrapper")
				.wrap('<a href="' + item.link + '" class="flickr_badge_image"></a>');
		});
	});
});

/* =========================================================

// jquery.innerfade.js

// Datum: 2008-02-14
// Firma: Medienfreunde Hofmann & Baldes GbR
// Author: Torsten Baldes
// Mail: t.baldes@medienfreunde.com
// Web: http://medienfreunde.com

// based on the work of Matt Oakes http://portfolio.gizone.co.uk/applications/slideshow/
// and Ralf S. Engelschall http://trainofthoughts.org/

 *
 *  <ul id="news">
 *      <li>content 1</li>
 *      <li>content 2</li>
 *      <li>content 3</li>
 *  </ul>
 *
 *  $('#news').innerfade({
 *	  animationtype: Type of animation 'fade' or 'slide' (Default: 'fade'),
 *	  speed: Fading-/Sliding-Speed in milliseconds or keywords (slow, normal or fast) (Default: 'normal'),
 *	  timeout: Time between the fades in milliseconds (Default: '2000'),
 *	  type: Type of slideshow: 'sequence', 'random' or 'random_start' (Default: 'sequence'),
 * 		containerheight: Height of the containing element in any css-height-value (Default: 'auto'),
 *	  runningclass: CSS-Class which the container get’s applied (Default: 'innerfade'),
 *	  children: optional children selector (Default: null)
 *  });
 *

// ========================================================= */
(function($){$.fn.innerfade=function(options){return this.each(function(){$.innerfade(this,options)})};$.innerfade=function(container,options){var settings={'animationtype':'fade','speed':'normal','type':'sequence','timeout':2000,'containerheight':$(container).css('height'),'runningclass':'innerfade','children':null};if(options)$.extend(settings,options);if(settings.children===null)var elements=$(container).children();else var elements=$(container).children(settings.children);if(elements.length>1){$(container).css('position','relative').css('height',settings.containerheight).addClass(settings.runningclass);for(var i=0;i<elements.length;i++){$(elements[i]).css('z-index',String(elements.length-i)).css('position','absolute').hide()};if(settings.type=="sequence"){setTimeout(function(){$.innerfade.next(elements,settings,1,0)},settings.timeout);$(elements[0]).show()}else if(settings.type=="random"){var last=Math.floor(Math.random()*(elements.length));setTimeout(function(){do{current=Math.floor(Math.random()*(elements.length))}while(last==current);$.innerfade.next(elements,settings,current,last)},settings.timeout);$(elements[last]).show()}else if(settings.type=='random_start'){settings.type='sequence';var current=Math.floor(Math.random()*(elements.length));setTimeout(function(){$.innerfade.next(elements,settings,(current+1)%elements.length,current)},settings.timeout);$(elements[current]).show()}else{alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'')}}};$.innerfade.next=function(elements,settings,current,last){if(settings.animationtype=='slide'){$(elements[last]).slideUp(settings.speed);$(elements[current]).slideDown(settings.speed)}else if(settings.animationtype=='fade'){$(elements[last]).fadeOut(settings.speed);$(elements[current]).fadeIn(settings.speed,function(){removeFilter($(this)[0])})}else alert('Innerfade-animationtype must either be \'slide\' or \'fade\'');if(settings.type=="sequence"){if((current+1)<elements.length){current=current+1;last=current-1}else{current=0;last=elements.length-1}}else if(settings.type=="random"){last=current;while(current==last)current=Math.floor(Math.random()*elements.length)}else alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');setTimeout((function(){$.innerfade.next(elements,settings,current,last)}),settings.timeout)}})(jQuery);function removeFilter(element){if(element.style.removeAttribute){element.style.removeAttribute('filter')}}

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

/**
 * jQuery.browser.mobile (http://detectmobilebrowser.com/)
 *
 * jQuery.browser.mobile will be true if the browser is a mobile device
 *
 **/
(function(a){jQuery.browser.mobile=/android.+mobile|iPad|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|e\-|e\/|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(di|rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|xda(\-|2|g)|yas\-|your|zeto|zte\-/i.test(a.substr(0,4))})(navigator.userAgent||navigator.vendor||window.opera);
