/* make all tabs in dropdown and all teasers take the height of the heighest tab/teaser */

jQuery(document).ready(function() {
	//check if there is a link inside list element 
	
	$('.services_content li').each(function(){
		if($(this).find('a').text() == $(this).text()) {
			$(this).addClass('red_arrow');
		} else {
			$(this).addClass('black_arrow');
		}
	})
	
	$('.referenceContent li').each(function(){
		if($(this).find('a').text() == $(this).text()) {
			$(this).addClass('red_arrow');
		} else {
			$(this).addClass('black_arrow');
		}
	})
	
	$('.browsebox li').removeClass('red_arrow');
	$('.browsebox li').removeClass('black_arrow');
	
		
			/*
	if ( ($('.services_content li a').length) || ($('.referenceContent li a').length) ) {
		$('.services_content li').addClass('red_arrow');
	} else {
		$('.services_content li').addClass('black_arrow');
	}
	*/
	if($.trim($(".links").text()) == "") {
		  $(".links").remove();
		}
	if($.trim($(".address").text()) == "") {
		  $(".address").remove();
		}
	//replace & with &amp
	
	//$('body).html($(this).html().replace(/&amp;/g,'<span class="amp">&amp;<\/span>'));

	//open big dropdown on click 
	
	$('body').bind('click', function(event){
		//alert($(event.target).attr('class'));
		if ($(event.target).attr('class')!='dropdown' &&
			$(event.target).attr('class')!='menu_title' &&
			!$(event.target).parents('.dropdown').get(0)){
			$('.dropdown_click').removeClass('dropdown_click');
		}
	})
	$('#mainMenu .menu').click(function() {
		$('.dropdown_click').removeClass('dropdown_click');
		$(this).children('.dropdown').addClass('dropdown_click');
	});
	$('#mainMenu .menu').hover(function(){
		var current_menu = this;
		$('#mainMenu .menu').each(function(){
			if (this != current_menu){
				$(this).find('.dropdown_click').removeClass('dropdown_click');
			}
		})
	},
	function(){})

	
    var max = 0;
    var dropdownWidth = 0;
    
    $(".dropdown").each(function(){
    	$(this).find(".tabsContent .tabsInner ul li").each(function(){
    		$(this).find('.col').each(function(){ 
    			if ($(this).height() > max)
    				max = $(this).height();
    		});  if(max>0) { //alert(max);
    		$(this).find('.col').css('height', (max)+'px')};
    		//$(this).find('.col .first').css('height', (max+50)+'px')};
    		max=0;
		});
    });   
    
    //equalize column height just for the first dropdown (the one with tabs)
    
    $("li.first .dropdown").each(function(){
    	$(this).find(".tabsContent .tabsInner > ul > li").each(function(){
    		$(this).css('display','block');
    		$(this).find('.col').each(function(){ 
    			if ($(this).height() > max) { 
    				max = $(this).height(); }
    		});  if(max>0) { //alert(max);
    		$(this).find('.col').css('height', (max)+'px')};
    		//$(this).find('.col .first').css('height', (max+50)+'px')};
    		max=0;
    		$('.dropdown .tabsContent .tabsInner > ul > li').css('display','none');
		});
    });   
    
	$(".dropdown").each(function(){
		$(this).find(".tabsContent .tabsInner > ul > li").each(function(){
			$(this).css('position','absolute');
			$(this).css('top','-9999px');
			$(this).css('left','-9999px');
			$(this).css('display','block');
			//var col_div = $(this).find('.col');
			$contor = 0;
			$(this).find('.col').each(function(){
				//alert($(this).html()); alert(width);
				//alert($(this).html())
			//alert($(this).outerWidth());
				dropdownWidth = dropdownWidth + 200;
				$contor++;
			});
			dropdownWidth = dropdownWidth - 2;
			$(this).removeAttr("style");
			dropdownWidth = dropdownWidth;
			if ($(this).parents().hasClass('first')) {
				if(dropdownWidth<760) {dropdownWidth = 760}
				$(this).width(dropdownWidth);
			} else {
				$(this).width(dropdownWidth);
			}
			$(this).append('<div class="dropdown_bt"><div class="dropdown_bt_left"></div><div class="dropdown_bt_right"></div></div>');
			$(this).find('.dropdown_bt_left').width(dropdownWidth-200);
			//alert($(this).parents('.dropdown-top').html())
			dropdownWidth = 0;
		});
		
	});
    
    var max = 0;
    $(".teaser-block").each(function(){
        if ($(this).height() > max)
            max = $(this).height();
    });
    $(".teaser-block").height(max+20);
    $(".teaser-block .more").css('padding-top',max+20);
    $(".teaser-block .more").css('background-position', '0 '+(max+24)+'px');
    $(".teaser-block .more").css('display', 'block');
   
    var max = 0;
    $(".referenceCol").each(function(){
        if ($(this).height() > max)
            max = $(this).height();
    });
    $(".referenceCol").height(max+10);
    $(".referenceCol .more").css('padding-top',max+10);
    $(".referenceCol .more").css('background-position', '0 '+(max+14)+'px');
    $(".referenceCol .morex").css('padding-top',max+10);
    $(".referenceCol .morex").css('background-position', '0 '+(max+14)+'px');
    $(".referenceContainer").removeClass('referenceContainer');
    $(".referenceCol .more").css('display', 'block');
    $(".referenceCol .morex").css('display', 'block');

    var max = 0;
    $(".blockContainer").each(function(){
        if ($(this).height() > max)
            max = $(this).height();
    });
    $(".blockContainer").height(max+20);
});

