// variables
var OSName="Unknown OS";
var LbOptions = {
	padding: 20,
	opacity: 0.65,
	showTitle: true,
	allowresize: true,
	theme: 'light_square'
}



// listeners
jQuery(document).ready(function(){
	
	//testje
	$('#content .post a:has(img):first').css('background-color','white');
	$('#content .post a:has(img):first img').css('visibility','hidden');
	
	//OS
	if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
	if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
	if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
	if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";
	
	//active States
	var curURL = document.location.href;
	$("a[href='"+curURL+"']").addClass('active');
	
	if(curURL.substring(36,44)=="projects"){
		$('#content').css('background-image','url('+templatePath+'/img/content.gif)');
		$('#content').css('background-position','342px 110px');
	}
	
	
	//crosses
	$("ul#navigation ul:not(.main) a[href='"+curURL+"']").each(function(){
		var activeNav = $(this);
		var activeOffset = activeNav.offset();
		activeNav.append('<img src="'+templatePath+'/img/active.gif" style="position:fixed;left:93px;top:'+(activeOffset.top+3)+'px" />');
	});
	
	
	//
	/*$('li.showReel:first').flash({
		src: templatePath + '/swf/anim.swf',
		width: 128,
		height: 110,
		wmode: 'transparent',
		flashvars: {}
	}, {
		version: 9
	});*/
	
	//body width fixes
	resetWidthLayout();
	
	//smooth to top animation
	jQuery('a[href*=#]').click(function() {
		
		if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
		
			var $target = jQuery(this.hash);
			$target = $target.length && $target ||
			jQuery('[name=' + this.hash.slice(1) + ']');
			
			if ($target.length) {
				var targetOffset = $target.offset().top;
				jQuery('html,body').animate({scrollTop: targetOffset});
				return false;
			}
		}
	});
	
	//make search field empty
	jQuery('#s').click(function(){jQuery(this).attr("value","")});
	
	//place link img above post
	jQuery('.post').each(function(){
		
		var thisPost = jQuery(this);
		var curLink = thisPost.find('a:has(img):first');
		var postDate = thisPost.find('em.date:first').html();
		var postId = thisPost.attr('id');
		var imgId = postId+'_img';
		
		curLink.attr('id',imgId);
		curLink.addClass('lightbox-enabled');
		curLink.insertBefore(thisPost);
		
		var thaImg = $('#' + imgId + ' img');
		
		if (thaImg.css('display') == 'block') {
			
			if (thaImg.height()>15) {
			
				var thisH = thaImg.height();
				var imgUrl = thaImg.attr('src');
				
				thaImg.css({
					'display': 'none',
					'margin': '0px'
				});
				curLink.css({
					'display': 'block',
					'padding': '0px',
					'margin': '0px',
					'border': '0px',
					'margin-bottom': '-30px',
					'width': '442px'
				});
				$('#content').css('padding-top', '140px');
				
				//alert($('#'+imgId).height());
				
				$('#' + imgId).flash({
					src: templatePath + '/swf/pictureRemplacement.swf',
					width: 442,
					height: thisH + 66,
					wmode: 'transparent',
					bgcolor: 'white',
					style: {
						'background-color': 'white'
					},
					flashvars: {
						date: postDate,
						pictureUrl: imgUrl,
						height: thisH,
						jsParameter: '',
						projectId: imgId
					}
				}, {
					version: 9
				});
				
			}
			else {
			
				thaImg.load(function(){
					
					var thisH = thaImg.height();
					var imgUrl = thaImg.attr('src');
					
					thaImg.css({
						'display': 'none',
						'margin': '0px'
					});
					curLink.css({
						'display': 'block',
						'padding': '0px',
						'margin': '0px',
						'border': '0px',
						'margin-bottom': '-30px',
						'width': '442px'
					});
					$('#content').css('padding-top', '140px');
					
					//alert($('#'+imgId).height());
					
					$('#' + imgId).flash({
						src: templatePath + '/swf/pictureRemplacement.swf',
						width: 442,
						height: thisH + 66,
						wmode: 'transparent',
						bgcolor: 'white',
						style: {
							'background-color': 'white'
						},
						flashvars: {
							date: postDate,
							pictureUrl: imgUrl,
							height: thisH,
							jsParameter: '',
							projectId: imgId
						}
					}, {
						version: 9
					});
					
				});
				
			}
			
		} else {
			
			curLink = thisPost.find('img:first');
			curLink.attr('id',imgId);
			curLink.addClass('lightbox-enabled');
			curLink.insertBefore(thisPost);
			
			thaImg = $('#' + imgId);
			
			if (thaImg) {
				
				if (thaImg.height()>15) {
					
					var thisH = thaImg.height();
					var imgUrl = thaImg.attr('src');
					
					thaImg.wrap('<div id="'+imgId+'_wrapper"></div>');
					thaWrapper = $('#'+imgId+'_wrapper');
					
					thaImg.css({
					 'display': 'none',
					 'margin': '0px'
					 });
					thaWrapper.css({
						'display': 'block',
						'padding': '0px',
						'margin': '0px',
						'border': '0px',
						'margin-bottom': '-30px',
						'width': '442px'
					});
					$('#content').css('padding-top', '140px');
					
					//alert($('#'+imgId).height());
					
					thaWrapper.flash({
						src: templatePath + '/swf/pictureRemplacement.swf',
						width: 442,
						height: thisH + 66,
						wmode: 'transparent',
						bgcolor: 'white',
						style: {
							'background-color': 'white'
						},
						flashvars: {
							date: postDate,
							pictureUrl: imgUrl,
							height: thisH,
							jsParameter: '',
							projectId: imgId
						}
					}, {
						version: 9
					});
					
				}
				else {
				
					thaImg.load(function(){
						
						var thisH = thaImg.height();
						var imgUrl = thaImg.attr('src');
						
						thaImg.wrap('<div id="'+imgId+'_wrapper"></div>');
						thaWrapper = $('#'+imgId+'_wrapper');
						
						thaImg.css({
						 'display': 'none',
						 'margin': '0px'
						 });
						thaWrapper.css({
							'display': 'block',
							'padding': '0px',
							'margin': '0px',
							'border': '0px',
							'margin-bottom': '-30px',
							'width': '442px'
						});
						$('#content').css('padding-top', '140px');
						
						//alert($('#'+imgId).height());
						
						thaWrapper.flash({
							src: templatePath + '/swf/pictureRemplacement.swf',
							width: 442,
							height: thisH + 66,
							wmode: 'transparent',
							bgcolor: 'white',
							style: {
								'background-color': 'white'
							},
							flashvars: {
								date: postDate,
								pictureUrl: imgUrl,
								height: thisH,
								jsParameter: '',
								projectId: imgId
							}
						}, {
							version: 9
						});
						
					});
					
				}
			
			}
			
		}
		
	});
	
	
	//prettyPhoto
	$("a[rel^='lightbox']").prettyPhoto(LbOptions);
	
});


