/* Author: S&B

*/

jQuery(document).ready(function($) {
    
	// open pdf files in new window
	$('a[href$=pdf]').each(function() {
 	 $(this).attr('target', '_blank');
	});

    $('#feature-wrapper article:first').animate({ opacity: 1 }, 1000, function() {
    	$('#feature-wrapper').cycle({timeout: 8000, before: function(currSlide, nextSlide, opts) { 
        // change timeout for first slide 
        var index = $(this).parent().children().index(this); 
        opts.timeout = index == 0 ? 8000 : 5000; 
        $(this).css("z-index",2);
    }, cssAfter: {zIndex: 0}});
	});
	
	
	function onBefore() { 
    	 $(this).css("z-index",2);
	}; 
    
    $('#teaser.gallery').cycle({timeout: 3000});
    
    // tooltips
	$("#pager-top > a").tipTip({maxWidth: "auto", edgeOffset: 10, defaultPosition: "right"});
	$("#pager-bottom > a").tipTip({maxWidth: "auto", edgeOffset: 10, defaultPosition: "right"});
	
	// adaptive images
	if ((screen.width<=991) && (screen.width>=768)) {
	//alert('resize');
		$("#feature-wrapper img").attr("src", $("img").attr("src").replace(/([^.]*)\.(.*)/, "$1-712.$2"));
	}
	
	// project toggler
	$('a.trigger').click(function() {
		
		var additional_height = 10;
		// set additional height for ie
		if ($(".ie6, .ie7, .ie8").length > 0) {
				additional_height = additional_height + 45;
		}
		if ($('a.trigger').hasClass('trigger-minus')) {
			$('#project-teaser > article').animate({height: '45px'});
		} else {
	  		$('#project-teaser > article').animate({height: ($('#project-information').height()+additional_height)+'px'});
		}
			$('a.trigger').toggleClass('trigger-minus');
	});
	
	if ($(".ie6, .ie7").length > 0) {
		var aImgs = $("#project-teaser > img");
		//imgSizer.collate(aImgs);
	}
});

/* nav */
//$('div.primary_nav > ul').superfish({autoArrows: false, pathClass: 'current_page_ancestor'});
$('div.primary_nav > ul') 
  .find('li.current_page_item,li.current_page_parent,li.current_page_ancestor ') 
    .addClass('current') 
    .end() 
  .superfish({autoArrows: false, pathClass: 'current'}); 

/* image resizer IE */
if ($(".ie6, .ie7").length > 0) {
var imgSizer = {
	Config : {
		imgCache : []
		,spacer : "/wp-content/themes/s-b/images/spacer.gif"
	}

	,collate : function(aScope) {
		var isOldIE = (document.all && !window.opera && !window.XDomainRequest) ? 1 : 0;
		if (isOldIE && document.getElementsByTagName) {
			var c = imgSizer;
			var imgCache = c.Config.imgCache;

			var images = (aScope && aScope.length) ? aScope : document.getElementsByTagName("img");
			for (var i = 0; i < images.length; i++) {
				images[i].origWidth = images[i].offsetWidth;
				images[i].origHeight = images[i].offsetHeight;

				imgCache.push(images[i]);
				c.ieAlpha(images[i]);
				images[i].style.width = "100%";
			}

			if (imgCache.length) {
				c.resize(function() {
					for (var i = 0; i < imgCache.length; i++) {
						var ratio = (imgCache[i].offsetWidth / imgCache[i].origWidth);
						imgCache[i].style.height = (imgCache[i].origHeight * ratio) + "px";
					}
				});
			}
		}
	}

	,ieAlpha : function(img) {
		var c = imgSizer;
		if (img.oldSrc) {
			img.src = img.oldSrc;
		}
		var src = img.src;
		img.style.width = img.offsetWidth + "px";
		img.style.height = img.offsetHeight + "px";
		img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')"
		img.oldSrc = src;
		img.src = c.Config.spacer;
	}

	// Ghettomodified version of Simon Willison's addLoadEvent() -- http://simonwillison.net/2004/May/26/addLoadEvent/
	,resize : function(func) {
		var oldonresize = window.onresize;
		if (typeof window.onresize != 'function') {
			window.onresize = func;
		} else {
			window.onresize = function() {
				if (oldonresize) {
					oldonresize();
				}
				func();
			}
		}
	}
}
}

