jQuery.noConflict();
(function( $ ) {
	$.extend({
		
		dd_actions: function() {
			if($('.mainnav_holder > ul').length){
				$('div.ddmenu_holder').css('visibility','visible');
				$('div.ddmenu_holder').css('display','none');
				
				var navtime;
				var navNewtime;
				$('.mainnav_holder > ul > li:has(.ddmenu_content *)').hover(
					function() {
						$('.mainnav_holder > ul > li').removeClass('onHover');
						var $t = $(this);
						clearTimeout(navtime);
						clearTimeout(navNewtime);
						navNewtime = setTimeout(function(){
							$t.addClass('onHover');
						}, 400);						
					},
					function() {
						var $t = $(this);
						clearTimeout(navNewtime);
						navtime = setTimeout(function(){
							$('.mainnav_holder > ul > li').removeClass('onHover');
						}, 400);
					}
				);
			}
			if($('.dropdown_1').length){
				$('.dropdown_1').each(function(){
					var $menu_item = $(this).prev('a');
					var $dd_menu = $(this);
					var mainNavWidth = $('.mainnav_holder').outerWidth();
					var leftSpace = $menu_item.offset().left-$('.mainnav_holder').offset().left;
					var rightSpace = mainNavWidth - leftSpace - $menu_item.parent().width();
					$menu_item.parent().css('position','relative');
					
					if(leftSpace + $dd_menu.width() < mainNavWidth){
						$dd_menu.css('left', -9+'px');
					}
					else {
						$dd_menu.css({'left':'auto','right':-9+'px'});
					}
				});
			}
			if($('.dropdown_2').length){
				$('.dropdown_2').each(function(){
					var $menu_item = $(this).prev('a');
					var $dd_menu = $(this);
					
					var mainNavWidth = $('.mainnav_holder').outerWidth();
					var leftSpace = $menu_item.offset().left-$('.mainnav_holder').offset().left;
					var rightSpace = mainNavWidth - leftSpace - $menu_item.parent().width();
					
					if(leftSpace + $dd_menu.width()-18 < mainNavWidth){
						$menu_item.parent().css('position','relative');
						$dd_menu.css('left', -9+'px');
					}
					else {
						if(rightSpace + $dd_menu.width()-18 < mainNavWidth){
							$menu_item.parent().css('position','relative');
							$dd_menu.css({'left':'auto','right':-9+'px'});
						}
					}
				});
			}
		},
		
        equalHeight: function() {
            $('.EqualHeight').children().height('auto');
            $('.EqualHeight').each(function(){
                var maxHeight = 0;
                $(this).children().each(function(){
                    if($(this).height() > maxHeight ){
                        maxHeight = $(this).height();
                    }
                });
                $(this).children().height(maxHeight);
            });
        },
	        
	    equalHeightLine: function() {
            $('.EqualHeightLine').children().height('auto');
            $('.EqualHeightLine').each(function(){
                var maxHeight = 0;
                var prevItem;
                var count = 0;
                $(this).children().each(function(){
                    count++;
                    if (count % 2 == 0){
                        maxHeight = prevItem.height();
                        if($(this).height() > maxHeight ){
                            maxHeight = $(this).height();
                        }
                        $(this).height(maxHeight);
                        prevItem.height(maxHeight);
                    }
                    prevItem = $(this);
                });
            });
        },
		
		fTrim: function(s){
			return s.replace(/^\s*(.*?)\s*$/,"$1");
		},
		
		cuSelApply: function() {
			var cuselParams = {
				changedEl: "select",
				scrollArrows: true
			}
			cuSel(cuselParams);
		},
		
		tipApply: function() {

        /*$('.tooltipped, .tooltippedWithDelay').tooltip({
				layout: '<div><span class="tooltip_arrow "></span></div>',
				offset: [-12, 0]
			});*/
			/*$('.tooltipped').cluetip({
				positionBy: 'fixed',
				cluetipClass: 'def_ct',
				splitTitle: '|',
				dropShadow: false,
				width: 110,
				arrows: true,
				showTitle: false,
				cursor: '',
				hoverIntent: false,
				sticky: true,
				mouseOutClose: true,
				closePosition:'title',
				onShow: function(ct, c){
					ct.css('top', $(this).offset().top-ct.outerHeight()-14+'px');
					ct.css('left', $(this).offset().left-ct.outerWidth()/2+$(this).outerWidth()/2+'px');
				}
			});

            $('.tooltippedWithDelay, .tooltipped').cluetip({
				positionBy: 'fixed',
				cluetipClass: 'def_ct',
				splitTitle: '|',
				dropShadow: false,
				width: 110,
				arrows: true,
				showTitle: false,
				cursor: '',
				hoverIntent: false,
				sticky: true,
				mouseOutClose: true,
				closePosition:'title',
				onShow: function(ct, c){
					ct.css('top', $(this).offset().top-ct.outerHeight()-10+'px');
					ct.css('left', $(this).offset().left-ct.outerWidth()/2+$(this).outerWidth()/2+'px');
				}
			});*/
		},
		
		productselector_control: function(){
			$('.main_productselector .productselector_controls a').click(function(){
				if(!$(this).parent().hasClass('active')){
					$(this).parent().parent().children().removeClass('active');
					$(this).parent().parent().next().find('.cusel_widthHolder').addClass("classDisCuselWrapper");
					$(this).parent().parent().next().find('.cusel_widthHolder').children('.cusel').addClass("classDisCusel");
					$(this).parent().addClass('active');
					$(this).parent().parent().next().find('.cusel_widthHolder').eq($(this).parent().index()).removeClass("classDisCuselWrapper");
					$(this).parent().parent().next().find('.cusel_widthHolder').eq($(this).parent().index()).children('.cusel').removeClass("classDisCusel");
				}
				else {
				}
				return false;
			});
		},

        popup_close: function(){
            $('.messagePopup img').click(function () {
            $('.form_overlay').remove();
            $('.messagePopup').remove();
            return false;
            });
        },
		
		lightbox_apply: function(){
			if($('a[rel="lightbox"]').length){
				$('a[rel="lightbox"]').lightBox({
					overlayOpacity: 0.7,
					imageLoading: '/images/gyproc/icons/lightbox-ico-loading.gif',
					imageBtnClose: '/images/gyproc/icons/close_icon_blue.png',
					imageBtnPrev: '/images/gyproc/icons/lightbox-btn-prev.gif',
					imageBtnNext: '/images/gyproc/icons/lightbox-btn-next.gif',
					imageBlank: '/images/gyproc/icons/lightbox-blank.gif',
					btnCloseText: 'Sluit venster'
					
				});
			}
		},

		tab_controllers_functionality: function(){
			if($('#tab_controllers').length && $('.product_content').children().length>1){
				$('.product_content').children().hide();
				$('.product_content').children(":eq("+ $('#tab_controllers li.active').index() + ")").show();
				$('#tab_controllers li a').click(function(){
					$(this).parent().siblings().removeClass('active');
					$(this).parent().addClass('active');
					$('.product_content').children().hide();
					$('.product_content').children(":eq("+ $('#tab_controllers li.active').index() + ")").show();
					
					if($.browser.msie){
						var ieTimeout;
						var ieSelector = $('.product_content').children(":eq("+ $('#tab_controllers li.active').index() + ")").find('ol');
						ieSelector.css('display','none');
						ieTimeout = setTimeout(function() {ieSelector.css('display','block');clearTimeout(ieTimeout);}, 1);
					}
					return false;
				});
			}
		},
		
		inputtextValuesSet: function(){
			$('input[type="text"], textarea').each(function(){
				if($(this).attr('title') && $.fTrim($(this).val()).length === 0 ){
					$(this).val($(this).attr('title'));
				}
				$(this).blur(function(){
					var sValue = $.fTrim(this.value);
					if(sValue.length===0){
						$(this).val($(this).attr('title'));
					}
				});
				$(this).focus(function(){
					var sValue = $.fTrim(this.value);
					if(sValue==$(this).attr('title')){
						this.value="";
					}
				});
			});
		},
		
		init: function() {		
			$.inputtextValuesSet();
			$.equalHeight();
			$.equalHeightLine();
			$.dd_actions();
			$.cuSelApply();
			//$.tipApply();
			$.productselector_control();
			$.lightbox_apply();
			$.tab_controllers_functionality();
			$.popup_close();
		}
	});
} )(jQuery);


jQuery(document).delegate('.tooltipped, .tooltippedWithDelay', "mouseenter", function() {
    if (!jQuery(this).data("tooltip")) {
        jQuery(this).tooltip({layout: '<div><span class="tooltip_arrow "></span></div>', offset: [-10, 0]});
        jQuery(this).trigger("mouseenter");
    }
});

jQuery(document).ready(function($){
	$.init();

/*	$('.SortOptions').click(function() {
		$('.cusel span:contains("Geluidsisolatie R"), .cusel div.cuselText:contains("Geluidsisolatie R")').html("Geluidsisolatie R<sub>w</sub>");
		return true;
	});
*/	
	$('.product_content .product_cheracteristics dt').each(function(){
			if( $.trim($(this).text()).length) return;
			else $(this).hide();
	});
});

var addthis_config = {ui_language: 'nl'};