jQuery(window).resize(function(){resetWidthLayout();});



//functions
function resetWidthLayout(){
	
	if (jQuery('body').width() < 1068) {
		
		jQuery('#sidebar').css('width', '150px');
		
	} else {
		
		jQuery('#sidebar').css('width', 'auto');
		
	}
	
	if (jQuery(window).height() < 620) {
		
		jQuery('h1').css('position', 'absolute');
		jQuery('#goHome').css('position', 'absolute');
		jQuery('#navigation').css('position', 'absolute');
		jQuery('body').css('background-attachment', 'scroll');
	
	} else {
		
		if ($.browser.msie && (jQuery.browser.version.substr(0, 3) == "6.0")) {
		//bummer
		} else {
			jQuery('h1').css('position', 'fixed');
			jQuery('#goHome').css('position', 'fixed');
			jQuery('#navigation').css('position', 'fixed');
			jQuery('body').css('background-attachment', 'fixed');
		}
		
	}
	
	var sHeight = jQuery('#sidebar').height()+130;
	if(jQuery(window).height() < sHeight){
		jQuery('#sidebar').css('position', 'absolute');
	}else{
		if ($.browser.msie && (jQuery.browser.version.substr(0, 3) == "6.0")) {
			//bummer
			} else {
				jQuery('#sidebar').css('position', 'fixed');
			}
	}
	
}

function callFromFlash(el){
	
	thaA = $('a#' + el);
	
	if ($.browser.msie) {
		//alert('click!');
		thaA.click();
	}else if($.browser.mozilla && OSName == "MacOS"){
		//alert('a#' + el);
		thaA.click();
	}
	
}