/* filter check/uncheck */

jQuery(document).ready(function() {
	$("#filter .button").click(function () {
		$("INPUT[type='checkbox']").attr('checked', false);
		$('span').removeClass('jNiceChecked');
		//$("#filter").find('input[@type=checkbox]').attr('checked', '');
	});
	$(".filters li").each(function() {
		//$(this).addClass('checked');
	});
	$(".filters li .jNiceWrapper input").change(function () {
		//alert('input');
		//$(this).parents('li').toggleClass("checked");
	});
	$(".filters li .label").click(function () {
		//alert('label');
		//$(this).parent().toggleClass("checked");
	});
	
	$(".routeSubmit").click(function () {
		$(this).parents("form").submit();
	});
});

//choose which accordion menu to remain open 
function sidebarMenu() {
	
	$("#sidebarMenu").children().each(function(){
	    if ($(this).hasClass("open")) {
	    	$(this).children().eq(1).css('display','block');
	    } else { $(this).children().eq(1).css('display','none'); }
	});
	/*
	var activemenu  = 0;
	var i = 0;
	$("#sidebarMenu").children().each(function(){
	    if ($(this).hasClass("open")) {
			activemenu = i;
	    }
		i++;
	});
		jQuery('#sidebarMenu').accordion({ 
		    header: 'div.title', 
		    active: true, 
		    alwaysOpen: false, 
		    animated: false, 
		    autoheight: false,
		    active: activemenu
		});
	*/
}

/* start slider */
function impaqSmallSlider(time) {
        
	   $('#slides').slides({
   		    preload: true,
   		    preloadImage: 'css/layout/preloader.gif',
   		    play: time,
   		    pause: 2500,
   		    hoverPause: true,
   		    animationStart: function(){
   		     $('.caption').animate({
   		      bottom:-35
   		     },100);
   		    },
   		    animationComplete: function(current){
   		     $('.caption').animate({
   		      bottom:0
   		     },200);
   		     
   		    }
   		});
}
function clog(toLog) {
	if(window.console && console.log) {
		console.log(toLog);
	}
}

