jQuery(function($) {
	
	//IE fixes
	if ($.browser.msie) {
		
		//All IE
		$('#meta-nav li:first-child, #nav li:first-child').addClass('first-child');
		
		//IE6
		if ($.browser.version <= 6){
			$('div.collapsible h2')
				.mouseover(function(){$(this).addClass('hover')})
				.mouseout(function(){$(this).removeClass('hover')});
		}
	}
	
	//emptyOnFocus
	$('#search').emptyOnFocus();
	
	//egualheights home boxes
	$('#home #inner').equalHeights();
	
	$('.twoColBox').equalHeights();
	
	//Combobox contact
	$('#contact-select select').combobox({
		arrowURL: 'fileadmin/images/combobox-arrow.png'
	});
	
	//Collapsbile
	$('div.collapsible').each(function(){
		var $container = $(this),
			$title = $('h2', $container),
			$content = $('.collapsible-content', $container),
			$contentInner = $('span', $content),
			$link = $('a', $content),
			column = false;
			
		if ($container.parent().is('#column')) var column = true;
		
		$title.click(function(){
			if (!column) {
				if ($contentInner.is(':empty')) {
					document.location.href = $link[0].href;
					return false;
				}
			}				
			if ($container.is('.expanded')) {
				$container.removeClass('expanded');
				if (column) { $content.hide(); }
				else { $content.slideUp('fast'); }
			} else {
				$container.addClass('expanded');
				if (column) { $content.show(); }
				else { $content.slideDown('fast'); }
			}
		});
		
	});
	
	//Call Me
	$('a[href*=#callMe]').click(function(){
		$.facebox({ div: '#callMe' });
		return false;
	});
	
	$('body').click(function(event){		
		
		//Closing the window
		if ($(event.target).is('.cancel_submit') || $(event.target).is('[href="#close"]')) {
			$.facebox.close();
			return false;
		}
		
		if ($(event.target).is('.cm_submit')) {
				
			//Call Me
			var $callMe = $context = $(event.target).parents('#callMe'),			
				$callMeForm = $('form', $context),
				$callMeName = $('[name=name]', $context),
				$callMeCompany = $('[name=company]', $context),
				$callMeMail = $('[name=mail]', $context),
				$callMePhone = $('[name=phone]', $context),
				$callMeQuestion = $('[name=question]', $context),
				$callMeID = $('[name=contactID]', $context),
				$callMeLangID = $('[name=langID]', $context),
				$callMePageID = $('[name=pageID]', $context),
				$loader = $('<div class="loading">Loading</div>'),			
						
				errors = false,				
				text_only_pattern = /^[\ba-zA-Z\s-]$/,
				emailPattern = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
				number_only_pattern = /^[a-z0-9]+([\\s]{1}[a-z0-9]|[a-z0-9])+$/i;				
			
			//Removing the errors
			$('.error', $callMe).removeClass('error');
			
			
			if ($callMeName.val() == '') {
				errors = true;
				$callMeName.parent().addClass('error');
			}
			//if ($callMeName.val().match(text_only_pattern)) {
				//alert("huh?");
			//}
			if ($callMePhone.val() == '') {
				errors = true;
				$callMePhone.parent().addClass('error');
			}
			if ($callMeMail.val() == '') {
				errors = true;
				$callMeMail.parent().addClass('error');
			}
			if (!$callMeMail.val().match(emailPattern)) {
				errors = true;
				$callMeMail.parent().addClass('error');
			}
			if ($callMeQuestion.val() == '') {
				errors = true;
				$callMeQuestion.parent().addClass('error');
			}
			
			if (!errors) {		
				$callMeForm.remove(); //Removing the form
				
				$context.append($loader); //Loading animation
				
				//Ajax request
				$.post( 
					$callMeForm.attr('action'),
					{ name: $callMeName.val(), company: $callMeCompany.val(), phone: $callMePhone.val(), mail: $callMeMail.val(), question: $callMeQuestion.val(), contactID: $callMeID.val(), langID: $callMeLangID.val() },
					function(response){
						responseText = response.substr(response.indexOf('<p>'), response.indexOf('</p>'));
						$context.append(responseText);
						$loader.remove();
						//window.location.pathname ='index.php?id=120&page='+$callMePageID.val();
					}
				);
			}
			
			return false;
		}
		
	});

	
	//news archive list
	$('.news-amenu-container ul li:last').css({"border-right":"none"});
	
	//Cufon
	Cufon.replace('#nav > li > a, .home-box h2, #column h2');
	Cufon.replace('#statement h1', { fontFamily: 'Gill Sans Std Light' });
	//Cufon.replace('#statement p', { fontFamily: 'Gill Sans Std Light' });
	
	
	//Layout fix
	var $small = $('.small');
	if ($small.children().length == 0) {
		$small.prev('.column').removeClass('column');
		$small.remove();		
	}
	
});

function ShowLightBoxFlash(image) {
	var nr = image.length;
	var imagesHTML = '';
	
	for(i=0; i < nr; i++){
		//document.getElementById("myFlashLightbox").href = image[i];
		//showLightbox(document.getElementById("myFlashLightbox"));
		imagesHTML += '<a href="'+image[i]+'" id="myFlashLightboxImage'+i+'" rel="lightbox">image</a>';
	}
	document.getElementById("myFlashLightbox").innerHTML = imagesHTML;	
	//showLightbox(document.getElementById("myFlashLightboxImage0"));
		
	jQuery('#myFlashLightbox a').lightBox({fixedNavigation:true});
	jQuery('#myFlashLightbox').find('a').eq(0).trigger('click');

}


function recordOutboundLink(link, category, action) {
  try {
    var pageTracker=_gat._getTracker("UA-7550865-6");
    pageTracker._trackEvent(category, action);
    if(link.target == '_blank') {
    	//setTimeout('window.open("'+link.href+'","nibc.com")', 100);
    	window.open(link.href,"");
    } else {
    	setTimeout('document.location = "' + link.href + '"', 100);
    }
  }catch(err){}
}

/*
 * News Archive
 */
jQuery(function($){
	
	var $form = $('#perpageSel'),
		$select = $('#dateSelect'),
		$dateStart = $('#dateStart'),
		$dateEnd = $('#dateEnd');		
	
	$select.change(function(){
		
		var dateStart = new Date(),
			dateEnd = new Date();
		
		//Setting the date
		dateStart.setFullYear($select.val(),0,1);
		dateEnd.setFullYear($select.val(),11,30);
		
		//Converting to timestamp / epoch in seconds
		dateStart = parseInt(dateStart.getTime()/1000);
		dateEnd = parseInt(dateEnd.getTime()/1000);		
		
		//Setting the dates into the hidden fields
		$dateStart.val(dateStart);
		$dateEnd.val(dateEnd);
		
		//Submitting the form
		$form.submit();		
		
	});

});
