// Browser Update-Script
var $buoop = { reminder: 1 };
$buoop.ol = window.onload; 
window.onload=function(){ 
	if ($buoop.ol) { $buoop.ol(); }
	var e = document.createElement("script"); 
	e.setAttribute("type", "text/javascript"); 
	e.setAttribute("src", "http://browser-update.org/update.js"); 
	document.body.appendChild(e); 
};


// Product Listings
function productFancybox() {
	$("#content-imagelist").click( function(){
		var content = [];
		
		$("#image_container .image_item").each(function(){
			content.push( { 'href': $(this).find('img').attr("href"), 'title' : $(this).find('p').html()} );
		});
		$.fancybox( content, {'titlePosition': 'inside'} );
	});
}

var timer=null;
if (jQuery) {

	// Functions extending the jQuery object
	jQuery.fn.extend({
		
		// Adjusts height of containers
		equalHeight: function(resizeElement) {
			$resizeElement = ( resizeElement && $(resizeElement, this) ) ? $(resizeElement, this) : this.children();

		    var tallest = 0;
		    $resizeElement.each(function() {
		        var thisHeight = $(this).height();
		        if (thisHeight > tallest) { tallest = thisHeight; }
		    });		
		
			if (isIE7OrLower) { $resizeElement.css({'height': tallest}); }
			$resizeElement.css({'min-height': tallest});
			return this; 
		},
		
		// Set default value for search fields
		toggleValue: function() {
			return this.each(function(){
				var defaultText = $(this).prevAll('label').html();
				$(this).data('defaultText', defaultText);

				$(this).bind('focus', function() {
					if ( $(this).val() == $(this).data('defaultText') ) { $(this).removeClass('default-value').val(''); }
				}).bind('blur', function() {
					if ( $(this).val() == '' ) { $(this).val( $(this).addClass('default-value').data('defaultText') ); }
				})
				.blur();
			});
		}
		
	});
	
	
	
	// DOM ready
	$(function()
	{
		// Init behavoior: search box
		$('#searchquery, #dealerlocatorquery').toggleValue();
			
		// Homepage features
		$('body#home div.teaser-container').equalHeight('.shadow-240 ul, .shadow-208 ul, .shadow-234 ul');
		$('#content div.teaser-container, #content-wide div.teaser-container').equalHeight('.shadow-240, .shadow-208, .shadow-234');

		if (jQuery.fn.fancybox)
		{
			// References: Carousel
			if (jQuery.fn.jCarouselLite)
			{
				$('#content .reference-carousel-items')
					.equalHeight('> .carousel-list > li')
					.jCarouselLite({
						btnNext: ".next",
						btnPrev: ".prev",
						circular: false
					})
					.find('a').fancybox({
						'type'              : 'iframe',
						'width'             : 935,
						'height'            : 600
						/* 'autoDimensions' : false, */
					});
			}

		
			// Gallery pages
			$('#content-gallery a.gallery').fancybox({
				'titlePosition': 'inside',
				'titleFormat'  : function(title, currentArray, currentIndex, currentOpts)
				{
					var imageTitle = title.split('¶');
					var fbTitle = '';

					// Do we have title and description?
					if (imageTitle.length === 2) {
						if ( $.trim(imageTitle[0]) !== '') { fbTitle = '<strong>' + imageTitle[0] + '</strong><br />'; }
						fbTitle += imageTitle[1];
					} else {
						fbTitle = imageTitle[0];
					}
					return fbTitle;
				}	
			});

			
			
			
			if( $(".productitem").length === 1 ) {
				var url = $(".productitem a.productlink").attr("href");
				$("#content").hide();
				$(".product, .productlist").hide();
				$("#product_generated")
					.empty()
					.load( url + " .shadow-718 > *", productFancybox )
					.show();
				$("#content").show();
			} else {
				$(".productlist a.productlink, .productdownloads a.productlink").each( function() {
					var url = $(this).attr("href");
					$(this).attr("href","#product_content");
					$(this).click( function(){
						
						$("#content").hide();
						$(".product_close").show();
						$(".product, .productlist").hide();
						$("#product_generated")
							.load( url + " .shadow-718 > *", productFancybox )
							.show();
						$("#content").show();
						
					});
				});
			}
			
			$(".product_close").click( function() {
				$("#content").hide();
				$(".product, .productlist").show();
				$("#product_generated")
					.empty()
					.hide();
				$(".product_close").hide();
				$("#content").show();
			});


			// open downloads from product overview in a fancybox 
			$(".productdownloads .downloadlink").fancybox( { 'autoDimensions': false, 'width':495, 'height':350 } );
			
		}


		// Form links (Sidebar)
		$('a.formlink').click( function(e) {
			
			if( $('#content').length ){
				if( $('#searchbody').length || $('#home').length ){
					var $htmlInject = $('#formframe-wide').html();
				} else{
					var $htmlInject = $('#formframe').html();
				}
				var $selector = '#content';
			} else {
				var $htmlInject = $('#formframe-wide').html();
				var $selector = '#content-wide';
			}
			
			var $stage = $($selector+", #sidebar, #searchbody #nav-left")
				.hide()
				.filter( '#content, #content-wide' );
			
			// Check if injected element already exists
			var $formFrame = $('.formframe');
			if ( $formFrame.length ) {
				$formFrame.show();
			} else {
				$stage.before( $htmlInject );
				$formFrame = $('.formframe');
			}
				
			$formFrame
				.find('a.close-frame').click(function() {
					$formFrame.hide();
					$('#content, #content-wide, #searchbody #nav-left, #sidebar').show();
					clearInterval(timer);
				})
				.end()
				.find( 'iframe' )
				.attr( 'src', e.target )
				.ready(function () {
					// Set specific variable to represent all iframe tags.
					var iFrames = document.getElementsByTagName('iframe');
					
					function resizeFrame(iframe){
						if( iframe !=null )
							iframe.style.height = iframe.contentWindow.document.body.scrollHeight + 'px';
						
					}
					function processFrames(){
						for (var i = 0, j = iFrames.length; i < j; i++)
					      {
							resizeFrame(iFrames[i]);
					      }
					}
					function checkFrame(){
						if($('iframe').contents().length >0){
							timer=setInterval(processFrames, 100);
						}
						else {
							setTimeout(checkFrame,100);
						}
					}
					
					if ($.browser.safari || $.browser.opera)
					{

						// Safari and Opera need a kick-start.
						for (var i = 0, j = iFrames.length; i < j; i++)
						{
							var iSource = iFrames[i].src;
							iFrames[i].src = '';
							iFrames[i].src = iSource;
						}
						
					}/*
					else{
						$('iframe').load(function(){
							resizeFrame(this);
						});
					}*/
					$('iframe').load(function(){
						this.style.height = this.contentWindow.document.body.scrollHeight + 'px';
						parent.setTimeout(checkFrame, 1000);
					});
					//setTimeout(checkFrame,1000);
				});
			return false; // Don't execute link
		});

		
		// guided navigation
		$("#accordion > li > a").click( function(){
			$(this).next().toggle();
			return false;
		});

		
		if (jQuery.fn.tablesorter)
		{
			$(".joblist").tablesorter({ 

				textExtraction: function(node){
					var classString = node.attributes['class'];
					
					if( classString != null && classString.nodeValue.lastIndexOf("date") > -1 ){
						var datemilis = classString.nodeValue.substring( classString.nodeValue.lastIndexOf("date")+4 );
						return datemilis;
					}
					var childAnchors = node.getElementsByTagName("a");
					if( childAnchors.length > 0 ){
						var returnString="";
						for( var i=0; i < childAnchors.length; i++ ){
							returnString += childAnchors[i].innerHTML + " ";
						}
						return returnString;
					}
					return node.innerHTML;
					
				}
			});
		}	

		

		
		

	});	
}