function impaqSlider(time) {
	
	var $wrapper = $('#slider'),
		$ul = $wrapper.children('ul'),
		$slides = $('.slide', $wrapper), // $slides = $wrapper.find('.slide');
		$nextButton = $('a.next', $wrapper),
		$prevButton = $('a.prev', $wrapper),
		$pages = null;
		currentIndex = 0,
		total = $slides.size(),
		slideshowTimer = null,
		isAnimated = false;
		
	if($('#slider .slides_container').children().size() <= 1) {
		
		$nextButton.hide();
		$prevButton.hide();
		return;
	}
	
	// old slider
	impaqSmallSlider(time);
	
	time = time || 0;
	//time = 4000;
	
	var slideshow = {
		isRunning: false,
		start: function() {
			if (slideshow.isRunning == false) {
				
				slideshow.isRunning = true;
				slideshowTimer = setInterval(next, time);
			}
		},
		stop: function() {
			clearInterval(slideshowTimer);
			slideshow.isRunning = false;
			slideshowTimer = null;
		}
	}
		
	
	
	function createPagination() {
		
		var paginationHTML = '<ul class="pagination">';
		for (var i = 0; i < total; i++) {
			paginationHTML += '<li><a href="#' + i + '">' + i + '</a></li>';
		}
		$wrapper.append(paginationHTML);
		paginationHTML += '</ul>';
		
		$('ul.pagination', $wrapper).css('width', total * 12);
		$pages = $('ul.pagination li', $wrapper);
		$pages.eq(currentIndex).addClass('current');
		
		$pages.children('a').bind('click', function(e) {
			e.preventDefault();
			move($(this).parent().index());
		});
	}
	
	function bringCurrentToTop() {
		
		$slides.each(function(index) {
			if (index == currentIndex) {
				$(this).css('zIndex', 10);
			} else {
				$(this).css('zIndex', 0);
			}
		});
	}
	
	function resetPosition() {
		
		$slides.add($ul).css('left', 0);
	}
	
	function move(index, direction) {
		
		// exit if the new position is the same with the current position, or animation is running
		if(isAnimated || index == currentIndex) {
			return;
		}
		
		isAnimated = true;
		
		// set movement direction if not specified
		if(typeof direction == 'undefined') {
			direction = currentIndex < index ? 1 : -1;
		}
		
		// move the next/prev slide to the immediate right/left of the viewport
		$slides.eq(index).css('left', direction * 100 + '%');
		
		// update pagination
		$pages.removeClass('current').eq(index).addClass('current');
		
		$ul.animate({
			left: -direction * 100 + '%'
		}, {
			duration: 500,
			queue: false,
			complete: function() { 
				currentIndex = index; 
				bringCurrentToTop();
				resetPosition();
				isAnimated = false;
			}
		});
	}
	
	function next() {
		var nextIndex = (currentIndex + 1 >= total) ? 0 : currentIndex + 1;
		move(nextIndex, 1);
	}
	
	function prev() {
		var prevIndex = (currentIndex - 1 < 0) ? total - 1 : currentIndex - 1;
		move(prevIndex, -1);
	}
	
	// next/prev buttons event bindings
	$nextButton.bind('click', function(e) {
		e.preventDefault();
		next();
	});
	$prevButton.bind('click', function(e) {
		e.preventDefault();
		prev();
	});
	
	// pause on hover, resume play on mouseout
	$wrapper.bind({
		mouseenter: function() {
			slideshow.stop();
		},
		mouseleave: function() {
			if(time > 0) {
				slideshow.start();
			}
		}
	});
	
	// initialization
	bringCurrentToTop();
	resetPosition();
	createPagination();
	if(time > 0) slideshow.start();
}

/* end slider */
$(document).ready(function(){
	/*if (window.History){
		var History = window.History; // Note: We are using a capital H instead of a lower h

				// Bind to State Change
		History.Adapter.bind(window,'statechange',function(){ // Note: We are using statechange instead of popstate
			// Log the State
			var State = History.getState(); // Note: We are using History.getState() instead of event.state
			History.log('statechange:', State.data, State.title, State.url);
		});
	}*/
	if($.browser.msie){
	        Printfix();
	    }
	});


	var roundedboxes;

	function Printfix(){
	    roundedboxes = $(".divwithHTC,.anotherdivwithHTC");
	    window.onbeforeprint = removeRoundedboxes;
	    window.onafterprint = addRoundedboxes;
	}

	function removeRoundedboxes(){
	     $(roundedboxes).css("behavior","none");
	    }
	   
	    function addRoundedboxes(){
	     $(roundedboxes).css("behavior","url(inc/PIE.htc)");
	    }
	
	    
